forked from baron/baron-sso
감사로그 수행자 표시
This commit is contained in:
@@ -129,6 +129,18 @@ type devMockKetoOutboxRepository struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
type devMockAuthProvider struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *devMockAuthProvider) GetEnrichedProfile(c *fiber.Ctx) (*domain.UserProfileResponse, error) {
|
||||
args := m.Called(c)
|
||||
if profile, ok := args.Get(0).(*domain.UserProfileResponse); ok {
|
||||
return profile, args.Error(1)
|
||||
}
|
||||
return nil, args.Error(1)
|
||||
}
|
||||
|
||||
func (m *devMockKetoOutboxRepository) Create(ctx context.Context, entry *domain.KetoOutbox) error {
|
||||
return m.Called(ctx, entry).Error(0)
|
||||
}
|
||||
@@ -208,6 +220,66 @@ func devTestJWKSFirstKeyString(t *testing.T, jwks map[string]any, field string)
|
||||
|
||||
// --- Tests ---
|
||||
|
||||
func TestGetCurrentProfile_SetsAuditUserContext(t *testing.T) {
|
||||
mockAuth := new(devMockAuthProvider)
|
||||
handler := &DevHandler{Auth: mockAuth}
|
||||
app := fiber.New()
|
||||
|
||||
mockAuth.On("GetEnrichedProfile", mock.Anything).Return(&domain.UserProfileResponse{
|
||||
ID: "0a5b7284-e88a-4fdf-b56f-98d0435b24f5",
|
||||
Role: domain.RoleUser,
|
||||
}, nil)
|
||||
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
profile := handler.getCurrentProfile(c)
|
||||
return c.JSON(fiber.Map{
|
||||
"profile_id": profile.ID,
|
||||
"user_id": c.Locals("user_id"),
|
||||
})
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||
resp, _ := app.Test(req)
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
var body map[string]string
|
||||
assert.NoError(t, json.NewDecoder(resp.Body).Decode(&body))
|
||||
assert.NoError(t, resp.Body.Close())
|
||||
assert.Equal(t, "0a5b7284-e88a-4fdf-b56f-98d0435b24f5", body["profile_id"])
|
||||
assert.Equal(t, "0a5b7284-e88a-4fdf-b56f-98d0435b24f5", body["user_id"])
|
||||
}
|
||||
|
||||
func TestGetCurrentProfile_PreservesExistingAuditUserContext(t *testing.T) {
|
||||
handler := &DevHandler{}
|
||||
app := fiber.New()
|
||||
|
||||
app.Use(func(c *fiber.Ctx) error {
|
||||
c.Locals("user_profile", &domain.UserProfileResponse{
|
||||
ID: "profile-user",
|
||||
Role: domain.RoleUser,
|
||||
})
|
||||
c.Locals("user_id", "existing-user")
|
||||
return c.Next()
|
||||
})
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
profile := handler.getCurrentProfile(c)
|
||||
return c.JSON(fiber.Map{
|
||||
"profile_id": profile.ID,
|
||||
"user_id": c.Locals("user_id"),
|
||||
})
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||
resp, _ := app.Test(req)
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
var body map[string]string
|
||||
assert.NoError(t, json.NewDecoder(resp.Body).Decode(&body))
|
||||
assert.NoError(t, resp.Body.Close())
|
||||
assert.Equal(t, "profile-user", body["profile_id"])
|
||||
assert.Equal(t, "existing-user", body["user_id"])
|
||||
}
|
||||
|
||||
func TestListClients_Success(t *testing.T) {
|
||||
transport := roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
||||
if r.URL.Path == "/clients" {
|
||||
|
||||
Reference in New Issue
Block a user