From b597c5cfa7d7e249c01a166ad6b5ceee6223b7a3 Mon Sep 17 00:00:00 2001 From: Rethakgetse-Manaka Date: Mon, 24 Jun 2024 00:50:15 +0200 Subject: [PATCH] All possible tests were written and pass --- occupi-backend/tests/handlers_test.go | 122 ++++++++++++++++++++++++++ occupi-backend/tests/utils_test.go | 21 +++++ 2 files changed, 143 insertions(+) diff --git a/occupi-backend/tests/handlers_test.go b/occupi-backend/tests/handlers_test.go index adacd911..3e272c5d 100644 --- a/occupi-backend/tests/handlers_test.go +++ b/occupi-backend/tests/handlers_test.go @@ -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("test@example.com", 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": ["test@example.com"], + "Creator": "test@example.com", + "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 { diff --git a/occupi-backend/tests/utils_test.go b/occupi-backend/tests/utils_test.go index 4c8a6c53..6cc213ad 100644 --- a/occupi-backend/tests/utils_test.go +++ b/occupi-backend/tests/utils_test.go @@ -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) +}