finished basic submission testing
[mediagoblin.git] / mediagoblin / tests / test_submission.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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 import urlparse
18 from os import getcwd
19
20 from nose.tools import assert_equal
21
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
26
27 IMAGE_ROOT = getcwd() + '/mediagoblin/tests/test_submission/'
28 GOOD_JPG = 'good.jpg'
29 GOOD_PNG = 'good.png'
30 EVIL_FILE = 'evil'
31 EVIL_JPG = 'evil.jpg'
32 EVIL_PNG = 'evil.png'
33
34
35 # TODO:
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
40
41 class TestSubmission:
42 def setUp(self):
43 self.test_app = get_test_app()
44
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')
53 test_user.save()
54
55 self.test_app.post(
56 '/auth/login/', {
57 'username': u'chris',
58 'password': 'toast'})
59
60 def test_missing_fields(self):
61 # Test blank form
62 # ---------------
63 util.clear_test_template_context()
64 response = self.test_app.post(
65 '/submit/', {})
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.']
69
70 # Test blank file
71 # ---------------
72 util.clear_test_template_context()
73 response = self.test_app.post(
74 '/submit/', {
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.']
79
80
81 def test_normal_uploads(self):
82 # FYI:
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
86 # read from disk.
87
88 # Test JPG
89 # --------
90 util.clear_test_template_context()
91 response = self.test_app.post(
92 '/submit/', {
93 'title': 'Normal upload 1'
94 }, upload_files=[(
95 'file', IMAGE_ROOT + GOOD_JPG)])
96
97 # User should be redirected
98 response.follow()
99 assert_equal(
100 urlparse.urlsplit(response.location)[2],
101 '/submit/success/')
102 assert util.TEMPLATE_TEST_CONTEXT.has_key(
103 'mediagoblin/submit/success.html')
104
105 # Test PNG
106 # --------
107 util.clear_test_template_context()
108 response = self.test_app.post(
109 '/submit/', {
110 'title': 'Normal upload 2'
111 }, upload_files=[(
112 'file', IMAGE_ROOT + GOOD_PNG)])
113
114 response.follow()
115 assert_equal(
116 urlparse.urlsplit(response.location)[2],
117 '/submit/success/')
118 assert util.TEMPLATE_TEST_CONTEXT.has_key(
119 'mediagoblin/submit/success.html')
120
121 # TODO: Test additional supported formats
122
123
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(
129 '/submit/', {
130 'title': 'Malicious Upload 2'
131 }, upload_files=[(
132 'file', IMAGE_ROOT + EVIL_FILE)])
133
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!']
137
138 # Test non-supported file with .jpg extension
139 # -------------------------------------------
140 util.clear_test_template_context()
141 response = self.test_app.post(
142 '/submit/', {
143 'title': 'Malicious Upload 2'
144 }, upload_files=[(
145 'file', IMAGE_ROOT + EVIL_JPG)])
146
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!']
150
151 # Test non-supported file with .png extension
152 # -------------------------------------------
153 util.clear_test_template_context()
154 response = self.test_app.post(
155 '/submit/', {
156 'title': 'Malicious Upload 3'
157 }, upload_files=[(
158 'file', IMAGE_ROOT + EVIL_PNG)])
159
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!']
163