-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53852e3
commit 3bb8b0b
Showing
1 changed file
with
60 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,6 +122,14 @@ func TestViewBookingsHandler(t *testing.T) { | |
} | ||
} | ||
|
||
type testCase struct { | ||
name string | ||
payload string | ||
expectedStatusCode int | ||
expectedMessage string | ||
setupFunc func() string // Return booking ID for valid setup | ||
} | ||
|
||
// Helper function to create a mock booking for testing | ||
func createMockBooking(r *gin.Engine, payload string, cookies []*http.Cookie) (map[string]interface{}, error) { | ||
req, err := http.NewRequest("POST", "/api/book-room", bytes.NewBuffer([]byte(payload))) | ||
|
@@ -220,6 +228,57 @@ func sendRequestAndVerifyResponse(t *testing.T, r *gin.Engine, method, url strin | |
|
||
assert.Equal(t, expectedMessage, actualResponse["message"], "handler returned unexpected message") | ||
} | ||
func getSharedTestCases(r *gin.Engine, cookies []*http.Cookie) []testCase { | ||
return []testCase{ | ||
{ | ||
name: "Valid Request", | ||
payload: `{ | ||
"bookingId": "mock_id", | ||
"creator": "[email protected]" | ||
}`, | ||
expectedStatusCode: http.StatusOK, | ||
expectedMessage: "Successfully checked in!", | ||
setupFunc: func() string { | ||
// Insert a booking to be cancelled using the helper function | ||
bookingPayload := `{ | ||
"roomId": "12345", | ||
"emails": ["[email protected]"], | ||
"creator": "[email protected]", | ||
"floorNo": "1", | ||
"roomName": "Test Room", | ||
"date": "2024-07-01T00:00:00Z", | ||
"start": "2024-07-01T09:00:00Z", | ||
"end": "2024-07-01T10:00:00Z" | ||
}` | ||
response, err := createMockBooking(r, bookingPayload, cookies) | ||
if err != nil { | ||
panic(fmt.Sprintf("could not create mock booking: %v", err)) | ||
} | ||
return response["data"].(string) // Assuming "data" contains the booking ID | ||
}, | ||
}, | ||
{ | ||
name: "Invalid Request Payload", | ||
payload: `{ | ||
"bookingID": "", | ||
"creator": "[email protected]" | ||
}`, | ||
expectedStatusCode: http.StatusBadRequest, | ||
expectedMessage: "Invalid request payload", | ||
setupFunc: func() string { return "" }, | ||
}, | ||
{ | ||
name: "Booking Not Found", | ||
payload: `{ | ||
"bookingId": "nonexistent", | ||
"creator": "[email protected]" | ||
}`, | ||
expectedStatusCode: http.StatusNotFound, | ||
expectedMessage: "Booking not found", | ||
setupFunc: func() string { return "" }, | ||
}, | ||
} | ||
} | ||
|
||
// Tests the CancelBooking handler | ||
func TestCancelBooking(t *testing.T) { | ||
|
@@ -403,61 +462,7 @@ func TestCheckIn(t *testing.T) { | |
r, cookies := setupTestEnvironment(t) | ||
|
||
// Define test cases | ||
testCases := []struct { | ||
name string | ||
payload string | ||
expectedStatusCode int | ||
expectedMessage string | ||
setupFunc func() string // Return booking ID for valid setup | ||
}{ | ||
{ | ||
name: "Valid Request", | ||
payload: `{ | ||
"bookingId": "mock_id", | ||
"creator": "[email protected]" | ||
}`, | ||
expectedStatusCode: http.StatusOK, | ||
expectedMessage: "Successfully checked in!", | ||
setupFunc: func() string { | ||
// Insert a booking to be cancelled using the helper function | ||
bookingPayload := `{ | ||
"roomId": "12345", | ||
"emails": ["[email protected]"], | ||
"creator": "[email protected]", | ||
"floorNo": "1", | ||
"roomName": "Test Room", | ||
"date": "2024-07-01T00:00:00Z", | ||
"start": "2024-07-01T09:00:00Z", | ||
"end": "2024-07-01T10:00:00Z" | ||
}` | ||
response, err := createMockBooking(r, bookingPayload, cookies) | ||
if err != nil { | ||
t.Fatalf("could not create mock booking: %v", err) | ||
} | ||
return response["data"].(string) // Assuming "data" contains the booking ID | ||
}, | ||
}, | ||
{ | ||
name: "Invalid Request Payload", | ||
payload: `{ | ||
"bookingID": "", | ||
"creator": "[email protected]" | ||
}`, | ||
expectedStatusCode: http.StatusBadRequest, | ||
expectedMessage: "Invalid request payload", | ||
setupFunc: func() string { return "" }, | ||
}, | ||
{ | ||
name: "Booking Not Found", | ||
payload: `{ | ||
"bookingId": "nonexistent", | ||
"creator": "[email protected]" | ||
}`, | ||
expectedStatusCode: http.StatusNotFound, | ||
expectedMessage: "Booking not found", | ||
setupFunc: func() string { return "" }, | ||
}, | ||
} | ||
testCases := getSharedTestCases(r, cookies) | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
|