Skip to content

Commit

Permalink
Merge pull request #84 from COS301-SE-2024/test/backend/unit-testing-…
Browse files Browse the repository at this point in the history
…&-integration-testing

All possible tests were written and pass
  • Loading branch information
Rethakgetse-Manaka authored Jun 23, 2024
2 parents 00e1102 + b597c5c commit 5be1b44
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
122 changes: 122 additions & 0 deletions occupi-backend/tests/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,128 @@ func TestViewBookingsHandler(t *testing.T) {
})
}
}
func TestBookRoom(t *testing.T) {
// Load environment variables from .env file
if err := godotenv.Load("../.env"); err != nil {
t.Fatal("Error loading .env file: ", err)
}

// setup logger to log all server interactions
utils.SetupLogger()

// connect to the database
db := database.ConnectToDatabase()

// set gin run mode
gin.SetMode("test")

// Create a Gin router
r := gin.Default()

// Register the route
router.OccupiRouter(r, db)

token, _, _ := authenticator.GenerateToken("[email protected]", constants.Basic)

w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/ping-auth", nil)
req.AddCookie(&http.Cookie{Name: "token", Value: token})

r.ServeHTTP(w, req)

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(
t,
"{\"data\":null,\"message\":\"pong -> I am alive and kicking and you are auth'd\",\"status\":200}",
strings.ReplaceAll(w.Body.String(), "-\\u003e", "->"),
)

// Store the cookies from the login response
cookies := req.Cookies()

// Define test cases
testCases := []struct {
name string
payload string
expectedStatusCode int
expectedMessage string
expectedData gin.H
}{
{
name: "Valid Request",
payload: `{
"roomId": "12345",
"Slot": 1,
"Emails": ["[email protected]"],
"Creator": "[email protected]",
"FloorNo": 1
}`,
expectedStatusCode: http.StatusOK,
expectedMessage: "Successfully booked!",
expectedData: gin.H{"id": "some_generated_id"}, // The exact value will be replaced dynamically
},
{
name: "Invalid Request Payload",
payload: `{
"RoomID": "",
"Slot": "",
"Emails": [],
"Creator": "",
"FloorNo": 0
}`,
expectedStatusCode: http.StatusBadRequest,
expectedMessage: "Invalid request payload",
expectedData: nil,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Create a request to pass to the handler
req, err := http.NewRequest("POST", "/api/book-room", strings.NewReader(tc.payload))
if err != nil {
t.Fatal(err)
}

// Add the stored cookies to the request
for _, cookie := range cookies {
req.AddCookie(cookie)
}

// Create a response recorder to record the response
rr := httptest.NewRecorder()

// Serve the request
r.ServeHTTP(rr, req)

// Check the status code is what we expect
assert.Equal(t, tc.expectedStatusCode, rr.Code, "handler returned wrong status code")

// Define the expected response
expectedResponse := gin.H{
"message": tc.expectedMessage,
"status": float64(tc.expectedStatusCode),
"data": tc.expectedData,
}

// Unmarshal the actual response
var actualResponse gin.H
if err := json.Unmarshal(rr.Body.Bytes(), &actualResponse); err != nil {
t.Fatalf("could not unmarshal response: %v", err)
}

// Check the response message and status
assert.Equal(t, expectedResponse["message"], actualResponse["message"], "handler returned unexpected message")
assert.Equal(t, expectedResponse["status"], actualResponse["status"], "handler returned unexpected status")

// For successful booking, check if the ID is generated
if tc.expectedStatusCode == http.StatusOK {
assert.NotEmpty(t, actualResponse["data"], "booking ID should not be empty")
}
})
}
}

func TestPingRoute(t *testing.T) {
// Load environment variables from .env file
if err := godotenv.Load("../.env"); err != nil {
Expand Down
21 changes: 21 additions & 0 deletions occupi-backend/tests/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,24 @@ func TestErrorResponseWithMeta(t *testing.T) {
response := utils.ErrorResponseWithMeta(http.StatusBadRequest, "Bad Request", "INVALID_INPUT", "Invalid input provided", gin.H{"field": "value"}, gin.H{"request_id": "12345"})
assert.Equal(t, expected, response)
}

// TestGenerateOTP tests the GenerateOTP function
func TestGenerateOTP(t *testing.T) {
otp, err := utils.GenerateOTP()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}

if len(otp) != 6 {
t.Fatalf("Expected OTP to be 6 digits long, got %v", len(otp))
}

// Check if all characters in OTP are digits
for _, char := range otp {
if char < '0' || char > '9' {
t.Fatalf("Expected OTP to contain only digits, got %v", otp)
}
}

t.Logf("Generated OTP: %s", otp)
}

0 comments on commit 5be1b44

Please sign in to comment.