Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/COS301-SE-2024/occupi in…
Browse files Browse the repository at this point in the history
…to develop
  • Loading branch information
waveyboym committed Jul 28, 2024
2 parents cf452aa + a2e0914 commit 51082aa
Show file tree
Hide file tree
Showing 5 changed files with 1,408 additions and 10 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test-and-cov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ on:

jobs:
test-api:
name: 🧪 Test API
name: 🧪 Test API 🔌
runs-on: ubuntu-latest

# set working directory
Expand Down Expand Up @@ -109,7 +109,7 @@ jobs:

# Test job
test-web:
name: 🧪 Test web
name: 🧪 Test web 💻
runs-on: ubuntu-latest

# set working directory
Expand Down Expand Up @@ -141,7 +141,7 @@ jobs:
verbose: true

test-mobile:
name: 🧪 Test mobile
name: 🧪 Test mobile 📱
runs-on: ubuntu-latest

# set working directory
Expand Down
5 changes: 3 additions & 2 deletions occupi-backend/pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,9 @@ func ConfirmCancellation(ctx *gin.Context, appsession *models.AppSession, id str

// Find the booking by bookingId, roomId, and check if the email is in the emails object
filter := bson.M{
"_id": id,
"creator": email}
"occupiId": id,
"creator": email,
}

// Delete the booking
_, err := collection.DeleteOne(ctx, filter)
Expand Down
2 changes: 1 addition & 1 deletion occupi-backend/pkg/sender/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func PublishMessage(appsession *models.AppSession, notification models.ScheduledNotification) error {
// if gin run mode is test randomly return error
if configs.GetGinRunMode() == "test" {
return utils.RandomError()
return nil
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
Expand Down
174 changes: 174 additions & 0 deletions occupi-backend/tests/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,3 +915,177 @@ func TestCheckIfUserIsLoggingInFromKnownLocation_withCache(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, cachedUser)
}

func TestGetUserDetailsPerformance(t *testing.T) {
userStruct := models.User{
OccupiID: "123456",
Password: "hashedpassword123",
Email: "[email protected]",
Role: constants.Admin,
OnSite: true,
IsVerified: true,
NextVerificationDate: time.Now(), // this will be updated once the email is verified
TwoFAEnabled: false,
KnownLocations: []models.Location{
{
City: "Cape Town",
Region: "Western Cape",
Country: "South Africa",
},
},
Details: models.Details{
ImageID: "",
Name: "Michael",
DOB: time.Now(),
Gender: "Male",
Pronouns: "He/Him",
},
Notifications: models.Notifications{
Invites: true,
BookingReminder: true,
},
Security: models.Security{
MFA: false,
Biometrics: false,
ForceLogout: false,
},
Status: "5",
Position: "Chief Executive Engineer",
DepartmentNo: "01",
ExpoPushToken: "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
}

// Create database connection and Cache
db := configs.ConnectToDatabase(constants.AdminDBAccessOption)
Cache := configs.CreateCache()

// Create a new ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
w := httptest.NewRecorder()

// Create a response writer and context
ctx, _ := gin.CreateTestContext(w)

// Create a new AppSession with the Cache
appsessionWithCache := &models.AppSession{
DB: db,
Cache: Cache,
}
// Create a new AppSession without the Cache
appsessionWithoutCache := &models.AppSession{
DB: db,
Cache: nil,
}

// Mock the DB response
collection := db.Database(configs.GetMongoDBName()).Collection("Users")

_, err := collection.InsertOne(ctx, userStruct)
if err != nil {
t.Fatalf("Failed to insert test user into database: %v", err)
}

// Test performance with Cache
startTime := time.Now()
for i := 0; i < 1000; i++ {
database.GetUserDetails(ctx, appsessionWithCache, userStruct.Email)
}
durationWithCache := time.Since(startTime)

// Test performance without Cache
startTime = time.Now()
for i := 0; i < 1000; i++ {
database.GetUserDetails(ctx, appsessionWithoutCache, userStruct.Email)
}
durationWithoutCache := time.Since(startTime)

// Assert that the Cache improves the speed
if durationWithoutCache <= durationWithCache {
t.Errorf("Cache did not improve performance: duration with Cache %v, duration without Cache %v", durationWithCache, durationWithoutCache)
}
}

func TestGetUserDetails_withCache(t *testing.T) {
userStruct := models.User{
OccupiID: "123456",
Password: "hashedpassword123",
Email: "[email protected]",
Role: constants.Admin,
OnSite: true,
IsVerified: true,
NextVerificationDate: time.Now(), // this will be updated once the email is verified
TwoFAEnabled: false,
KnownLocations: []models.Location{
{
City: "Cape Town",
Region: "Western Cape",
Country: "South Africa",
},
},
Details: models.Details{
ImageID: "",
Name: "Michael",
DOB: time.Now(),
Gender: "Male",
Pronouns: "He/Him",
},
Notifications: models.Notifications{
Invites: true,
BookingReminder: true,
},
Security: models.Security{
MFA: false,
Biometrics: false,
ForceLogout: false,
},
Status: "5",
Position: "Chief Executive Engineer",
DepartmentNo: "01",
ExpoPushToken: "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
}
// Create database connection and Cache
db := configs.ConnectToDatabase(constants.AdminDBAccessOption)
Cache := configs.CreateCache()

// Create a new ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
w := httptest.NewRecorder()

// Create a response writer and context
ctx, _ := gin.CreateTestContext(w)

// Create a new AppSession with the Cache
appSession := &models.AppSession{
DB: db,
Cache: Cache,
}

// Mock the DB response
collection := db.Database(configs.GetMongoDBName()).Collection("Users")

_, err := collection.InsertOne(ctx, userStruct)
if err != nil {
t.Fatalf("Failed to insert test user into database: %v", err)
}

// Verify the user is not in the Cache before calling the function
nocachedUser, err := Cache.Get(cache.UserKey(userStruct.Email))

assert.NotNil(t, err)
assert.Nil(t, nocachedUser)

// call the function to test
user, err := database.GetUserDetails(ctx, appSession, userStruct.Email)

// Verify the response
assert.Equal(t, userStruct.Email, user.Email)
assert.Equal(t, userStruct.Details.Name, user.Name)
assert.Equal(t, userStruct.Details.Gender, user.Gender)
assert.Equal(t, userStruct.Details.Pronouns, user.Pronouns)
assert.Equal(t, userStruct.Details.ContactNo, user.Number)
assert.Nil(t, err)

// Verify the user is in the Cache
cachedUser, err := Cache.Get(cache.UserKey(userStruct.Email))

assert.Nil(t, err)
assert.NotNil(t, cachedUser)
}
Loading

0 comments on commit 51082aa

Please sign in to comment.