1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 from six.moves.urllib.parse import parse_qs, urlparse
24 from mediagoblin import mg_globals
25 from mediagoblin.tools import processing
26 from mediagoblin.tests.tools import fixture_add_user
27 from mediagoblin.tests.test_submission import GOOD_PNG
28 from mediagoblin.tests import test_oauth2 as oauth
31 class TestHTTPCallback(object):
32 @pytest.fixture(autouse=True)
33 def setup(self, test_app):
34 self.test_app = test_app
36 self.db = mg_globals.database
38 self.user_password = u'secret'
39 self.user = fixture_add_user(u'call_back', self.user_password)
44 self.test_app.post('/auth/login/', {
45 'username': self.user.username,
46 'password': self.user_password})
48 def get_access_token(self, client_id, client_secret, code):
49 response = self.test_app.get('/oauth-2/access_token', {
51 'client_id': client_id,
52 'client_secret': client_secret})
54 response_data = json.loads(response.body.decode())
56 return response_data['access_token']
58 def test_callback(self):
59 ''' Test processing HTTP callback '''
60 self.oauth = oauth.TestOAuth()
61 self.oauth.setup(self.test_app)
63 redirect, client_id = self.oauth.test_4_authorize_confidential_client()
65 code = parse_qs(urlparse(redirect.location).query)['code'][0]
67 client = self.db.OAuthClient.query.filter(
68 self.db.OAuthClient.identifier == six.text_type(client_id)).first()
70 client_secret = client.secret
72 access_token = self.get_access_token(client_id, client_secret, code)
74 callback_url = 'https://foo.example?secrettestmediagoblinparam'
76 self.test_app.post('/api/submit?client_id={0}&access_token={1}\
77 &client_secret={2}'.format(
82 'callback_url': callback_url},
83 upload_files=[('file', GOOD_PNG)])
85 assert processing.TESTS_CALLBACKS[callback_url]['state'] == u'processed'