From c126634e160ef9cca1368b8fb9a2ccdfd961109d Mon Sep 17 00:00:00 2001 From: chan Date: Wed, 4 Mar 2026 15:49:05 +0900 Subject: [PATCH] test: add backend and e2e tests for bulk actions and tree search --- adminfront/tests/bulk_actions.spec.ts | 72 +++++++++++++++++++ backend/internal/handler/user_handler_test.go | 65 +++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 adminfront/tests/bulk_actions.spec.ts diff --git a/adminfront/tests/bulk_actions.spec.ts b/adminfront/tests/bulk_actions.spec.ts new file mode 100644 index 00000000..69168578 --- /dev/null +++ b/adminfront/tests/bulk_actions.spec.ts @@ -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/); + }); +}); diff --git a/backend/internal/handler/user_handler_test.go b/backend/internal/handler/user_handler_test.go index 131d53a4..994c1225 100644 --- a/backend/internal/handler/user_handler_test.go +++ b/backend/internal/handler/user_handler_test.go @@ -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)