From 59cb8eb6057c9f2df70c0bb4c2c9dd63ea7479a5 Mon Sep 17 00:00:00 2001 From: Harsh Khandeparkar Date: Sat, 13 Jan 2024 15:24:18 +0530 Subject: [PATCH] fix: updated student bloglink tests --- tests/student_test.go | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/tests/student_test.go b/tests/student_test.go index a8e6e2d..f266935 100644 --- a/tests/student_test.go +++ b/tests/student_test.go @@ -203,8 +203,8 @@ func TestStudentBloglinkSessionHijacking(t *testing.T) { expectResponseJSONBodyToBe(t, res, utils.HTTPMessage{StatusCode: http.StatusUnauthorized, Message: "Login username and given username do not match."}) } -// Test an existing user request to /student/bloglink/ with proper authentication and input -func tStudentBlogLinkExistingUser(db *gorm.DB, t *testing.T) { +// Test an existing user request to /student/bloglink/ with proper authentication and input but with end evals failed. +func tStudentBlogLinkExistingUserNotPassed(db *gorm.DB, t *testing.T) { // Test login fields testUsername := getTestUsername() testLoginFields := utils.LoginJwtFields{Username: testUsername} @@ -220,6 +220,27 @@ func tStudentBlogLinkExistingUser(db *gorm.DB, t *testing.T) { res := executeRequest(req, db) + expectStatusCodeToBe(t, res, http.StatusOK) + expectResponseJSONBodyToBe(t, res, utils.HTTPMessage{StatusCode: http.StatusBadRequest, Message: fmt.Sprintf("Student `%s` has not passed end evaluations.", testUsername)}) +} + +// Test an existing user request to /student/bloglink/ with proper authentication and input and with passed end evals +func tStudentBlogLinkExistingUserPassed(db *gorm.DB, t *testing.T) { + // Test login fields + testUsername := getTestUsername() + testLoginFields := utils.LoginJwtFields{Username: testUsername} + + testJwt, _ := utils.GenerateLoginJwtString(testLoginFields) + + db.Table("students").Create(&models.Student{Username: testUsername, PassedEndEvals: true}) + + // Execute the bloglink request + reqFields := controllers.StudentBlogLinkReqFields{Username: testUsername, BlogLink: "https://grugbrain.dev/"} + req := createStudentBlogLinkRequest(&reqFields) + req.Header.Add("Bearer", testJwt) + + res := executeRequest(req, db) + expectStatusCodeToBe(t, res, http.StatusOK) expectResponseJSONBodyToBe(t, res, utils.HTTPMessage{StatusCode: http.StatusOK, Message: "BlogLink successfully updated."}) } @@ -253,11 +274,19 @@ func TestStudentBlogLink(t *testing.T) { setTestJwtSecretKey() defer unsetTestJwtSecretKey() - // Existing student test + // Failed existing student test + t.Run( + "Test: existing student bloglink request with end evals failed", + func(t *testing.T) { + tStudentBlogLinkExistingUserNotPassed(db, t) + }, + ) + + // Passed existing student test t.Run( - "Test: existing student bloglink request", + "Test: existing student bloglink request with end evals passed", func(t *testing.T) { - tStudentBlogLinkExistingUser(db, t) + tStudentBlogLinkExistingUserPassed(db, t) }, )