diff --git a/controllers/mentor.go b/controllers/mentor.go index 769ad6a..92ac8d1 100644 --- a/controllers/mentor.go +++ b/controllers/mentor.go @@ -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, diff --git a/controllers/mentor_test.go b/controllers/mentor_test.go index 0eab895..a994485 100644 --- a/controllers/mentor_test.go +++ b/controllers/mentor_test.go @@ -100,6 +100,30 @@ 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) + + 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 @@ -125,6 +149,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 { diff --git a/controllers/student.go b/controllers/student.go index 7240b6f..1651385 100644 --- a/controllers/student.go +++ b/controllers/student.go @@ -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, diff --git a/controllers/student_test.go b/controllers/student_test.go index 2509dc2..fb8794d 100644 --- a/controllers/student_test.go +++ b/controllers/student_test.go @@ -101,6 +101,30 @@ 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) + + 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 @@ -126,6 +150,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 {