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
23 from mediagoblin
import mg_globals
24 from mediagoblin
.tools
import template
, pluginapi
25 from mediagoblin
.tests
.tools
import get_test_app
, fixture_add_user
28 _log
= logging
.getLogger(__name__
)
30 def resource(filename
):
32 Borrowed from the submission tests
34 return resource_filename('mediagoblin.tests', 'test_submission/' + filename
)
37 GOOD_JPG
= resource('good.jpg')
38 GOOD_PNG
= resource('good.png')
39 EVIL_FILE
= resource('evil')
40 EVIL_JPG
= resource('evil.jpg')
41 EVIL_PNG
= resource('evil.png')
42 BIG_BLUE
= resource('bigblue.png')
45 class TestAPI(object):
47 self
.app
= get_test_app()
48 self
.db
= mg_globals
.database
50 self
.user_password
= u
'4cc355_70k3N'
51 self
.user
= fixture_add_user(u
'joapi', self
.user_password
)
56 'username': self
.user
.username
,
57 'password': self
.user_password
})
59 def get_context(self
, template_name
):
60 return template
.TEMPLATE_TEST_CONTEXT
[template_name
]
62 def http_auth_headers(self
):
63 return {'Authorization': 'Basic {0}'.format(
64 base64
.b64encode(':'.join([
66 self
.user_password
])))}
68 def do_post(self
, data
, **kwargs
):
69 url
= kwargs
.pop('url', '/api/submit')
70 do_follow
= kwargs
.pop('do_follow', False)
72 if not 'headers' in kwargs
.keys():
73 kwargs
['headers'] = self
.http_auth_headers()
75 response
= self
.app
.post(url
, data
, **kwargs
)
82 def upload_data(self
, filename
):
83 return {'upload_files': [('file', filename
)]}
85 def test_1_test_test_view(self
):
88 response
= self
.app
.get(
90 headers
=self
.http_auth_headers())
92 assert response
.body
== \
93 '{"username": "joapi", "email": "joapi@example.com"}'
95 def test_2_test_submission(self
):
98 response
= self
.do_post(
99 {'title': 'Great JPG!'},
100 **self
.upload_data(GOOD_JPG
))
102 assert response
.status_int
== 200
104 assert self
.db
.MediaEntry
.query
.filter_by(title
='Great JPG!').first()