aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/tests/test_http_callback.py
blob: bce479cad85ae923a3ce93436b674de3fe3ee258 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import json

from urlparse import urlparse, parse_qs

from mediagoblin import mg_globals
from mediagoblin.tools import template, processing
from mediagoblin.tests.tools import get_test_app, fixture_add_user
from mediagoblin.tests.test_submission import GOOD_PNG
from mediagoblin.tests import test_oauth as oauth


class TestHTTPCallback(object):
    def setUp(self):
        self.app = get_test_app()
        self.db = mg_globals.database

        self.user_password = 'secret'
        self.user = fixture_add_user('call_back', self.user_password)
        self.oauth = oauth.TestOAuth()
        self.oauth.setUp()

        self.login()

    def login(self):
        self.app.post('/auth/login/', {
            'username': self.user.username,
            'password': self.user_password})

    def get_access_token(self, client_id, code):
        response = self.app.get('/oauth/access_token', {
                'code': code,
                'client_id': client_id})

        response_data = json.loads(response.body)

        return response_data['access_token']

    def test_callback(self):
        ''' Test processing HTTP callback '''
        redirect, client_id = self.oauth.test_4_authorize_confidential_client()

        code = parse_qs(urlparse(redirect.location).query)['code'][0]

        access_token = self.get_access_token(client_id, code)

        callback_url = 'https://foo.example?secrettestmediagoblinparam'

        res = self.app.post('/api/submit?client_id={0}&access_token={1}'\
                .format(
                    client_id,
                    access_token), {
            'title': 'Test',
            'callback_url': callback_url},
            upload_files=[('file', GOOD_PNG)])

        assert processing.TESTS_CALLBACKS[callback_url]['state'] == u'processed'