Mercurial > p > roundup > code
comparison test/test_liveserver.py @ 6977:ff2c8b430738
flake8 - remove re.compile from method arg + test + doc
changed 2 methods defined like:
def method(..., dre=re.compile(r'...')):
moved re.compile to module variables and passed the var name
def method(..., dre=var_name):
while doing this I found out that a url of .../issue0001 will behave
like .../issue1. Who knew. Documented in customizing.
Tested same in test_liveserver. Added msg1 as well so I could verify
msg0001 worked.
Also added some range tests as well.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Wed, 14 Sep 2022 17:48:51 -0400 |
| parents | cb2ed1e8c852 |
| children | 3c4047cdc77a |
comparison
equal
deleted
inserted
replaced
| 6976:3917ae82fb24 | 6977:ff2c8b430738 |
|---|---|
| 1 import shutil, errno, pytest, json, gzip, mimetypes, os, re | 1 import shutil, errno, pytest, json, gzip, mimetypes, os, re |
| 2 | 2 |
| 3 from roundup import date as rdate | |
| 3 from roundup import i18n | 4 from roundup import i18n |
| 4 from roundup import password | 5 from roundup import password |
| 5 from roundup.anypy.strings import b2s | 6 from roundup.anypy.strings import b2s |
| 6 from roundup.cgi.wsgi_handler import RequestDispatcher | 7 from roundup.cgi.wsgi_handler import RequestDispatcher |
| 7 from .wsgi_liveserver import LiveServerTestCase | 8 from .wsgi_liveserver import LiveServerTestCase |
| 92 | 93 |
| 93 # add an issue to allow testing retrieval. | 94 # add an issue to allow testing retrieval. |
| 94 # also used for text searching. | 95 # also used for text searching. |
| 95 result = cls.db.issue.create(title="foo bar RESULT") | 96 result = cls.db.issue.create(title="foo bar RESULT") |
| 96 | 97 |
| 98 # add a message to allow retrieval | |
| 99 result = cls.db.msg.create(author = "1", | |
| 100 content = "a message foo bar RESULT", | |
| 101 date=rdate.Date(), | |
| 102 messageid="test-msg-id") | |
| 97 cls.db.commit() | 103 cls.db.commit() |
| 98 cls.db.close() | 104 cls.db.close() |
| 99 | 105 |
| 100 # Force locale config to find locales in checkout not in | 106 # Force locale config to find locales in checkout not in |
| 101 # installed directories | 107 # installed directories |
| 189 self.assertTrue(b'Roundup' in f.content) | 195 self.assertTrue(b'Roundup' in f.content) |
| 190 self.assertTrue(b'Aufgabenliste' in f.content) | 196 self.assertTrue(b'Aufgabenliste' in f.content) |
| 191 self.assertTrue(b'dauerhaft anmelden?' in f.content) | 197 self.assertTrue(b'dauerhaft anmelden?' in f.content) |
| 192 | 198 |
| 193 def test_byte_Ranges(self): | 199 def test_byte_Ranges(self): |
| 194 """ Roundup only handles one simple two number range. | 200 """ Roundup only handles one simple two number range, or |
| 201 a single number to start from: | |
| 195 Range: 10-20 | 202 Range: 10-20 |
| 196 | 203 Range: 10- |
| 197 The following are not supported. | 204 |
| 205 The following is not supported. | |
| 198 Range: 10-20, 25-30 | 206 Range: 10-20, 25-30 |
| 199 Range: 10- | 207 Range: -10 |
| 200 | 208 |
| 201 Also If-Range only supports strong etags not dates or weak etags. | 209 Also If-Range only supports strong etags not dates or weak etags. |
| 202 | 210 |
| 203 """ | 211 """ |
| 204 | 212 |
| 218 # compression disabled for length < 100, so we can use 11 here | 226 # compression disabled for length < 100, so we can use 11 here |
| 219 self.assertEqual(f.headers['content-length'], '11') | 227 self.assertEqual(f.headers['content-length'], '11') |
| 220 self.assertEqual(f.headers['content-range'], | 228 self.assertEqual(f.headers['content-range'], |
| 221 "bytes 0-10/%s"%expected_length) | 229 "bytes 0-10/%s"%expected_length) |
| 222 | 230 |
| 231 # get bytes 11-21 unconditionally (0 index really??) | |
| 232 hdrs = {"Range": "bytes=10-20"} | |
| 233 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) | |
| 234 self.assertEqual(f.status_code, 206) | |
| 235 self.assertEqual(f.content, b"ge styles *") | |
| 236 # compression disabled for length < 100, so we can use 11 here | |
| 237 self.assertEqual(f.headers['content-length'], '11') | |
| 238 self.assertEqual(f.headers['content-range'], | |
| 239 "bytes 10-20/%s"%expected_length) | |
| 240 | |
| 241 # get all bytest starting from 11 | |
| 242 hdrs = {"Range": "bytes=11-"} | |
| 243 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) | |
| 244 self.assertEqual(f.status_code, 206) | |
| 245 self.assertEqual(f.headers['content-range'], | |
| 246 "bytes 11-%s/%s"%(int(expected_length) - 1, | |
| 247 expected_length)) | |
| 248 self.assertIn("SHA:", f.content) # detect sha sum at end of file | |
| 249 | |
| 223 # conditional request 11 bytes since etag matches 206 code | 250 # conditional request 11 bytes since etag matches 206 code |
| 251 hdrs = {"Range": "bytes=0-10"} | |
| 224 hdrs['If-Range'] = etag | 252 hdrs['If-Range'] = etag |
| 225 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) | 253 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) |
| 226 self.assertEqual(f.status_code, 206) | 254 self.assertEqual(f.status_code, 206) |
| 227 self.assertEqual(f.content, b"/* main pag") | 255 self.assertEqual(f.content, b"/* main pag") |
| 228 # compression disabled for length < 100, so we can use 11 here | 256 # compression disabled for length < 100, so we can use 11 here |
| 260 print(hdrs) | 288 print(hdrs) |
| 261 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) | 289 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) |
| 262 self.assertEqual(f.status_code, 416) | 290 self.assertEqual(f.status_code, 416) |
| 263 self.assertEqual(f.headers['content-range'], | 291 self.assertEqual(f.headers['content-range'], |
| 264 "bytes */%s"%expected_length) | 292 "bytes */%s"%expected_length) |
| 265 | 293 |
| 294 # invalid range multiple ranges | |
| 295 hdrs['Range'] = "bytes=0-10, 20-45" | |
| 296 print(hdrs) | |
| 297 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) | |
| 298 self.assertEqual(f.status_code, 200) | |
| 299 self.assertNotIn('content-range', f.headers, | |
| 300 'content-range should not be present') | |
| 301 self.assertIn("SHA:", f.content) # detect sha sum at end of file | |
| 302 | |
| 303 # invalid range is single number not number followed by - | |
| 304 hdrs['Range'] = "bytes=1" | |
| 305 print(hdrs) | |
| 306 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) | |
| 307 self.assertEqual(f.status_code, 200) | |
| 308 self.assertNotIn('content-range', f.headers, | |
| 309 'content-range should not be present') | |
| 310 self.assertIn("SHA:", f.content) # detect sha sum at end of file | |
| 311 | |
| 312 # range is invalid first number not a number | |
| 313 hdrs['Range'] = "bytes=boom-99" # bad first value | |
| 314 print(hdrs) | |
| 315 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) | |
| 316 self.assertEqual(f.status_code, 200) | |
| 317 self.assertNotIn('content-range', f.headers, | |
| 318 'content-range should not be present') | |
| 319 self.assertIn("SHA:", f.content) # detect sha sum at end of file | |
| 320 | |
| 321 # range is invalid last number not a number | |
| 322 hdrs['Range'] = "bytes=1-boom" # bad last value | |
| 323 print(hdrs) | |
| 324 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) | |
| 325 self.assertEqual(f.status_code, 200) | |
| 326 self.assertNotIn('content-range', f.headers, | |
| 327 'content-range should not be present') | |
| 328 self.assertIn("SHA:", f.content) # detect sha sum at end of file | |
| 329 | |
| 330 # range is invalid first position empty | |
| 331 hdrs['Range'] = "bytes=-11" # missing first value | |
| 332 print(hdrs) | |
| 333 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) | |
| 334 self.assertEqual(f.status_code, 200) | |
| 335 self.assertNotIn('content-range', f.headers, | |
| 336 'content-range should not be present') | |
| 337 self.assertIn("SHA:", f.content) # detect sha sum at end of file | |
| 338 | |
| 339 # range is invalid #2 < #1 | |
| 340 hdrs['Range'] = "bytes=11-1" # inverted range | |
| 341 print(hdrs) | |
| 342 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) | |
| 343 self.assertEqual(f.status_code, 200) | |
| 344 self.assertNotIn('content-range', f.headers, | |
| 345 'content-range should not be present') | |
| 346 self.assertIn("SHA:", f.content) # detect sha sum at end of file | |
| 347 | |
| 348 # range is invalid negative first number | |
| 349 hdrs['Range'] = "bytes=-1-11" # negative first number | |
| 350 print(hdrs) | |
| 351 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) | |
| 352 self.assertEqual(f.status_code, 200) | |
| 353 self.assertNotIn('content-range', f.headers, | |
| 354 'content-range should not be present') | |
| 355 self.assertIn("SHA:", f.content) # detect sha sum at end of file | |
| 356 | |
| 357 # range is invalid negative second number | |
| 358 hdrs['Range'] = "bytes=1--11" # negative second number | |
| 359 print(hdrs) | |
| 360 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) | |
| 361 self.assertEqual(f.status_code, 200) | |
| 362 self.assertNotIn('content-range', f.headers, | |
| 363 'content-range should not be present') | |
| 364 self.assertIn("SHA:", f.content) # detect sha sum at end of file | |
| 365 | |
| 366 # range is unsupported units | |
| 367 hdrs['Range'] = "badunits=1-11" | |
| 368 print(hdrs) | |
| 369 f = requests.get(self.url_base() + "/@@file/style.css", headers=hdrs) | |
| 370 self.assertEqual(f.status_code, 200) | |
| 371 self.assertNotIn('content-range', f.headers, | |
| 372 'content-range should not be present') | |
| 373 self.assertIn("SHA:", f.content) # detect sha sum at end of file | |
| 374 | |
| 375 | |
| 376 # valid range, invalid file | |
| 377 hdrs['Range'] = "bytes=0-11" | |
| 378 print(hdrs) | |
| 379 f = requests.get(self.url_base() + "/@@file/style_nope.css", | |
| 380 headers=hdrs) | |
| 381 self.assertEqual(f.status_code, 404) | |
| 382 self.assertNotIn('content-range', f.headers, | |
| 383 'content-range should not be present') | |
| 384 | |
| 266 def test_rest_preflight_collection(self): | 385 def test_rest_preflight_collection(self): |
| 267 # no auth for rest csrf preflight | 386 # no auth for rest csrf preflight |
| 268 f = requests.options(self.url_base() + '/rest/data/user', | 387 f = requests.options(self.url_base() + '/rest/data/user', |
| 269 headers = {'content-type': "", | 388 headers = {'content-type': "", |
| 270 'x-requested-with': "rest", | 389 'x-requested-with': "rest", |
| 560 # etc. from f.headers. | 679 # etc. from f.headers. |
| 561 self.assertDictEqual({ key: value for (key, value) in f.headers.items() if key in expected }, expected) | 680 self.assertDictEqual({ key: value for (key, value) in f.headers.items() if key in expected }, expected) |
| 562 | 681 |
| 563 | 682 |
| 564 def test_load_issue1(self): | 683 def test_load_issue1(self): |
| 565 f = requests.get(self.url_base() + '/issue1>', | 684 import pdb; pdb.set_trace() |
| 685 for tail in [ | |
| 686 '/issue1', # normal url | |
| 687 '/issue00001', # leading 0's should be stripped from id | |
| 688 '/issue1>' # surprise this works too, should it?? | |
| 689 ]: | |
| 690 f = requests.get(self.url_base() + tail, | |
| 691 headers = { 'Accept-Encoding': 'gzip', | |
| 692 'Accept': '*/*'}) | |
| 693 | |
| 694 self.assertIn(b'foo bar RESULT', f.content) | |
| 695 self.assertEqual(f.status_code, 200) | |
| 696 | |
| 697 def test_load_msg1(self): | |
| 698 # leading 0's should be stripped from id | |
| 699 f = requests.get(self.url_base() + '/msg0001', | |
| 566 headers = { 'Accept-Encoding': 'gzip', | 700 headers = { 'Accept-Encoding': 'gzip', |
| 567 'Accept': '*/*'}) | 701 'Accept': '*/*'}) |
| 568 | 702 |
| 569 self.assertIn(b'foo bar RESULT', f.content) | 703 self.assertIn(b'foo bar RESULT', f.content) |
| 570 self.assertEqual(f.status_code, 200) | 704 self.assertEqual(f.status_code, 200) |
| 1171 # re-open the database to get the updated INDEXER | 1305 # re-open the database to get the updated INDEXER |
| 1172 cls.db = cls.instance.open('admin') | 1306 cls.db = cls.instance.open('admin') |
| 1173 | 1307 |
| 1174 result = cls.db.issue.create(title="foo bar RESULT") | 1308 result = cls.db.issue.create(title="foo bar RESULT") |
| 1175 | 1309 |
| 1310 # add a message to allow retrieval | |
| 1311 result = cls.db.msg.create(author = "1", | |
| 1312 content = "a message foo bar RESULT", | |
| 1313 date=rdate.Date(), | |
| 1314 messageid="test-msg-id") | |
| 1315 | |
| 1176 cls.db.commit() | 1316 cls.db.commit() |
| 1177 cls.db.close() | 1317 cls.db.close() |
| 1178 | 1318 |
| 1179 # Force locale config to find locales in checkout not in | 1319 # Force locale config to find locales in checkout not in |
| 1180 # installed directories | 1320 # installed directories |
