1
0
forked from baron/baron-sso

test: add backend and e2e tests for bulk actions and tree search

This commit is contained in:
2026-03-04 15:49:05 +09:00
parent a5102d9b25
commit c126634e16
2 changed files with 137 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
import { expect, test } from "@playwright/test";
test.describe("Bulk Actions and Tree Search", () => {
test.beforeEach(async ({ page }) => {
// Authenticate as Super Admin
await page.addInitScript(() => {
const authority = "http://localhost:5000/oidc";
const client_id = "adminfront";
const key = `oidc.user:${authority}:${client_id}`;
window.localStorage.setItem(key, JSON.stringify({
access_token: "fake", profile: { sub: "admin", role: "super_admin" }, expires_at: 9999999999
}));
});
// Mock APIs
await page.route("**/api/v1/user/me", async (route) => {
await route.fulfill({ json: { id: "admin", role: "super_admin" } });
});
await page.route("**/api/v1/admin/users?*", async (route) => {
await route.fulfill({ json: {
items: [
{ id: "u-1", name: "User One", email: "u1@test.com", status: "active", role: "user", createdAt: new Date().toISOString() },
{ id: "u-2", name: "User Two", email: "u2@test.com", status: "active", role: "user", createdAt: new Date().toISOString() },
],
total: 2
}});
});
await page.route("**/api/v1/admin/tenants/t-1", async (route) => {
await route.fulfill({ json: { id: "t-1", name: "Main Tenant", slug: "main" } });
});
await page.route("**/api/v1/admin/tenants/t-1/organization", async (route) => {
await route.fulfill({ json: [
{ id: "g-1", name: "Engineering", slug: "eng", tenantId: "t-1" },
{ id: "g-2", name: "Sales", slug: "sales", tenantId: "t-1" },
]});
});
});
test("should show bulk action bar when users are selected", async ({ page }) => {
await page.goto("/users");
// Check individual row
await page.locator('input[type="checkbox"]').nth(1).check();
await expect(page.getByText("1명 선택됨")).toBeVisible();
await expect(page.getByRole("button", { name: /활성화|Active/i })).toBeVisible();
// Check select all
await page.locator('input[type="checkbox"]').first().check();
await expect(page.getByText("2명 선택됨")).toBeVisible();
// Clear selection
await page.getByRole("button", { name: "Plus" }).click(); // The close icon
await expect(page.getByText("명 선택됨")).not.toBeVisible();
});
test("should filter and highlight nodes in organization tree", async ({ page }) => {
await page.goto("/tenants/t-1");
await page.getByRole("link", { name: /하위 테넌트 관리|Sub-tenant/i }).click();
const searchInput = page.getByPlaceholder(/조직도 내 검색|Search in tree/i);
await expect(searchInput).toBeVisible();
await searchInput.fill("Eng");
// Check if Engineering row is highlighted
const engRow = page.locator('tr:has-text("Engineering")');
await expect(engRow).toHaveClass(/bg-primary\/10/);
});
});

View File

@@ -236,6 +236,71 @@ func TestUserHandler_BulkCreateUsers(t *testing.T) {
})
}
func TestUserHandler_BulkUpdateUsers(t *testing.T) {
app := fiber.New()
mockKratos := new(MockKratosAdmin)
h := &UserHandler{KratosAdmin: mockKratos}
app.Put("/users/bulk", func(c *fiber.Ctx) error {
c.Locals("user_profile", &domain.UserProfileResponse{Role: domain.RoleSuperAdmin})
return h.BulkUpdateUsers(c)
})
t.Run("Success - Update Role and Status", func(t *testing.T) {
mockKratos.On("GetIdentity", mock.Anything, "u-1").Return(&service.KratosIdentity{
ID: "u-1", Traits: map[string]interface{}{"email": "u1@test.com"}, State: "active",
}, nil).Once()
mockKratos.On("UpdateIdentity", mock.Anything, "u-1", mock.Anything, "inactive").Return(&service.KratosIdentity{}, nil).Once()
status := "inactive"
payload := map[string]interface{}{
"userIds": []string{"u-1"},
"status": &status,
}
body, _ := json.Marshal(payload)
req := httptest.NewRequest("PUT", "/users/bulk", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
resp, _ := app.Test(req)
assert.Equal(t, 200, resp.StatusCode)
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
results := result["results"].([]interface{})
assert.True(t, results[0].(map[string]interface{})["success"].(bool))
})
}
func TestUserHandler_BulkDeleteUsers(t *testing.T) {
app := fiber.New()
mockKratos := new(MockKratosAdmin)
h := &UserHandler{KratosAdmin: mockKratos}
app.Delete("/users/bulk", func(c *fiber.Ctx) error {
c.Locals("user_profile", &domain.UserProfileResponse{Role: domain.RoleSuperAdmin})
return h.BulkDeleteUsers(c)
})
t.Run("Success - Delete multiple", func(t *testing.T) {
mockKratos.On("GetIdentity", mock.Anything, "u-1").Return(&service.KratosIdentity{ID: "u-1"}, nil).Once()
mockKratos.On("GetIdentity", mock.Anything, "u-2").Return(&service.KratosIdentity{ID: "u-2"}, nil).Once()
mockKratos.On("DeleteIdentity", mock.Anything, "u-1").Return(nil).Once()
mockKratos.On("DeleteIdentity", mock.Anything, "u-2").Return(nil).Once()
payload := map[string]interface{}{
"userIds": []string{"u-1", "u-2"},
}
body, _ := json.Marshal(payload)
req := httptest.NewRequest("DELETE", "/users/bulk", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
resp, _ := app.Test(req)
assert.Equal(t, 200, resp.StatusCode)
})
}
func TestUserHandler_UpdateUser_AdminOnlyField(t *testing.T) {
app := fiber.New()
mockKratos := new(MockKratosAdmin)