Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Do Not Allow Student to Register as Mentor #159

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions controllers/mentor.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,29 @@ func RegisterMentor(w http.ResponseWriter, r *http.Request) {
return
}

// Check if a student of the same username exists
student := models.Student{}
tx = db.
Table("students").
Where("username = ?", reqFields.Username).
First(&student)
if tx.Error != nil && tx.Error != gorm.ErrRecordNotFound {
utils.LogErrAndRespond(r, w, tx.Error, "Database error.", http.StatusInternalServerError)
return
}
student_exists := student.Username == reqFields.Username

if student_exists {
utils.LogWarnAndRespond(
r,
w,
fmt.Sprintf("The username `%s` already exists as a student.", reqFields.Username),
http.StatusBadRequest,
)

return
}

// Create a db entry if the mentor doesn't exist
tx = db.Create(&models.Mentor{
Username: reqFields.Username,
Expand Down
33 changes: 33 additions & 0 deletions controllers/mentor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,31 @@ func tMentorRegExistingUser(db *gorm.DB, t *testing.T) {
expectResponseJSONBodyToBe(t, res, utils.HTTPMessage{Code: 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}

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

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

_ = executeRequest(req, db)

//
YoganshSharma marked this conversation as resolved.
Show resolved Hide resolved
mentorFields := controllers.RegisterMentorReqFields{Username: testUsername}
req = createMentorRegRequest(&mentorFields)
req.Header.Add("Bearer", testJwt)

res := executeRequest(req, db)

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

// Test requests to /mentor/form/ with proper authentication and input
func TestMentorRegOK(t *testing.T) {
// Set up a local test database path
Expand All @@ -125,6 +150,14 @@ func TestMentorRegOK(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 {
Expand Down
23 changes: 23 additions & 0 deletions controllers/student.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,29 @@ func RegisterStudent(w http.ResponseWriter, r *http.Request) {
return
}

// Check if a mentor of the same username exists
mentor := models.Mentor{}
tx = db.
Table("mentors").
Where("username = ?", reqFields.Username).
First(&mentor)
if tx.Error != nil && tx.Error != gorm.ErrRecordNotFound {
utils.LogErrAndRespond(r, w, tx.Error, "Database error.", http.StatusInternalServerError)
return
}
mentor_exists := mentor.Username == reqFields.Username

if mentor_exists {
utils.LogWarnAndRespond(
r,
w,
fmt.Sprintf("The username `%s` already exists as a mentor.", reqFields.Username),
http.StatusBadRequest,
)

return
}

// Create a db entry if the student doesn't exist
tx = db.Create(&models.Student{
Username: reqFields.Username,
Expand Down
33 changes: 33 additions & 0 deletions controllers/student_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,31 @@ func tStudentRegExistingUser(db *gorm.DB, t *testing.T) {
expectResponseJSONBodyToBe(t, res, utils.HTTPMessage{Code: http.StatusBadRequest, Message: fmt.Sprintf("Student `%s` already exists.", testUsername)})
}

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

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

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

_ = executeRequest(req, db)

//
YoganshSharma marked this conversation as resolved.
Show resolved Hide resolved
studentsFields := controllers.RegisterStudentReqFields{Username: testUsername}
req = createStudentRegRequest(&studentsFields)
req.Header.Add("Bearer", testJwt)

res := executeRequest(req, db)

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

// Test requests to /student/form/ with proper authentication and input
func TestStudentRegOK(t *testing.T) {
// Set up a local test database path
Expand All @@ -126,6 +151,14 @@ func TestStudentRegOK(t *testing.T) {
tStudentRegExistingUser(db, t)
},
)

// Mentor registering as student test
t.Run(
"Test: Mentor registering as student.",
func(t *testing.T) {
tStudentRegAsMentor(db, t)
},
)
}

func createStudentBlogLinkRequest(reqFields *controllers.StudentBlogLinkReqFields) *http.Request {
Expand Down
Loading