Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions backend/gsr_booking/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ class Meta:
class GSRShareCodeSerializer(serializers.ModelSerializer):
status = serializers.SerializerMethodField()
expires_at = serializers.SerializerMethodField()
booking_id = serializers.PrimaryKeyRelatedField(
source="booking", queryset=GSRBooking.objects.all(), write_only=True
booking_id = serializers.SlugRelatedField(
slug_field="booking_id",
source="booking",
queryset=GSRBooking.objects.all(),
write_only=True,
)

class Meta:
Expand Down Expand Up @@ -126,11 +129,23 @@ class SharedGSRBookingSerializer(serializers.ModelSerializer):

building = serializers.CharField(source="gsr.name")
is_valid = serializers.SerializerMethodField()
owner_name = serializers.SerializerMethodField()

class Meta:
model = GSRBooking
fields = ["room_name", "building", "start", "end", "is_valid"]
fields = ["room_name", "building", "start", "end", "is_valid", "owner_name"]
read_only_fields = fields

def get_owner_name(self, obj):
user = obj.reservation.creator if obj.reservation else obj.user
if not user:
return "Unknown"

full_name = f"{user.first_name} {user.last_name}".strip()
if full_name:
return full_name

return user.username

def get_is_valid(self, obj):
return obj.end and obj.end > timezone.now()
12 changes: 6 additions & 6 deletions backend/tests/gsr_booking/test_share_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def setUp(self):
def test_create_share_code_success(self):
# Creates a gsr share code successfully
self.client.force_authenticate(user=self.owner)
response = self.client.post("/api/gsr/share/", {"booking_id": self.booking.id})
response = self.client.post("/api/gsr/share/", {"booking_id": self.booking.booking_id})
self.assertEqual(response.status_code, 201)
payload = json.loads(response.content)

Expand All @@ -72,13 +72,13 @@ def test_create_share_code_duplicate(self):
self.client.force_authenticate(user=self.owner)

# First creation
response1 = self.client.post("/api/gsr/share/", {"booking_id": self.booking.id})
response1 = self.client.post("/api/gsr/share/", {"booking_id": self.booking.booking_id})
self.assertEqual(response1.status_code, 201)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@khoiddinh ensure that you're checking for status code 201 instead of 200.

payload1 = json.loads(response1.content)
first_code = payload1["code"]

# Second creation (should return existing code)
response2 = self.client.post("/api/gsr/share/", {"booking_id": self.booking.id})
response2 = self.client.post("/api/gsr/share/", {"booking_id": self.booking.booking_id})
self.assertEqual(response2.status_code, 201) # Changed from 200 to 201
payload2 = json.loads(response2.content)

Expand All @@ -89,7 +89,7 @@ def test_create_share_code_duplicate(self):
self.assertEqual(GSRShareCode.objects.filter(booking=self.booking).count(), 1)

def test_create_share_code_without_auth(self):
response = self.client.post("/api/gsr/share/", {"booking_id": self.booking.id})
response = self.client.post("/api/gsr/share/", {"booking_id": self.booking.booking_id})
self.assertEqual(response.status_code, 403)
self.assertEqual(GSRShareCode.objects.count(), 0)

Expand Down Expand Up @@ -217,7 +217,7 @@ def test_create_share_code_for_expired_booking_code_invalid(self):
self.booking.save(update_fields=["end"])

self.client.force_authenticate(user=self.owner)
response = self.client.post("/api/gsr/share/", {"booking_id": self.booking.id})
response = self.client.post("/api/gsr/share/", {"booking_id": self.booking.booking_id})
self.assertEqual(response.status_code, 201)
payload = json.loads(response.content)
self.assertEqual(payload["status"], "expired")
Expand Down Expand Up @@ -247,7 +247,7 @@ def test_create_share_code_replaces_expired(self):

# Create again
self.client.force_authenticate(user=self.owner)
response = self.client.post("/api/gsr/share/", {"booking_id": self.booking.id})
response = self.client.post("/api/gsr/share/", {"booking_id": self.booking.booking_id})
self.assertEqual(response.status_code, 201) # Changed from 200 to 201
payload = json.loads(response.content)

Expand Down
Loading