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