@griddo/ax 12.15.1-astro-rc.0 → 12.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/config/__tests__/getCSSModuleLocalIdent.test.js +30 -0
- package/config/griddo-config/index.js +3 -2
- package/config/webpack/getCSSModuleLocalIdent.js +10 -0
- package/config/webpack.config.js +1 -1
- package/package.json +8 -6
- package/public/img/logos/logoGriddoDxp.svg +22 -0
- package/public/img/logos/logoGriddoG.svg +10 -0
- package/src/GlobalStore.tsx +32 -25
- package/src/__tests__/GlobalStore.session.test.tsx +98 -0
- package/src/__tests__/api/global.test.ts +64 -0
- package/src/__tests__/api/session.test.ts +311 -0
- package/src/__tests__/api/sessionRenewal.test.ts +705 -0
- package/src/__tests__/api/utils.test.ts +373 -0
- package/src/__tests__/components/Fields/FileField/FileField.test.tsx +33 -6
- package/src/__tests__/components/Fields/TranslateButton/TranslateButton.test.tsx +121 -4
- package/src/__tests__/components/FieldsBehavior/FieldsBehavior.test.tsx +5 -3
- package/src/__tests__/components/Login/BrandPalette.test.tsx +145 -0
- package/src/__tests__/components/Login/Login.test.tsx +2 -33
- package/src/__tests__/components/Login/LoginCollapsible.test.tsx +271 -0
- package/src/__tests__/components/Login/LoginDropdownCard.test.tsx +311 -0
- package/src/__tests__/components/Login/LoginFooter.test.tsx +128 -0
- package/src/__tests__/components/Login/LoginGriddoId.test.tsx +213 -0
- package/src/__tests__/components/Login/LoginInlineError.test.tsx +116 -0
- package/src/__tests__/components/Login/RecoveryModal/RecoveryModal.test.tsx +3 -4
- package/src/__tests__/components/SsoDenied/SsoDenied.test.tsx +86 -0
- package/src/__tests__/config/webpackAliases.test.ts +18 -0
- package/src/__tests__/containers/App/reducer.griddoId.test.ts +18 -0
- package/src/__tests__/containers/App/session.test.ts +397 -0
- package/src/__tests__/containers/App/signInOut.test.ts +159 -0
- package/src/__tests__/helpers/griddoId.test.ts +56 -0
- package/src/__tests__/hooks/useShouldBeSaved.test.tsx +78 -0
- package/src/__tests__/locales/loginLocale.test.ts +37 -0
- package/src/__tests__/modules/App/Routing/NavMenu.test.tsx +54 -0
- package/src/__tests__/modules/App/Routing/PrivateRoute.test.tsx +64 -0
- package/src/__tests__/modules/App/Routing/Routing.startup.test.tsx +79 -0
- package/src/__tests__/modules/Login/Login.test.tsx +169 -0
- package/src/__tests__/modules/Login/loginError.uc.test.tsx +294 -0
- package/src/__tests__/modules/Login/signInOut.test.tsx +125 -0
- package/src/__tests__/modules/Redirects/RedirectPanel/RedirectPanel.test.tsx +9 -0
- package/src/__tests__/modules/Redirects/Redirects/Redirects.create.test.tsx +19 -0
- package/src/__tests__/modules/Redirects/Redirects/Redirects.edit.test.tsx +19 -0
- package/src/__tests__/modules/Redirects/Redirects/Redirects.filter.test.tsx +19 -0
- package/src/__tests__/modules/SsoDenied/SsoDenied.test.tsx +30 -0
- package/src/__tests__/routes/publicRoutes.test.ts +36 -0
- package/src/api/global.tsx +24 -1
- package/src/api/session.tsx +197 -0
- package/src/api/sessionRenewal.tsx +114 -0
- package/src/api/utils.tsx +22 -12
- package/src/{helpers/categoryColumns.tsx → components/CategoryCell/buildCategoryColumns.tsx} +3 -2
- package/src/components/Circle/index.tsx +1 -1
- package/src/components/Circle/style.tsx +4 -2
- package/src/components/ErrorPage/index.tsx +1 -1
- package/src/components/ErrorPage/style.tsx +11 -9
- package/src/components/ErrorToast/index.tsx +2 -0
- package/src/components/Fields/TranslateButton/index.tsx +4 -9
- package/src/{helpers/fields.tsx → components/FieldsBehavior/getFieldByPath.tsx} +1 -1
- package/src/components/FieldsBehavior/index.tsx +3 -1
- package/src/components/Login/LoginFooter/index.tsx +16 -0
- package/src/components/Login/LoginFooter/style.tsx +17 -0
- package/src/components/Login/LoginForm/index.tsx +147 -79
- package/src/components/Login/LoginForm/style.tsx +147 -24
- package/src/components/Login/LoginGriddoId/index.tsx +56 -0
- package/src/components/Login/LoginGriddoId/style.tsx +35 -0
- package/src/components/Login/LoginSSO/index.tsx +4 -7
- package/src/components/Login/LoginSSO/style.tsx +10 -26
- package/src/components/Login/LoginSlider/style.tsx +5 -5
- package/src/components/Login/index.tsx +35 -6
- package/src/components/Login/style.tsx +40 -24
- package/src/components/Notification/index.tsx +3 -1
- package/src/components/SsoDenied/index.tsx +46 -0
- package/src/components/SsoDenied/style.tsx +37 -0
- package/src/components/index.tsx +11 -7
- package/src/containers/App/actions.tsx +114 -6
- package/src/containers/App/interfaces.tsx +2 -2
- package/src/containers/App/reducer.tsx +9 -0
- package/src/containers/Users/actions.tsx +28 -24
- package/src/helpers/griddoId.ts +28 -0
- package/src/helpers/index.tsx +4 -5
- package/src/hooks/forms.tsx +8 -5
- package/src/locales/en-US.ts +13 -0
- package/src/locales/es-ES.ts +13 -0
- package/src/locales/index.ts +9 -1
- package/src/modules/App/Routing/NavMenu/index.tsx +4 -4
- package/src/modules/Content/PageItem/index.tsx +6 -8
- package/src/modules/Login/index.tsx +33 -14
- package/src/modules/SsoDenied/index.tsx +9 -0
- package/src/modules/StructuredData/StructuredDataList/GlobalPageItem/index.tsx +6 -8
- package/src/modules/StructuredData/StructuredDataList/StructuredDataItem/index.tsx +9 -11
- package/src/modules/Users/UserEdit/index.tsx +9 -2
- package/src/routes/publicRoutes.tsx +3 -0
- package/src/themes/brandPalette.ts +16 -0
- package/public/img/logos/logoSQY.svg +0 -3
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { cleanup, render, screen } from "@testing-library/react";
|
|
2
|
+
import userEvent from "@testing-library/user-event";
|
|
3
|
+
import { ThemeProvider } from "styled-components";
|
|
4
|
+
import "@testing-library/jest-dom";
|
|
5
|
+
|
|
6
|
+
import Login, { type ILoginProps } from "@ax/components/Login";
|
|
7
|
+
import { parseTheme } from "@ax/helpers";
|
|
8
|
+
import { BRAND_PALETTE } from "@ax/themes/brandPalette";
|
|
9
|
+
import globalTheme from "@ax/themes/theme.json";
|
|
10
|
+
|
|
11
|
+
afterEach(cleanup);
|
|
12
|
+
|
|
13
|
+
const baseSettings = {
|
|
14
|
+
cloudinaryName: "cloudinary",
|
|
15
|
+
globalLogoBig: "",
|
|
16
|
+
globalLogoMini: "",
|
|
17
|
+
siteLogoBig: "",
|
|
18
|
+
siteLogoMini: "",
|
|
19
|
+
welcomeText1: "Welcome back to",
|
|
20
|
+
welcomeText2: "Griddo",
|
|
21
|
+
autoSummary: false,
|
|
22
|
+
autoTranslation: false,
|
|
23
|
+
SSOActivated: false,
|
|
24
|
+
GriddoIdEnabled: true,
|
|
25
|
+
SSOWelcomeText: "",
|
|
26
|
+
useForms: false,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const renderLogin = (settings: Partial<typeof baseSettings> = {}) =>
|
|
30
|
+
render(
|
|
31
|
+
<ThemeProvider theme={parseTheme(globalTheme)}>
|
|
32
|
+
<Login
|
|
33
|
+
handleSubmit={vi.fn((e: React.FormEvent) => e.preventDefault())}
|
|
34
|
+
email=""
|
|
35
|
+
handleEmail={vi.fn()}
|
|
36
|
+
password=""
|
|
37
|
+
handlePassword={vi.fn()}
|
|
38
|
+
isLoggingIn={false}
|
|
39
|
+
rememberMe={false}
|
|
40
|
+
handleRememberMe={vi.fn()}
|
|
41
|
+
isLoginSuccess={false}
|
|
42
|
+
handleLoginSuccess={vi.fn()}
|
|
43
|
+
handleSSO={vi.fn()}
|
|
44
|
+
handleGriddoId={vi.fn()}
|
|
45
|
+
errorSSO={null}
|
|
46
|
+
settings={{ ...baseSettings, ...settings } as ILoginProps["settings"]}
|
|
47
|
+
/>
|
|
48
|
+
</ThemeProvider>,
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
const toggle = () => screen.getByRole("button", { name: "Login with email and password" });
|
|
52
|
+
const card = () => screen.getByTestId("login-dropdown");
|
|
53
|
+
const slot = () => document.querySelector("#error") as HTMLElement;
|
|
54
|
+
|
|
55
|
+
describe("[UC-AX-sign-in-out] #error único dentro de la tarjeta (AC15)", () => {
|
|
56
|
+
beforeEach(() => vi.spyOn(window.navigator, "language", "get").mockReturnValue("en-US"));
|
|
57
|
+
|
|
58
|
+
it("AC15 · el #error vive dentro de la tarjeta, entre la cabecera y el formulario, y es único", () => {
|
|
59
|
+
renderLogin();
|
|
60
|
+
|
|
61
|
+
expect(document.querySelectorAll("#error")).toHaveLength(1);
|
|
62
|
+
expect(card()).toContainElement(slot());
|
|
63
|
+
expect(toggle().compareDocumentPosition(slot()) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
|
|
64
|
+
expect(
|
|
65
|
+
slot().compareDocumentPosition(screen.getByPlaceholderText("Type your email")) & Node.DOCUMENT_POSITION_FOLLOWING,
|
|
66
|
+
).toBeTruthy();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("AC15 · plegado el #error se pinta encima del botón (hueco fijo) y abierto va tras la cabecera", async () => {
|
|
70
|
+
renderLogin();
|
|
71
|
+
|
|
72
|
+
expect(slot()).toHaveStyle({ order: "-1" });
|
|
73
|
+
|
|
74
|
+
await userEvent.click(toggle());
|
|
75
|
+
|
|
76
|
+
expect(slot()).toHaveStyle({ order: "0" });
|
|
77
|
+
expect(document.querySelectorAll("#error")).toHaveLength(1);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("AC15 · la caja del botón (fondo, borde, radio 8) vive en el botón plegado y la tarjeta abierta la toma el contenedor", async () => {
|
|
81
|
+
renderLogin();
|
|
82
|
+
|
|
83
|
+
expect(toggle()).toHaveStyle({
|
|
84
|
+
backgroundColor: BRAND_PALETTE.navyControl,
|
|
85
|
+
border: "1px solid rgba(219,221,233,.3)",
|
|
86
|
+
borderRadius: "8px",
|
|
87
|
+
});
|
|
88
|
+
expect(card()).toHaveStyle({ padding: "0px" });
|
|
89
|
+
|
|
90
|
+
await userEvent.click(toggle());
|
|
91
|
+
|
|
92
|
+
expect(card()).toHaveStyle({
|
|
93
|
+
backgroundColor: BRAND_PALETTE.navyControl,
|
|
94
|
+
borderRadius: "12px",
|
|
95
|
+
padding: "11px 25px 28px 23px",
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("AC15 · el #error es una región role=alert (se anuncia) también con el panel plegado", () => {
|
|
100
|
+
renderLogin();
|
|
101
|
+
|
|
102
|
+
expect(slot()).toHaveAttribute("role", "alert");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("AC15 · login normal sin Griddo ID: sigue habiendo un único #error", () => {
|
|
106
|
+
renderLogin({ GriddoIdEnabled: false });
|
|
107
|
+
|
|
108
|
+
expect(document.querySelectorAll("#error")).toHaveLength(1);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("AC15 · SSO de Comillas: sigue habiendo un único #error", () => {
|
|
112
|
+
renderLogin({ SSOActivated: true });
|
|
113
|
+
|
|
114
|
+
expect(document.querySelectorAll("#error")).toHaveLength(1);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import * as React from "react";
|
|
1
|
+
import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
|
|
3
2
|
import Axios from "axios";
|
|
4
3
|
import { ThemeProvider } from "styled-components";
|
|
5
|
-
import {
|
|
4
|
+
import type { MockedFunction } from "vitest";
|
|
6
5
|
import "@testing-library/jest-dom";
|
|
7
6
|
|
|
8
|
-
import { parseTheme } from "@ax/helpers";
|
|
9
7
|
import RecoveryModal, { type IRecoveryProps } from "@ax/components/Login/LoginForm/RecoveryModal";
|
|
8
|
+
import { parseTheme } from "@ax/helpers";
|
|
10
9
|
import globalTheme from "@ax/themes/theme.json";
|
|
11
10
|
|
|
12
11
|
afterEach(cleanup);
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { cleanup, render, screen } from "@testing-library/react";
|
|
2
|
+
import { ThemeProvider } from "styled-components";
|
|
3
|
+
import "@testing-library/jest-dom";
|
|
4
|
+
|
|
5
|
+
import SsoDenied from "@ax/components/SsoDenied";
|
|
6
|
+
import { parseTheme } from "@ax/helpers";
|
|
7
|
+
import globalTheme from "@ax/themes/theme.json";
|
|
8
|
+
|
|
9
|
+
afterEach(() => {
|
|
10
|
+
cleanup();
|
|
11
|
+
window.history.pushState({}, "", "/");
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const SWITCH_URL = "https://api.uni.es/sso/switch-account";
|
|
15
|
+
|
|
16
|
+
const renderS6 = () =>
|
|
17
|
+
render(
|
|
18
|
+
<ThemeProvider theme={parseTheme(globalTheme)}>
|
|
19
|
+
<SsoDenied switchAccountUrl={SWITCH_URL} />
|
|
20
|
+
</ThemeProvider>,
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
const setLanguage = (language: string) => vi.spyOn(window.navigator, "language", "get").mockReturnValue(language);
|
|
24
|
+
|
|
25
|
+
describe("S6 — acceso denegado", () => {
|
|
26
|
+
beforeEach(() => setLanguage("es-ES"));
|
|
27
|
+
|
|
28
|
+
it("dice «You don't have access to this instance» y no menciona motivo ni email", () => {
|
|
29
|
+
const { container } = renderS6();
|
|
30
|
+
|
|
31
|
+
expect(screen.getByRole("heading", { name: "You don't have access to this instance" })).toBeInTheDocument();
|
|
32
|
+
expect(container.textContent).not.toMatch(/motivo|reason|@/i);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("«Sign in with another account» es un enlace al cambio de cuenta de la API", () => {
|
|
36
|
+
renderS6();
|
|
37
|
+
|
|
38
|
+
expect(screen.getByRole("link", { name: "Sign in with another account" })).toHaveAttribute("href", SWITCH_URL);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("el enlace está subrayado", () => {
|
|
42
|
+
renderS6();
|
|
43
|
+
|
|
44
|
+
expect(
|
|
45
|
+
getComputedStyle(screen.getByRole("link", { name: "Sign in with another account" })).textDecoration,
|
|
46
|
+
).toContain("underline");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("el foco va al título al abrirla", () => {
|
|
50
|
+
renderS6();
|
|
51
|
+
|
|
52
|
+
expect(screen.getByRole("heading")).toHaveFocus();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("no refleja parámetros de la URL: texto fijo con ?reason=<script> y ?email=x", () => {
|
|
56
|
+
window.history.pushState({}, "", "/sso-denied?reason=%3Cscript%3Ealert(1)%3C/script%3E&email=x@y.z");
|
|
57
|
+
|
|
58
|
+
const { container } = renderS6();
|
|
59
|
+
|
|
60
|
+
expect(container.textContent).not.toContain("<script>");
|
|
61
|
+
expect(container.textContent).not.toContain("x@y.z");
|
|
62
|
+
expect(container.querySelector("script")).toBeNull();
|
|
63
|
+
expect(screen.getByRole("heading", { name: "You don't have access to this instance" })).toBeInTheDocument();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("abierta por URL directa muestra la misma pantalla", () => {
|
|
67
|
+
window.history.pushState({}, "", "/sso-denied");
|
|
68
|
+
|
|
69
|
+
renderS6();
|
|
70
|
+
|
|
71
|
+
expect(screen.getByRole("heading", { name: "You don't have access to this instance" })).toBeInTheDocument();
|
|
72
|
+
expect(screen.getByRole("link", { name: "Sign in with another account" })).toBeInTheDocument();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it.each([
|
|
76
|
+
["en-US", "You don't have access to this instance", "Sign in with another account"],
|
|
77
|
+
["es-ES", "You don't have access to this instance", "Sign in with another account"],
|
|
78
|
+
["fr-FR", "You don't have access to this instance", "Sign in with another account"],
|
|
79
|
+
])("idioma %s", (language, title, link) => {
|
|
80
|
+
setLanguage(language);
|
|
81
|
+
renderS6();
|
|
82
|
+
|
|
83
|
+
expect(screen.getByRole("heading", { name: title })).toBeInTheDocument();
|
|
84
|
+
expect(screen.getByRole("link", { name: link })).toBeInTheDocument();
|
|
85
|
+
});
|
|
86
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import paths from "../../../tsconfig.paths.json";
|
|
4
|
+
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
|
+
const { projectAliases } = require(resolve(__dirname, "../../../config/griddo-config")) as {
|
|
7
|
+
projectAliases: Record<string, string>;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
// Vitest y `tsc` leen `tsconfig.paths.json`; webpack, no: sus alias salen de `config/griddo-config`. Un alias que
|
|
11
|
+
// solo exista en el primero pasa los tests y rompe `pnpm start` (p. ej. `@ax/themes/brandPalette`).
|
|
12
|
+
describe("alias de webpack = alias de tsconfig", () => {
|
|
13
|
+
const roots = [...new Set(Object.keys(paths.compilerOptions.paths).map((alias) => alias.replace(/\/\*$/, "")))];
|
|
14
|
+
|
|
15
|
+
it.each(roots)("%s está declarado en los alias de webpack", (root) => {
|
|
16
|
+
expect(projectAliases).toHaveProperty([root]);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { SET_GLOBAL_SETTINGS } from "@ax/containers/App/constants";
|
|
2
|
+
import { appInitialState, appReducer } from "@ax/containers/App/reducer";
|
|
3
|
+
|
|
4
|
+
describe("GriddoIdEnabled en los settings de AX", () => {
|
|
5
|
+
it("está apagado por defecto: si GET /settings falla se ve el login de siempre", () => {
|
|
6
|
+
expect(appInitialState.globalSettings.GriddoIdEnabled).toBe(false);
|
|
7
|
+
expect(appInitialState.globalSettings.SSOActivated).toBe(false);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("el valor de la API llega al estado", () => {
|
|
11
|
+
const state = appReducer(appInitialState, {
|
|
12
|
+
type: SET_GLOBAL_SETTINGS,
|
|
13
|
+
payload: { globalSettings: { ...appInitialState.globalSettings, GriddoIdEnabled: true } },
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
expect(state.globalSettings.GriddoIdEnabled).toBe(true);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
import { global, languages, users } from "@ax/api";
|
|
2
|
+
import { appActions } from "@ax/containers/App";
|
|
3
|
+
import { LOGOUT, SET_TOKEN } from "@ax/containers/App/constants";
|
|
4
|
+
import { appReducer } from "@ax/containers/App/reducer";
|
|
5
|
+
import { usersActions } from "@ax/containers/Users";
|
|
6
|
+
|
|
7
|
+
import type { Mock } from "vitest";
|
|
8
|
+
|
|
9
|
+
import { getSession, registerSessionBridge, resetSessionState, setSession } from "../../../api/session";
|
|
10
|
+
import history from "../../../routes/history";
|
|
11
|
+
|
|
12
|
+
const persisted = (token: string, refreshToken: string, sessionUpdatedAt: number) =>
|
|
13
|
+
JSON.stringify({
|
|
14
|
+
token: JSON.stringify(token),
|
|
15
|
+
refreshToken: JSON.stringify(refreshToken),
|
|
16
|
+
sessionUpdatedAt: JSON.stringify(sessionUpdatedAt),
|
|
17
|
+
_persist: JSON.stringify({ version: -1, rehydrated: true }),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
let onExpired: Mock<() => void>;
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
localStorage.clear();
|
|
24
|
+
resetSessionState();
|
|
25
|
+
onExpired = vi.fn();
|
|
26
|
+
registerSessionBridge({ onExpired });
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe("login guarda el par de sesión", () => {
|
|
30
|
+
const runLogin = async (data: Record<string, unknown>) => {
|
|
31
|
+
const dispatch = vi.fn();
|
|
32
|
+
vi.spyOn(global, "login").mockResolvedValue({ status: 200, data } as any);
|
|
33
|
+
vi.spyOn(languages, "getLanguages").mockResolvedValue({ status: 200, data: { items: [] } } as any);
|
|
34
|
+
vi.spyOn(usersActions, "getUser").mockReturnValue((async () => undefined) as any);
|
|
35
|
+
const result = await appActions.login({ email: "a@b.c", password: "x" })(dispatch, vi.fn() as any);
|
|
36
|
+
return { dispatch, result };
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
it("login con refresh: acción SET_TOKEN con acceso, refresh y versión; sesión en memoria", async () => {
|
|
40
|
+
const { dispatch, result } = await runLogin({ id: 7, token: "jwt", refreshToken: "r1" });
|
|
41
|
+
|
|
42
|
+
expect(result).toBe(true);
|
|
43
|
+
const setToken = dispatch.mock.calls.map(([a]) => a).find((a) => a.type === SET_TOKEN);
|
|
44
|
+
expect(setToken.payload).toEqual({ token: "jwt", refreshToken: "r1", sessionUpdatedAt: expect.any(Number) });
|
|
45
|
+
expect(setToken.payload.sessionUpdatedAt).toBeGreaterThan(0);
|
|
46
|
+
expect(getSession()).toMatchObject({ token: "jwt", refreshToken: "r1" });
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("login con API antigua (sin refresh): refresh vacío y sesión sin refresh", async () => {
|
|
50
|
+
const { dispatch } = await runLogin({ id: 7, token: "jwt" });
|
|
51
|
+
|
|
52
|
+
const setToken = dispatch.mock.calls.map(([a]) => a).find((a) => a.type === SET_TOKEN);
|
|
53
|
+
expect(setToken.payload.refreshToken).toBe("");
|
|
54
|
+
expect(getSession()).toMatchObject({ token: "jwt", refreshToken: null });
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("el login SSO (petitionId) usa el mismo camino y guarda el refresh", async () => {
|
|
58
|
+
const dispatch = vi.fn();
|
|
59
|
+
const login = vi.spyOn(global, "login").mockResolvedValue({
|
|
60
|
+
status: 200,
|
|
61
|
+
data: { id: 1, token: "sso-jwt", refreshToken: "sso-r" },
|
|
62
|
+
} as any);
|
|
63
|
+
vi.spyOn(languages, "getLanguages").mockResolvedValue({ status: 200, data: { items: [] } } as any);
|
|
64
|
+
vi.spyOn(usersActions, "getUser").mockReturnValue((async () => undefined) as any);
|
|
65
|
+
|
|
66
|
+
await appActions.login({ petitionId: "abc" })(dispatch, vi.fn() as any);
|
|
67
|
+
|
|
68
|
+
expect(login).toHaveBeenCalledWith({ username: undefined, password: undefined, petitionId: "abc" });
|
|
69
|
+
expect(getSession()).toMatchObject({ token: "sso-jwt", refreshToken: "sso-r" });
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe("createPassword y cambio de contraseña guardan la sesión nueva", () => {
|
|
74
|
+
it("createPassword guarda acceso y refresh", async () => {
|
|
75
|
+
const dispatch = vi.fn();
|
|
76
|
+
vi.spyOn(global, "createPassword").mockResolvedValue({
|
|
77
|
+
status: 200,
|
|
78
|
+
data: { token: "t", refreshToken: "r" },
|
|
79
|
+
} as any);
|
|
80
|
+
vi.spyOn(usersActions, "getRoles").mockReturnValue((async () => undefined) as any);
|
|
81
|
+
vi.spyOn(usersActions, "getUser").mockReturnValue((async () => undefined) as any);
|
|
82
|
+
|
|
83
|
+
await usersActions.createPassword(5, { password: "p" } as any)(dispatch, vi.fn() as any);
|
|
84
|
+
|
|
85
|
+
const setToken = dispatch.mock.calls.map(([a]) => a).find((a) => a?.type === SET_TOKEN);
|
|
86
|
+
expect(setToken.payload).toMatchObject({ token: "t", refreshToken: "r" });
|
|
87
|
+
expect(getSession()).toMatchObject({ token: "t", refreshToken: "r" });
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("cambiar la contraseña devuelve par nuevo y AX sustituye la sesión", async () => {
|
|
91
|
+
setSession("old", "r-old");
|
|
92
|
+
const dispatch = vi.fn();
|
|
93
|
+
vi.spyOn(users, "updatePassword").mockResolvedValue({
|
|
94
|
+
status: 200,
|
|
95
|
+
data: { token: "new", refreshToken: "r-new" },
|
|
96
|
+
} as any);
|
|
97
|
+
|
|
98
|
+
const ok = await usersActions.updatePassword(5, { password: "x" })(dispatch);
|
|
99
|
+
|
|
100
|
+
expect(ok).toBe(true);
|
|
101
|
+
expect(getSession()).toMatchObject({ token: "new", refreshToken: "r-new" });
|
|
102
|
+
expect(dispatch.mock.calls.map(([a]) => a).some((a) => a?.type === SET_TOKEN && a.payload.token === "new")).toBe(
|
|
103
|
+
true,
|
|
104
|
+
);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("cambiar la contraseña de otra persona (sin par en la respuesta) no toca la sesión", async () => {
|
|
108
|
+
setSession("mine", "r-mine");
|
|
109
|
+
const dispatch = vi.fn();
|
|
110
|
+
vi.spyOn(users, "updatePassword").mockResolvedValue({ status: 200, data: {} } as any);
|
|
111
|
+
|
|
112
|
+
await usersActions.updatePassword(9, { password: "x" })(dispatch);
|
|
113
|
+
|
|
114
|
+
expect(getSession()).toMatchObject({ token: "mine", refreshToken: "r-mine" });
|
|
115
|
+
expect(dispatch.mock.calls.some(([a]) => a?.type === SET_TOKEN)).toBe(false);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("un cambio de contraseña fallido no toca la sesión", async () => {
|
|
119
|
+
setSession("mine", "r-mine");
|
|
120
|
+
vi.spyOn(users, "updatePassword").mockResolvedValue({ status: 400, data: { code: 400, message: "x" } } as any);
|
|
121
|
+
|
|
122
|
+
await usersActions.updatePassword(5, { password: "x" })(vi.fn());
|
|
123
|
+
|
|
124
|
+
expect(getSession()).toMatchObject({ token: "mine" });
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe("logout", () => {
|
|
129
|
+
it("avisa a la API con el acceso (aunque esté caducado) y el refresh, y limpia el estado local", () => {
|
|
130
|
+
localStorage.setItem("persist:app", persisted("expired-jwt", "r1", 5));
|
|
131
|
+
localStorage.setItem("sitesConfig", "keep");
|
|
132
|
+
localStorage.setItem("other", "drop");
|
|
133
|
+
const logoutApi = vi.spyOn(global, "logout").mockResolvedValue({ status: 204 } as any);
|
|
134
|
+
|
|
135
|
+
const action = appActions.logout();
|
|
136
|
+
|
|
137
|
+
expect(logoutApi).toHaveBeenCalledWith({ token: "expired-jwt", refreshToken: "r1" });
|
|
138
|
+
expect(action).toEqual({ type: LOGOUT, payload: { token: "", refreshToken: "", sessionUpdatedAt: 0 } });
|
|
139
|
+
expect(localStorage.getItem("other")).toBeNull();
|
|
140
|
+
expect(localStorage.getItem("sitesConfig")).toBe("keep");
|
|
141
|
+
expect(getSession()).toBeNull();
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("sin sesión no llama a la API", () => {
|
|
145
|
+
const logoutApi = vi.spyOn(global, "logout").mockResolvedValue({ status: 204 } as any);
|
|
146
|
+
|
|
147
|
+
appActions.logout();
|
|
148
|
+
|
|
149
|
+
expect(logoutApi).not.toHaveBeenCalled();
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("si la API de logout falla o rechaza, el cierre local no se rompe", async () => {
|
|
153
|
+
setSession("jwt", "r");
|
|
154
|
+
vi.spyOn(global, "logout").mockRejectedValue(new Error("boom"));
|
|
155
|
+
|
|
156
|
+
expect(() => appActions.logout()).not.toThrow();
|
|
157
|
+
await Promise.resolve();
|
|
158
|
+
expect(getSession()).toBeNull();
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("un 401 del logout no vuelve a despachar el cierre de sesión", async () => {
|
|
162
|
+
setSession("jwt", "r");
|
|
163
|
+
vi.spyOn(global, "logout").mockResolvedValue({ status: 401 } as any);
|
|
164
|
+
|
|
165
|
+
appActions.logout();
|
|
166
|
+
await Promise.resolve();
|
|
167
|
+
|
|
168
|
+
expect(onExpired).not.toHaveBeenCalled();
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("el reducer vacía acceso, refresh y versión", () => {
|
|
172
|
+
const state = appReducer(
|
|
173
|
+
{ ...appReducer(undefined, { type: "x" }), token: "t", refreshToken: "r", sessionUpdatedAt: 9 } as Parameters<
|
|
174
|
+
typeof appReducer
|
|
175
|
+
>[0],
|
|
176
|
+
appActions.logout(),
|
|
177
|
+
);
|
|
178
|
+
expect(state).toMatchObject({ token: "", refreshToken: "", sessionUpdatedAt: 0 });
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
describe("logoutAndNavigate", () => {
|
|
183
|
+
it("navega a /logout y marca la sesión como cerrada (un cierre remoto no lanza otro)", async () => {
|
|
184
|
+
const push = vi.spyOn(history, "push").mockImplementation(() => undefined);
|
|
185
|
+
|
|
186
|
+
await appActions.logoutAndNavigate()(vi.fn());
|
|
187
|
+
|
|
188
|
+
expect(push).toHaveBeenCalledWith("/logout", undefined);
|
|
189
|
+
// después de un cierre manual/remoto, un 401 en vuelo no vuelve a despachar
|
|
190
|
+
const { endSession } = await import("../../../api/session");
|
|
191
|
+
endSession();
|
|
192
|
+
expect(onExpired).not.toHaveBeenCalled();
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
describe("logoutAndEndCentralSession (botón «Logout» del menú)", () => {
|
|
197
|
+
const URL_OK = "https://api.uni.es/sso/logout";
|
|
198
|
+
let assign: Mock<(url: string) => void>;
|
|
199
|
+
let push: ReturnType<typeof vi.spyOn>;
|
|
200
|
+
|
|
201
|
+
const state = (GriddoIdEnabled: boolean) => () => ({ app: { globalSettings: { GriddoIdEnabled } } }) as never;
|
|
202
|
+
const run = (dispatch = vi.fn(), enabled = true) => appActions.logoutAndEndCentralSession()(dispatch, state(enabled));
|
|
203
|
+
|
|
204
|
+
beforeEach(() => {
|
|
205
|
+
assign = vi.fn();
|
|
206
|
+
vi.stubGlobal("location", { ...window.location, assign });
|
|
207
|
+
push = vi.spyOn(history, "push").mockImplementation(() => undefined);
|
|
208
|
+
setSession("jwt", "r1");
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
afterEach(() => {
|
|
212
|
+
vi.useRealTimers();
|
|
213
|
+
vi.unstubAllGlobals();
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("sesión de Griddo ID: limpia lo local, navega a la URL de cierre central y no pasa por /logout", async () => {
|
|
217
|
+
localStorage.setItem("sitesConfig", "keep");
|
|
218
|
+
localStorage.setItem("other", "drop");
|
|
219
|
+
const logoutApi = vi
|
|
220
|
+
.spyOn(global, "logout")
|
|
221
|
+
.mockResolvedValue({ status: 200, data: { endSessionUrl: URL_OK } } as any);
|
|
222
|
+
const dispatch = vi.fn();
|
|
223
|
+
|
|
224
|
+
await run(dispatch);
|
|
225
|
+
|
|
226
|
+
expect(logoutApi).toHaveBeenCalledTimes(1);
|
|
227
|
+
expect(logoutApi).toHaveBeenCalledWith({ token: "jwt", refreshToken: "r1" });
|
|
228
|
+
expect(assign).toHaveBeenCalledExactlyOnceWith(URL_OK);
|
|
229
|
+
expect(push).not.toHaveBeenCalled();
|
|
230
|
+
expect(getSession()).toBeNull();
|
|
231
|
+
expect(localStorage.getItem("other")).toBeNull();
|
|
232
|
+
expect(localStorage.getItem("sitesConfig")).toBe("keep");
|
|
233
|
+
expect(dispatch).toHaveBeenCalledWith({
|
|
234
|
+
type: LOGOUT,
|
|
235
|
+
payload: { token: "", refreshToken: "", sessionUpdatedAt: 0 },
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("marca la sesión como cerrada: un 401 en vuelo no lanza otro cierre", async () => {
|
|
240
|
+
vi.spyOn(global, "logout").mockResolvedValue({ status: 200, data: { endSessionUrl: URL_OK } } as any);
|
|
241
|
+
|
|
242
|
+
await run();
|
|
243
|
+
const { endSession } = await import("../../../api/session");
|
|
244
|
+
|
|
245
|
+
endSession();
|
|
246
|
+
|
|
247
|
+
expect(onExpired).not.toHaveBeenCalled();
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it.each([
|
|
251
|
+
["204", { status: 204 }],
|
|
252
|
+
["401", { status: 401 }],
|
|
253
|
+
["200 sin endSessionUrl", { status: 200, data: {} }],
|
|
254
|
+
["200 con endSessionUrl vacío", { status: 200, data: { endSessionUrl: "" } }],
|
|
255
|
+
["endSessionUrl que no es una URL", { status: 200, data: { endSessionUrl: "no-es-una-url" } }],
|
|
256
|
+
["endSessionUrl javascript:", { status: 200, data: { endSessionUrl: "javascript:alert(1)" } }],
|
|
257
|
+
["endSessionUrl data:", { status: 200, data: { endSessionUrl: "data:text/html,x" } }],
|
|
258
|
+
["204 con endSessionUrl en el cuerpo", { status: 204, data: { endSessionUrl: URL_OK } }],
|
|
259
|
+
])("%s: limpia lo local y navega a /logout como hoy", async (_name, response) => {
|
|
260
|
+
vi.spyOn(global, "logout").mockResolvedValue(response as any);
|
|
261
|
+
|
|
262
|
+
await run();
|
|
263
|
+
|
|
264
|
+
expect(assign).not.toHaveBeenCalled();
|
|
265
|
+
expect(push).toHaveBeenCalledWith("/logout", undefined);
|
|
266
|
+
expect(getSession()).toBeNull();
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it("si la API rechaza, el cierre local sigue y navega a /logout", async () => {
|
|
270
|
+
vi.spyOn(global, "logout").mockRejectedValue(new Error("Network Error"));
|
|
271
|
+
|
|
272
|
+
await run();
|
|
273
|
+
|
|
274
|
+
expect(assign).not.toHaveBeenCalled();
|
|
275
|
+
expect(push).toHaveBeenCalledWith("/logout", undefined);
|
|
276
|
+
expect(getSession()).toBeNull();
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it("si la API no responde en 5 s, no se queda esperando: cierra lo local y navega a /logout", async () => {
|
|
280
|
+
vi.useFakeTimers();
|
|
281
|
+
vi.spyOn(global, "logout").mockReturnValue(new Promise(() => undefined) as any);
|
|
282
|
+
|
|
283
|
+
const pending = run();
|
|
284
|
+
|
|
285
|
+
await vi.advanceTimersByTimeAsync(4999);
|
|
286
|
+
expect(push).not.toHaveBeenCalled();
|
|
287
|
+
|
|
288
|
+
await vi.advanceTimersByTimeAsync(1);
|
|
289
|
+
await pending;
|
|
290
|
+
|
|
291
|
+
expect(assign).not.toHaveBeenCalled();
|
|
292
|
+
expect(push).toHaveBeenCalledWith("/logout", undefined);
|
|
293
|
+
expect(getSession()).toBeNull();
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it("sin sesión no llama a la API y navega a /logout", async () => {
|
|
297
|
+
resetSessionState();
|
|
298
|
+
const logoutApi = vi.spyOn(global, "logout").mockResolvedValue({ status: 204 } as any);
|
|
299
|
+
|
|
300
|
+
await run();
|
|
301
|
+
|
|
302
|
+
expect(logoutApi).not.toHaveBeenCalled();
|
|
303
|
+
expect(assign).not.toHaveBeenCalled();
|
|
304
|
+
expect(push).toHaveBeenCalledWith("/logout", undefined);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it("una URL http también se acepta (entornos locales)", async () => {
|
|
308
|
+
vi.spyOn(global, "logout").mockResolvedValue({
|
|
309
|
+
status: 200,
|
|
310
|
+
data: { endSessionUrl: "http://localhost:3001/sso/logout" },
|
|
311
|
+
} as any);
|
|
312
|
+
|
|
313
|
+
await run();
|
|
314
|
+
|
|
315
|
+
expect(assign).toHaveBeenCalledExactlyOnceWith("http://localhost:3001/sso/logout");
|
|
316
|
+
});
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
describe("logoutAndEndCentralSession sin Griddo ID (Comillas o contraseña): como en producción", () => {
|
|
320
|
+
let assign: Mock<(url: string) => void>;
|
|
321
|
+
let push: ReturnType<typeof vi.spyOn>;
|
|
322
|
+
|
|
323
|
+
const run = () =>
|
|
324
|
+
appActions.logoutAndEndCentralSession()(vi.fn(), (() => ({
|
|
325
|
+
app: { globalSettings: { GriddoIdEnabled: false } },
|
|
326
|
+
})) as never);
|
|
327
|
+
|
|
328
|
+
beforeEach(() => {
|
|
329
|
+
assign = vi.fn();
|
|
330
|
+
vi.stubGlobal("location", { ...window.location, assign });
|
|
331
|
+
push = vi.spyOn(history, "push").mockImplementation(() => undefined);
|
|
332
|
+
setSession("jwt", "r1");
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
afterEach(() => vi.unstubAllGlobals());
|
|
336
|
+
|
|
337
|
+
it("navega a /logout sin esperar a la API (aunque no responda) y no llama a la API desde la acción", async () => {
|
|
338
|
+
const logoutApi = vi.spyOn(global, "logout").mockReturnValue(new Promise(() => undefined) as any);
|
|
339
|
+
|
|
340
|
+
await run();
|
|
341
|
+
|
|
342
|
+
expect(push).toHaveBeenCalledWith("/logout", undefined);
|
|
343
|
+
expect(logoutApi).not.toHaveBeenCalled();
|
|
344
|
+
expect(assign).not.toHaveBeenCalled();
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it("no usa endSessionUrl aunque la API lo devolviera", async () => {
|
|
348
|
+
vi.spyOn(global, "logout").mockResolvedValue({
|
|
349
|
+
status: 200,
|
|
350
|
+
data: { endSessionUrl: "https://api.uni.es/sso/logout" },
|
|
351
|
+
} as any);
|
|
352
|
+
|
|
353
|
+
await run();
|
|
354
|
+
|
|
355
|
+
expect(assign).not.toHaveBeenCalled();
|
|
356
|
+
expect(push).toHaveBeenCalledWith("/logout", undefined);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it("marca la sesión como cerrada (un 401 en vuelo no lanza otro cierre)", async () => {
|
|
360
|
+
await run();
|
|
361
|
+
const { endSession } = await import("../../../api/session");
|
|
362
|
+
|
|
363
|
+
endSession();
|
|
364
|
+
|
|
365
|
+
expect(onExpired).not.toHaveBeenCalled();
|
|
366
|
+
});
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
describe("estado de sesión en el reducer", () => {
|
|
370
|
+
it("SET_TOKEN con par y versión se funde en el estado", () => {
|
|
371
|
+
const state = appReducer(undefined, {
|
|
372
|
+
type: SET_TOKEN,
|
|
373
|
+
payload: { token: "t", refreshToken: "r", sessionUpdatedAt: 4 },
|
|
374
|
+
});
|
|
375
|
+
expect(state).toMatchObject({ token: "t", refreshToken: "r", sessionUpdatedAt: 4 });
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
it("el estado inicial no tiene refresh ni versión", () => {
|
|
379
|
+
expect(appReducer(undefined, { type: "x" })).toMatchObject({ token: "", refreshToken: "", sessionUpdatedAt: 0 });
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
it("applySession guarda el par y lo despacha con su versión", () => {
|
|
383
|
+
const dispatch = vi.fn();
|
|
384
|
+
appActions.applySession("a", "b")(dispatch);
|
|
385
|
+
expect(dispatch).toHaveBeenCalledWith({
|
|
386
|
+
type: SET_TOKEN,
|
|
387
|
+
payload: { token: "a", refreshToken: "b", sessionUpdatedAt: getSession()?.updatedAt },
|
|
388
|
+
});
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it("applySession sin refresh despacha cadena vacía", () => {
|
|
392
|
+
const dispatch = vi.fn();
|
|
393
|
+
appActions.applySession("a")(dispatch);
|
|
394
|
+
expect(dispatch.mock.calls[0][0].payload.refreshToken).toBe("");
|
|
395
|
+
expect(getSession()?.refreshToken).toBeNull();
|
|
396
|
+
});
|
|
397
|
+
});
|