#50 feat: add StoreSettings model and API for store address and map coordinates
This commit is contained in:
157
tienda_ilusion/don_confiao/tests/test_store_settings.py
Normal file
157
tienda_ilusion/don_confiao/tests/test_store_settings.py
Normal file
@@ -0,0 +1,157 @@
|
||||
from django.contrib.auth.models import User
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APIClient, APITestCase
|
||||
from rest_framework_simplejwt.tokens import RefreshToken
|
||||
|
||||
from ..models.store_settings import StoreSettings
|
||||
from .Mixins import LoginMixin
|
||||
|
||||
URL = "/don_confiao/api/store_settings"
|
||||
|
||||
|
||||
class TestStoreSettingsModel(APITestCase, LoginMixin):
|
||||
def test_singleton_returns_single_instance(self):
|
||||
first = StoreSettings.get_singleton()
|
||||
second = StoreSettings.get_singleton()
|
||||
self.assertEqual(first.pk, second.pk)
|
||||
self.assertEqual(StoreSettings.objects.count(), 1)
|
||||
|
||||
def test_singleton_created_without_address(self):
|
||||
instance = StoreSettings.get_singleton()
|
||||
self.assertEqual(instance.address, "")
|
||||
|
||||
def test_str(self):
|
||||
instance = StoreSettings.get_singleton()
|
||||
self.assertEqual(str(instance), f"StoreSettings (id={instance.id})")
|
||||
|
||||
|
||||
class TestStoreSettingsAPIPermissions(APITestCase, LoginMixin):
|
||||
def setUp(self):
|
||||
self.url = URL
|
||||
|
||||
def test_get_unauthenticated(self):
|
||||
client = APIClient()
|
||||
response = client.get(self.url)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
def test_patch_unauthenticated(self):
|
||||
client = APIClient()
|
||||
response = client.patch(
|
||||
self.url,
|
||||
{"address": "Calle falsa 123"},
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||
|
||||
def test_patch_non_admin(self):
|
||||
user = User.objects.create_user(
|
||||
username="regularuser",
|
||||
email="regular@example.com",
|
||||
password="regularpass",
|
||||
)
|
||||
refresh = RefreshToken.for_user(user)
|
||||
client = APIClient()
|
||||
client.credentials(
|
||||
HTTP_AUTHORIZATION=f"Bearer {str(refresh.access_token)}"
|
||||
)
|
||||
response = client.patch(
|
||||
self.url,
|
||||
{"address": "Calle falsa 123"},
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
||||
|
||||
def test_patch_publico(self):
|
||||
user = User.objects.create_user(
|
||||
username="publico",
|
||||
email="publico@example.com",
|
||||
password="publicopass",
|
||||
)
|
||||
user.profile.user_type = "publico"
|
||||
user.profile.save()
|
||||
refresh = RefreshToken.for_user(user)
|
||||
client = APIClient()
|
||||
client.credentials(
|
||||
HTTP_AUTHORIZATION=f"Bearer {str(refresh.access_token)}"
|
||||
)
|
||||
response = client.patch(
|
||||
self.url,
|
||||
{"address": "Calle falsa 123"},
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
||||
|
||||
|
||||
class TestStoreSettingsAPI(APITestCase, LoginMixin):
|
||||
def setUp(self):
|
||||
self.login()
|
||||
self.url = URL
|
||||
|
||||
def test_get_creates_singleton(self):
|
||||
self.assertEqual(StoreSettings.objects.count(), 0)
|
||||
response = self.client.get(self.url)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(StoreSettings.objects.count(), 1)
|
||||
data = response.json()
|
||||
self.assertIn("id", data)
|
||||
self.assertIn("address", data)
|
||||
self.assertIn("latitude", data)
|
||||
self.assertIn("longitude", data)
|
||||
self.assertIn("updated_at", data)
|
||||
|
||||
def test_patch_updates_settings(self):
|
||||
self.client.get(self.url)
|
||||
response = self.client.patch(
|
||||
self.url,
|
||||
{
|
||||
"address": "Carrera 5 # 10-20, Bogotá",
|
||||
"latitude": 4.6097,
|
||||
"longitude": -74.0817,
|
||||
},
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
data = response.json()
|
||||
self.assertEqual(data["address"], "Carrera 5 # 10-20, Bogotá")
|
||||
self.assertEqual(data["latitude"], 4.6097)
|
||||
self.assertEqual(data["longitude"], -74.0817)
|
||||
|
||||
settings = StoreSettings.objects.get(pk=1)
|
||||
self.assertEqual(settings.address, "Carrera 5 # 10-20, Bogotá")
|
||||
self.assertEqual(settings.latitude, 4.6097)
|
||||
self.assertEqual(settings.longitude, -74.0817)
|
||||
|
||||
def test_patch_partial(self):
|
||||
self.client.get(self.url)
|
||||
response = self.client.patch(
|
||||
self.url,
|
||||
{"address": "Solo dirección"},
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
settings = StoreSettings.objects.get(pk=1)
|
||||
self.assertEqual(settings.address, "Solo dirección")
|
||||
self.assertIsNone(settings.latitude)
|
||||
|
||||
def test_patch_reuses_singleton(self):
|
||||
self.client.get(self.url)
|
||||
self.client.patch(
|
||||
self.url,
|
||||
{"address": "Primera"},
|
||||
format="json",
|
||||
)
|
||||
self.client.patch(
|
||||
self.url,
|
||||
{"address": "Segunda"},
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(StoreSettings.objects.count(), 1)
|
||||
|
||||
def test_patch_invalid_coordinates(self):
|
||||
self.client.get(self.url)
|
||||
response = self.client.patch(
|
||||
self.url,
|
||||
{"latitude": "not-a-number"},
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
Reference in New Issue
Block a user