|
5 | 5 | from rest_framework import status |
6 | 6 | from rest_framework.test import APITestCase |
7 | 7 |
|
8 | | -from scram.route_manager.models import Client |
| 8 | +from scram.route_manager.models import ActionType, Client, Entry, Route |
9 | 9 |
|
10 | 10 |
|
11 | 11 | class TestAddRemoveIP(APITestCase): |
@@ -125,3 +125,84 @@ def test_unauthenticated_users_have_no_list_access(self): |
125 | 125 | """Ensure an unauthenticated client can't list Entries.""" |
126 | 126 | response = self.client.get(self.entry_url, format="json") |
127 | 127 | self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |
| 128 | + |
| 129 | + |
| 130 | +class TestIsActive(APITestCase): |
| 131 | + """Test the is_active endpoint.""" |
| 132 | + |
| 133 | + def setUp(self): |
| 134 | + """Set up test data.""" |
| 135 | + self.url = reverse("api:v1:is_active-list") |
| 136 | + self.authorized_client = Client.objects.create( |
| 137 | + hostname="authorized_client.es.net", |
| 138 | + uuid="0e7e1cbd-7d73-4968-bc4b-ce3265dc2fd3", |
| 139 | + is_authorized=True, |
| 140 | + ) |
| 141 | + self.authorized_client.authorized_actiontypes.set([1]) |
| 142 | + self.actiontype, _ = ActionType.objects.get_or_create(pk=1, defaults={"name": "block"}) |
| 143 | + |
| 144 | + # Create some active entries |
| 145 | + |
| 146 | + # Active IPv4 |
| 147 | + route_v4 = Route.objects.create(route="192.0.2.100") |
| 148 | + Entry.objects.create( |
| 149 | + route=route_v4, is_active=True, comment="test active", who="test", actiontype=self.actiontype |
| 150 | + ) |
| 151 | + |
| 152 | + # Active IPv6 |
| 153 | + route_v6 = Route.objects.create(route="2001:db8::1") |
| 154 | + Entry.objects.create( |
| 155 | + route=route_v6, is_active=True, comment="test active v6", who="test", actiontype=self.actiontype |
| 156 | + ) |
| 157 | + |
| 158 | + # Deactivated IPv4 entry |
| 159 | + route_inactive = Route.objects.create(route="192.0.2.200") |
| 160 | + Entry.objects.create( |
| 161 | + route=route_inactive, is_active=False, comment="inactive", who="test", actiontype=self.actiontype |
| 162 | + ) |
| 163 | + |
| 164 | + # Deactived IPv6 entry |
| 165 | + route_inactive = Route.objects.create(route="2001:db8::5") |
| 166 | + Entry.objects.create( |
| 167 | + route=route_inactive, is_active=False, comment="inactive", who="test", actiontype=self.actiontype |
| 168 | + ) |
| 169 | + |
| 170 | + def test_active_ipv4_returns_true(self): |
| 171 | + """Check that an active IPv4 returns is_active=true.""" |
| 172 | + response = self.client.get(self.url, {"cidr": "192.0.2.100"}) |
| 173 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 174 | + self.assertEqual(len(response.data["results"]), 1) |
| 175 | + self.assertEqual(response.data["results"][0]["is_active"], True) |
| 176 | + self.assertEqual(response.data["results"][0]["route"], "192.0.2.100/32") |
| 177 | + |
| 178 | + def test_active_ipv6_returns_true(self): |
| 179 | + """Check that an active IPv6 returns is_active=true.""" |
| 180 | + response = self.client.get(self.url, {"cidr": "2001:db8::1"}) |
| 181 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 182 | + self.assertEqual(len(response.data["results"]), 1) |
| 183 | + self.assertEqual(response.data["results"][0]["is_active"], True) |
| 184 | + self.assertEqual(response.data["results"][0]["route"], "2001:db8::1/128") |
| 185 | + |
| 186 | + def test_inactive_entry_ipv4_returns_false(self): |
| 187 | + """Check that an inactive entry returns is_active=false.""" |
| 188 | + response = self.client.get(self.url, {"cidr": "192.0.2.200"}) |
| 189 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 190 | + self.assertEqual(len(response.data["results"]), 1) |
| 191 | + self.assertEqual(response.data["results"][0]["is_active"], False) |
| 192 | + self.assertEqual(response.data["results"][0]["route"], "192.0.2.200/32") |
| 193 | + |
| 194 | + def test_inactive_entry_ipv6_returns_false(self): |
| 195 | + """Check that an inactive entry returns is_active=false.""" |
| 196 | + response = self.client.get(self.url, {"cidr": "2001:db8::5"}) |
| 197 | + self.assertEqual(len(response.data["results"]), 1) |
| 198 | + self.assertEqual(response.data["results"][0]["is_active"], False) |
| 199 | + self.assertEqual(response.data["results"][0]["route"], "2001:db8::5/128") |
| 200 | + |
| 201 | + def test_unauthenticated_access_allowed(self): |
| 202 | + """Ensure unauthenticated clients can check if IPs are active.""" |
| 203 | + # Logout any authenticated user |
| 204 | + self.client.logout() |
| 205 | + response = self.client.get(self.url, {"cidr": "192.0.2.100"}) |
| 206 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 207 | + self.assertEqual(len(response.data["results"]), 1) |
| 208 | + self.assertEqual(response.data["results"][0]["is_active"], True) |
0 commit comments