1 # Copyright (C) 2011 Oregon State University et al.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18 from django
.test
.client
import Client
20 from django_test_tools
.users
import UserTestMixin
23 class ViewTestMixin():
25 Helper for testing standard things on a view like anonymous users,
26 unauthorized users, and permission tests
28 this works with predefined users with password=secret and permission defined
29 as needed for the specific test.
32 def assert_404(self
, url
, args
, data
=dict(), method
='get'):
34 Verifies that invalid url args will result in 404
36 @param url - url to test
37 @param args - args for the url string
38 @param data - dictionary of data to be passed to the request
39 @param method - http method to be used
42 superuser
= UserTestMixin
.create_user('superuser', is_superuser
=True)
43 method
= getattr(c
, method
)
45 # test 404s - replace each argument one at a time with a nonexistent value
46 self
.assertTrue(c
.login(username
=superuser
.username
, password
='secret'))
47 for i
in range(len(args
)):
48 temp_args
= [arg
for arg
in args
]
49 temp_args
[i
] = 'DOES.NOT.EXIST.WILL.FAIL'
50 response
= method(url
% tuple(temp_args
), data
)
51 self
.assertEqual(404, response
.status_code
)
53 def assert_401(self
, url
, args
, data
=dict(), method
='get'):
55 Asserts that an anonymous user will be required to login
57 @param url - url to test
58 @param args - args for the url string
59 @param data - dictionary of data to be passed to the request
60 @param method - http method to be used
63 method
= getattr(c
, method
)
64 response
= method(url
% args
, data
, follow
=True)
65 self
.assertEqual(200, response
.status_code
)
66 self
.assertTemplateUsed(response
, 'registration/login.html')
68 def assert_standard_fails(self
, url
, args
, data
=dict(), method
='get', login_required
=True, authorized
=True):
70 shortcut function for running standard tests:
72 * assert_401 for anonymous user
73 * assert_403 for a user with no permissions
75 @param url - url to test
76 @param args - args for the url string
77 @param data - dictionary of data to be passed to the request
78 @param method - http method to be used
79 @param login_required - run assert_401 test, default=True
80 @param authorized - run assert_403 test for unauthorized user, default=True
84 self
.assert_401(url
, args
, data
, method
)
88 unauthorized
= UserTestMixin
.create_user('unauthorized')
89 self
.assert_403(url
, args
, [unauthorized
], data
, method
)
91 # test 404s - replace each argument one at a time with a nonexistent value
92 self
.assert_404(url
, args
)
94 def assert_403(self
, url
, args
, users
, data
=dict(), method
='get'):
96 all users given to this function must fail access
98 @param url - url to test
99 @param args - args for the url string
100 @param users - list of users, all of which must result in 403
101 @param data - dictionary of data to be passed to the request
102 @param method - http method to be used
105 client_method
= getattr(c
, method
)
108 self
.assertTrue(c
.login(username
=user
.username
, password
='secret'))
109 response
= client_method(url
% args
, data
)
110 self
.assertEqual(403, response
.status_code
)
112 def assert_200(self
, url
, args
, users
, template
=None, \
113 mime
=None, tests
=None, setup
=False, data
=dict(), method
='get',
116 all users given to this function must fail access
118 @param url - url to test
119 @param args - args for the url string
120 @param users - list of users, all of which must result in 403
121 @param template - if given, template that responses should use
122 @param mime - if given, mime for response
123 @param tests - a function that executes additional tests
124 on the responses from the client
125 @param setup - call setup before each iteration
126 @param data - dictionary of data to be passed to the request
127 @param method - http method to be used
128 @param follow - follow http redirect
130 mime
= mime
if mime
else 'text/html; charset=utf-8'
132 client_method
= getattr(c
, method
)
138 self
.assertTrue(c
.login(username
=user
.username
, password
='secret'))
139 response
= client_method(url
% args
, data
, follow
=follow
)
140 self
.assertEqual(200, response
.status_code
, 'user unauthorized: %s' % user
.username
)
141 if template
is not None:
142 self
.assertTemplateUsed(response
, template
)
144 self
.assertEqual(response
['content-type'], mime
)
146 if tests
is not None:
147 tests(user
, response
)
149 def assert_view_missing_fields(self
, url
, args
, data
, fields
=None, \
150 template
=None, mime
=None, tests
=None, method
='post'):
152 Tests fields that should raise an error in a view, usually from form
155 @param url - url to test
156 @param args - args for the url string
157 @param data - dictionary of data to be passed to the request
158 @param fields - list of field keys that are required
159 @param template - if given, template that responses should use
160 @param mime - if given, mime for response
161 @param tests - a function that executes additional tests
162 on the responses from the client
163 @param method - http method to be used
165 fields
= data
.keys
if fields
is None else fields
166 mime
= mime
if mime
else 'text/html; charset=utf-8'
168 client_method
= getattr(c
, method
)
169 superuser
= UserTestMixin
.create_user('superuser', is_superuser
=True)
171 self
.assertTrue(c
.login(username
=superuser
.username
, password
='secret'))
173 # check required fields
178 response
= client_method(url
%args
, data_
)
179 self
.assertEqual(200, response
.status_code
)
180 if template
is not None:
181 self
.assertTemplateUsed(response
, template
)
183 self
.assertEqual(response
['content-type'], mime
)
184 if tests
is not None:
185 tests(superuser
, response
)
187 def assert_view_values(self
, url
, args
, data
, fields
, \
188 template
=None, mime
=None, tests
=None, method
='post'):
190 Tests fields that should raise an error for a specific type of invalid
191 data is sent. This is used for blackbox testing form validation via
192 the view it is used in.
194 @param url - url to test
195 @param args - args for the url string
196 @param data - dictionary of data to be passed to the request
197 @param fields - list of dictionaries of invalid data combinations
198 @param template - if given, template that responses should use
199 @param mime - if given, mime for response
200 @param tests - a function that executes additional tests
201 on the responses from the client
202 @param method - http method to be used
204 mime
= mime
if mime
else 'text/html; charset=utf-8'
206 client_method
= getattr(c
, method
)
207 superuser
= UserTestMixin
.create_user('superuser', is_superuser
=True)
209 self
.assertTrue(c
.login(username
=superuser
.username
, password
='secret'))
211 # check required fields
212 for values
in fields
:
215 response
= client_method(url
%args
, data_
)
216 self
.assertEqual(200, response
.status_code
)
217 if template
is not None:
218 self
.assertTemplateUsed(response
, template
)
220 self
.assertEqual(response
['content-type'], mime
)
221 if tests
is not None:
222 tests(superuser
, response
)