Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into docs
Browse files Browse the repository at this point in the history
  • Loading branch information
koss-service committed Oct 12, 2023
2 parents c110850 + 1546fe2 commit 660a534
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<!-- UPDATE -->
<div align="center">
<a href="https://github.com/kossiitkgp/KWoC-Backend">
<img width="140" alt="KWoC Logo" src="https://raw.githubusercontent.com/kossiitkgp/design/master/logo/kwoc_logo.png">
<img width="140" alt="KWoC Logo" src="https://raw.githubusercontent.com/kossiitkgp/design/master/logo/kwoc/kwoc_logo.png">
</a>

<h3 align="center">KWoC Backend v2.0</h3>
Expand All @@ -35,6 +35,8 @@
<a href="https://kwoc.kossiitkgp.org">Kharagpur Winter of Code</a>
·
<a href="https://github.com/kossiitkgp/KWoC-Frontend">Frontend</a>
·
<a href="https://kossiitkgp.org/KWoC-Backend/">Endpoint Docs</a>
</p>
</div>

Expand Down
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
32 changes: 32 additions & 0 deletions controllers/mentor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
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
32 changes: 32 additions & 0 deletions controllers/student_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 660a534

Please sign in to comment.