From 2fb97c7246593389ec8c3367290f7a3d6f7c007b Mon Sep 17 00:00:00 2001
From: Harsh Khandeparkar
Date: Sat, 5 Aug 2023 16:29:24 +0530
Subject: [PATCH 1/5] Update README.md
Signed-off-by: Harsh Khandeparkar
---
README.md | 2 ++
1 file changed, 2 insertions(+)
diff --git a/README.md b/README.md
index fcf922c3..b3825ffd 100644
--- a/README.md
+++ b/README.md
@@ -35,6 +35,8 @@
Kharagpur Winter of Code
·
Frontend
+ ·
+ Endpoint Docs
From bfef4cf043f4f206f5d3cc1d84d1ace0c84b5b7d Mon Sep 17 00:00:00 2001
From: Harsh Khandeparkar
Date: Mon, 4 Sep 2023 16:41:46 +0530
Subject: [PATCH 2/5] fix: updated kwoc logo link
Signed-off-by: Harsh Khandeparkar
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index b3825ffd..5fd4f0d1 100644
--- a/README.md
+++ b/README.md
@@ -21,7 +21,7 @@
-
+
KWoC Backend v2.0
From 60e91ee716c7cae838ad85b20eb41c2e2f593417 Mon Sep 17 00:00:00 2001
From: Yogansh Sharma
Date: Thu, 12 Oct 2023 18:43:12 +0530
Subject: [PATCH 3/5] fix: student are not allowed to register as a mentor
---
controllers/mentor.go | 23 +++++++++++++++++++++++
controllers/mentor_test.go | 33 +++++++++++++++++++++++++++++++++
2 files changed, 56 insertions(+)
diff --git a/controllers/mentor.go b/controllers/mentor.go
index 769ad6a0..657e4290 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{}
+ tx1 := db.
+ Table("students").
+ Where("username = ?", reqFields.Username).
+ First(&student)
+ if tx1.Error != nil && tx1.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 0eab895b..bd5a60f8 100644
--- a/controllers/mentor_test.go
+++ b/controllers/mentor_test.go
@@ -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 user 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 +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 {
From f19562fd359ab80d2568b6f6727b8cb2d6a1fa55 Mon Sep 17 00:00:00 2001
From: Yogansh Sharma
Date: Thu, 12 Oct 2023 19:09:19 +0530
Subject: [PATCH 4/5] fix:mentors are not allowed to register as student
---
controllers/mentor.go | 4 ++--
controllers/mentor_test.go | 2 +-
controllers/student.go | 23 +++++++++++++++++++++++
controllers/student_test.go | 33 +++++++++++++++++++++++++++++++++
4 files changed, 59 insertions(+), 3 deletions(-)
diff --git a/controllers/mentor.go b/controllers/mentor.go
index 657e4290..92ac8d1a 100644
--- a/controllers/mentor.go
+++ b/controllers/mentor.go
@@ -101,11 +101,11 @@ func RegisterMentor(w http.ResponseWriter, r *http.Request) {
// Check if a student of the same username exists
student := models.Student{}
- tx1 := db.
+ tx = db.
Table("students").
Where("username = ?", reqFields.Username).
First(&student)
- if tx1.Error != nil && tx1.Error != gorm.ErrRecordNotFound {
+ if tx.Error != nil && tx.Error != gorm.ErrRecordNotFound {
utils.LogErrAndRespond(r, w, tx.Error, "Database error.", http.StatusInternalServerError)
return
}
diff --git a/controllers/mentor_test.go b/controllers/mentor_test.go
index bd5a60f8..f598abf0 100644
--- a/controllers/mentor_test.go
+++ b/controllers/mentor_test.go
@@ -100,7 +100,7 @@ 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 user registration request to /mentor/form/ with proper authentication and input
+// 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()
diff --git a/controllers/student.go b/controllers/student.go
index 7240b6f7..16513853 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 2509dc2d..82653526 100644
--- a/controllers/student_test.go
+++ b/controllers/student_test.go
@@ -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)
+
+ //
+ 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 +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 {
From 071c60396484b5e29bfd32991178861be42c36d4 Mon Sep 17 00:00:00 2001
From: Yogansh Sharma
Date: Thu, 12 Oct 2023 20:15:53 +0530
Subject: [PATCH 5/5] fix: removed empty comments
---
controllers/mentor_test.go | 1 -
controllers/student_test.go | 1 -
2 files changed, 2 deletions(-)
diff --git a/controllers/mentor_test.go b/controllers/mentor_test.go
index f598abf0..a994485e 100644
--- a/controllers/mentor_test.go
+++ b/controllers/mentor_test.go
@@ -114,7 +114,6 @@ func tMentorRegAsStudent(db *gorm.DB, t *testing.T) {
_ = executeRequest(req, db)
- //
mentorFields := controllers.RegisterMentorReqFields{Username: testUsername}
req = createMentorRegRequest(&mentorFields)
req.Header.Add("Bearer", testJwt)
diff --git a/controllers/student_test.go b/controllers/student_test.go
index 82653526..fb8794d8 100644
--- a/controllers/student_test.go
+++ b/controllers/student_test.go
@@ -115,7 +115,6 @@ func tStudentRegAsMentor(db *gorm.DB, t *testing.T) {
_ = executeRequest(req, db)
- //
studentsFields := controllers.RegisterStudentReqFields{Username: testUsername}
req = createStudentRegRequest(&studentsFields)
req.Header.Add("Bearer", testJwt)