Oops, forgot to assign dump_old_app to self, heh.
[mediagoblin.git] / mediagoblin / tests / test_api.py
CommitLineData
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
18import logging
19import base64
20
21from pkg_resources import resource_filename
22
23from mediagoblin import mg_globals
24from mediagoblin.tools import template, pluginapi
1be247b3 25from mediagoblin.tests.tools import get_app, fixture_add_user
57c6473a
JW
26
27
28_log = logging.getLogger(__name__)
29
30def resource(filename):
31 '''
32 Borrowed from the submission tests
33 '''
34 return resource_filename('mediagoblin.tests', 'test_submission/' + filename)
35
36
37GOOD_JPG = resource('good.jpg')
38GOOD_PNG = resource('good.png')
39EVIL_FILE = resource('evil')
40EVIL_JPG = resource('evil.jpg')
41EVIL_PNG = resource('evil.png')
42BIG_BLUE = resource('bigblue.png')
43
44
45class TestAPI(object):
958080be 46 def setup(self):
1be247b3 47 self.app = get_app(dump_old_app=False)
57c6473a
JW
48 self.db = mg_globals.database
49
50 self.user_password = u'4cc355_70k3N'
51 self.user = fixture_add_user(u'joapi', self.user_password)
52
53 def login(self):
54 self.app.post(
55 '/auth/login/', {
56 'username': self.user.username,
57 'password': self.user_password})
58
59 def get_context(self, template_name):
60 return template.TEMPLATE_TEST_CONTEXT[template_name]
61
62 def http_auth_headers(self):
63 return {'Authorization': 'Basic {0}'.format(
64 base64.b64encode(':'.join([
65 self.user.username,
66 self.user_password])))}
67
68 def do_post(self, data, **kwargs):
69 url = kwargs.pop('url', '/api/submit')
70 do_follow = kwargs.pop('do_follow', False)
71
72 if not 'headers' in kwargs.keys():
73 kwargs['headers'] = self.http_auth_headers()
74
75 response = self.app.post(url, data, **kwargs)
76
77 if do_follow:
78 response.follow()
79
80 return response
81
82 def upload_data(self, filename):
83 return {'upload_files': [('file', filename)]}
84
85 def test_1_test_test_view(self):
86 self.login()
87
88 response = self.app.get(
89 '/api/test',
90 headers=self.http_auth_headers())
91
92 assert response.body == \
93 '{"username": "joapi", "email": "joapi@example.com"}'
94
95 def test_2_test_submission(self):
96 self.login()
97
98 response = self.do_post(
99 {'title': 'Great JPG!'},
100 **self.upload_data(GOOD_JPG))
101
102 assert response.status_int == 200
103
1eac751b 104 assert self.db.MediaEntry.query.filter_by(title=u'Great JPG!').first()