|
1 | | -##test for api/auth/add |
2 | | -##07801a1a4cdbf1945e22ac8439f1db27fe813f7a |
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +"""Add Authentication |
| 4 | +Tests for 'New authentication credentials added to existing object' (200), |
| 5 | +'Authentication credentials were created and added' (201), 'Bad request' (400), |
| 6 | +'That object already exists for this account' (409) |
| 7 | +""" |
| 8 | + |
3 | 9 | from django.test import TestCase, Client |
4 | 10 | from rest_framework.test import APIClient |
5 | 11 | from rest_framework.authtoken.models import Token |
6 | 12 | from django.contrib.auth.models import User |
| 13 | +from authentication.models import Authentication |
7 | 14 |
|
8 | 15 | class AuthenticationTestCase(TestCase): |
| 16 | + fixtures = ['tests/fixtures/test_data'] |
| 17 | + |
9 | 18 | def setUp(self): |
10 | 19 | self.client = APIClient() |
11 | 20 |
|
| 21 | + def test_credentials_created_response(self): |
| 22 | + """Add authentication is successful (200) |
| 23 | + """ |
12 | 24 |
|
13 | | - # Creating a user for authentication |
14 | | - self.user = User.objects.create(username='testuser') |
15 | | - |
16 | | - # Checking if user already has token, if not then creating one |
17 | | - if not Token.objects.filter(user=self.user).exists(): |
18 | | - self.token = Token.objects.create(user=self.user) |
19 | | - else: |
20 | | - self.token = Token.objects.get(user=self.user) |
21 | | - def test_success_response(self): |
22 | | - # successfull request |
23 | | - data = { |
24 | | - "iss": "Reeya1", |
25 | | - "sub": "ReeyaGupta1" |
26 | | - } |
| 25 | + token = Token.objects.get(user=User.objects.get(username='test50')).key |
| 26 | + data = {"iss": "Reeya1","sub": "ReeyaGupta1"} |
27 | 27 |
|
28 | | - self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key) |
| 28 | + self.client.credentials(HTTP_AUTHORIZATION='Token ' + token) |
29 | 29 | response = self.client.post('/api/auth/add/', data=data) |
30 | | - '''fails with "AssertionError: 201 != 200": |
31 | | - This test case is expecting a status code of 200 (OK) when making a successful request, |
32 | | - but it receives a status code of 201 (Created) instead. It seems like the endpoint I am |
33 | | - testing is returning a 201 status code instead of the expected 200. To fix this, I |
34 | | - updated the test case to expect a status code of 201. |
35 | | - ''' |
36 | 30 | self.assertEqual(response.status_code, 201) |
37 | 31 |
|
38 | | - def test_credentials_created_response(self): |
39 | | - # Simulate a request where authentication credentials were created and added |
40 | | - data = { |
41 | | - "iss": "Reeya1", |
42 | | - "sub": "ReeyaGupta1" |
43 | | - } |
44 | | - self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key) |
45 | | - response = self.client.post('/api/auth/add/', data=data) |
46 | | - self.assertEqual(response.status_code, 201) |
| 32 | + def test_credentials_added(self): |
| 33 | + """New authentication credentials added to existing object (200) |
| 34 | + """ |
| 35 | + |
| 36 | + token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key |
| 37 | + data = {"iss": "new","sub": "new One"} |
| 38 | + self.client.credentials(HTTP_AUTHORIZATION='Token ' + token) |
| 39 | + response = self.client.post('/api/auth/add/', data=data, format='json') |
| 40 | + self.assertEqual(response.status_code, 200) |
47 | 41 |
|
48 | 42 | def test_bad_request_response(self): |
49 | | - # bad request |
50 | | - data = { |
51 | | - # Missing required fields |
52 | | - } |
53 | | - self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key) |
54 | | - response = self.client.post('/api/auth/add/', data=data) |
| 43 | + """Bad request (400) |
| 44 | + """ |
| 45 | + |
| 46 | + token = Token.objects.get(user=User.objects.get(username='test50')).key |
| 47 | + data = {"Missing required fields"} |
| 48 | + self.client.credentials(HTTP_AUTHORIZATION='Token ' + token) |
| 49 | + response = self.client.post('/api/auth/add/', data=data, format='json') |
55 | 50 | self.assertEqual(response.status_code, 400) |
56 | 51 |
|
57 | 52 | def test_object_already_exists_response(self): |
58 | | - # an object that already exists for this account |
59 | | - data = { |
60 | | - "iss": "Reeya", |
61 | | - "sub": "ReeyaGupta" |
62 | | - } |
63 | | - self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key) |
64 | | - response = self.client.post('/api/auth/add/', data=data) |
65 | | - '''fails with "AssertionError: 201 != 409" |
66 | | - Similarly, this test case is expecting 409 (Conflict) when trying to |
67 | | - create an object that already exists, but it receives 201 (Created). |
68 | | - This endpoint is not handling the object's existence as expected. |
69 | | - I updated the test case to expect a status code of 201 when creating an object |
70 | | - that already exists. |
71 | | - ''' |
72 | | - self.assertEqual(response.status_code, 201) |
| 53 | + """That object already exists for this account (409) |
| 54 | + """ |
| 55 | + |
| 56 | + token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key |
| 57 | + data = {"iss": "Reeya1","sub": "ReeyaGupta1"} |
| 58 | + self.client.credentials(HTTP_AUTHORIZATION='Token ' + token) |
| 59 | + response = self.client.post('/api/auth/add/', data=data, format='json') |
| 60 | + self.assertEqual(response.status_code, 409) |
0 commit comments