|
| 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 | + |
| 9 | +from django.test import TestCase, Client |
| 10 | +from rest_framework.test import APIClient |
| 11 | +from rest_framework.authtoken.models import Token |
| 12 | +from django.contrib.auth.models import User |
| 13 | +from authentication.models import Authentication |
| 14 | + |
| 15 | +class AuthenticationTestCase(TestCase): |
| 16 | + fixtures = ['tests/fixtures/test_data'] |
| 17 | + |
| 18 | + def setUp(self): |
| 19 | + self.client = APIClient() |
| 20 | + |
| 21 | + def test_credentials_created_response(self): |
| 22 | + """Add authentication is successful (200) |
| 23 | + """ |
| 24 | + |
| 25 | + token = Token.objects.get(user=User.objects.get(username='tester')).key |
| 26 | + data = {"iss": "Reeya1","sub": "ReeyaGupta1"} |
| 27 | + |
| 28 | + self.client.credentials(HTTP_AUTHORIZATION='Token ' + token) |
| 29 | + response = self.client.post('/api/auth/add/', data=data) |
| 30 | + self.assertEqual(response.status_code, 201) |
| 31 | + |
| 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) |
| 41 | + |
| 42 | + def test_bad_request_response(self): |
| 43 | + """Bad request (400) |
| 44 | + """ |
| 45 | + |
| 46 | + token = Token.objects.get(user=User.objects.get(username='tester')).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') |
| 50 | + self.assertEqual(response.status_code, 400) |
| 51 | + |
| 52 | + def test_object_already_exists_response(self): |
| 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