diff --git a/solvway-admin-portal/app/api/users/[id]/change-password/route.test.ts b/solvway-admin-portal/app/api/users/[id]/change-password/route.test.ts deleted file mode 100644 index eb530cd..0000000 --- a/solvway-admin-portal/app/api/users/[id]/change-password/route.test.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { describe, it, expect, vi, beforeEach } from "vitest"; - -const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn(), getToken: vi.fn() })); -vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi })); -vi.mock("@/app/lib/server/session", () => ({ getToken: mocks.getToken })); - -import { POST } from "./route"; - -function ctx(id: string) { - return { params: Promise.resolve({ id }) }; -} -function req(body: unknown) { - return new Request("http://localhost/api/users/9/change-password", { - method: "POST", - body: JSON.stringify(body), - }) as unknown as Parameters[0]; -} - -beforeEach(() => { - mocks.callMobileApi.mockReset(); - mocks.getToken.mockReset(); -}); - -describe("POST /api/users/[id]/change-password", () => { - it("requires a password", async () => { - mocks.getToken.mockResolvedValue("jwt-abc"); - const res = await POST(req({}), ctx("9")); - expect(res.status).toBe(400); - expect(mocks.callMobileApi).not.toHaveBeenCalled(); - }); - - it("forwards to POST /api/admin/users/{id}/changePassword", async () => { - mocks.getToken.mockResolvedValue("jwt-abc"); - mocks.callMobileApi.mockResolvedValue({ ok: true, data: { updated: 1 } }); - await POST(req({ password: "N3wPassw0rd!" }), ctx("9")); - expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/users/9/changePassword", { - method: "POST", - body: { password: "N3wPassw0rd!" }, - token: "jwt-abc", - }); - }); -}); diff --git a/solvway-admin-portal/app/api/users/[id]/change-password/route.ts b/solvway-admin-portal/app/api/users/[id]/change-password/route.ts deleted file mode 100644 index 985b610..0000000 --- a/solvway-admin-portal/app/api/users/[id]/change-password/route.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { NextResponse, type NextRequest } from "next/server"; -import { callMobileApi } from "@/app/lib/server/mobileApi"; -import { getToken } from "@/app/lib/server/session"; -import { errorResponse, unauthorized, badRequest } from "@/app/lib/server/apiRoute"; - -type Ctx = { params: Promise<{ id: string }> }; - -export async function POST(req: NextRequest, { params }: Ctx) { - const token = await getToken(); - if (!token) return unauthorized(); - const { id } = await params; - const body = await req.json().catch(() => ({})); - const password = (body as { password?: string }).password; - if (!password) return badRequest("password is required."); - - const result = await callMobileApi<{ updated: number }>(`/api/admin/users/${encodeURIComponent(id)}/changePassword`, { - method: "POST", - body: { password }, - token, - }); - if (!result.ok) return errorResponse(result); - return NextResponse.json(result.data); -} diff --git a/solvway-admin-portal/app/components/users/ChangePasswordDialog.test.tsx b/solvway-admin-portal/app/components/users/ChangePasswordDialog.test.tsx deleted file mode 100644 index 9ab6bea..0000000 --- a/solvway-admin-portal/app/components/users/ChangePasswordDialog.test.tsx +++ /dev/null @@ -1,83 +0,0 @@ -import { render, screen, waitFor } from "@testing-library/react"; -import userEvent from "@testing-library/user-event"; -import { describe, it, expect, vi, beforeEach } from "vitest"; - -const mocks = vi.hoisted(() => ({ changeAdminUserPassword: vi.fn() })); -vi.mock("@/app/lib/services/adminUsers", () => ({ changeAdminUserPassword: mocks.changeAdminUserPassword })); - -import { ChangePasswordDialog } from "./ChangePasswordDialog"; - -beforeEach(() => { - mocks.changeAdminUserPassword.mockReset(); -}); - -async function fillAndSubmit(password: string, confirm: string) { - await userEvent.type(screen.getByLabelText("New password"), password); - await userEvent.type(screen.getByLabelText("Confirm password"), confirm); - await userEvent.click(screen.getByRole("button", { name: "Change password" })); -} - -describe("ChangePasswordDialog", () => { - it("states the password rules", () => { - render(); - expect(screen.getByText("At least 8 characters, with at least one capital letter and one small letter.")).toBeInTheDocument(); - }); - - it("blocks submit when the fields are empty", async () => { - render(); - await userEvent.click(screen.getByRole("button", { name: "Change password" })); - - expect(await screen.findByText(/enter and confirm/i)).toBeInTheDocument(); - expect(mocks.changeAdminUserPassword).not.toHaveBeenCalled(); - }); - - it("blocks submit when the password is too short", async () => { - render(); - // 7 characters: the backend admin rule needs at least 8. - await fillAndSubmit("Abcdefg", "Abcdefg"); - - expect(await screen.findByText(/must be at least 8 characters/i)).toBeInTheDocument(); - expect(mocks.changeAdminUserPassword).not.toHaveBeenCalled(); - }); - - it("blocks submit when the passwords do not match", async () => { - render(); - await fillAndSubmit("Passw0rd!", "Different1!"); - - expect(await screen.findByText(/do not match/i)).toBeInTheDocument(); - expect(mocks.changeAdminUserPassword).not.toHaveBeenCalled(); - }); - - it("submits a matching password", async () => { - mocks.changeAdminUserPassword.mockResolvedValue({ updated: 1 }); - render(); - await fillAndSubmit("Passw0rd!", "Passw0rd!"); - - await waitFor(() => expect(mocks.changeAdminUserPassword).toHaveBeenCalledWith(9, "Passw0rd!")); - expect(await screen.findByText(/changed successfully/i)).toBeInTheDocument(); - }); - - it("shows an error when the request fails", async () => { - mocks.changeAdminUserPassword.mockRejectedValue(new Error("Server error")); - render(); - await fillAndSubmit("Passw0rd!", "Passw0rd!"); - - expect(await screen.findByText("Server error")).toBeInTheDocument(); - }); - - it("disables submit while the request is pending", async () => { - let resolve!: (v: { updated: number }) => void; - mocks.changeAdminUserPassword.mockReturnValue( - new Promise((r) => { - resolve = r; - }), - ); - render(); - await userEvent.type(screen.getByLabelText("New password"), "Passw0rd!"); - await userEvent.type(screen.getByLabelText("Confirm password"), "Passw0rd!"); - await userEvent.click(screen.getByRole("button", { name: "Change password" })); - - await waitFor(() => expect(screen.getByRole("button", { name: "Change password" })).toBeDisabled()); - resolve({ updated: 1 }); - }); -}); diff --git a/solvway-admin-portal/app/components/users/ChangePasswordDialog.tsx b/solvway-admin-portal/app/components/users/ChangePasswordDialog.tsx deleted file mode 100644 index 4f1497c..0000000 --- a/solvway-admin-portal/app/components/users/ChangePasswordDialog.tsx +++ /dev/null @@ -1,121 +0,0 @@ -"use client"; - -import { useState, type FormEvent } from "react"; -import { Alert, AlertDescription } from "@/app/components/ui/alert"; -import { Button } from "@/app/components/ui/button"; -import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/app/components/ui/dialog"; -import { Input } from "@/app/components/ui/input"; -import { Label } from "@/app/components/ui/label"; -import { ErrorState } from "@/app/components/ui/states"; -import { useT } from "@/app/lib/i18n/MessagesProvider"; -import { changeAdminUserPassword } from "@/app/lib/services/adminUsers"; - -const MIN_LENGTH = 8; // services-system ADMIN_PWD_FORMAT minimum - -export function ChangePasswordDialog({ - userId, - onClose, -}: { - userId: number; - onClose: () => void; -}) { - const t = useT(); - const [password, setPassword] = useState(""); - const [confirm, setConfirm] = useState(""); - const [fieldError, setFieldError] = useState(null); - const [error, setError] = useState(null); - const [success, setSuccess] = useState(false); - const [saving, setSaving] = useState(false); - - async function handleSubmit(event: FormEvent) { - event.preventDefault(); - setError(null); - - if (!password || !confirm) { - setFieldError(t("UsrPwEnterBoth")); - return; - } - if (password.length < MIN_LENGTH) { - setFieldError(t("UsrPwTooShort", { min: MIN_LENGTH })); - return; - } - if (password !== confirm) { - setFieldError(t("UsrPwMismatch")); - return; - } - setFieldError(null); - - setSaving(true); - try { - await changeAdminUserPassword(userId, password); - setSuccess(true); - } catch (err) { - setError(err instanceof Error ? err.message : "UsrPwChangeFailed"); - } finally { - setSaving(false); - } - } - - return ( - !open && onClose()}> - - - {t("UsrChangePassword")} - - - {success ? ( - <> - - {t("UsrPwChanged")} - - - - - - ) : ( -
- {error && } - {fieldError && ( -

- {fieldError} -

- )} - -
- - setPassword(e.target.value)} - disabled={saving} - aria-describedby="new-password-rules" - /> -

{t("AdminPasswordHint")}

-
- -
- - setConfirm(e.target.value)} - disabled={saving} - /> -
- - - - - - - )} -
-
- ); -} diff --git a/solvway-admin-portal/app/lib/i18n/defaultMessages.ts b/solvway-admin-portal/app/lib/i18n/defaultMessages.ts index af986d2..072d983 100644 --- a/solvway-admin-portal/app/lib/i18n/defaultMessages.ts +++ b/solvway-admin-portal/app/lib/i18n/defaultMessages.ts @@ -115,13 +115,6 @@ export const DEFAULT_MESSAGES: Record = { Edit: "Edit", Export: "Export", UiMoreActionsFor: "More actions for {label}", - UsrPwEnterBoth: "Enter and confirm the new password.", - UsrPwTooShort: "Password must be at least {min} characters.", - UsrPwMismatch: "Passwords do not match.", - UsrPwChangeFailed: "Failed to change password.", - UsrChangePassword: "Change password", - UsrPwChanged: "Password changed successfully.", - UsrConfirmPassword: "Confirm password", UsrUsernameRequired: "Username is required.", UsrEmailRequired: "Email is required.", UsrEmailInvalid: "Enter a valid email address.", diff --git a/solvway-admin-portal/app/lib/services/adminUsers.test.ts b/solvway-admin-portal/app/lib/services/adminUsers.test.ts index d8b0c36..df97e76 100644 --- a/solvway-admin-portal/app/lib/services/adminUsers.test.ts +++ b/solvway-admin-portal/app/lib/services/adminUsers.test.ts @@ -7,7 +7,6 @@ import { listAdminUsers, getAdminUser, saveAdminUser, - changeAdminUserPassword, sendAdminUserPasswordReset, USER_STATUS_FILTER_OPTIONS, } from "./adminUsers"; @@ -48,15 +47,6 @@ describe("saveAdminUser", () => { }); }); -describe("changeAdminUserPassword", () => { - it("POSTs the password to /api/users/{id}/change-password", async () => { - mocks.apiPost.mockResolvedValue({ updated: 1 }); - const result = await changeAdminUserPassword(9, "N3wPassw0rd!"); - expect(mocks.apiPost).toHaveBeenCalledWith("/api/users/9/change-password", { password: "N3wPassw0rd!" }); - expect(result).toEqual({ updated: 1 }); - }); -}); - describe("sendAdminUserPasswordReset", () => { it("POSTs to /api/users/{id}/reset-password", async () => { mocks.apiPost.mockResolvedValue({ updated: 1 }); diff --git a/solvway-admin-portal/app/lib/services/adminUsers.ts b/solvway-admin-portal/app/lib/services/adminUsers.ts index d0abc14..971eee5 100644 --- a/solvway-admin-portal/app/lib/services/adminUsers.ts +++ b/solvway-admin-portal/app/lib/services/adminUsers.ts @@ -63,10 +63,6 @@ export async function saveAdminUser(payload: AdminUser): Promise { : apiPost("/api/users", payload); } -export async function changeAdminUserPassword(id: number, password: string): Promise<{ updated: number }> { - return apiPost<{ updated: number }>(`/api/users/${id}/change-password`, { password }); -} - export async function sendAdminUserPasswordReset(id: number): Promise<{ updated: number }> { return apiPost<{ updated: number }>(`/api/users/${id}/reset-password`, undefined); } diff --git a/solvway-admin-portal/app/users/[id]/page.test.tsx b/solvway-admin-portal/app/users/[id]/page.test.tsx index 5e3c82e..b1858e2 100644 --- a/solvway-admin-portal/app/users/[id]/page.test.tsx +++ b/solvway-admin-portal/app/users/[id]/page.test.tsx @@ -6,7 +6,6 @@ import { ApiError } from "@/app/lib/clientApi"; const mocks = vi.hoisted(() => ({ getAdminUser: vi.fn(), saveAdminUser: vi.fn(), - changeAdminUserPassword: vi.fn(), sendAdminUserPasswordReset: vi.fn(), useAdminAuth: vi.fn(), listAdminRoles: vi.fn(), @@ -15,7 +14,6 @@ const mocks = vi.hoisted(() => ({ vi.mock("@/app/lib/services/adminUsers", () => ({ getAdminUser: mocks.getAdminUser, saveAdminUser: mocks.saveAdminUser, - changeAdminUserPassword: mocks.changeAdminUserPassword, sendAdminUserPasswordReset: mocks.sendAdminUserPasswordReset, })); vi.mock("@/app/lib/services/adminRoles", () => ({ listAdminRoles: mocks.listAdminRoles })); @@ -41,7 +39,6 @@ const USER = { beforeEach(() => { mocks.getAdminUser.mockReset(); mocks.saveAdminUser.mockReset(); - mocks.changeAdminUserPassword.mockReset(); mocks.sendAdminUserPasswordReset.mockReset(); mocks.listAdminRoles.mockReset().mockResolvedValue([{ id: 1, name: "ROLE_IT" }]); mocks.useAdminAuth.mockReset().mockReturnValue({ loading: false, hasModule: () => true }); @@ -134,18 +131,13 @@ describe("EditUserPage", () => { ).toBeInTheDocument(); }); - it("opens the change password dialog and calls changeAdminUserPassword", async () => { + it("offers no direct password change, only the emailed reset", async () => { mocks.getAdminUser.mockResolvedValue(USER); - mocks.changeAdminUserPassword.mockResolvedValue({ updated: 1 }); render(); await waitFor(() => expect(screen.getByLabelText("Name")).toHaveValue("Jane")); - await userEvent.click(screen.getByRole("button", { name: /change password/i })); - await userEvent.type(screen.getByLabelText("New password"), "Passw0rd!"); - await userEvent.type(screen.getByLabelText("Confirm password"), "Passw0rd!"); - await userEvent.click(screen.getByRole("button", { name: "Change password" })); - - await waitFor(() => expect(mocks.changeAdminUserPassword).toHaveBeenCalledWith(9, "Passw0rd!")); + expect(screen.queryByRole("button", { name: /change password/i })).not.toBeInTheDocument(); + expect(screen.getByRole("button", { name: /send password reset/i })).toBeInTheDocument(); }); it("sends a password reset after confirming", async () => { diff --git a/solvway-admin-portal/app/users/[id]/page.tsx b/solvway-admin-portal/app/users/[id]/page.tsx index 9bd02eb..d6c1cad 100644 --- a/solvway-admin-portal/app/users/[id]/page.tsx +++ b/solvway-admin-portal/app/users/[id]/page.tsx @@ -9,7 +9,6 @@ import { PageHeader } from "@/app/components/ui/page-header"; import { Skeleton } from "@/app/components/ui/skeleton"; import { EmptyState, ErrorState } from "@/app/components/ui/states"; import { RequireModule } from "@/app/lib/RequireModule"; -import { ChangePasswordDialog } from "@/app/components/users/ChangePasswordDialog"; import { UserForm } from "@/app/components/users/UserForm"; import { ApiError } from "@/app/lib/clientApi"; import { getAdminUser, saveAdminUser, sendAdminUserPasswordReset } from "@/app/lib/services/adminUsers"; @@ -33,7 +32,6 @@ function EditUserPageContent() { ? { code: "UsrCreatedBanner" } : null, ); - const [changePasswordOpen, setChangePasswordOpen] = useState(false); const [confirmingReset, setConfirmingReset] = useState(false); const [resetError, setResetError] = useState(null); const [resetSending, setResetSending] = useState(false); @@ -127,9 +125,6 @@ function EditUserPageContent() { title={t("UsrEditNamed", { username: user.username })} actions={ <> -