cff25776851f68ff5d8a756502ce659f5749382c
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
21 from pkg_resources
import resource_filename
25 from mediagoblin
import mg_globals
26 from mediagoblin
.tools
import template
, pluginapi
27 from mediagoblin
.tests
.tools
import fixture_add_user
30 _log
= logging
.getLogger(__name__
)
32 def resource(filename
):
34 Borrowed from the submission tests
36 return resource_filename('mediagoblin.tests', 'test_submission/' + filename
)
39 GOOD_JPG
= resource('good.jpg')
40 GOOD_PNG
= resource('good.png')
41 EVIL_FILE
= resource('evil')
42 EVIL_JPG
= resource('evil.jpg')
43 EVIL_PNG
= resource('evil.png')
44 BIG_BLUE
= resource('bigblue.png')
47 class TestAPI(object):
49 self
.db
= mg_globals
.database
51 self
.user_password
= u
'4cc355_70k3N'
52 self
.user
= fixture_add_user(u
'joapi', self
.user_password
)
54 def login(self
, test_app
):
57 'username': self
.user
.username
,
58 'password': self
.user_password
})
60 def get_context(self
, template_name
):
61 return template
.TEMPLATE_TEST_CONTEXT
[template_name
]
63 def http_auth_headers(self
):
64 return {'Authorization': 'Basic {0}'.format(
65 base64
.b64encode(':'.join([
67 self
.user_password
])))}
69 def do_post(self
, data
, test_app
, **kwargs
):
70 url
= kwargs
.pop('url', '/api/submit')
71 do_follow
= kwargs
.pop('do_follow', False)
73 if not 'headers' in kwargs
.keys():
74 kwargs
['headers'] = self
.http_auth_headers()
76 response
= test_app
.post(url
, data
, **kwargs
)
83 def upload_data(self
, filename
):
84 return {'upload_files': [('file', filename
)]}
86 def test_1_test_test_view(self
, test_app
):
89 response
= test_app
.get(
91 headers
=self
.http_auth_headers())
93 assert response
.body
== \
94 '{"username": "joapi", "email": "joapi@example.com"}'
96 def test_2_test_submission(self
, test_app
):
99 response
= self
.do_post(
100 {'title': 'Great JPG!'},
102 **self
.upload_data(GOOD_JPG
))
104 assert response
.status_int
== 200
106 assert self
.db
.MediaEntry
.query
.filter_by(title
=u
'Great JPG!').first()