Added documentation for the batchaddmedia gmg tool to the mediagoblin docs.
[mediagoblin.git] / mediagoblin / tests / test_http_callback.py
CommitLineData
0df00eb6
JW
1# GNU MediaGoblin -- federated, autonomous media hosting
2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3#
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.
8#
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.
13#
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/>.
16
17import json
18
2455a54f 19import pytest
0df00eb6
JW
20from urlparse import urlparse, parse_qs
21
22from mediagoblin import mg_globals
e2890c72 23from mediagoblin.tools import processing
5c2ece74 24from mediagoblin.tests.tools import fixture_add_user
0df00eb6 25from mediagoblin.tests.test_submission import GOOD_PNG
1c694fbe 26from mediagoblin.tests import test_oauth2 as oauth
0df00eb6
JW
27
28
29class TestHTTPCallback(object):
2455a54f
CAW
30 @pytest.fixture(autouse=True)
31 def setup(self, test_app):
32 self.test_app = test_app
33
0df00eb6
JW
34 self.db = mg_globals.database
35
fc7b1b17
SS
36 self.user_password = u'secret'
37 self.user = fixture_add_user(u'call_back', self.user_password)
0df00eb6 38
2455a54f 39 self.login()
0df00eb6 40
2455a54f
CAW
41 def login(self):
42 self.test_app.post('/auth/login/', {
0df00eb6
JW
43 'username': self.user.username,
44 'password': self.user_password})
45
2455a54f 46 def get_access_token(self, client_id, client_secret, code):
1c694fbe 47 response = self.test_app.get('/oauth-2/access_token', {
0df00eb6 48 'code': code,
111a609d
JW
49 'client_id': client_id,
50 'client_secret': client_secret})
0df00eb6
JW
51
52 response_data = json.loads(response.body)
53
54 return response_data['access_token']
55
2455a54f 56 def test_callback(self):
0df00eb6 57 ''' Test processing HTTP callback '''
111a609d 58 self.oauth = oauth.TestOAuth()
2455a54f 59 self.oauth.setup(self.test_app)
111a609d 60
2455a54f 61 redirect, client_id = self.oauth.test_4_authorize_confidential_client()
0df00eb6
JW
62
63 code = parse_qs(urlparse(redirect.location).query)['code'][0]
64
111a609d
JW
65 client = self.db.OAuthClient.query.filter(
66 self.db.OAuthClient.identifier == unicode(client_id)).first()
67
68 client_secret = client.secret
69
2455a54f 70 access_token = self.get_access_token(client_id, client_secret, code)
0df00eb6
JW
71
72 callback_url = 'https://foo.example?secrettestmediagoblinparam'
73
2455a54f 74 self.test_app.post('/api/submit?client_id={0}&access_token={1}\
111a609d 75&client_secret={2}'.format(
0df00eb6 76 client_id,
111a609d
JW
77 access_token,
78 client_secret), {
0df00eb6
JW
79 'title': 'Test',
80 'callback_url': callback_url},
81 upload_files=[('file', GOOD_PNG)])
82
83 assert processing.TESTS_CALLBACKS[callback_url]['state'] == u'processed'