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