-
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.
Merge pull request #138 from COS301-SE-2024/feature/backend/RetrieveU…
…serDetails User Details Endpoint
- Loading branch information
Showing
6 changed files
with
216 additions
and
3 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 |
---|---|---|
|
@@ -499,7 +499,7 @@ If there are any errors during the process, appropriate error messages are retur | |
|
||
- **Method** | ||
|
||
`POST` | ||
`GET` | ||
|
||
- **Request Body** | ||
|
||
|
@@ -526,7 +526,53 @@ If there are any errors during the process, appropriate error messages are retur | |
- **Code:** 500 | ||
- **Content:** `{ "status": 500, "message": "Failed to get rooms", "error": {"code":"INTERNAL_SERVER_ERROR","details":null,"message":"Failed to get rooms"} }` | ||
|
||
|
||
### GetUserDetails | ||
|
||
This endpoint is used to view the details of a user in the Occupi system. | ||
Upon a successful request, the details of the user are returned. | ||
If there are any errors during the process, appropriate error messages are returned. | ||
|
||
- **URL** | ||
|
||
`/api/[email protected]` | ||
|
||
- **Method** | ||
|
||
`GET` | ||
|
||
- **Request Body** | ||
|
||
- **Content** | ||
|
||
```json copy | ||
{ | ||
"email": "string" | ||
} | ||
``` | ||
|
||
**Success Response** | ||
|
||
- **Code:** 200 | ||
- **Content:** `{ "status": 200, "message": "Successfully fetched user details!", "data": {"user details"} }` | ||
|
||
**Error Response** | ||
|
||
- **Code:** 400 | ||
- **Content:** `{ "status": 400, "message": "Invalid request payload", "error": {"code":"BAD_REQUEST","details":null,"message":"missing field required: <name of field>"}, }` | ||
|
||
**Error Response** | ||
|
||
- **Code:** 404 | ||
- **Content:** `{ "status": 404, "message": "User not found", "error": {"code":"BAD_REQUEST","details":null,"message":"User not found"} }` | ||
|
||
**Error Response** | ||
|
||
- **Code:** 500 | ||
- **Content:** `{ "status": 500, "message": "Failed to get user details", "error": {"code":"INTERNAL_SERVER_ERROR","details":null,"message":"Failed to get user details"} }` | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
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
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
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
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
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ package tests | |
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
@@ -12,6 +13,8 @@ import ( | |
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
|
||
"github.com/gin-gonic/gin" | ||
|
||
|
@@ -157,6 +160,9 @@ func createMockBooking(r *gin.Engine, payload string, cookies []*http.Cookie) (m | |
|
||
return response, nil | ||
} | ||
func BoolPtr(b bool) *bool { | ||
return &b | ||
} | ||
|
||
// SetupTestEnvironment initializes the test environment and returns the router and cookies | ||
func setupTestEnvironment(t *testing.T) (*gin.Engine, []*http.Cookie) { | ||
|
@@ -195,13 +201,29 @@ func setupTestEnvironment(t *testing.T) (*gin.Engine, []*http.Cookie) { | |
return r, cookies | ||
} | ||
|
||
// Clean up the test database | ||
func CleanupTestDatabase(db *mongo.Database) { | ||
collection := db.Collection("users") | ||
collection.DeleteMany(context.Background(), bson.M{}) | ||
} | ||
|
||
// Helper function to send a request and verify the response | ||
func sendRequestAndVerifyResponse(t *testing.T, r *gin.Engine, method, url string, payload string, cookies []*http.Cookie, expectedStatusCode int, expectedMessage string) { | ||
// Create a request to pass to the handler | ||
req, err := http.NewRequest(method, url, bytes.NewBuffer([]byte(payload))) | ||
var req *http.Request | ||
var err error | ||
|
||
if method == http.MethodGet { | ||
req, err = http.NewRequest(method, url, nil) | ||
} else { | ||
req, err = http.NewRequest(method, url, bytes.NewBuffer([]byte(payload))) | ||
} | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Set the request header | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
// Add the stored cookies to the request | ||
|
@@ -279,6 +301,8 @@ func getSharedTestCases(r *gin.Engine, cookies []*http.Cookie) []testCase { | |
} | ||
} | ||
|
||
// Tests the ViewUserDetails handler | ||
|
||
// Tests the CancelBooking handler | ||
func TestCancelBooking(t *testing.T) { | ||
// Setup the test environment | ||
|
@@ -372,6 +396,77 @@ func TestCancelBooking(t *testing.T) { | |
} | ||
} | ||
|
||
// Tests the GetUserDetails handler | ||
// func TestGetUserDetails(t *testing.T) { | ||
// // connect to the database | ||
// db := configs.ConnectToDatabase(constants.AdminDBAccessOption) | ||
// collection := db.Database("Occupi").Collection("users") | ||
// parsedTime, _ := time.Parse(time.RFC3339, "2024-09-15T00:00:00Z") | ||
// documents := []interface{}{ | ||
// models.UserDetails{ | ||
// OccupiID: "OCCUPI20245311", | ||
// Password: "hashedpassword", | ||
// Email: "[email protected]", | ||
// Role: "admin", | ||
// OnSite: true, | ||
// IsVerified: true, | ||
// NextVerificationDate: parsedTime, | ||
// Details: &models.Details{ | ||
// ContactNo: "123-456-7890", | ||
// DOB: parsedTime, | ||
// Gender: "male", | ||
// Name: "John Doe", | ||
// Pronouns: "He/him", | ||
// }, | ||
// Notifications: &models.Notifications{ | ||
// Allow: BoolPtr(true), | ||
// BookingReminder: BoolPtr(true), | ||
// Max_Capacity: BoolPtr(true), | ||
// }, | ||
// Security: &models.Security{ | ||
// MFA: BoolPtr(true), | ||
// Biometrics: BoolPtr(true), | ||
// }, | ||
// Position: "Manager", | ||
// Status: "Active", | ||
// }, | ||
// } | ||
// setup.InsertData(collection, documents) | ||
// // Setup the test environment | ||
// r, cookies := setupTestEnvironment(t) | ||
|
||
// // Define test cases | ||
// testCases := []testCase{ | ||
// { | ||
// name: "Valid Request", | ||
// payload: "/api/[email protected]", | ||
// expectedStatusCode: http.StatusOK, | ||
// expectedMessage: "Successfully fetched user details", | ||
// setupFunc: func() string { return "" }, | ||
// }, | ||
// { | ||
// name: "Invalid Request", | ||
// payload: "/api/user-details", | ||
// expectedStatusCode: http.StatusBadRequest, | ||
// expectedMessage: "Invalid request payload", | ||
// setupFunc: func() string { return "" }, | ||
// }, | ||
// { | ||
// name: "User Not Found", | ||
// payload: "/api/[email protected]", | ||
// expectedStatusCode: http.StatusNotFound, | ||
// expectedMessage: "User not found", | ||
// setupFunc: func() string { return "" }, | ||
// }, | ||
// } | ||
|
||
// for _, tc := range testCases { | ||
// t.Run(tc.name, func(t *testing.T) { | ||
// sendRequestAndVerifyResponse(t, r, "GET", tc.payload, "", cookies, tc.expectedStatusCode, tc.expectedMessage) | ||
// }) | ||
// } | ||
// } | ||
|
||
// Tests the BookRoom handler | ||
func TestBookRoom(t *testing.T) { | ||
// Setup the test environment | ||
|