Commit | Line | Data |
---|---|---|
247a3b78 | 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 | ||
17 | ||
18 | import logging | |
19 | import base64 | |
58a7292f | 20 | import json |
247a3b78 | 21 | |
22 | import pytest | |
23 | ||
24 | from mediagoblin import mg_globals | |
25 | from mediagoblin.tools import template, pluginapi | |
26 | from mediagoblin.tests.tools import fixture_add_user | |
27 | from .resources import GOOD_JPG, GOOD_PNG, EVIL_FILE, EVIL_JPG, EVIL_PNG, \ | |
28 | BIG_BLUE | |
29 | ||
30 | ||
31 | _log = logging.getLogger(__name__) | |
32 | ||
33 | ||
34 | class TestAPI(object): | |
35 | def setup(self): | |
36 | self.db = mg_globals.database | |
37 | ||
38 | self.user_password = u'4cc355_70k3N' | |
c9115b89 JT |
39 | self.user = fixture_add_user(u'joapi', self.user_password, |
40 | privileges=[u'active',u'uploader']) | |
247a3b78 | 41 | |
42 | def login(self, test_app): | |
43 | test_app.post( | |
44 | '/auth/login/', { | |
45 | 'username': self.user.username, | |
46 | 'password': self.user_password}) | |
47 | ||
48 | def get_context(self, template_name): | |
49 | return template.TEMPLATE_TEST_CONTEXT[template_name] | |
50 | ||
51 | def http_auth_headers(self): | |
58a7292f CAW |
52 | return {'Authorization': ('Basic {0}'.format( |
53 | base64.b64encode((':'.join([ | |
247a3b78 | 54 | self.user.username, |
58a7292f | 55 | self.user_password])).encode('ascii')).decode()))} |
247a3b78 | 56 | |
57 | def do_post(self, data, test_app, **kwargs): | |
58 | url = kwargs.pop('url', '/api/submit') | |
59 | do_follow = kwargs.pop('do_follow', False) | |
60 | ||
61 | if not 'headers' in kwargs.keys(): | |
62 | kwargs['headers'] = self.http_auth_headers() | |
63 | ||
64 | response = test_app.post(url, data, **kwargs) | |
65 | ||
66 | if do_follow: | |
67 | response.follow() | |
68 | ||
69 | return response | |
70 | ||
71 | def upload_data(self, filename): | |
72 | return {'upload_files': [('file', filename)]} | |
73 | ||
74 | def test_1_test_test_view(self, test_app): | |
75 | self.login(test_app) | |
76 | ||
77 | response = test_app.get( | |
78 | '/api/test', | |
79 | headers=self.http_auth_headers()) | |
80 | ||
21cbf829 | 81 | assert json.loads(response.body.decode()) == { |
58a7292f | 82 | "username": "joapi", "email": "joapi@example.com"} |
247a3b78 | 83 | |
84 | def test_2_test_submission(self, test_app): | |
85 | self.login(test_app) | |
86 | ||
87 | response = self.do_post( | |
88 | {'title': 'Great JPG!'}, | |
89 | test_app, | |
90 | **self.upload_data(GOOD_JPG)) | |
91 | ||
92 | assert response.status_int == 200 | |
93 | ||
94 | assert self.db.MediaEntry.query.filter_by(title=u'Great JPG!').first() |