1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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/>.
20 from nose
.tools
import assert_equal
22 from mediagoblin
.auth
import lib
as auth_lib
23 from mediagoblin
.tests
.tools
import setup_fresh_app
, get_test_app
24 from mediagoblin
import mg_globals
25 from mediagoblin
import util
27 IMAGE_ROOT
= getcwd() + '/mediagoblin/tests/test_submission/'
36 # - Define test files as globals
37 # - supported mime types
38 # - unsupported mime type with supported extension
39 # - Remove any imports that aren't neccessary
43 self
.test_app
= get_test_app()
45 # TODO: Possibly abstract into a decorator like:
46 # @as_authenticated_user('chris')
47 test_user
= mg_globals
.database
.User()
48 test_user
['username'] = u
'chris'
49 test_user
['email'] = u
'chris@example.com'
50 test_user
['email_verified'] = True
51 test_user
['status'] = u
'active'
52 test_user
['pw_hash'] = auth_lib
.bcrypt_gen_password_hash('toast')
60 def test_missing_fields(self
):
63 util
.clear_test_template_context()
64 response
= self
.test_app
.post(
66 context
= util
.TEMPLATE_TEST_CONTEXT
['mediagoblin/submit/start.html']
67 form
= context
['submit_form']
68 assert form
.file.errors
== [u
'You must provide a file.']
72 util
.clear_test_template_context()
73 response
= self
.test_app
.post(
75 'title': 'test title'})
76 context
= util
.TEMPLATE_TEST_CONTEXT
['mediagoblin/submit/start.html']
77 form
= context
['submit_form']
78 assert form
.file.errors
== [u
'You must provide a file.']
81 def test_normal_uploads(self
):
83 # upload_files is for file uploads. It should be a list of
84 # [(fieldname, filename, file_content)]. You can also use
85 # just [(fieldname, filename)] and the file content will be
90 util
.clear_test_template_context()
91 response
= self
.test_app
.post(
93 'title': 'Normal upload 1'
95 'file', IMAGE_ROOT
+ GOOD_JPG
)])
97 # User should be redirected
100 urlparse
.urlsplit(response
.location
)[2],
102 assert util
.TEMPLATE_TEST_CONTEXT
.has_key(
103 'mediagoblin/submit/success.html')
107 util
.clear_test_template_context()
108 response
= self
.test_app
.post(
110 'title': 'Normal upload 2'
112 'file', IMAGE_ROOT
+ GOOD_PNG
)])
116 urlparse
.urlsplit(response
.location
)[2],
118 assert util
.TEMPLATE_TEST_CONTEXT
.has_key(
119 'mediagoblin/submit/success.html')
121 # TODO: Test additional supported formats
124 def test_malicious_uploads(self
):
125 # Test non-suppoerted file with non-supported extension
126 # -----------------------------------------------------
127 util
.clear_test_template_context()
128 response
= self
.test_app
.post(
130 'title': 'Malicious Upload 2'
132 'file', IMAGE_ROOT
+ EVIL_FILE
)])
134 context
= util
.TEMPLATE_TEST_CONTEXT
['mediagoblin/submit/start.html']
135 form
= context
['submit_form']
136 assert form
.file.errors
== ['The file doesn\'t seem to be an image!']
138 # Test non-supported file with .jpg extension
139 # -------------------------------------------
140 util
.clear_test_template_context()
141 response
= self
.test_app
.post(
143 'title': 'Malicious Upload 2'
145 'file', IMAGE_ROOT
+ EVIL_JPG
)])
147 context
= util
.TEMPLATE_TEST_CONTEXT
['mediagoblin/submit/start.html']
148 form
= context
['submit_form']
149 assert form
.file.errors
== ['The file doesn\'t seem to be an image!']
151 # Test non-supported file with .png extension
152 # -------------------------------------------
153 util
.clear_test_template_context()
154 response
= self
.test_app
.post(
156 'title': 'Malicious Upload 3'
158 'file', IMAGE_ROOT
+ EVIL_PNG
)])
160 context
= util
.TEMPLATE_TEST_CONTEXT
['mediagoblin/submit/start.html']
161 form
= context
['submit_form']
162 assert form
.file.errors
== ['The file doesn\'t seem to be an image!']