comparison test/test_liveserver.py @ 8218:32aaf5dc562b

fix(REST): issue2551383; improve errors for bad json, fix PUT docs While adding fuzz testing for email addresses via REST /rest/data/user/1/address, I had an error when setting the address to the same value it currently had. Traced this to a bug in userauditor.py. Fixed the bug. Documented in upgrading.txt. While trying to track down issue, I realized invalid json was being accepted without error. So I fixed the code that parses the json and have it return an error. Also modified some tests that broke (used invalid json, or passed body (e.g. DELETE) but shouldn't have. Add tests for bad json to verify new code. Fixed test that wasn't initializing the body_file in each loop, so the test wasn't actually supplying a body. Also realised PUT documentation was not correct. Output format isn't quite like GET. Fuss tests for email address also added.
author John Rouillard <rouilj@ieee.org>
date Tue, 17 Dec 2024 19:42:46 -0500
parents cd76d5d59c37
children 9404d56d830f
comparison
equal deleted inserted replaced
8217:cd76d5d59c37 8218:32aaf5dc562b
214 @given(sampled_from(['@verbose', '@page_size', '@page_index']), 214 @given(sampled_from(['@verbose', '@page_size', '@page_index']),
215 text(min_size=1)) 215 text(min_size=1))
216 @example("@verbose", "1#") 216 @example("@verbose", "1#")
217 @example("@verbose", "#1stuff") 217 @example("@verbose", "#1stuff")
218 @settings(max_examples=_max_examples, 218 @settings(max_examples=_max_examples,
219 deadline=10000) # 10000ms 219 deadline=10000) # in ms
220 def test_class_url_param_accepting_integer_values(self, param, value): 220 def test_class_url_param_accepting_integer_values(self, param, value):
221 """Tests all integer args for rest url. @page_* is the 221 """Tests all integer args for rest url. @page_* is the
222 same code for all *. 222 same code for all *.
223 """ 223 """
224 session, _response = self.create_login_session() 224 session, _response = self.create_login_session()
242 242
243 @given(sampled_from(['@verbose']), text(min_size=1)) 243 @given(sampled_from(['@verbose']), text(min_size=1))
244 @example("@verbose", "10#") 244 @example("@verbose", "10#")
245 @example("@verbose", u'Ø\U000dd990') 245 @example("@verbose", u'Ø\U000dd990')
246 @settings(max_examples=_max_examples, 246 @settings(max_examples=_max_examples,
247 deadline=10000) # 10000ms 247 deadline=10000) # in ms
248 def test_element_url_param_accepting_integer_values(self, param, value): 248 def test_element_url_param_accepting_integer_values(self, param, value):
249 """Tests args accepting int for rest url. 249 """Tests args accepting int for rest url.
250 """ 250 """
251 session, _response = self.create_login_session() 251 session, _response = self.create_login_session()
252 url = '%s/rest/data/status/1' % (self.url_base()) 252 url = '%s/rest/data/status/1' % (self.url_base())
264 if value[0] in ('#', '&'): 264 if value[0] in ('#', '&'):
265 self.assertEqual(f.status_code, 200) 265 self.assertEqual(f.status_code, 200)
266 else: 266 else:
267 # invalid value for param 267 # invalid value for param
268 self.assertEqual(f.status_code, 400) 268 self.assertEqual(f.status_code, 400)
269
270 @given(emails())
271 @settings(max_examples=_max_examples,
272 deadline=10000) # in ms
273 def test_email_param(self,email):
274 session, _response = self.create_login_session()
275 url = '%s/rest/data/user/1/address' % (self.url_base())
276 headers = {"Accept": "application/json",
277 "Content-Type": "application/json",
278 "x-requested-with": "rest",
279 "Origin": self.url_base(),
280 "Referer": self.url_base()
281 }
282
283 #--header 'If-Match: "e2e6cc43c3475a4a3d9e5343617c11c3"' \
284
285 f = session.get(url)
286 stored_email = f.json()['data']['data']
287 headers['If-Match'] = f.headers['etag']
288
289 payload = {'data': email}
290 f = session.put(url, json=payload, headers=headers)
291
292 self.assertEqual(f.status_code, 200)
293
294 if stored_email == email:
295 # if the email we are setting is the same as present, we
296 # don't make a change so the attribute dict is empty aka false.
297 self.assertEqual(f.json()['data']['attribute'], {})
298 else:
299 self.assertEqual(f.json()['data']['attribute']['address'],
300 email)
301
269 302
270 @skip_requests 303 @skip_requests
271 class BaseTestCases(WsgiSetup, ClientSetup): 304 class BaseTestCases(WsgiSetup, ClientSetup):
272 """Class with all tests to run against wsgi server. Is reused when 305 """Class with all tests to run against wsgi server. Is reused when
273 wsgi server is started with various feature flags 306 wsgi server is started with various feature flags

Roundup Issue Tracker: http://roundup-tracker.org/