Skip to content

Commit

Permalink
feat: reminders
Browse files Browse the repository at this point in the history
  • Loading branch information
piyush7833 committed Jun 27, 2024
1 parent e74321d commit 42cd7b9
Show file tree
Hide file tree
Showing 5 changed files with 276 additions and 6 deletions.
1 change: 1 addition & 0 deletions test/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Check failure on line 36 in test/init.go

View workflow job for this annotation

GitHub Actions / lint

undefined: routes.ReminderRoutes
return router
}

Expand Down
40 changes: 37 additions & 3 deletions test/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -78,28 +81,59 @@ 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)

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)

t.Run("TestUpdateUserRelationErrorNotAllowed", TestUpdateUserRelationErrorNotAllowed)
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")
Expand Down
224 changes: 224 additions & 0 deletions test/reminders_test.go
Original file line number Diff line number Diff line change
@@ -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)

Check failure on line 74 in test/reminders_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: config.GetValidReminderColumns
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)

Check failure on line 83 in test/reminders_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: config.GetValidReminderColumns
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)

Check failure on line 96 in test/reminders_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: config.GetValidReminderColumns
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)

Check failure on line 105 in test/reminders_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: config.GetValidReminderColumns
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)

Check failure on line 136 in test/reminders_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: config.GetValidReminderColumns
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)

Check failure on line 149 in test/reminders_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: config.GetValidReminderColumns
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)

Check failure on line 161 in test/reminders_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: config.GetValidReminderColumns
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)

Check failure on line 174 in test/reminders_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: config.GetValidReminderColumns
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)

Check failure on line 196 in test/reminders_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: config.GetValidReminderColumns
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)
}
16 changes: 13 additions & 3 deletions test/userRelation_test.go → test/user-relation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 42cd7b9

Please sign in to comment.