Skip to content

Commit

Permalink
feat: added testMode to enable all routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Devansh-bit committed Dec 17, 2023
1 parent 54cf329 commit 9d62b83
Show file tree
Hide file tree
Showing 7 changed files with 376 additions and 365 deletions.
2 changes: 1 addition & 1 deletion cmd/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func main() {
}

log.Info().Msg("Creating mux router")
router := server.NewRouter(db)
router := server.NewRouter(db, false)

port := os.Getenv("BACKEND_PORT")
if port == "" {
Expand Down
2 changes: 1 addition & 1 deletion controllers/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
// Ref: https://semaphoreci.com/community/tutorials/building-and-testing-a-rest-api-in-go-with-gorilla-mux-and-postgresql#h-writing-tests-for-the-api
func executeRequest(req *http.Request, db *gorm.DB) *httptest.ResponseRecorder {
rr := httptest.NewRecorder()
router := server.NewRouter(db)
router := server.NewRouter(db, true)

router.ServeHTTP(rr, req)

Expand Down
217 changes: 101 additions & 116 deletions controllers/mentor_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package controllers_test

import (
"bytes"
"encoding/json"
"fmt"
"gorm.io/gorm"
"net/http"
"reflect"
"strings"
Expand All @@ -11,36 +13,20 @@ import (
"github.com/kossiitkgp/kwoc-backend/v2/controllers"
"github.com/kossiitkgp/kwoc-backend/v2/models"
"github.com/kossiitkgp/kwoc-backend/v2/utils"

)

// func createMentorRegRequest(reqFields *controllers.RegisterMentorReqFields) *http.Request {
// reqBody, _ := json.Marshal(reqFields)

// req, _ := http.NewRequest(
// "POST",
// "/mentor/form/",
// bytes.NewReader(reqBody),
// )

// return req
// }

// func createMentorUpdateRequest(reqFields *controllers.UpdateMentorReqFields) *http.Request {
// reqBody, _ := json.Marshal(reqFields)
func createMentorRegRequest(reqFields *controllers.RegisterMentorReqFields) *http.Request {
reqBody, _ := json.Marshal(reqFields)

// req, _ := http.NewRequest(
// "PUT",
// "/mentor/form/",
// bytes.NewReader(reqBody),
// )

// return req
// }
req, _ := http.NewRequest(
"POST",
"/mentor/form/",
bytes.NewReader(reqBody),
)

// Mentor Registration Endpoint Disabled
return req
}

/*
// Test unauthenticated request to /mentor/form/
func TestMentorRegNoAuth(t *testing.T) {
testRequestNoAuth(t, "POST", "/mentor/form/")
Expand Down Expand Up @@ -70,121 +56,120 @@ func TestMentorRegSessionHijacking(t *testing.T) {
expectStatusCodeToBe(t, res, http.StatusUnauthorized)
expectResponseJSONBodyToBe(t, res, utils.HTTPMessage{StatusCode: http.StatusUnauthorized, Message: "Login username and given username do not match."})
}
*/

// Test unauthenticated request to /mentor/form/ [put]
// func TestMentorUpdateNoAuth(t *testing.T) {
// testRequestNoAuth(t, "PUT", "/mentor/form/")
// }
//Test unauthenticated request to /mentor/form/ [put]
func TestMentorUpdateNoAuth(t *testing.T) {
testRequestNoAuth(t, "PUT", "/mentor/form/")
}

// // Test request to /mentor/form/ [put] with invalid jwt
// func TestMentorUpdateInvalidAuth(t *testing.T) {
// testRequestInvalidAuth(t, "PUT", "/mentor/form/")
// }
// Test request to /mentor/form/ [put] with invalid jwt
func TestMentorUpdateInvalidAuth(t *testing.T) {
testRequestInvalidAuth(t, "PUT", "/mentor/form/")
}

// // Test a new user registration request to /mentor/form/ with proper authentication and input
// func tMentorRegNewUser(db *gorm.DB, t *testing.T) {
// // Test login fields
// testUsername := getTestUsername()
// testLoginFields := utils.LoginJwtFields{Username: testUsername}
// Test a new user registration request to /mentor/form/ with proper authentication and input
func tMentorRegNewUser(db *gorm.DB, t *testing.T) {
// Test login fields
testUsername := getTestUsername()
testLoginFields := utils.LoginJwtFields{Username: testUsername}

// testJwt, _ := utils.GenerateLoginJwtString(testLoginFields)
// reqFields := controllers.RegisterMentorReqFields{Username: testUsername}
testJwt, _ := utils.GenerateLoginJwtString(testLoginFields)
reqFields := controllers.RegisterMentorReqFields{Username: testUsername}

// req := createMentorRegRequest(&reqFields)
// req.Header.Add("Bearer", testJwt)
req := createMentorRegRequest(&reqFields)
req.Header.Add("Bearer", testJwt)

// res := executeRequest(req, db)
res := executeRequest(req, db)

// expectStatusCodeToBe(t, res, http.StatusOK)
// expectResponseJSONBodyToBe(t, res, utils.HTTPMessage{StatusCode: http.StatusOK, Message: "Mentor registration successful."})
// }
expectStatusCodeToBe(t, res, http.StatusOK)
expectResponseJSONBodyToBe(t, res, utils.HTTPMessage{StatusCode: http.StatusOK, Message: "Mentor registration successful."})
}

// // Test an existing user registration request to /mentor/form/ with proper authentication and input
// func tMentorRegExistingUser(db *gorm.DB, t *testing.T) {
// // Test login fields
// testUsername := getTestUsername()
// testLoginFields := utils.LoginJwtFields{Username: testUsername}
// Test an existing user registration request to /mentor/form/ with proper authentication and input
func tMentorRegExistingUser(db *gorm.DB, t *testing.T) {
// Test login fields
testUsername := getTestUsername()
testLoginFields := utils.LoginJwtFields{Username: testUsername}

// testJwt, _ := utils.GenerateLoginJwtString(testLoginFields)
// reqFields := controllers.RegisterMentorReqFields{Username: testUsername}
testJwt, _ := utils.GenerateLoginJwtString(testLoginFields)
reqFields := controllers.RegisterMentorReqFields{Username: testUsername}

// req := createMentorRegRequest(&reqFields)
// req.Header.Add("Bearer", testJwt)
req := createMentorRegRequest(&reqFields)
req.Header.Add("Bearer", testJwt)

// _ = executeRequest(req, db)
_ = executeRequest(req, db)

// // Execute the same request again
// req = createMentorRegRequest(&reqFields)
// req.Header.Add("Bearer", testJwt)
// Execute the same request again
req = createMentorRegRequest(&reqFields)
req.Header.Add("Bearer", testJwt)

// res := executeRequest(req, db)
res := executeRequest(req, db)

// expectStatusCodeToBe(t, res, http.StatusBadRequest)
// expectResponseJSONBodyToBe(t, res, utils.HTTPMessage{StatusCode: http.StatusBadRequest, Message: fmt.Sprintf("Mentor `%s` already exists.", testUsername)})
// }
expectStatusCodeToBe(t, res, http.StatusBadRequest)
expectResponseJSONBodyToBe(t, res, utils.HTTPMessage{StatusCode: http.StatusBadRequest, Message: fmt.Sprintf("Mentor `%s` already exists.", testUsername)})
}

// // Test an existing student registration request to /mentor/form/ with proper authentication and input
// func tMentorRegAsStudent(db *gorm.DB, t *testing.T) {
// // Test login fields
// testUsername := getTestUsername()
// testLoginFields := utils.LoginJwtFields{Username: testUsername}
// Test an existing student registration request to /mentor/form/ with proper authentication and input
func tMentorRegAsStudent(db *gorm.DB, t *testing.T) {
// Test login fields
testUsername := getTestUsername()
testLoginFields := utils.LoginJwtFields{Username: testUsername}

// testJwt, _ := utils.GenerateLoginJwtString(testLoginFields)
// // studentFields := controllers.RegisterStudentReqFields{Username: testUsername}
testJwt, _ := utils.GenerateLoginJwtString(testLoginFields)
// studentFields := controllers.RegisterStudentReqFields{Username: testUsername}

// // req := createStudentRegRequest(&studentFields)
// // req.Header.Add("Bearer", testJwt)
// req := createStudentRegRequest(&studentFields)
// req.Header.Add("Bearer", testJwt)

// // _ = executeRequest(req, db)
// db.Table("students").Create(&models.Student{Username: testUsername})
// _ = executeRequest(req, db)
db.Table("students").Create(&models.Student{Username: testUsername})

// mentorFields := controllers.RegisterMentorReqFields{Username: testUsername}
// req := createMentorRegRequest(&mentorFields)
// req.Header.Add("Bearer", testJwt)
mentorFields := controllers.RegisterMentorReqFields{Username: testUsername}
req := createMentorRegRequest(&mentorFields)
req.Header.Add("Bearer", testJwt)

// res := executeRequest(req, db)
res := executeRequest(req, db)

// expectStatusCodeToBe(t, res, http.StatusBadRequest)
// expectResponseJSONBodyToBe(t, res, utils.HTTPMessage{StatusCode: http.StatusBadRequest, Message: fmt.Sprintf("The username `%s` already exists as a student.", testUsername)})
// }
expectStatusCodeToBe(t, res, http.StatusBadRequest)
expectResponseJSONBodyToBe(t, res, utils.HTTPMessage{StatusCode: http.StatusBadRequest, Message: fmt.Sprintf("The username `%s` already exists as a student.", testUsername)})
}

// Currently Disabled

// // Test requests to /mentor/form/ with proper authentication and input
// func TestMentorRegOK(t *testing.T) {
// // Set up a local test database path
// db := setTestDB()
// defer unsetTestDB()

// // Generate a jwt secret key for testing
// setTestJwtSecretKey()
// defer unsetTestJwtSecretKey()

// // New mentor registration test
// t.Run(
// "Test: new mentor registration.",
// func(t *testing.T) {
// tMentorRegNewUser(db, t)
// },
// )

// // Existing mentor registration test
// t.Run(
// "Test: existing mentor registration.",
// func(t *testing.T) {
// tMentorRegExistingUser(db, t)
// },
// )

// // Student registering as mentor test
// t.Run(
// "Test: Student registering as mentor.",
// func(t *testing.T) {
// tMentorRegAsStudent(db, t)
// },
// )
// }
// Test requests to /mentor/form/ with proper authentication and input
func TestMentorRegOK(t *testing.T) {
// Set up a local test database path
db := setTestDB()
defer unsetTestDB()

// Generate a jwt secret key for testing
setTestJwtSecretKey()
defer unsetTestJwtSecretKey()

// New mentor registration test
t.Run(
"Test: new mentor registration.",
func(t *testing.T) {
tMentorRegNewUser(db, t)
},
)

// Existing mentor registration test
t.Run(
"Test: existing mentor registration.",
func(t *testing.T) {
tMentorRegExistingUser(db, t)
},
)

// Student registering as mentor test
t.Run(
"Test: Student registering as mentor.",
func(t *testing.T) {
tMentorRegAsStudent(db, t)
},
)
}

func createFetchMentorRequest() *http.Request {
req, _ := http.NewRequest(
Expand Down
Loading

0 comments on commit 9d62b83

Please sign in to comment.