Added OAuth test
[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
22from mediagoblin.tools import template, processing
23from mediagoblin.tests.tools import get_test_app, fixture_add_user
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):
30 self.app = get_test_app()
31 self.db = mg_globals.database
32
33 self.user_password = 'secret'
34 self.user = fixture_add_user('call_back', self.user_password)
35 self.oauth = oauth.TestOAuth()
36 self.oauth.setUp()
37
38 self.login()
39
40 def login(self):
41 self.app.post('/auth/login/', {
42 'username': self.user.username,
43 'password': self.user_password})
44
45 def get_access_token(self, client_id, code):
46 response = self.app.get('/oauth/access_token', {
47 'code': code,
48 'client_id': client_id})
49
50 response_data = json.loads(response.body)
51
52 return response_data['access_token']
53
54 def test_callback(self):
55 ''' Test processing HTTP callback '''
56 redirect, client_id = self.oauth.test_4_authorize_confidential_client()
57
58 code = parse_qs(urlparse(redirect.location).query)['code'][0]
59
60 access_token = self.get_access_token(client_id, code)
61
62 callback_url = 'https://foo.example?secrettestmediagoblinparam'
63
64 res = self.app.post('/api/submit?client_id={0}&access_token={1}'\
65 .format(
66 client_id,
67 access_token), {
68 'title': 'Test',
69 'callback_url': callback_url},
70 upload_files=[('file', GOOD_PNG)])
71
72 assert processing.TESTS_CALLBACKS[callback_url]['state'] == u'processed'