|
| 1 | +from django.test import TestCase |
| 2 | +from rest_framework.authtoken.models import Token |
| 3 | +from rest_framework.test import APIClient |
| 4 | +from django.contrib.auth.models import User |
| 5 | + |
| 6 | +class PrefixCreateTestCase(TestCase): |
| 7 | + fixtures = ['tests/fixtures/test_data'] |
| 8 | + |
| 9 | + def setUp(self): |
| 10 | + |
| 11 | + # Checking if the user 'bco_api_user' already exists |
| 12 | + try: |
| 13 | + self.user = User.objects.get(username='bco_api_user') |
| 14 | + except User.DoesNotExist: |
| 15 | + self.user = User.objects.create_user(username='bco_api_user') |
| 16 | + |
| 17 | + # Checking if user already has token, if not then creating one |
| 18 | + if not Token.objects.filter(user=self.user).exists(): |
| 19 | + self.token = Token.objects.create(user=self.user) |
| 20 | + else: |
| 21 | + self.token = Token.objects.get(user=self.user) |
| 22 | + |
| 23 | + def test_create_prefix_success(self): |
| 24 | + data = { |
| 25 | + "POST_api_prefixes_create": [ |
| 26 | + { |
| 27 | + "owner_group": "bco_publisher", |
| 28 | + "owner_user": "anon", |
| 29 | + "prefixes": [ |
| 30 | + { |
| 31 | + "description": "Just a test prefix.", |
| 32 | + "expiration_date": "2023-01-01-01-01-01", |
| 33 | + "prefix": "testR" |
| 34 | + }, |
| 35 | + { |
| 36 | + "description": "Just another prefix.", |
| 37 | + "expiration_date": "2023-01-01-01-01-01", |
| 38 | + "prefix": "othER" |
| 39 | + } |
| 40 | + ] |
| 41 | + } |
| 42 | + ] |
| 43 | + } |
| 44 | + |
| 45 | + response = self.client.post('/api/prefixes/create/', data=data, format='json') |
| 46 | + self.assertEqual(response.status_code, 201) |
| 47 | + |
| 48 | + def test_create_prefix_bad_request(self): |
| 49 | + data = { |
| 50 | + # Incomplete or invalid data |
| 51 | + } |
| 52 | + |
| 53 | + response = self.client.post('/api/prefixes/create/', data=data, format='json') |
| 54 | + self.assertEqual(response.status_code, 400) |
| 55 | + |
| 56 | + def test_create_prefix_unauthorized(self): |
| 57 | + self.client.credentials(HTTP_AUTHORIZATION='Token invalid_token') |
| 58 | + data = { |
| 59 | + "POST_api_prefixes_create": [ |
| 60 | + { |
| 61 | + "owner_group": "bco_publisher", |
| 62 | + "owner_user": "anon", |
| 63 | + "prefixes": [ |
| 64 | + { |
| 65 | + "description": "Just a test prefix.", |
| 66 | + "expiration_date": "2023-01-01-01-01-01", |
| 67 | + "prefix": "testR" |
| 68 | + } |
| 69 | + ] |
| 70 | + } |
| 71 | + ] |
| 72 | + } |
| 73 | + |
| 74 | + response = self.client.post('/api/prefixes/create/', data=data, format='json') |
| 75 | + self.assertEqual(response.status_code, 401) |
| 76 | + |
| 77 | + def test_create_prefix_forbidden(self): |
| 78 | + data = { |
| 79 | + "POST_api_prefixes_create": [ |
| 80 | + { |
| 81 | + "owner_group": "non_existing_group", |
| 82 | + "owner_user": "anon", |
| 83 | + "prefixes": [ |
| 84 | + { |
| 85 | + "description": "Just a test prefix.", |
| 86 | + "expiration_date": "2023-01-01-01-01-01", |
| 87 | + "prefix": "testR" |
| 88 | + } |
| 89 | + ] |
| 90 | + } |
| 91 | + ] |
| 92 | + } |
| 93 | + |
| 94 | + response = self.client.post('/api/prefixes/create/', data=data, format='json') |
| 95 | + self.assertEqual(response.status_code, 403) |
| 96 | + |
| 97 | + def test_create_prefix_conflict(self): |
| 98 | + data = { |
| 99 | + "POST_api_prefixes_create": [ |
| 100 | + { |
| 101 | + "owner_group": "bco_publisher", |
| 102 | + "owner_user": "anon", |
| 103 | + "prefixes": [ |
| 104 | + { |
| 105 | + "description": "Just a test prefix.", |
| 106 | + "expiration_date": "2023-01-01-01-01-01", |
| 107 | + "prefix": "testR" |
| 108 | + } |
| 109 | + ] |
| 110 | + } |
| 111 | + ] |
| 112 | + } |
| 113 | + |
| 114 | + # Create the prefix first |
| 115 | + self.client.post('/api/prefixes/create/', data=data, format='json') |
| 116 | + |
| 117 | + # Try to create the same prefix again |
| 118 | + response = self.client.post('/api/prefixes/create/', data=data, format='json') |
| 119 | + self.assertEqual(response.status_code, 409) |
0 commit comments