diff --git a/test/helpers.go b/test/helpers.go index a8ed2f0..71ba335 100644 --- a/test/helpers.go +++ b/test/helpers.go @@ -45,6 +45,7 @@ func TestPostRequest(t *testing.T, data string, url string, expectedStatusCode i var errorResponse helpers.ErrorResponse err = json.NewDecoder(res.Body).Decode(&errorResponse) if err != nil { + // fmt.Println(res.Body) t.Fatalf("failed to decode error response: %v", err) } t.Logf("Error message: %s", errorResponse.Message) diff --git a/test/init.go b/test/init.go index 349f314..8c02697 100644 --- a/test/init.go +++ b/test/init.go @@ -33,6 +33,7 @@ func InitRoutes() *mux.Router { routes.AuthRoutes(router.PathPrefix("/api").Subrouter()) routes.UserRoutes(router.PathPrefix("/api/user").Subrouter(), protectedRouter.PathPrefix("/user").Subrouter()) routes.UserRelationRoutes(router.PathPrefix("/api/ur").Subrouter(), protectedRouter.PathPrefix("/ur").Subrouter()) + routes.ReminderRoutes(router.PathPrefix("/api/reminder").Subrouter(), protectedRouter.PathPrefix("/reminder").Subrouter()) return router } diff --git a/test/order_test.go b/test/order_test.go index a3d1c88..a86ea23 100644 --- a/test/order_test.go +++ b/test/order_test.go @@ -53,8 +53,11 @@ func TestOrder(t *testing.T) { // // create other users after testing user routes t.Run("TestCreateOtherUsers", TestCreateOtherUsers) + t.Run("TestGetAllUserRelationNoneFound", TestGetAllUserRelationNoneFound) + t.Run("TestCreateUserRelation", TestCreateUserRelation) t.Run("TestCreateUserRelationErrorDuplicateRelation", TestCreateUserRelationErrorDuplicateRelation) + t.Run("TestCreateUserRelationErrorMissingField", TestCreateUserRelationErrorMissingField) // t.Run("TestCreateUserRelationErrorRelationAlreadyExist",TestCreateUserRelationErrorRelationAlreadyExist) //need to handle t.Run("TestCreateUserRelationErrorUnauthorized", TestCreateUserRelationErrorUnauthorized) t.Run("TestCreateUserRelationErrorInvalidRelatedUserId", TestCreateUserRelationErrorInvalidRelatedUserId) @@ -78,8 +81,26 @@ func TestOrder(t *testing.T) { t.Run("TestUpdateUserRelationErrorNotFound", TestUpdateUserRelationErrorNotFound) t.Run("TestUpdateUserRelationErrorUnauthorized", TestUpdateUserRelationErrorUnauthorized) - t.Run("TestDeleteUserRelationErrorNotFound", TestDeleteUserRelationErrorNotFound) - t.Run("TestDeleteUserRelationErrorUnauthorised", TestDeleteUserRelationErrorUnauthorised) + t.Run("TestGetAllReminderNoneFound", TestGetAllReminderNoneFound) + t.Run("TestCreateReminder", TestCreateReminder) + t.Run("TestCreateReminderErrorMissingField", TestCreateReminderErrorMissingField) + t.Run("TestCreateReminderErrorInvalidTime", TestCreateReminderErrorInvalidTime) + t.Run("TestCreateReminderErrorInvalidReceiverId", TestCreateReminderErrorInvalidReceiverId) + t.Run("TestCreateReminderErrorUnauthorized", TestCreateReminderErrorUnauthorized) + + t.Run("TestGetAllReminder", TestGetAllReminder) + t.Run("TestGetAllReminderErrorUnauthorized", TestGetAllReminderErrorUnauthorized) + t.Run("TestGetAllReminderErrorInvalidColumns", TestGetAllReminderErrorInvalidColumn) + + t.Run("TestGetParticularReminder", TestGetParticularReminder) + t.Run("TestGetParticularReminderErrorUnauthorized", TestGetParticularReminderErrorUnauthorized) + t.Run("TestGetParticularReminderErrorInvalidColumns", TestGetParticularReminderErrorInvalidColumns) + t.Run("TestGetParticularReminderErrorReminderNotFound", TestGetParticularReminderErrorNotFound) + + t.Run("TestUpdateReminder", TestUpdateReminder) + t.Run("TestUpdateReminderErrorUnauthorized", TestUpdateReminderErrorUnauthorized) + t.Run("TestUpdateReminderErrorInvalidColumns", TestUpdateReminderErrorInvalidColumn) + t.Run("TestUpdateReminderErrorReminderNotFound", TestUpdateReminderErrorNotFound) // //tests for related user logged in t.Run("Test login related user", TestLoginRelatedUserUsingEmail) @@ -87,6 +108,9 @@ func TestOrder(t *testing.T) { t.Run("TestUpdateUserRelation", TestUpdateUserRelation) t.Run("TestUpdateUserRelationErrorNotAllowedToRevertToPendingStatus", TestUpdateUserRelationErrorNotAllowedToRevertToPendingStatus) + t.Run("TestGetParticularUserRelation", TestGetParticularUserRelation) + t.Run("TestUpdateReminder", TestUpdateReminder) + //tests for third user who is not allowed for things t.Run("Test login not allowed user", TestLoginNotAllowedUserUsingPhone) @@ -94,12 +118,22 @@ func TestOrder(t *testing.T) { t.Run("TestGetParticularUserRelationErrorNotAllowed", TestGetParticularUserRelationErrorNotAllowed) t.Run("TestDeleteUserRelationErrorNotAllowed", TestDeleteUserRelationErrorNotAllowed) + t.Run("TestGetParticularReminderErrorNotAllowed", TestGetParticularReminderErrorNotAllowed) + t.Run("TestUpdateReminderErrorNotAllowed", TestUpdateReminderErrorNotAllowed) + t.Run("TestDeleteReminderErrorNotAllowd", TestDeleteReminderErrorNotAllowed) + // //delete/update tests for main user logged in user t.Run("Test login main user", TestLoginUser) t.Run("TestUpdateUser", TestUpdateUser) + + t.Run("TestDeleteUserRelationErrorNotFound", TestDeleteUserRelationErrorNotFound) + t.Run("TestDeleteUserRelationErrorUnauthorised", TestDeleteUserRelationErrorUnauthorised) t.Run("TestDeleteUserRelation", TestDeleteUserRelation) - t.Run("TestDeleteUserErrorUnauthorized", TestDeleteUserErrorUnauthorized) + t.Run("TestDeleteReminderErrorUnauthorized", TestDeleteReminderErrorUnauthorized) + t.Run("TestDeleteReminderErrorReminderNotFound", TestDeleteReminderErrorNotFound) + t.Run("TestDeleteReminder", TestDeleteReminder) + t.Run("TestDeleteUser", TestDeleteUser) fmt.Println("All tests passed successfully") diff --git a/test/reminders_test.go b/test/reminders_test.go new file mode 100644 index 0000000..24f0564 --- /dev/null +++ b/test/reminders_test.go @@ -0,0 +1,224 @@ +package test + +import ( + "fmt" + "testing" + + "github.com/piyush7833/Chat-Api/config" + "github.com/piyush7833/Chat-Api/services" +) + +// test for create reminder +func TestCreateReminder(t *testing.T) { + var whereClause = "username = " + "'related_user'" + res, err := services.GetRows("users", 0, []string{"id"}, config.GetValidUserColumns(), &whereClause, nil, nil, nil, true) + if err.StatusCode != 0 { + t.Errorf(err.Message) + return + } + reminderData := `{ + "message": "Test Reminder", + "time": "2025-05-01T12:00:00Z", + "tune" : "https://example.com/tune.mp3", + "receiverId": "` + fmt.Sprintf("%v", res[0]["id"]) + `" + }` + TestPostRequest(t, reminderData, "/api/protected/reminder/create", 201, "Reminder created successfully", true) +} + +func TestCreateReminderErrorMissingField(t *testing.T) { + reminderData := `{ + "message": "Test Reminder", + "time": "2025-05-01T12:00:00Z", + }` + TestPostRequest(t, reminderData, "/api/protected/reminder/create", 500, "Missing field", true) +} + +func TestCreateReminderErrorInvalidTime(t *testing.T) { + var whereClause = "username = " + "'related_user'" + res, err := services.GetRows("users", 0, []string{"id"}, config.GetValidUserColumns(), &whereClause, nil, nil, nil, true) + if err.StatusCode != 0 { + t.Errorf(err.Message) + return + } + reminderData := `{ + "message": "Test Reminder", + "time": "01 03", + "tune" : "https://example.com/tune.mp3", + "receiverId": "` + fmt.Sprintf("%v", res[0]["id"]) + `" + }` + TestPostRequest(t, reminderData, "/api/protected/reminder/create", 500, "Incorrect time format", true) +} + +func TestCreateReminderErrorInvalidReceiverId(t *testing.T) { + reminderData := `{ + "message": "Test Reminder", + "time": "2025-05-01T12:00:00Z", + "tune" : "https://example.com/tune.mp3", + "receiverId": "randomId" + }` + TestPostRequest(t, reminderData, "/api/protected/reminder/create", 404, "Reciever does not exists", true) +} + +func TestCreateReminderErrorUnauthorized(t *testing.T) { + reminderData := `{ + "message": "Test Reminder", + "time": "2025-05-01T12:00:00Z", + "tune" : "https://example.com/tune.mp3", + "receiverId": "randomId" + }` + TestPostRequest(t, reminderData, "/api/protected/reminder/create", 401, "login required", false) +} + +// test for get particular reminder +func TestGetParticularReminder(t *testing.T) { + res, err := services.GetRows("reminders", 0, []string{"id"}, config.GetValidReminderColumns(), nil, nil, nil, nil, true) + if err.StatusCode != 0 { + t.Errorf(err.Message) + return + } + TestGetRequest(t, "/api/protected/reminder/get?id="+fmt.Sprintf("%v", res[0]["id"]), 200, "Reminder fetched successfully", true) +} + +func TestGetParticularReminderErrorInvalidColumns(t *testing.T) { + res, err := services.GetRows("reminders", 0, []string{"id"}, config.GetValidReminderColumns(), nil, nil, nil, nil, true) + if err.StatusCode != 0 { + t.Errorf(err.Message) + return + } + TestGetRequest(t, "/api/protected/reminder/get?id="+fmt.Sprintf("%v", res[0]["id"])+"&columns=invalid", 400, "no valid columns selected", true) +} + +func TestGetParticularReminderErrorNotFound(t *testing.T) { + TestGetRequest(t, "/api/protected/reminder/get?id=randomId", 403, "Reminder does not exists or it is not associated with you", true) +} + +func TestGetParticularReminderErrorUnauthorized(t *testing.T) { + res, err := services.GetRows("reminders", 0, []string{"id"}, config.GetValidReminderColumns(), nil, nil, nil, nil, true) + if err.StatusCode != 0 { + t.Errorf(err.Message) + return + } + TestGetRequest(t, "/api/protected/reminder/get?id="+fmt.Sprintf("%v", res[0]["id"]), 401, "login required", false) +} + +func TestGetParticularReminderErrorNotAllowed(t *testing.T) { //logged in by third user + res, err := services.GetRows("reminders", 0, []string{"id"}, config.GetValidReminderColumns(), nil, nil, nil, nil, true) + if err.StatusCode != 0 { + t.Errorf(err.Message) + return + } + TestGetRequest(t, "/api/protected/reminder/get?id="+fmt.Sprintf("%v", res[0]["id"]), 403, "Reminder does not exists or it is not associated with you", true) +} + +// test for get all reminder +func TestGetAllReminder(t *testing.T) { + TestGetRequest(t, "/api/protected/reminder/get-all", 200, "Reminder fetched successfully", true) +} +func TestGetAllReminderSent(t *testing.T) { + TestGetRequest(t, "/api/protected/reminder/get-all?type=sent", 200, "Reminder fetched successfully", true) +} +func TestGetAllReminderRecieved(t *testing.T) { + TestGetRequest(t, "/api/protected/reminder/get-all?type=received", 200, "Reminder fetched successfully", true) +} + +func TestGetAllReminderErrorUnauthorized(t *testing.T) { + TestGetRequest(t, "/api/protected/reminder/get-all", 401, "login required", false) +} +func TestGetAllReminderNoneFound(t *testing.T) { + TestGetRequest(t, "/api/protected/reminder/get-all", 200, "No reminder found", true) +} +func TestGetAllReminderErrorInvalidColumn(t *testing.T) { + TestGetRequest(t, "/api/protected/reminder/get-all?columns=invalid", 400, "no valid columns selected", true) +} + +// test for update reminder +func TestUpdateReminder(t *testing.T) { + res, err := services.GetRows("reminders", 0, []string{"id"}, config.GetValidReminderColumns(), nil, nil, nil, nil, true) + if err.StatusCode != 0 { + t.Errorf(err.Message) + return + } + reminderData := `{ + "message": "Test Reminder Updated", + "time": "2025-05-01T12:00:00Z" + }` + TestPatchRequest(t, reminderData, "/api/protected/reminder/update?id="+fmt.Sprintf("%v", res[0]["id"]), 200, "Reminder updated successfully", true) +} + +func TestUpdateReminderErrorInvalidColumn(t *testing.T) { + res, err := services.GetRows("reminders", 0, []string{"id"}, config.GetValidReminderColumns(), nil, nil, nil, nil, true) + if err.StatusCode != 0 { + t.Errorf(err.Message) + return + } + reminderData := `{ + "receiverId": "randomId" + }` + TestPatchRequest(t, reminderData, "/api/protected/reminder/update?id="+fmt.Sprintf("%v", res[0]["id"]), 400, "no valid columns selected", true) +} + +func TestUpdateReminderErrorUnauthorized(t *testing.T) { + res, err := services.GetRows("reminders", 0, []string{"id"}, config.GetValidReminderColumns(), nil, nil, nil, nil, true) + if err.StatusCode != 0 { + t.Errorf(err.Message) + return + } + reminderData := `{ + "message": "Test Reminder Updated", + "time": "2025-05-01T12:00:00Z" + }` + TestPatchRequest(t, reminderData, "/api/protected/reminder/update?id="+fmt.Sprintf("%v", res[0]["id"]), 401, "login required", false) +} + +func TestUpdateReminderErrorNotAllowed(t *testing.T) { //logged in by third user + res, err := services.GetRows("reminders", 0, []string{"id"}, config.GetValidReminderColumns(), nil, nil, nil, nil, true) + if err.StatusCode != 0 { + t.Errorf(err.Message) + return + } + reminderData := `{ + "message": "Test Reminder Updated", + "time": "2025-05-01T12:00:00Z" + }` + TestPatchRequest(t, reminderData, "/api/protected/reminder/update?id="+fmt.Sprintf("%v", res[0]["id"]), 403, "You can't update the reminder which is not associated to you", true) +} + +func TestUpdateReminderErrorNotFound(t *testing.T) { + reminderData := `{ + "message": "Test Reminder Updated", + "time": "2025-05-01T12:00:00Z" + }` + TestPatchRequest(t, reminderData, "/api/protected/reminder/update?id=randomId", 403, "You can't update the reminder which is not associated to you", true) +} + +// test for delete reminder +func TestDeleteReminder(t *testing.T) { + res, err := services.GetRows("reminders", 0, []string{"id"}, config.GetValidReminderColumns(), nil, nil, nil, nil, true) + if err.StatusCode != 0 { + t.Errorf(err.Message) + return + } + TestDeleteRequest(t, "/api/protected/reminder/delete?id="+fmt.Sprintf("%v", res[0]["id"]), 200, "Reminder deleted successfully", true) +} + +func TestDeleteReminderErrorUnauthorized(t *testing.T) { + res, err := services.GetRows("reminders", 0, []string{"id"}, config.GetValidReminderColumns(), nil, nil, nil, nil, true) + if err.StatusCode != 0 { + t.Errorf(err.Message) + return + } + TestDeleteRequest(t, "/api/protected/reminder/delete?id="+fmt.Sprintf("%v", res[0]["id"]), 401, "login required", false) +} + +func TestDeleteReminderErrorNotAllowed(t *testing.T) { //logged in by third user + res, err := services.GetRows("reminders", 0, []string{"id"}, config.GetValidReminderColumns(), nil, nil, nil, nil, true) + if err.StatusCode != 0 { + t.Errorf(err.Message) + return + } + TestDeleteRequest(t, "/api/protected/reminder/delete?id="+fmt.Sprintf("%v", res[0]["id"]), 403, "You can't delete the reminder which is not associated to you", true) +} + +func TestDeleteReminderErrorNotFound(t *testing.T) { + TestDeleteRequest(t, "/api/protected/reminder/delete?id=randomId", 403, "You can't delete the reminder which is not associated to you", true) +} diff --git a/test/userRelation_test.go b/test/user-relation_test.go similarity index 93% rename from test/userRelation_test.go rename to test/user-relation_test.go index bf850de..cf14c8b 100644 --- a/test/userRelation_test.go +++ b/test/user-relation_test.go @@ -22,6 +22,12 @@ func TestCreateUserRelation(t *testing.T) { // fmt.Println(userRelation) TestPostRequest(t, userRelation, "/api/protected/ur/create", 201, "User relation created successfully", true) } +func TestCreateUserRelationErrorMissingField(t *testing.T) { + userRelation := `{ + }` + // fmt.Println(userRelation) + TestPostRequest(t, userRelation, "/api/protected/ur/create", 500, "Missing required field", true) +} func TestCreateUserRelationErrorDuplicateRelation(t *testing.T) { var whereClause = "username = 'related_user'" res, err := services.GetRows("users", 0, []string{"id"}, config.GetValidUserColumns(), &whereClause, nil, nil, nil, true) @@ -82,6 +88,9 @@ func TestGetAllUserRelationParticularType(t *testing.T) { func TestGetAllUserRelationParticularTypeParticularStatus(t *testing.T) { TestGetRequest(t, "/api/protected/ur/get-all?type=sent&status=pending", 200, "User relations fetched successfully", true) } +func TestGetAllUserRelationNoneFound(t *testing.T) { + TestGetRequest(t, "/api/protected/ur/get-all", 200, "no userReltion found", true) +} func TestGetAllUserRelationErrorUnauthorized(t *testing.T) { TestGetRequest(t, "/api/protected/ur/get-all", 401, "login required", false) } @@ -107,7 +116,7 @@ func TestGetParticularUserRelation(t *testing.T) { } func TestGetParticularUserRelationErrorUserRelationNotFound(t *testing.T) { url := fmt.Sprintf("/api/protected/ur/get?id=%v", "randomId") - TestGetRequest(t, url, 404, "No userRelation found", true) + TestGetRequest(t, url, 403, "User relation does not exists or it is not associated with you", true) } func TestGetParticularUserRelationErrorInvalidColumn(t *testing.T) { res, err := services.GetRows("userRelation", 0, []string{"id"}, config.GetValidUserColumns(), nil, nil, nil, nil, true) @@ -137,6 +146,7 @@ func TestGetParticularUserRelationErrorNotAllowed(t *testing.T) { //can be test TestGetRequest(t, url, 403, "Only user and related user are allowed to view this relation", true) } +// update user relation tests func TestUpdateUserRelation(t *testing.T) { //their is only one user relation exist which is sent by logged in user and sender can not change status so this will be tested in last res, err := services.GetRows("userRelation", 0, []string{"id"}, config.GetValidUserColumns(), nil, nil, nil, nil, true) if err.StatusCode != 0 { @@ -166,7 +176,7 @@ func TestUpdateUserRelationErrorNotFound(t *testing.T) { userRelation := `{ "invalid": "friends" }` - TestPatchRequest(t, userRelation, url, 404, "no userRelation found", true) + TestPatchRequest(t, userRelation, url, 403, "User relation does not exists or it is not associated with you", true) } func TestUpdateUserRelationErrorUnauthorized(t *testing.T) { res, err := services.GetRows("userRelation", 0, []string{"id"}, config.GetValidUserColumns(), nil, nil, nil, nil, true) @@ -217,7 +227,7 @@ func TestDeleteUserRelation(t *testing.T) { } func TestDeleteUserRelationErrorNotFound(t *testing.T) { url := fmt.Sprintf("/api/protected/ur/delete?id=%v", "randomId") - TestDeleteRequest(t, url, 404, "userRelation not found", true) + TestDeleteRequest(t, url, 403, "userRelation not found", true) } func TestDeleteUserRelationErrorUnauthorised(t *testing.T) { res, err := services.GetRows("userRelation", 0, []string{"id"}, config.GetValidUserColumns(), nil, nil, nil, nil, true)