def test_user_created(self): """ An succesful authentication should create a new user """ authentication_backend = OIDCAuthenticationBackend() authentication_backend.get_token = Mock() authentication_backend.store_tokens = Mock() # Mock verify token and payload authentication_backend.verify_token = Mock() authentication_backend.verify_token.return_value = { "payload_foo": "foo_data" } # Mock user creation FOO_USER = get_test_user() authentication_backend.get_or_create_user = Mock() authentication_backend.get_or_create_user.return_value = FOO_USER authenticated_result = authentication_backend.authenticate( MOCK_AUTH_REQUEST) authentication_backend.get_token.assert_called_once() authentication_backend.verify_token.assert_called_once() authentication_backend.store_tokens.assert_called_once() # Most importantly, the get_or_create_user function is called, and it's return value is given authentication_backend.get_or_create_user.assert_called_once() self.assertEquals(authenticated_result, FOO_USER)
def test_create_fail(self, mock_get_cases_from_settings): """ Should fail and create no Itinerary if no cases are available """ self.assertEqual(Itinerary.objects.count(), 0) mock_get_cases_from_settings.return_value = [] url = reverse("v1:itinerary-list") client = get_authenticated_client() user = get_test_user() response = client.post( url, { "team_members": [{ "user": { "id": user.id } }], "settings": { "opening_date": "2020-04-24", "target_length": 8, }, }, format="json", ) self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) self.assertEqual(Itinerary.objects.count(), 0)
def test_authenticated_post_create_author(self): """ The author of the case should automatically be set to the authenticated user who made the POST request """ self.assertEquals(Case.objects.count(), 0) team = baker.make(CaseTeam) reason = baker.make(CaseReason, team=team) url = reverse("cases-list") client = get_authenticated_client() response = client.post( url, { "description": "Foo", "team": team.pk, "reason": reason.pk, "address": { "bag_id": "foo bag ID" }, }, format="json", ) test_user = get_test_user() case = Case.objects.get(id=response.data["id"]) self.assertEquals(case.author, test_user)
def test_with_authentication_code(self, mock_AuthenticationBackend): """ succeeds if an authentication code is sent """ mock_AuthenticationBackend.authenticate = Mock() mock_AuthenticationBackend.authenticate.return_value = get_test_user() url = reverse("oidc-authenticate") client = get_unauthenticated_client() response = client.post(url, {"code": "FOO-CODE"}) self.assertEquals(response.status_code, status.HTTP_200_OK)
def test_no_itineraries_with_date(self): """ Returns an empty list if no itineraries exist for given date """ itinerary = Itinerary.objects.create() user = get_test_user() itinerary.add_team_members([user.id]) url = reverse("v1:itinerary-list") client = get_authenticated_client() response = client.get(url, {"created_at": "2022-04-02"}) expected_respsonse = {"itineraries": []} self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.json(), expected_respsonse)
def test_with_authentication_code_response(self, mock_AuthenticationBackend): """ Returns a refresh and access token if authentication is succesful """ mock_AuthenticationBackend.authenticate = Mock() mock_AuthenticationBackend.authenticate.return_value = get_test_user() url = reverse("v1:oidc-authenticate") client = get_unauthenticated_client() response = client.post(url, {"code": "FOO-CODE"}) token_response = response.json() # The response contains a refresh and an access token and a user object self.assertEquals(list(token_response.keys()), ["refresh", "access", "user"]) self.assertIsNotNone(token_response["refresh"]) self.assertIsNotNone(token_response["access"]) self.assertIsNotNone(token_response["user"])
"type": summon_type.id, "persons": [ { "first_name": "foo_first_name", "preposition": "foo_preposition", "last_name": "foo_last_name", }, ], } url = reverse("summons-list") client = get_authenticated_client() response = client.post(url, data, format="json") test_user = get_test_user() summon = Summon.objects.get(id=response.data["id"]) self.assertEquals(test_user, summon.author) def test_authenticated_post_invalid_case(self): """ A post should fail if the given Case doesn't exist """ self.assertEquals(Summon.objects.count(), 0) case = baker.make(Case) summon_type = baker.make(SummonType, team=case.team) data = { "description": "foo_description",