closes #4
This commit is contained in:
59
dta_service/core/tests/test_property_location_restriction.py
Normal file
59
dta_service/core/tests/test_property_location_restriction.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from django.test import TestCase
|
||||
from rest_framework.test import APIClient
|
||||
from rest_framework import status
|
||||
from core.models import Property, PropertyOwner, User
|
||||
from unittest.mock import patch
|
||||
|
||||
class PropertyLocationRestrictionTests(TestCase):
|
||||
def setUp(self):
|
||||
self.client = APIClient()
|
||||
self.user = User.objects.create(email="owner@example.com", first_name="Owner", last_name="User", user_type="property_owner")
|
||||
self.owner = PropertyOwner.objects.create(user=self.user)
|
||||
self.client.force_authenticate(user=self.user)
|
||||
|
||||
self.il_property = Property.objects.create(
|
||||
owner=self.owner,
|
||||
address="123 IL St",
|
||||
city="Chicago",
|
||||
state="IL",
|
||||
zip_code="60601",
|
||||
market_value=100000,
|
||||
realestate_api_id=1,
|
||||
property_status="off_market"
|
||||
)
|
||||
|
||||
self.ny_property = Property.objects.create(
|
||||
owner=self.owner,
|
||||
address="456 NY St",
|
||||
city="New York",
|
||||
state="NY",
|
||||
zip_code="10001",
|
||||
market_value=200000,
|
||||
realestate_api_id=2,
|
||||
property_status="off_market"
|
||||
)
|
||||
|
||||
def test_activate_il_property_success(self):
|
||||
# Mock the attorney letter check to pass
|
||||
with patch('core.services.document_service.DocumentService.check_engagement_letter_accepted', return_value=True):
|
||||
response = self.client.patch(
|
||||
f'/api/properties/{self.il_property.id}/',
|
||||
{'property_status': 'active'},
|
||||
format='json'
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.il_property.refresh_from_db()
|
||||
self.assertEqual(self.il_property.property_status, 'active')
|
||||
|
||||
def test_activate_ny_property_failure(self):
|
||||
# Mock the attorney letter check to pass (though it shouldn't be reached)
|
||||
with patch('core.services.document_service.DocumentService.check_engagement_letter_accepted', return_value=True):
|
||||
response = self.client.patch(
|
||||
f'/api/properties/{self.ny_property.id}/',
|
||||
{'property_status': 'active'},
|
||||
format='json'
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertIn("Only properties in Illinois can be set to active.", str(response.data))
|
||||
self.ny_property.refresh_from_db()
|
||||
self.assertEqual(self.ny_property.property_status, 'off_market')
|
||||
@@ -96,6 +96,21 @@ class PropertyViewSet(viewsets.ModelViewSet):
|
||||
else:
|
||||
serializer.save()
|
||||
|
||||
def perform_update(self, serializer):
|
||||
# Check if status is being set to active
|
||||
new_status = serializer.validated_data.get("property_status")
|
||||
if new_status == "active":
|
||||
# Check state
|
||||
state = serializer.validated_data.get("state", serializer.instance.state)
|
||||
if state != "IL":
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
raise ValidationError(
|
||||
{"detail": "Only properties in Illinois can be set to active."}
|
||||
)
|
||||
|
||||
serializer.save()
|
||||
|
||||
@action(detail=True, methods=["post"])
|
||||
def increment_view_count(self, request, pk=None):
|
||||
property_obj = self.get_object()
|
||||
|
||||
Reference in New Issue
Block a user