Skip to content

Commit

Permalink
team auth middlewareのテスト
Browse files Browse the repository at this point in the history
  • Loading branch information
pirosiki197 committed Jan 4, 2025
1 parent 4e91c64 commit d1ef3d9
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions server/handler/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,67 @@ func TestAuthMiddleware(t *testing.T) {
})
}
}

func TestTeamAuthMiddleware(t *testing.T) {
t.Parallel()

ctrl := gomock.NewController(t)

mockRepo := repomock.NewMockRepository(ctrl)
mockSessManager := sessmock.NewMockSessionManager(ctrl)
handler := NewHandler(mockRepo, mockSessManager)

const userID = "test-user-id"

needAuthorize := handler.TeamAuthMiddleware()(func(c echo.Context) error {
return c.NoContent(http.StatusOK)
})

tests := []struct {
name string
setup func()
expectStatus int
}{
{
name: "ok",
setup: func() {
mockRepo.EXPECT().FindTeam(gomock.Any(), "test-team-id").Return(domain.Team{
ID: "test-team-id",
Members: []domain.User{
{ID: userID},
},
}, nil)
},
expectStatus: http.StatusOK,
},
{
name: "user is not a member of the team",
setup: func() {
mockRepo.EXPECT().FindTeam(gomock.Any(), "test-team-id").Return(domain.Team{
ID: "test-team-id",
Members: []domain.User{{ID: "another-user-id"}},
}, nil)
},
expectStatus: http.StatusForbidden,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()

c := echo.New().NewContext(req, rec)
c.SetParamNames("teamID")
c.SetParamValues("test-team-id")
c.Set("userID", userID)

tt.setup()

_ = needAuthorize(c)
if rec.Code != tt.expectStatus {
t.Errorf("unexpected status code: expected=%d, got=%d", tt.expectStatus, rec.Code)
}
})
}
}

0 comments on commit d1ef3d9

Please sign in to comment.