Add invitation code validation and auto-generation for guests
Validate uniqueness of invitation codes during guest creation to prevent duplicates. Automatically generate an 8-character code if none is provided, ensuring consistent data handling. Updated tests and schemas to support these changes.
This commit is contained in:
@@ -39,6 +39,33 @@ class TestGuestsRouter:
|
||||
assert data["full_name"] == guest_data["full_name"]
|
||||
assert data["email"] == guest_data["email"]
|
||||
|
||||
def test_create_guest_fails_on_duplicate_invitation_code(self, guest_data):
|
||||
# First create the guest successfully
|
||||
response_initial = self.client.post(self.endpoint, json=guest_data)
|
||||
assert response_initial.status_code == 200
|
||||
|
||||
# Attempt to create another guest with the same invitation code
|
||||
new_guest_data = guest_data.copy()
|
||||
new_guest_data["email"] = "new.email@example.com"
|
||||
response_duplicate = self.client.post(self.endpoint, json=new_guest_data)
|
||||
|
||||
assert response_duplicate.status_code == 400
|
||||
assert response_duplicate.json()["detail"] == "Guest with this invitation code already exists"
|
||||
|
||||
|
||||
def test_create_guest_generates_invitation_code_if_not_provided(self, guest_data):
|
||||
# Remove invitation_code to test auto-generation
|
||||
guest_data_without_code = guest_data.copy()
|
||||
guest_data_without_code.pop("invitation_code", None)
|
||||
|
||||
response = self.client.post(self.endpoint, json=guest_data_without_code)
|
||||
assert response.status_code == 200
|
||||
|
||||
data = response.json()
|
||||
print(data)
|
||||
assert "invitation_code" in data
|
||||
assert len(data["invitation_code"]) == 8
|
||||
|
||||
def test_create_guest_missing_required_fields_fails(self):
|
||||
incomplete_payload = {
|
||||
"email": "john.doe@example.com"
|
||||
|
||||
Reference in New Issue
Block a user