Abort and print when dependencies not met.
[ganeti_webmgr.git] / django_test_tools / views.py
blob0bd7fdddf34e3c71d235be5043e38b9e0bf1b2c5
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, method=method)
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 @param url - url to test
117 @param args - args for the url string
118 @param users - list of users, all of which should result in 200
119 @param template - if given, template that responses should use
120 @param mime - if given, mime for response
121 @param tests - a function that executes additional tests
122 on the responses from the client
123 @param setup - call setup before each iteration
124 @param data - dictionary of data to be passed to the request
125 @param method - http method to be used
126 @param follow - follow http redirect
128 mime = mime if mime else 'text/html; charset=utf-8'
129 c = Client()
130 client_method = getattr(c, method)
132 for user in users:
133 if setup:
134 self.setUp()
136 self.assertTrue(c.login(username=user.username, password='secret'))
137 response = client_method(url % args, data, follow=follow)
138 self.assertEqual(200, response.status_code, 'user unauthorized: %s' % user.username )
139 if template is not None:
140 self.assertTemplateUsed(response, template)
141 if mime is not None:
142 self.assertEqual(response['content-type'], mime)
144 if tests is not None:
145 tests(user, response)
147 def assert_view_missing_fields(self, url, args, data, fields=None, \
148 template=None, mime=None, tests=None, method='post'):
150 Tests fields that should raise an error in a view, usually from form
151 validation
153 @param url - url to test
154 @param args - args for the url string
155 @param data - dictionary of data to be passed to the request
156 @param fields - list of field keys that are required
157 @param template - if given, template that responses should use
158 @param mime - if given, mime for response
159 @param tests - a function that executes additional tests
160 on the responses from the client
161 @param method - http method to be used
163 fields = data.keys if fields is None else fields
164 mime = mime if mime else 'text/html; charset=utf-8'
165 c = Client()
166 client_method = getattr(c, method)
167 superuser = UserTestMixin.create_user('superuser', is_superuser=True)
169 self.assertTrue(c.login(username=superuser.username, password='secret'))
171 # check required fields
172 for name in fields:
173 data_ = data.copy()
174 del data_[name]
176 response = client_method(url%args, data_)
177 self.assertEqual(200, response.status_code )
178 if template is not None:
179 self.assertTemplateUsed(response, template)
180 if mime is not None:
181 self.assertEqual(response['content-type'], mime)
182 if tests is not None:
183 tests(superuser, response)
185 def assert_view_values(self, url, args, data, fields, \
186 template=None, mime=None, tests=None, method='post'):
188 Tests fields that should raise an error for a specific type of invalid
189 data is sent. This is used for blackbox testing form validation via
190 the view it is used in.
192 @param url - url to test
193 @param args - args for the url string
194 @param data - dictionary of data to be passed to the request
195 @param fields - list of dictionaries of invalid data combinations
196 @param template - if given, template that responses should use
197 @param mime - if given, mime for response
198 @param tests - a function that executes additional tests
199 on the responses from the client
200 @param method - http method to be used
202 mime = mime if mime else 'text/html; charset=utf-8'
203 c = Client()
204 client_method = getattr(c, method)
205 superuser = UserTestMixin.create_user('superuser', is_superuser=True)
207 self.assertTrue(c.login(username=superuser.username, password='secret'))
209 # check required fields
210 for values in fields:
211 data_ = data.copy()
212 data_.update(values)
213 response = client_method(url%args, data_)
214 self.assertEqual(200, response.status_code )
215 if template is not None:
216 self.assertTemplateUsed(response, template)
217 if mime is not None:
218 self.assertEqual(response['content-type'], mime)
219 if tests is not None:
220 tests(superuser, response)