Tests for auth refresh, ad viewability and URL safety; nginx, Dockerfile, README
- Jest: single-flight refresh, session expiry vs transient errors, token save on login change, logout revoke, IAB viewability, safe URLs, HTML escaping, password policy, ad session id - test:ci with explicit testMatch (dot-directories in worktree paths hid all tests) - nginx: gzip, nosniff/Referrer-Policy/X-Frame-Options, no-cache for index.html - Dockerfile: npm ci, REACT_APP_WS_ERROR_QUEUE build arg - README: new backend contracts, flags, compatibility notes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
60
src/context/AuthContext.test.jsx
Normal file
60
src/context/AuthContext.test.jsx
Normal file
@@ -0,0 +1,60 @@
|
||||
import { render, act } from "@testing-library/react";
|
||||
import { AuthProvider, useAuthContext } from "./AuthContext";
|
||||
|
||||
let ctx;
|
||||
function Probe() {
|
||||
ctx = useAuthContext();
|
||||
return null;
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
localStorage.clear();
|
||||
global.fetch = jest.fn().mockResolvedValue({ ok: true, status: 204 });
|
||||
});
|
||||
|
||||
describe("AuthContext.updateUser", () => {
|
||||
it("сохраняет новую пару токенов из ответа PUT /users/{id} и не кладёт её в user", () => {
|
||||
localStorage.setItem("token", "old-access");
|
||||
localStorage.setItem("refreshToken", "old-refresh");
|
||||
render(<AuthProvider><Probe /></AuthProvider>);
|
||||
|
||||
act(() => {
|
||||
ctx.updateUser({ id: 1, login: "new-login", token: "new-access", refreshToken: "new-refresh" });
|
||||
});
|
||||
|
||||
expect(localStorage.getItem("token")).toBe("new-access");
|
||||
expect(localStorage.getItem("refreshToken")).toBe("new-refresh");
|
||||
expect(JSON.parse(localStorage.getItem("user"))).toEqual({ id: 1, login: "new-login" });
|
||||
expect(ctx.user).toEqual({ id: 1, login: "new-login" });
|
||||
expect(ctx.token).toBe("new-access");
|
||||
});
|
||||
|
||||
it("без новых токенов (логин не менялся) оставляет прежние", () => {
|
||||
localStorage.setItem("token", "access");
|
||||
localStorage.setItem("refreshToken", "refresh");
|
||||
render(<AuthProvider><Probe /></AuthProvider>);
|
||||
|
||||
act(() => {
|
||||
ctx.updateUser({ id: 1, login: "same", email: null, phone: null });
|
||||
});
|
||||
|
||||
expect(localStorage.getItem("token")).toBe("access");
|
||||
expect(localStorage.getItem("refreshToken")).toBe("refresh");
|
||||
});
|
||||
});
|
||||
|
||||
describe("AuthContext.logout", () => {
|
||||
it("отзывает refresh-токен на сервере и чистит сессию", () => {
|
||||
localStorage.setItem("token", "access");
|
||||
localStorage.setItem("refreshToken", "refresh");
|
||||
render(<AuthProvider><Probe /></AuthProvider>);
|
||||
|
||||
act(() => { ctx.logout(); });
|
||||
|
||||
const [url, init] = global.fetch.mock.calls[0];
|
||||
expect(url).toMatch(/\/api\/v1\/auth\/logout$/);
|
||||
expect(JSON.parse(init.body)).toEqual({ refreshToken: "refresh" });
|
||||
expect(localStorage.getItem("token")).toBeNull();
|
||||
expect(ctx.isAuthenticated).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user