Skip to content

Commit

Permalink
Merge pull request #183 from Devansh-bit/rem-fetchall-mentors
Browse files Browse the repository at this point in the history
Removed obsolete FetchAllMentors Endpoint
  • Loading branch information
harshkhandeparkar authored Dec 23, 2023
2 parents 25559b6 + 8febf73 commit d70c415
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 108 deletions.
29 changes: 0 additions & 29 deletions controllers/mentor.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,35 +140,6 @@ func RegisterMentor(w http.ResponseWriter, r *http.Request) {
utils.RespondWithHTTPMessage(r, w, http.StatusOK, "Mentor registration successful.")
}

// FetchAllMentors godoc
//
// @Summary Fetches all mentors
// @Description Fetches the public details for all the mentors
// @Accept plain
// @Produce json
// @Success 200 {object} []Mentor "Mentor fetch successful"
// @Failure 500 {object} utils.HTTPMessage "Database Error fetching mentors"
// @Security JWT
// @Router /mentor/all [get]
func FetchAllMentors(w http.ResponseWriter, r *http.Request) {
app := r.Context().Value(middleware.APP_CTX_KEY).(*middleware.App)
db := app.Db

var mentors []Mentor

tx := db.
Table("mentors").
Select("name", "username").
Find(&mentors)

if tx.Error != nil {
utils.LogErrAndRespond(r, w, tx.Error, "Database Error fetching mentors", http.StatusInternalServerError)
return
}

utils.RespondWithJson(r, w, mentors)
}

// /mentor/dashboard/ functions

func CreateMentorDashboard(mentor models.Mentor, db *gorm.DB) MentorDashboard {
Expand Down
72 changes: 0 additions & 72 deletions controllers/mentor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,78 +164,6 @@ func TestMentorRegOK(t *testing.T) {
)
}

func createFetchMentorRequest() *http.Request {
req, _ := http.NewRequest(
"GET",
"/mentor/all/",
nil,
)
return req
}

// Test unauthenticated request to /mentor/all/
func TestFetchMentorNoAuth(t *testing.T) {
testRequestNoAuth(t, "GET", "/mentor/all/")
}

// Test request to /mentor/all/ with invalid jwt
func TestFetchMentorInvalidAuth(t *testing.T) {
testRequestInvalidAuth(t, "GET", "/mentor/all/")
}

func TestFetchMentorOK(t *testing.T) {
const numMentors = 10
// Set up a local test database path
db := setTestDB()
defer unsetTestDB()

// Generate a jwt secret key for testing
setTestJwtSecretKey()
defer unsetTestJwtSecretKey()

// Test login fields
testUsername := getTestUsername()
testLoginFields := utils.LoginJwtFields{Username: testUsername}

testJwt, _ := utils.GenerateLoginJwtString(testLoginFields)

modelMentors := make([]models.Mentor, 0, numMentors)
var testMentors [numMentors]controllers.Mentor
for i := 0; i < numMentors; i++ {
modelMentors = append(modelMentors,
models.Mentor{
Name: fmt.Sprintf("Test%d", i),
Username: fmt.Sprintf("test%d", i),
Email: fmt.Sprintf("test%[email protected]", i),
})
testMentors[i] = controllers.Mentor{
Name: fmt.Sprintf("Test%d", i),
Username: fmt.Sprintf("test%d", i),
}

}
_ = db.Table("mentors").Create(modelMentors)

req := createFetchMentorRequest()
req.Header.Add("Bearer", testJwt)

res := executeRequest(req, db)

var resMentors []controllers.Mentor
_ = json.NewDecoder(res.Body).Decode(&resMentors)

expectStatusCodeToBe(t, res, http.StatusOK)
if len(resMentors) != numMentors {
t.Fatalf("Not getting expected numbers of mentors from /mentor/all/")
}

for i, mentor := range resMentors {
if mentor != testMentors[i] {
t.Fatalf("Incorrect mentors returned from /mentor/all/")
}
}
}

// Test unauthenticated request to /mentor/dashboard/
func TestMentorDashboardNoAuth(t *testing.T) {
testRequestNoAuth(t, "GET", "/mentor/dashboard/")
Expand Down
7 changes: 0 additions & 7 deletions server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,6 @@ func getRoutes(app *middleware.App) []Route {
middleware.WithLogin(middleware.WrapApp(app, controllers.UpdateMentorDetails)),
false,
},
{
"Fetch All Mentors",
"GET",
"/mentor/all/",
middleware.WithLogin(middleware.WrapApp(app, controllers.FetchAllMentors)),
false,
},
{
"Mentor Dashboard",
"GET",
Expand Down

0 comments on commit d70c415

Please sign in to comment.