Skip to content

Commit

Permalink
test(meeting): ✅ fix and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudhgray committed Oct 23, 2023
1 parent 490cfa4 commit f184db8
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 8 deletions.
12 changes: 6 additions & 6 deletions controllers/meeting.controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ func (mc *MeetingController) GetMeetingDetails(c *gin.Context) {

// Call the meeting service to get the meeting
meeting, err := mc.meetingService.GetMeetingByID(meetingID, teamID)
logger.Errorf("Failed to get meeting (meeting: %d, team:%d): "+err.Error(), meetingID, teamID)
if err != nil {
logger.Errorf("Failed to get meeting (meeting: %d, team:%d): "+err.Error(), meetingID, teamID)
c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to get the meeting", "error": err.Error()})
return
}
Expand All @@ -138,8 +138,8 @@ func (mc *MeetingController) StartMeeting(c *gin.Context) {

// Call the meeting service to start the meeting
meeting, err := mc.meetingService.StartMeeting(uint(meetingID), teamID)
logger.Warnf("Failed to start meeting (meeting: %d, team:%d): "+err.Error(), meetingID, teamID)
if err != nil {
logger.Warnf("Failed to start meeting (meeting: %d, team:%d): "+err.Error(), meetingID, teamID)
c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to start the meeting", "error": err.Error()})
return
}
Expand All @@ -158,8 +158,8 @@ func (mc *MeetingController) StartAttendance(c *gin.Context) {

// Call the meeting service to start attendance for the meeting
meeting, err := mc.meetingService.StartAttendance(uint(meetingID), teamID)
logger.Warnf("Failed to start attendance for meeting (meeting: %d, team:%d): "+err.Error(), meetingID, teamID)
if err != nil {
logger.Warnf("Failed to start attendance for meeting (meeting: %d, team:%d): "+err.Error(), meetingID, teamID)
c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to start attendance for the meeting", "error": err.Error()})
return
}
Expand All @@ -178,8 +178,8 @@ func (mc *MeetingController) EndAttendance(c *gin.Context) {

// Call the meeting service to end attendance for the meeting
meeting, err := mc.meetingService.EndAttendance(uint(meetingID), teamID)
logger.Warnf("Failed to end attendance for meeting (meeting: %d, team:%d): "+err.Error(), meetingID, teamID)
if err != nil {
logger.Warnf("Failed to end attendance for meeting (meeting: %d, team:%d): "+err.Error(), meetingID, teamID)
c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to end attendance for the meeting", "error": err.Error()})
return
}
Expand All @@ -198,8 +198,8 @@ func (mc *MeetingController) EndMeeting(c *gin.Context) {

// Call the meeting service to end the meeting
meeting, err := mc.meetingService.EndMeeting(uint(meetingID), teamID)
logger.Warnf("Failed to end meeting (meeting: %d, team:%d): "+err.Error(), meetingID, teamID)
if err != nil {
logger.Warnf("Failed to end meeting (meeting: %d, team:%d): "+err.Error(), meetingID, teamID)
c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to end the meeting", "error": err.Error()})
return
}
Expand All @@ -218,8 +218,8 @@ func (mc *MeetingController) DeleteMeetingByID(c *gin.Context) {

// Call the meeting service to delete the meeting
err = mc.meetingService.DeleteMeetingByID(uint(meetingID), teamID)
logger.Warnf("Failed to delete meeting (meeting: %d, team:%d): "+err.Error(), meetingID, teamID)
if err != nil {
logger.Warnf("Failed to delete meeting (meeting: %d, team:%d): "+err.Error(), meetingID, teamID)
c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to delete the meeting", "error": err.Error()})
return
}
Expand Down
156 changes: 156 additions & 0 deletions controllers/meeting.controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,159 @@ func TestMeetingController_StartAttendance(t *testing.T) {
w, _ = sendRequest("PUT", "/team/1/meetings/1/attendance/start")
assert.Equal(t, http.StatusInternalServerError, w.Code)
}

// test EndAttendance
func TestMeetingController_EndAttendance(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockService := mocks.NewMockMeetingService(ctrl)

r := gin.Default()
meetingController := NewMeetingController(mockService)

r.PUT("/team/:teamID/meetings/:meetingID/attendance/end", meetingController.EndAttendance)

// Helper function to send a request and check the response
sendRequest := func(method, path string) (*httptest.ResponseRecorder, *models.Meeting) {
req, _ := http.NewRequest(method, path, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w, parseMeetingResponse(w)
}

// Test case 1: Valid request
now := time.Now().Add(time.Hour)
meeting := createTestMeeting(now)
meeting.AttendanceOver = true
mockService.EXPECT().EndAttendance(uint(1), uint(1)).Return(meeting, nil)
w, responseMeeting := sendRequest("PUT", "/team/1/meetings/1/attendance/end")
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, meeting.Title, responseMeeting.Title)
assert.True(t, responseMeeting.AttendanceOver)

// Test case 2: Invalid meeting ID
w, _ = sendRequest("PUT", "/team/1/meetings/jj/attendance/end")
assert.Equal(t, http.StatusBadRequest, w.Code)

// Test case 3: Meeting already ended
mockService.EXPECT().EndAttendance(uint(1), uint(1)).Return(models.Meeting{}, errors.New("some error"))
w, _ = sendRequest("PUT", "/team/1/meetings/1/attendance/end")
assert.Equal(t, http.StatusInternalServerError, w.Code)
}

func TestMeetingController_EndMeeting(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockService := mocks.NewMockMeetingService(ctrl)

r := gin.Default()
meetingController := NewMeetingController(mockService)

r.PUT("/team/:teamID/meetings/:meetingID/end", meetingController.EndMeeting)

// Helper function to send a request and check the response
sendRequest := func(method, path string) (*httptest.ResponseRecorder, *models.Meeting) {
req, _ := http.NewRequest(method, path, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w, parseMeetingResponse(w)
}

// Test case 1: Valid request
now := time.Now().Add(time.Hour)
meeting := createTestMeeting(now)
meeting.MeetingOver = true
mockService.EXPECT().EndMeeting(uint(1), uint(1)).Return(meeting, nil)
w, responseMeeting := sendRequest("PUT", "/team/1/meetings/1/end")
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, meeting.Title, responseMeeting.Title)
assert.True(t, responseMeeting.MeetingOver)

// Test case 2: Invalid meeting ID
w, _ = sendRequest("PUT", "/team/1/meetings/jj/end")
assert.Equal(t, http.StatusBadRequest, w.Code)

// Test case 3: Meeting already ended
mockService.EXPECT().EndMeeting(uint(1), uint(1)).Return(models.Meeting{}, errors.New("some error"))
w, _ = sendRequest("PUT", "/team/1/meetings/1/end")
assert.Equal(t, http.StatusInternalServerError, w.Code)
}

// test DeleteMeeting
func TestMeetingController_DeleteMeeting(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockService := mocks.NewMockMeetingService(ctrl)

r := gin.Default()
meetingController := NewMeetingController(mockService)

r.DELETE("/team/:teamID/meetings/:meetingID", meetingController.DeleteMeetingByID)

// Helper function to send a request and check the response
sendRequest := func(method, path string) (*httptest.ResponseRecorder, *models.Meeting) {
req, _ := http.NewRequest(method, path, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w, parseMeetingResponse(w)
}

// Test case 1: Valid request
mockService.EXPECT().DeleteMeetingByID(uint(1), uint(1)).Return(nil)
w, _ := sendRequest("DELETE", "/team/1/meetings/1")
assert.Equal(t, http.StatusOK, w.Code)

// Test case 2: Invalid meeting ID
w, _ = sendRequest("DELETE", "/team/1/meetings/jj")
assert.Equal(t, http.StatusBadRequest, w.Code)

// Test case 3: Meeting already ended
mockService.EXPECT().DeleteMeetingByID(uint(1), uint(1)).Return(errors.New("some error"))
w, _ = sendRequest("DELETE", "/team/1/meetings/1")
assert.Equal(t, http.StatusInternalServerError, w.Code)
}

// test GetAttendanceForMeeting
func TestMeetingController_GetAttendanceForMeeting(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockService := mocks.NewMockMeetingService(ctrl)

r := gin.Default()
meetingController := NewMeetingController(mockService)

r.GET("/team/:teamID/meetings/:meetingID/attendance", meetingController.GetAttendanceForMeeting)

// parseMeetingAttendanceResponse parses the response body of GetAttendanceForMeeting
parseMeetingAttendanceResponse := func(w *httptest.ResponseRecorder) []models.MeetingAttendanceListResponse {
var response []models.MeetingAttendanceListResponse
_ = json.NewDecoder(w.Body).Decode(&response)
return response
}

// Helper function to send a request and check the response
sendRequest := func(method, path string) (*httptest.ResponseRecorder, []models.MeetingAttendanceListResponse) {
req, _ := http.NewRequest(method, path, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w, parseMeetingAttendanceResponse(w)
}

// Test case 1: Valid request
mockService.EXPECT().GetAttendanceForMeeting(uint(1), uint(1)).Return([]models.MeetingAttendanceListResponse{}, nil)
w, _ := sendRequest("GET", "/team/1/meetings/1/attendance")
assert.Equal(t, http.StatusOK, w.Code)

// Test case 2: Invalid meeting ID
w, _ = sendRequest("GET", "/team/1/meetings/jj/attendance")
assert.Equal(t, http.StatusBadRequest, w.Code)

// Test case 3: Meeting already ended
mockService.EXPECT().GetAttendanceForMeeting(uint(1), uint(1)).Return([]models.MeetingAttendanceListResponse{}, errors.New("some error"))
w, _ = sendRequest("GET", "/team/1/meetings/1/attendance")
assert.Equal(t, http.StatusInternalServerError, w.Code)
}
4 changes: 2 additions & 2 deletions controllers/user.controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ func (uc *UserController) GetMyRequests(c *gin.Context) {
err := error(nil)
if status == "" {
requests, err = uc.teamEntryRequestRepo.GetTeamEntryRequestsByUserID(user.ID)
logger.Errorf("Failed to get requests for user %d: "+err.Error(), user.ID)
if err != nil {
logger.Errorf("Failed to get requests for user %d: "+err.Error(), user.ID)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve requests"})
return
}
} else {
requests, err = uc.teamEntryRequestRepo.GetTeamEntryRequestsByUserIDAndStatus(user.ID, status)
logger.Errorf("Failed to get requests for user %d by %s: "+err.Error(), user.ID, status)
if err != nil {
logger.Errorf("Failed to get requests for user %d by %s: "+err.Error(), user.ID, status)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve requests"})
return
}
Expand Down
3 changes: 3 additions & 0 deletions services/meeting.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,9 @@ func TestMeetingService_MarkAttendanceForUserInMeeting(t *testing.T) {

// If the function is expected to succeed, we should create a mock MeetingAttendance.
if !tc.expectedError {
// mock repos get attendance by user id and meeting id
mockRepo.EXPECT().GetMeetingAttendanceByUserIDAndMeetingID(tc.userID, tc.meetingID).Return(models.MeetingAttendance{}, errors.New("existing attendance not found"))

mockAttendance := models.MeetingAttendance{
MeetingID: tc.meetingID,
UserID: tc.userID,
Expand Down

0 comments on commit f184db8

Please sign in to comment.