97 lines
3.9 KiB
Python
97 lines
3.9 KiB
Python
from django.test import Client, TestCase
|
|
|
|
from apps.accounts.models import User
|
|
from apps.farms.models import Flock, Kandang, Site
|
|
|
|
|
|
class FlockIotApiUrlTests(TestCase):
|
|
def setUp(self):
|
|
self.owner = User.objects.create_user(user_name="owner", password="secret")
|
|
self.admin = User.objects.create_user(
|
|
user_name="admin",
|
|
password="admin123",
|
|
status=User.STATUS_SUPERADMIN,
|
|
is_superuser=True,
|
|
)
|
|
self.staff = User.objects.create_user(
|
|
user_name="staff",
|
|
password="staff123",
|
|
status=User.STATUS_ACTIVE,
|
|
is_staff=True,
|
|
)
|
|
self.site = Site.objects.create(site_name="Site A", user=self.owner)
|
|
self.kandang = Kandang.objects.create(kandang_name="Kandang 1", site=self.site)
|
|
self.flock = Flock.objects.create(flock_name="Lantai 1", kandang=self.kandang)
|
|
|
|
def _csrf_headers(self, client: Client) -> dict[str, str]:
|
|
token = client.cookies["csrftoken"].value
|
|
return {"HTTP_X_CSRFTOKEN": token}
|
|
|
|
def _login(self, client: Client, user_name: str, password: str) -> None:
|
|
client.get("/api/v1/auth/csrf/")
|
|
response = client.post(
|
|
"/api/v1/auth/login/",
|
|
{"user_name": user_name, "password": password},
|
|
content_type="application/json",
|
|
**self._csrf_headers(client),
|
|
)
|
|
self.assertEqual(response.status_code, 200, response.content)
|
|
|
|
def test_superadmin_can_set_iot_api_url(self):
|
|
client = Client(enforce_csrf_checks=True)
|
|
self._login(client, "admin", "admin123")
|
|
url = "https://example.test/iot/lantai-1"
|
|
response = client.patch(
|
|
f"/api/v1/flocks/{self.flock.pk}/",
|
|
{"iot_api_url": f" {url} "},
|
|
content_type="application/json",
|
|
**self._csrf_headers(client),
|
|
)
|
|
self.assertEqual(response.status_code, 200, response.content)
|
|
self.assertEqual(response.json()["iot_api_url"], url)
|
|
self.flock.refresh_from_db()
|
|
self.assertEqual(self.flock.iot_api_url, url)
|
|
|
|
def test_superadmin_can_clear_iot_api_url(self):
|
|
self.flock.iot_api_url = "https://example.test/iot/lantai-1"
|
|
self.flock.save(update_fields=["iot_api_url"])
|
|
client = Client(enforce_csrf_checks=True)
|
|
self._login(client, "admin", "admin123")
|
|
response = client.patch(
|
|
f"/api/v1/flocks/{self.flock.pk}/",
|
|
{"iot_api_url": ""},
|
|
content_type="application/json",
|
|
**self._csrf_headers(client),
|
|
)
|
|
self.assertEqual(response.status_code, 200, response.content)
|
|
self.flock.refresh_from_db()
|
|
self.assertEqual(self.flock.iot_api_url, "")
|
|
|
|
def test_staff_cannot_set_iot_api_url(self):
|
|
client = Client(enforce_csrf_checks=True)
|
|
self._login(client, "staff", "staff123")
|
|
response = client.patch(
|
|
f"/api/v1/flocks/{self.flock.pk}/",
|
|
{"iot_api_url": "https://example.test/iot/lantai-1"},
|
|
content_type="application/json",
|
|
**self._csrf_headers(client),
|
|
)
|
|
self.assertEqual(response.status_code, 403, response.content)
|
|
self.flock.refresh_from_db()
|
|
self.assertEqual(self.flock.iot_api_url, "")
|
|
|
|
def test_create_flock_without_iot_api_url(self):
|
|
client = Client(enforce_csrf_checks=True)
|
|
self._login(client, "admin", "admin123")
|
|
response = client.post(
|
|
"/api/v1/flocks/",
|
|
{"flock_name": "Lantai 2", "kandang": self.kandang.pk},
|
|
content_type="application/json",
|
|
**self._csrf_headers(client),
|
|
)
|
|
self.assertEqual(response.status_code, 201, response.content)
|
|
data = response.json()
|
|
self.assertEqual(data.get("iot_api_url", ""), "")
|
|
flock = Flock.objects.get(pk=data["id"])
|
|
self.assertEqual(flock.iot_api_url, "")
|