How to use assert_http_401_unauthorized method in Django Test Plus

Best Python code snippet using django-test-plus_python

api_test.py

Source:api_test.py Github

copy

Full Screen

...359 def test_is_logged_in(self):360 """Test read, if user is authenticated"""361 self.client.logout()362 response = self.get(self.url_key + '-list')363 self.response_302 or self.assert_http_401_unauthorized(response, msg=response.content)364 self.client.force_login(user=self.profile.user)365 self.test_list()366 self.test_detail()367class WriteOnlyWithAdminAccessTest:368 def test_delete(self):369 """Test delete with and without can_edit_basedata permissions"""370 self.profile.admin_access = True371 self.profile.save()372 self._test_delete()373 self.profile.admin_access = False374 self.profile.save()375 self._test_delete_forbidden()376 def test_post(self):377 """Test post with and without can_edit_basedata permissions"""378 self.profile.admin_access = True379 self.profile.save()380 self._test_post()381 self.profile.admin_access = False382 self.profile.save()383 self._test_post_forbidden()384 def test_put_patch(self):385 """Test post with and without can_edit_basedata permissions"""386 self.profile.admin_access = True387 self.profile.save()388 self._test_put_patch()389 self.profile.admin_access = False390 self.profile.save()391 self._test_put_patch_forbidden()392 def test_is_logged_in(self):393 """Test read, if user is authenticated"""394 self.client.logout()395 response = self.get(self.url_key + '-list')396 self.response_302 or self.assert_http_401_unauthorized(response, msg=response.content)397 self.client.force_login(user=self.profile.user)398 self.test_list()399 self.test_detail()400class ReadOnlyWithAdminBasedataAccessTest:401 def test_detail(self):402 """Test detail with and without can_edit_basedata permissions or admin_access"""403 self.profile.admin_access = True404 self.profile.save()405 self._test_detail()406 self.profile.admin_access = False407 self.profile.save()408 self._test_detail_forbidden()409 self.profile.can_edit_basedata = True410 self.profile.save()411 self._test_detail()412 self.profile.can_edit_basedata = False413 self.profile.save()414 self._test_detail_forbidden()415 def test_get_urls(self):416 """Test get_urls with and without can_edit_basedata permissions"""417 self.profile.admin_access = True418 self.profile.save()419 self._test_get_urls()420 self.profile.admin_access = False421 self.profile.save()422 self._test_get_urls_forbidden()423 self.profile.can_edit_basedata = True424 self.profile.save()425 self._test_get_urls()426 self.profile.can_edit_basedata = False427 self.profile.save()428 self._test_get_urls_forbidden()429 def test_list(self):430 """Test list with and without can_edit_basedata permissions"""431 self.profile.admin_access = True432 self.profile.save()433 self._test_list()434 self.profile.admin_access = False435 self.profile.save()436 self._test_list_forbidden()437 self.profile.can_edit_basedata = True438 self.profile.save()439 self._test_list()440 self.profile.can_edit_basedata = False441 self.profile.save()442 self._test_list_forbidden()443 def test_is_logged_in(self):444 """Test get, if user is authenticated"""445 self.client.logout()446 response = self.get(self.url_key + '-list')447 self.response_302 or self.assert_http_401_unauthorized(response, msg=response.content)448 self.client.force_login(user=self.profile.user)449 # test_list() with check of admin_access450 self.profile.admin_access = True451 self.profile.save()452 self._test_list()453 self.profile.admin_access = False454 self.profile.save()455 self._test_list_forbidden()456 # test_detail()457 self.profile.admin_access = True458 self.profile.save()459 self._test_detail()460 self.profile.admin_access = False461 self.profile.save()462 self._test_detail_forbidden()463class SingletonWriteOnlyWithCanEditBaseDataTest:464 def test_put_patch(self):465 """Test for Singletons, put and patch with and without can_edit_basedata permissions"""466 self.profile.can_edit_basedata = True467 self.profile.save()468 self._test_put_patch()469 self.profile.can_edit_basedata = False470 self.profile.save()471 self._test_put_patch_forbidden()472 def test_is_logged_in(self):473 """Test read, if user is authenticated"""474 self.client.logout()475 response = self.get(self.url_key + '-list')476 self.response_302 or self.assert_http_401_unauthorized(response, msg=response.content)477 self.client.force_login(user=self.profile.user)478 self.test_detail()479class SingletonWriteOnlyWithAdminAccessTest:480 def test_put_patch(self):481 """Test for Singletons, put and patch with and without admin_access"""482 self.profile.admin_access= True483 self.profile.save()484 self._test_put_patch()485 self.profile.admin_access= False486 self.profile.save()487 self._test_put_patch_forbidden()488 def test_is_logged_in(self):489 """Test read, if user is authenticated"""490 self.client.logout()491 response = self.get(self.url_key + '-list')492 self.response_302 or self.assert_http_401_unauthorized(response, msg=response.content)493 self.client.force_login(user=self.profile.user)494 self.test_detail()495class TestAPIMixin:496 """test if view and serializer are working correctly """497 url_key = ""498 # replace with concrete factory in sub-class499 factory = None500 @classmethod501 def setUpTestData(cls):502 super().setUpTestData()503 cls.url_pks = dict()504 if cls.factory:505 cls.obj = cls.factory()506 cls.url_pk = dict(pk=cls.obj.pk)507class TestPermissionsMixin():508 """ test users permissions"""509 def test_is_logged_in(self):510 self.client.logout()511 response = self.get(self.url_key + '-list')512 self.response_302 or self.assert_http_401_unauthorized(response, msg=response.content)513 self.client.force_login(user=self.profile.user)514 self.test_list()515 self.test_detail()516 def test_can_edit_basedata(self):517 profile = self.profile518 original_permission = profile.can_edit_basedata519 original_admin_access = profile.admin_access520 # Testprofile, with permission to edit basedata521 profile.can_edit_basedata = True522 profile.admin_access = False523 profile.save()524 self.test_post()525 # Testprofile, without permission to edit basedata526 profile.can_edit_basedata = False...

Full Screen

Full Screen

tests.py

Source:tests.py Github

copy

Full Screen

...54 def test_unauthorized(self):55 response = self.client.get(56 self.url_task_list,57 )58 self.assert_http_401_unauthorized(response)59 def test_task_list(self):60 self.client.credentials(HTTP_AUTHORIZATION='Token {}'.format(self.token))61 response = self.client.get(62 self.url_task_list,63 )64 response_json = response.json()65 self.assertTrue(self.task_description in str(response_json))66 self.assertTrue(self.task_other_description not in str(response_json))67 def test_task_detail_unauthorized(self):68 response = self.client.get(69 self.url_detail_task,70 )71 self.assert_http_401_unauthorized(response)72 def test_task_detail(self):73 self.client.credentials(HTTP_AUTHORIZATION='Token {}'.format(self.token))74 response = self.client.get(75 self.url_detail_task,76 )77 response_json = response.json()78 self.assertEqual(self.task_description, response_json['description'])79 def test_task_detail_invalid(self):80 self.client.credentials(HTTP_AUTHORIZATION='Token {}'.format(self.token))81 response = self.client.get(82 self.url_detail_task2,83 )84 self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)85 def test_create_task_non_description(self):...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Django Test Plus automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful