Rename get_test_app to get_app.
[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
19from urlparse import urlparse, parse_qs
20
21from mediagoblin import mg_globals
e2890c72 22from mediagoblin.tools import processing
1be247b3 23from mediagoblin.tests.tools import get_app, fixture_add_user
0df00eb6
JW
24from mediagoblin.tests.test_submission import GOOD_PNG
25from mediagoblin.tests import test_oauth as oauth
26
27
28class TestHTTPCallback(object):
29 def setUp(self):
1be247b3 30 self.app = get_app(dump_old_app=False)
0df00eb6
JW
31 self.db = mg_globals.database
32
fc7b1b17
SS
33 self.user_password = u'secret'
34 self.user = fixture_add_user(u'call_back', self.user_password)
0df00eb6
JW
35
36 self.login()
37
38 def login(self):
39 self.app.post('/auth/login/', {
40 'username': self.user.username,
41 'password': self.user_password})
42
111a609d 43 def get_access_token(self, client_id, client_secret, code):
0df00eb6
JW
44 response = self.app.get('/oauth/access_token', {
45 'code': code,
111a609d
JW
46 'client_id': client_id,
47 'client_secret': client_secret})
0df00eb6
JW
48
49 response_data = json.loads(response.body)
50
51 return response_data['access_token']
52
53 def test_callback(self):
54 ''' Test processing HTTP callback '''
111a609d
JW
55
56 self.oauth = oauth.TestOAuth()
57 self.oauth.setUp()
58
0df00eb6
JW
59 redirect, client_id = self.oauth.test_4_authorize_confidential_client()
60
61 code = parse_qs(urlparse(redirect.location).query)['code'][0]
62
111a609d
JW
63 client = self.db.OAuthClient.query.filter(
64 self.db.OAuthClient.identifier == unicode(client_id)).first()
65
66 client_secret = client.secret
67
68 access_token = self.get_access_token(client_id, client_secret, code)
0df00eb6
JW
69
70 callback_url = 'https://foo.example?secrettestmediagoblinparam'
71
111a609d
JW
72 res = self.app.post('/api/submit?client_id={0}&access_token={1}\
73&client_secret={2}'.format(
0df00eb6 74 client_id,
111a609d
JW
75 access_token,
76 client_secret), {
0df00eb6
JW
77 'title': 'Test',
78 'callback_url': callback_url},
79 upload_files=[('file', GOOD_PNG)])
80
81 assert processing.TESTS_CALLBACKS[callback_url]['state'] == u'processed'