Mercurial > p > roundup > code
comparison test/rest_common.py @ 5601:fcbeff272828 REST-rebased
Integrate REST tests into db framework
| author | Ralf Schlatterbeck <rsc@runtux.com> |
|---|---|
| date | Wed, 30 Jan 2019 13:58:14 +0100 |
| parents | test/test_rest.py@e2c74d8121f3 |
| children | c40d04915e23 |
comparison
equal
deleted
inserted
replaced
| 5600:e2c74d8121f3 | 5601:fcbeff272828 |
|---|---|
| 1 import unittest | |
| 2 import os | |
| 3 import shutil | |
| 4 import errno | |
| 5 | |
| 6 from roundup.cgi.exceptions import * | |
| 7 from roundup import password, hyperdb | |
| 8 from roundup.rest import RestfulInstance | |
| 9 from roundup.backends import list_backends | |
| 10 from roundup.cgi import client | |
| 11 import random | |
| 12 | |
| 13 import db_test_base | |
| 14 | |
| 15 NEEDS_INSTANCE = 1 | |
| 16 | |
| 17 | |
| 18 class TestCase(): | |
| 19 | |
| 20 backend = None | |
| 21 | |
| 22 def setUp(self): | |
| 23 self.dirname = '_test_rest' | |
| 24 # set up and open a tracker | |
| 25 self.instance = db_test_base.setupTracker(self.dirname, self.backend) | |
| 26 | |
| 27 # open the database | |
| 28 self.db = self.instance.open('admin') | |
| 29 | |
| 30 # Get user id (user4 maybe). Used later to get data from db. | |
| 31 self.joeid = self.db.user.create( | |
| 32 username='joe', | |
| 33 password=password.Password('random'), | |
| 34 address='random@home.org', | |
| 35 realname='Joe Random', | |
| 36 roles='User' | |
| 37 ) | |
| 38 | |
| 39 self.db.commit() | |
| 40 self.db.close() | |
| 41 self.db = self.instance.open('joe') | |
| 42 | |
| 43 self.db.tx_Source = 'web' | |
| 44 | |
| 45 self.db.issue.addprop(tx_Source=hyperdb.String()) | |
| 46 self.db.msg.addprop(tx_Source=hyperdb.String()) | |
| 47 | |
| 48 self.db.post_init() | |
| 49 | |
| 50 thisdir = os.path.dirname(__file__) | |
| 51 vars = {} | |
| 52 execfile(os.path.join(thisdir, "tx_Source_detector.py"), vars) | |
| 53 vars['init'](self.db) | |
| 54 | |
| 55 env = { | |
| 56 'PATH_INFO': 'http://localhost/rounduptest/rest/', | |
| 57 'HTTP_HOST': 'localhost', | |
| 58 'TRACKER_NAME': 'rounduptest' | |
| 59 } | |
| 60 self.dummy_client = client.Client(self.instance, None, env, [], None) | |
| 61 self.empty_form = cgi.FieldStorage() | |
| 62 | |
| 63 self.server = RestfulInstance(self.dummy_client, self.db) | |
| 64 | |
| 65 def tearDown(self): | |
| 66 self.db.close() | |
| 67 try: | |
| 68 shutil.rmtree(self.dirname) | |
| 69 except OSError, error: | |
| 70 if error.errno not in (errno.ENOENT, errno.ESRCH): | |
| 71 raise | |
| 72 | |
| 73 def testGet(self): | |
| 74 """ | |
| 75 Retrieve all three users | |
| 76 obtain data for 'joe' | |
| 77 """ | |
| 78 # Retrieve all three users. | |
| 79 results = self.server.get_collection('user', self.empty_form) | |
| 80 self.assertEqual(self.dummy_client.response_code, 200) | |
| 81 self.assertEqual(len(results['data']), 3) | |
| 82 | |
| 83 # Obtain data for 'joe'. | |
| 84 results = self.server.get_element('user', self.joeid, self.empty_form) | |
| 85 results = results['data'] | |
| 86 self.assertEqual(self.dummy_client.response_code, 200) | |
| 87 self.assertEqual(results['attributes']['username'], 'joe') | |
| 88 self.assertEqual(results['attributes']['realname'], 'Joe Random') | |
| 89 | |
| 90 # Obtain data for 'joe'. | |
| 91 results = self.server.get_attribute( | |
| 92 'user', self.joeid, 'username', self.empty_form | |
| 93 ) | |
| 94 self.assertEqual(self.dummy_client.response_code, 200) | |
| 95 self.assertEqual(results['data']['data'], 'joe') | |
| 96 | |
| 97 def testFilter(self): | |
| 98 """ | |
| 99 Retrieve all three users | |
| 100 obtain data for 'joe' | |
| 101 """ | |
| 102 # create sample data | |
| 103 try: | |
| 104 self.db.status.create(name='open') | |
| 105 except ValueError: | |
| 106 pass | |
| 107 try: | |
| 108 self.db.status.create(name='closed') | |
| 109 except ValueError: | |
| 110 pass | |
| 111 try: | |
| 112 self.db.priority.create(name='normal') | |
| 113 except ValueError: | |
| 114 pass | |
| 115 try: | |
| 116 self.db.priority.create(name='critical') | |
| 117 except ValueError: | |
| 118 pass | |
| 119 self.db.issue.create( | |
| 120 title='foo4', | |
| 121 status=self.db.status.lookup('closed'), | |
| 122 priority=self.db.priority.lookup('critical') | |
| 123 ) | |
| 124 self.db.issue.create( | |
| 125 title='foo1', | |
| 126 status=self.db.status.lookup('open'), | |
| 127 priority=self.db.priority.lookup('normal') | |
| 128 ) | |
| 129 issue_open_norm = self.db.issue.create( | |
| 130 title='foo2', | |
| 131 status=self.db.status.lookup('open'), | |
| 132 priority=self.db.priority.lookup('normal') | |
| 133 ) | |
| 134 issue_closed_norm = self.db.issue.create( | |
| 135 title='foo3', | |
| 136 status=self.db.status.lookup('closed'), | |
| 137 priority=self.db.priority.lookup('normal') | |
| 138 ) | |
| 139 issue_closed_crit = self.db.issue.create( | |
| 140 title='foo4', | |
| 141 status=self.db.status.lookup('closed'), | |
| 142 priority=self.db.priority.lookup('critical') | |
| 143 ) | |
| 144 issue_open_crit = self.db.issue.create( | |
| 145 title='foo5', | |
| 146 status=self.db.status.lookup('open'), | |
| 147 priority=self.db.priority.lookup('critical') | |
| 148 ) | |
| 149 base_path = self.dummy_client.env['PATH_INFO'] + 'data/issue/' | |
| 150 | |
| 151 # Retrieve all issue status=open | |
| 152 form = cgi.FieldStorage() | |
| 153 form.list = [ | |
| 154 cgi.MiniFieldStorage('where_status', 'open') | |
| 155 ] | |
| 156 results = self.server.get_collection('issue', form) | |
| 157 self.assertEqual(self.dummy_client.response_code, 200) | |
| 158 self.assertIn(get_obj(base_path, issue_open_norm), results['data']) | |
| 159 self.assertIn(get_obj(base_path, issue_open_crit), results['data']) | |
| 160 self.assertNotIn( | |
| 161 get_obj(base_path, issue_closed_norm), results['data'] | |
| 162 ) | |
| 163 | |
| 164 # Retrieve all issue status=closed and priority=critical | |
| 165 form = cgi.FieldStorage() | |
| 166 form.list = [ | |
| 167 cgi.MiniFieldStorage('where_status', 'closed'), | |
| 168 cgi.MiniFieldStorage('where_priority', 'critical') | |
| 169 ] | |
| 170 results = self.server.get_collection('issue', form) | |
| 171 self.assertEqual(self.dummy_client.response_code, 200) | |
| 172 self.assertIn(get_obj(base_path, issue_closed_crit), results['data']) | |
| 173 self.assertNotIn(get_obj(base_path, issue_open_crit), results['data']) | |
| 174 self.assertNotIn( | |
| 175 get_obj(base_path, issue_closed_norm), results['data'] | |
| 176 ) | |
| 177 | |
| 178 # Retrieve all issue status=closed and priority=normal,critical | |
| 179 form = cgi.FieldStorage() | |
| 180 form.list = [ | |
| 181 cgi.MiniFieldStorage('where_status', 'closed'), | |
| 182 cgi.MiniFieldStorage('where_priority', 'normal,critical') | |
| 183 ] | |
| 184 results = self.server.get_collection('issue', form) | |
| 185 self.assertEqual(self.dummy_client.response_code, 200) | |
| 186 self.assertIn(get_obj(base_path, issue_closed_crit), results['data']) | |
| 187 self.assertIn(get_obj(base_path, issue_closed_norm), results['data']) | |
| 188 self.assertNotIn(get_obj(base_path, issue_open_crit), results['data']) | |
| 189 self.assertNotIn(get_obj(base_path, issue_open_norm), results['data']) | |
| 190 | |
| 191 def testPagination(self): | |
| 192 """ | |
| 193 Retrieve all three users | |
| 194 obtain data for 'joe' | |
| 195 """ | |
| 196 # create sample data | |
| 197 for i in range(0, random.randint(5, 10)): | |
| 198 self.db.issue.create(title='foo' + str(i)) | |
| 199 | |
| 200 # Retrieving all the issues | |
| 201 results = self.server.get_collection('issue', self.empty_form) | |
| 202 self.assertEqual(self.dummy_client.response_code, 200) | |
| 203 total_length = len(results['data']) | |
| 204 | |
| 205 # Pagination will be 70% of the total result | |
| 206 page_size = total_length * 70 // 100 | |
| 207 page_zero_expected = page_size | |
| 208 page_one_expected = total_length - page_zero_expected | |
| 209 | |
| 210 # Retrieve page 0 | |
| 211 form = cgi.FieldStorage() | |
| 212 form.list = [ | |
| 213 cgi.MiniFieldStorage('page_size', page_size), | |
| 214 cgi.MiniFieldStorage('page_index', 0) | |
| 215 ] | |
| 216 results = self.server.get_collection('issue', form) | |
| 217 self.assertEqual(self.dummy_client.response_code, 200) | |
| 218 self.assertEqual(len(results['data']), page_zero_expected) | |
| 219 | |
| 220 # Retrieve page 1 | |
| 221 form = cgi.FieldStorage() | |
| 222 form.list = [ | |
| 223 cgi.MiniFieldStorage('page_size', page_size), | |
| 224 cgi.MiniFieldStorage('page_index', 1) | |
| 225 ] | |
| 226 results = self.server.get_collection('issue', form) | |
| 227 self.assertEqual(self.dummy_client.response_code, 200) | |
| 228 self.assertEqual(len(results['data']), page_one_expected) | |
| 229 | |
| 230 # Retrieve page 2 | |
| 231 form = cgi.FieldStorage() | |
| 232 form.list = [ | |
| 233 cgi.MiniFieldStorage('page_size', page_size), | |
| 234 cgi.MiniFieldStorage('page_index', 2) | |
| 235 ] | |
| 236 results = self.server.get_collection('issue', form) | |
| 237 self.assertEqual(self.dummy_client.response_code, 200) | |
| 238 self.assertEqual(len(results['data']), 0) | |
| 239 | |
| 240 def testPut(self): | |
| 241 """ | |
| 242 Change joe's 'realname' | |
| 243 Check if we can't change admin's detail | |
| 244 """ | |
| 245 # change Joe's realname via attribute uri | |
| 246 form = cgi.FieldStorage() | |
| 247 form.list = [ | |
| 248 cgi.MiniFieldStorage('data', 'Joe Doe Doe') | |
| 249 ] | |
| 250 results = self.server.put_attribute( | |
| 251 'user', self.joeid, 'realname', form | |
| 252 ) | |
| 253 results = self.server.get_attribute( | |
| 254 'user', self.joeid, 'realname', self.empty_form | |
| 255 ) | |
| 256 self.assertEqual(self.dummy_client.response_code, 200) | |
| 257 self.assertEqual(results['data']['data'], 'Joe Doe Doe') | |
| 258 | |
| 259 # Reset joe's 'realname'. | |
| 260 form = cgi.FieldStorage() | |
| 261 form.list = [ | |
| 262 cgi.MiniFieldStorage('realname', 'Joe Doe') | |
| 263 ] | |
| 264 results = self.server.put_element('user', self.joeid, form) | |
| 265 results = self.server.get_element('user', self.joeid, self.empty_form) | |
| 266 self.assertEqual(self.dummy_client.response_code, 200) | |
| 267 self.assertEqual(results['data']['attributes']['realname'], 'Joe Doe') | |
| 268 | |
| 269 # check we can't change admin's details | |
| 270 results = self.server.put_element('user', '1', form) | |
| 271 self.assertEqual(self.dummy_client.response_code, 403) | |
| 272 self.assertEqual(results['error']['status'], 403) | |
| 273 | |
| 274 def testPost(self): | |
| 275 """ | |
| 276 Post a new issue with title: foo | |
| 277 Verify the information of the created issue | |
| 278 """ | |
| 279 form = cgi.FieldStorage() | |
| 280 form.list = [ | |
| 281 cgi.MiniFieldStorage('title', 'foo') | |
| 282 ] | |
| 283 results = self.server.post_collection('issue', form) | |
| 284 self.assertEqual(self.dummy_client.response_code, 201) | |
| 285 issueid = results['data']['id'] | |
| 286 results = self.server.get_element('issue', issueid, self.empty_form) | |
| 287 self.assertEqual(self.dummy_client.response_code, 200) | |
| 288 self.assertEqual(results['data']['attributes']['title'], 'foo') | |
| 289 self.assertEqual(self.db.issue.get(issueid, "tx_Source"), 'web') | |
| 290 | |
| 291 def testPostFile(self): | |
| 292 """ | |
| 293 Post a new file with content: hello\r\nthere | |
| 294 Verify the information of the created file | |
| 295 """ | |
| 296 form = cgi.FieldStorage() | |
| 297 form.list = [ | |
| 298 cgi.MiniFieldStorage('content', 'hello\r\nthere') | |
| 299 ] | |
| 300 results = self.server.post_collection('file', form) | |
| 301 self.assertEqual(self.dummy_client.response_code, 201) | |
| 302 fileid = results['data']['id'] | |
| 303 results = self.server.get_element('file', fileid, self.empty_form) | |
| 304 results = results['data'] | |
| 305 self.assertEqual(self.dummy_client.response_code, 200) | |
| 306 self.assertEqual(results['attributes']['content'], 'hello\r\nthere') | |
| 307 | |
| 308 def testAuthDeniedPut(self): | |
| 309 """ | |
| 310 Test unauthorized PUT request | |
| 311 """ | |
| 312 # Wrong permissions (caught by roundup security module). | |
| 313 form = cgi.FieldStorage() | |
| 314 form.list = [ | |
| 315 cgi.MiniFieldStorage('realname', 'someone') | |
| 316 ] | |
| 317 results = self.server.put_element('user', '1', form) | |
| 318 self.assertEqual(self.dummy_client.response_code, 403) | |
| 319 self.assertEqual(results['error']['status'], 403) | |
| 320 | |
| 321 def testAuthDeniedPost(self): | |
| 322 """ | |
| 323 Test unauthorized POST request | |
| 324 """ | |
| 325 form = cgi.FieldStorage() | |
| 326 form.list = [ | |
| 327 cgi.MiniFieldStorage('username', 'blah') | |
| 328 ] | |
| 329 results = self.server.post_collection('user', form) | |
| 330 self.assertEqual(self.dummy_client.response_code, 403) | |
| 331 self.assertEqual(results['error']['status'], 403) | |
| 332 | |
| 333 def testAuthAllowedPut(self): | |
| 334 """ | |
| 335 Test authorized PUT request | |
| 336 """ | |
| 337 self.db.setCurrentUser('admin') | |
| 338 form = cgi.FieldStorage() | |
| 339 form.list = [ | |
| 340 cgi.MiniFieldStorage('realname', 'someone') | |
| 341 ] | |
| 342 try: | |
| 343 self.server.put_element('user', '2', form) | |
| 344 except Unauthorised, err: | |
| 345 self.fail('raised %s' % err) | |
| 346 finally: | |
| 347 self.db.setCurrentUser('joe') | |
| 348 | |
| 349 def testAuthAllowedPost(self): | |
| 350 """ | |
| 351 Test authorized POST request | |
| 352 """ | |
| 353 self.db.setCurrentUser('admin') | |
| 354 form = cgi.FieldStorage() | |
| 355 form.list = [ | |
| 356 cgi.MiniFieldStorage('username', 'blah') | |
| 357 ] | |
| 358 try: | |
| 359 self.server.post_collection('user', form) | |
| 360 except Unauthorised, err: | |
| 361 self.fail('raised %s' % err) | |
| 362 finally: | |
| 363 self.db.setCurrentUser('joe') | |
| 364 | |
| 365 def testDeleteAttributeUri(self): | |
| 366 """ | |
| 367 Test Delete an attribute | |
| 368 """ | |
| 369 # create a new issue with userid 1 in the nosy list | |
| 370 issue_id = self.db.issue.create(title='foo', nosy=['1']) | |
| 371 | |
| 372 # remove the title and nosy | |
| 373 results = self.server.delete_attribute( | |
| 374 'issue', issue_id, 'title', self.empty_form | |
| 375 ) | |
| 376 self.assertEqual(self.dummy_client.response_code, 200) | |
| 377 | |
| 378 results = self.server.delete_attribute( | |
| 379 'issue', issue_id, 'nosy', self.empty_form | |
| 380 ) | |
| 381 self.assertEqual(self.dummy_client.response_code, 200) | |
| 382 | |
| 383 # verify the result | |
| 384 results = self.server.get_element('issue', issue_id, self.empty_form) | |
| 385 results = results['data'] | |
| 386 self.assertEqual(self.dummy_client.response_code, 200) | |
| 387 self.assertEqual(len(results['attributes']['nosy']), 0) | |
| 388 self.assertListEqual(results['attributes']['nosy'], []) | |
| 389 self.assertEqual(results['attributes']['title'], None) | |
| 390 | |
| 391 def testPatchAdd(self): | |
| 392 """ | |
| 393 Test Patch op 'Add' | |
| 394 """ | |
| 395 # create a new issue with userid 1 in the nosy list | |
| 396 issue_id = self.db.issue.create(title='foo', nosy=['1']) | |
| 397 | |
| 398 # add userid 2 to the nosy list | |
| 399 form = cgi.FieldStorage() | |
| 400 form.list = [ | |
| 401 cgi.MiniFieldStorage('op', 'add'), | |
| 402 cgi.MiniFieldStorage('nosy', '2') | |
| 403 ] | |
| 404 results = self.server.patch_element('issue', issue_id, form) | |
| 405 self.assertEqual(self.dummy_client.response_code, 200) | |
| 406 | |
| 407 # verify the result | |
| 408 results = self.server.get_element('issue', issue_id, self.empty_form) | |
| 409 results = results['data'] | |
| 410 self.assertEqual(self.dummy_client.response_code, 200) | |
| 411 self.assertEqual(len(results['attributes']['nosy']), 2) | |
| 412 self.assertListEqual(results['attributes']['nosy'], ['1', '2']) | |
| 413 | |
| 414 def testPatchReplace(self): | |
| 415 """ | |
| 416 Test Patch op 'Replace' | |
| 417 """ | |
| 418 # create a new issue with userid 1 in the nosy list and status = 1 | |
| 419 issue_id = self.db.issue.create(title='foo', nosy=['1'], status='1') | |
| 420 | |
| 421 # replace userid 2 to the nosy list and status = 3 | |
| 422 form = cgi.FieldStorage() | |
| 423 form.list = [ | |
| 424 cgi.MiniFieldStorage('op', 'replace'), | |
| 425 cgi.MiniFieldStorage('nosy', '2'), | |
| 426 cgi.MiniFieldStorage('status', '3') | |
| 427 ] | |
| 428 results = self.server.patch_element('issue', issue_id, form) | |
| 429 self.assertEqual(self.dummy_client.response_code, 200) | |
| 430 | |
| 431 # verify the result | |
| 432 results = self.server.get_element('issue', issue_id, self.empty_form) | |
| 433 results = results['data'] | |
| 434 self.assertEqual(self.dummy_client.response_code, 200) | |
| 435 self.assertEqual(results['attributes']['status'], '3') | |
| 436 self.assertEqual(len(results['attributes']['nosy']), 1) | |
| 437 self.assertListEqual(results['attributes']['nosy'], ['2']) | |
| 438 | |
| 439 def testPatchRemoveAll(self): | |
| 440 """ | |
| 441 Test Patch Action 'Remove' | |
| 442 """ | |
| 443 # create a new issue with userid 1 and 2 in the nosy list | |
| 444 issue_id = self.db.issue.create(title='foo', nosy=['1', '2']) | |
| 445 | |
| 446 # remove the nosy list and the title | |
| 447 form = cgi.FieldStorage() | |
| 448 form.list = [ | |
| 449 cgi.MiniFieldStorage('op', 'remove'), | |
| 450 cgi.MiniFieldStorage('nosy', ''), | |
| 451 cgi.MiniFieldStorage('title', '') | |
| 452 ] | |
| 453 results = self.server.patch_element('issue', issue_id, form) | |
| 454 self.assertEqual(self.dummy_client.response_code, 200) | |
| 455 | |
| 456 # verify the result | |
| 457 results = self.server.get_element('issue', issue_id, self.empty_form) | |
| 458 results = results['data'] | |
| 459 self.assertEqual(self.dummy_client.response_code, 200) | |
| 460 self.assertEqual(results['attributes']['title'], None) | |
| 461 self.assertEqual(len(results['attributes']['nosy']), 0) | |
| 462 self.assertEqual(results['attributes']['nosy'], []) | |
| 463 | |
| 464 def testPatchAction(self): | |
| 465 """ | |
| 466 Test Patch Action 'Action' | |
| 467 """ | |
| 468 # create a new issue with userid 1 and 2 in the nosy list | |
| 469 issue_id = self.db.issue.create(title='foo') | |
| 470 | |
| 471 # execute action retire | |
| 472 form = cgi.FieldStorage() | |
| 473 form.list = [ | |
| 474 cgi.MiniFieldStorage('op', 'action'), | |
| 475 cgi.MiniFieldStorage('action_name', 'retire') | |
| 476 ] | |
| 477 results = self.server.patch_element('issue', issue_id, form) | |
| 478 self.assertEqual(self.dummy_client.response_code, 200) | |
| 479 | |
| 480 # verify the result | |
| 481 self.assertTrue(self.db.issue.is_retired(issue_id)) | |
| 482 | |
| 483 def testPatchRemove(self): | |
| 484 """ | |
| 485 Test Patch Action 'Remove' only some element from a list | |
| 486 """ | |
| 487 # create a new issue with userid 1, 2, 3 in the nosy list | |
| 488 issue_id = self.db.issue.create(title='foo', nosy=['1', '2', '3']) | |
| 489 | |
| 490 # remove the nosy list and the title | |
| 491 form = cgi.FieldStorage() | |
| 492 form.list = [ | |
| 493 cgi.MiniFieldStorage('op', 'remove'), | |
| 494 cgi.MiniFieldStorage('nosy', '1, 2'), | |
| 495 ] | |
| 496 results = self.server.patch_element('issue', issue_id, form) | |
| 497 self.assertEqual(self.dummy_client.response_code, 200) | |
| 498 | |
| 499 # verify the result | |
| 500 results = self.server.get_element('issue', issue_id, self.empty_form) | |
| 501 results = results['data'] | |
| 502 self.assertEqual(self.dummy_client.response_code, 200) | |
| 503 self.assertEqual(len(results['attributes']['nosy']), 1) | |
| 504 self.assertEqual(results['attributes']['nosy'], ['3']) | |
| 505 | |
| 506 | |
| 507 def get_obj(path, id): | |
| 508 return { | |
| 509 'id': id, | |
| 510 'link': path + id | |
| 511 } | |
| 512 | |
| 513 if __name__ == '__main__': | |
| 514 runner = unittest.TextTestRunner() | |
| 515 unittest.main(testRunner=runner) |
