DataMigration: Update all hostnames in db to lowercase
[ganeti_webmgr.git] / django_test_tools / views.py
blobb39912e643083eabe571128be8d0d02390aa87e4
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
29 defined 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
46 # with a nonexistent value
47 self.assertTrue(c.login(username=superuser.username,
48 password='secret'))
49 for i in range(len(args)):
50 temp_args = [arg for arg in args]
51 temp_args[i] = 'DOES.NOT.EXIST.WILL.FAIL'
52 response = method(url % tuple(temp_args), data)
53 self.assertEqual(404, response.status_code)
55 def assert_401(self, url, args, data=dict(), method='get'):
56 """
57 Asserts that an anonymous user will be required to login
59 @param url - url to test
60 @param args - args for the url string
61 @param data - dictionary of data to be passed to the request
62 @param method - http method to be used
63 """
64 c = Client()
65 method = getattr(c, method)
66 response = method(url % args, data, follow=True)
67 self.assertEqual(200, response.status_code)
68 self.assertTemplateUsed(response, 'registration/login.html')
70 def assert_standard_fails(self, url, args,
71 data=dict(), method='get',
72 login_required=True, authorized=True):
73 """
74 shortcut function for running standard tests:
75 * assert_404
76 * assert_401 for anonymous user
77 * assert_403 for a user with no permissions
79 @param url - url to test
80 @param args - args for the url string
81 @param data - dictionary of data to be passed to the request
82 @param method - http method to be used
83 @param login_required - run assert_401 test, default=True
84 @param authorized - run assert_403 test for unauthorized user,
85 default=True
86 """
87 # unauthenticated
88 if login_required:
89 self.assert_401(url, args, data, method)
91 # unauthorized
92 if authorized:
93 unauthorized = UserTestMixin.create_user('unauthorized')
94 self.assert_403(url, args, [unauthorized], data, method)
96 # test 404s - replace each argument one at a time with a
97 # nonexistent value
98 self.assert_404(url, args, method=method)
100 def assert_403(self, url, args, users, data=dict(), method='get'):
102 all users given to this function must fail access
104 @param url - url to test
105 @param args - args for the url string
106 @param users - list of users, all of which must result in 403
107 @param data - dictionary of data to be passed to the request
108 @param method - http method to be used
110 c = Client()
111 client_method = getattr(c, method)
113 for user in users:
114 self.assertTrue(c.login(username=user.username, password='secret'))
115 response = client_method(url % args, data)
116 self.assertEqual(403, response.status_code)
118 def assert_200(self, url, args, users, template=None,
119 mime=None, tests=None, setup=False, data=dict(),
120 method='get', follow=False):
122 @param url - url to test
123 @param args - args for the url string
124 @param users - list of users, all of which should result in 200
125 @param template - if given, template that responses should use
126 @param mime - if given, mime for response
127 @param tests - a function that executes additional tests
128 on the responses from the client
129 @param setup - call setup before each iteration
130 @param data - dictionary of data to be passed to the request
131 @param method - http method to be used
132 @param follow - follow http redirect
134 mime = mime if mime else 'text/html; charset=utf-8'
135 c = Client()
136 client_method = getattr(c, method)
138 for user in users:
139 if setup:
140 self.setUp()
142 self.assertTrue(c.login(username=user.username, password='secret'))
143 response = client_method(url % args, data, follow=follow)
144 self.assertEqual(200, response.status_code,
145 'user unauthorized: %s' % user.username)
146 if template is not None:
147 self.assertTemplateUsed(response, template)
148 if mime is not None:
149 self.assertEqual(response['content-type'], mime)
151 if tests is not None:
152 tests(user, response)
154 def assert_view_missing_fields(self, url, args, data, fields=None,
155 template=None, mime=None, tests=None,
156 method='post'):
158 Tests fields that should raise an error in a view, usually from form
159 validation
161 @param url - url to test
162 @param args - args for the url string
163 @param data - dictionary of data to be passed to the request
164 @param fields - list of field keys that are required
165 @param template - if given, template that responses should use
166 @param mime - if given, mime for response
167 @param tests - a function that executes additional tests
168 on the responses from the client
169 @param method - http method to be used
171 fields = data.keys if fields is None else fields
172 mime = mime if mime else 'text/html; charset=utf-8'
173 c = Client()
174 client_method = getattr(c, method)
175 superuser = UserTestMixin.create_user('superuser', is_superuser=True)
177 self.assertTrue(c.login(username=superuser.username,
178 password='secret'))
180 # check required fields
181 for name in fields:
182 data_ = data.copy()
183 del data_[name]
185 response = client_method(url % args, data_)
186 self.assertEqual(200, response.status_code)
187 if template is not None:
188 self.assertTemplateUsed(response, template)
189 if mime is not None:
190 self.assertEqual(response['content-type'], mime)
191 if tests is not None:
192 tests(superuser, response)
194 def assert_view_values(self, url, args, data, fields,
195 template=None, mime=None, tests=None,
196 method='post'):
198 Tests fields that should raise an error for a specific type of invalid
199 data is sent. This is used for blackbox testing form validation via
200 the view it is used in.
202 @param url - url to test
203 @param args - args for the url string
204 @param data - dictionary of data to be passed to the request
205 @param fields - list of dictionaries of invalid data combinations
206 @param template - if given, template that responses should use
207 @param mime - if given, mime for response
208 @param tests - a function that executes additional tests
209 on the responses from the client
210 @param method - http method to be used
212 mime = mime if mime else 'text/html; charset=utf-8'
213 c = Client()
214 client_method = getattr(c, method)
215 superuser = UserTestMixin.create_user('superuser', is_superuser=True)
217 self.assertTrue(c.login(username=superuser.username,
218 password='secret'))
220 # check required fields
221 for values in fields:
222 data_ = data.copy()
223 data_.update(values)
224 response = client_method(url % args, data_)
225 self.assertEqual(200, response.status_code)
226 if template is not None:
227 self.assertTemplateUsed(response, template)
228 if mime is not None:
229 self.assertEqual(response['content-type'], mime)
230 if tests is not None:
231 tests(superuser, response)