cluster nodes list was missing tablesorter.js
[ganeti_webmgr.git] / django_test_tools / views.py
blob9451112629648250cbc3a96ee76cb0edf236a5cf
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,
16 # USA.
18 from django.test.client import Client
20 from django_test_tools.users import UserTestMixin
23 class ViewTestMixin():
24 """
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.
30 """
32 def assert_404(self, url, args, data=dict(), method='get'):
33 """
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
40 """
41 c = Client()
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'):
54 """
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
61 """
62 c = Client()
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):
69 """
70 shortcut function for running standard tests:
71 * assert_404
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
81 """
82 # unauthenticated
83 if login_required:
84 self.assert_401(url, args, data, method)
86 # unauthorized
87 if authorized:
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'):
95 """
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
104 c = Client()
105 client_method = getattr(c, method)
107 for user in users:
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',
114 follow=False):
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'
131 c = Client()
132 client_method = getattr(c, method)
134 for user in users:
135 if setup:
136 self.setUp()
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)
143 if mime is not None:
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
153 validation
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'
167 c = Client()
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
174 for name in fields:
175 data_ = data.copy()
176 del data_[name]
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)
182 if mime is not None:
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'
205 c = Client()
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:
213 data_ = data.copy()
214 data_.update(values)
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)
219 if mime is not None:
220 self.assertEqual(response['content-type'], mime)
221 if tests is not None:
222 tests(superuser, response)