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