refactor: refactor foundation

This commit is contained in:
2026-08-14 09:43:19 +03:00
parent 2e32d226d3
commit da585b157a
54 changed files with 4822 additions and 535 deletions
+37 -24
View File
@@ -1,11 +1,10 @@
import { HttpResponse, http } from "msw";
import { afterEach, describe, expect, it, vi } from "vitest";
import { server } from "@/test/server";
import { apiClient, configureAuthClient } from "./client";
describe("apiClient authentication", () => {
afterEach(() => {
configureAuthClient(null);
vi.unstubAllGlobals();
});
afterEach(() => configureAuthClient(null));
it("refreshes once and retries an unauthorized protected request", async () => {
let token = "expired";
@@ -13,35 +12,31 @@ describe("apiClient authentication", () => {
token = "fresh";
return true;
});
const fetch = vi
.fn()
.mockResolvedValueOnce(
new Response(JSON.stringify({ code: "UNAUTHORIZED" }), { status: 401 }),
)
.mockResolvedValueOnce(new Response(JSON.stringify([]), { status: 200 }));
vi.stubGlobal("fetch", fetch);
const authorization: string[] = [];
server.use(
http.get("*/api/v1/brokerage-accounts", ({ request }) => {
authorization.push(request.headers.get("Authorization") ?? "");
return authorization.length === 1
? HttpResponse.json({ code: "UNAUTHORIZED" }, { status: 401 })
: HttpResponse.json([]);
}),
);
configureAuthClient({ accessToken: () => token, refresh, clear: vi.fn() });
const { data } = await apiClient.GET("/api/v1/brokerage-accounts");
expect(data).toEqual([]);
expect(refresh).toHaveBeenCalledOnce();
expect(fetch.mock.calls[0][0].headers.get("Authorization")).toBe(
"Bearer expired",
);
expect(fetch.mock.calls[1][0].headers.get("Authorization")).toBe(
"Bearer fresh",
);
expect(authorization).toEqual(["Bearer expired", "Bearer fresh"]);
});
it("clears the session when refresh cannot restore it", async () => {
const clear = vi.fn();
const fetch = vi
.fn()
.mockResolvedValue(
new Response(JSON.stringify({ code: "UNAUTHORIZED" }), { status: 401 }),
);
vi.stubGlobal("fetch", fetch);
server.use(
http.get("*/api/v1/brokerage-accounts", () =>
HttpResponse.json({ code: "UNAUTHORIZED" }, { status: 401 }),
),
);
configureAuthClient({
accessToken: () => "expired",
refresh: async () => false,
@@ -51,6 +46,24 @@ describe("apiClient authentication", () => {
await apiClient.GET("/api/v1/brokerage-accounts");
expect(clear).toHaveBeenCalledOnce();
expect(fetch).toHaveBeenCalledOnce();
});
it("does not attach Bearer auth to authentication endpoints", async () => {
let authorization: string | null = null;
server.use(
http.post("*/api/v1/auth/refresh", ({ request }) => {
authorization = request.headers.get("Authorization");
return HttpResponse.json({ code: "UNAUTHORIZED" }, { status: 401 });
}),
);
configureAuthClient({
accessToken: () => "token",
refresh: vi.fn(),
clear: vi.fn(),
});
await apiClient.POST("/api/v1/auth/refresh");
expect(authorization).toBeNull();
});
});
+5 -1
View File
@@ -1,8 +1,10 @@
import createClient from "openapi-fetch";
import createQueryClient from "openapi-react-query";
import { apiBaseUrl } from "./config";
import type { paths } from "./generated/schema";
const apiBaseUrl = import.meta.env.VITE_API_BASE_URL ?? "http://localhost:3000";
type AuthHandlers = {
accessToken: () => string | null;
refresh: () => Promise<boolean>;
@@ -56,3 +58,5 @@ export const apiClient = createClient<paths>({
credentials: "include",
fetch: authenticatedFetch,
});
export const apiQueryClient = createQueryClient(apiClient);
-2
View File
@@ -1,2 +0,0 @@
export const apiBaseUrl =
import.meta.env.VITE_API_BASE_URL ?? "http://localhost:3000";
+1 -2
View File
@@ -1,2 +1 @@
export { apiClient, configureAuthClient } from "./client";
export { apiBaseUrl } from "./config";
export { apiClient, apiQueryClient, configureAuthClient } from "./client";
+936
View File
@@ -0,0 +1,936 @@
{
"openapi": "3.0.0",
"paths": {
"/api/v1/auth/register": {
"post": {
"operationId": "AuthController_register",
"parameters": [],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CredentialsDto"
}
}
}
},
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/AuthenticationDto"
}
}
}
},
"400": {
"description": "Invalid request",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"401": {
"description": "Authentication required",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"404": {
"description": "Resource not found",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"500": {
"description": "Internal server error",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
}
},
"tags": [
"auth"
]
}
},
"/api/v1/auth/login": {
"post": {
"operationId": "AuthController_login",
"parameters": [],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CredentialsDto"
}
}
}
},
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/AuthenticationDto"
}
}
}
},
"400": {
"description": "Invalid request",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"401": {
"description": "Authentication required",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"404": {
"description": "Resource not found",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"500": {
"description": "Internal server error",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
}
},
"tags": [
"auth"
]
}
},
"/api/v1/auth/refresh": {
"post": {
"operationId": "AuthController_refresh",
"parameters": [],
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/AuthenticationDto"
}
}
}
},
"400": {
"description": "Invalid request",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"401": {
"description": "Authentication required",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"404": {
"description": "Resource not found",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"500": {
"description": "Internal server error",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
}
},
"tags": [
"auth"
]
}
},
"/api/v1/auth/logout": {
"post": {
"operationId": "AuthController_logout",
"parameters": [],
"responses": {
"204": {
"description": ""
},
"400": {
"description": "Invalid request",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"401": {
"description": "Authentication required",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"404": {
"description": "Resource not found",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"500": {
"description": "Internal server error",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
}
},
"tags": [
"auth"
]
}
},
"/api/v1/auth/me": {
"get": {
"operationId": "AuthController_me",
"parameters": [],
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/UserDto"
}
}
}
},
"400": {
"description": "Invalid request",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"401": {
"description": "Authentication required",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"404": {
"description": "Resource not found",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"500": {
"description": "Internal server error",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
}
},
"security": [
{
"bearer": []
}
],
"tags": [
"auth"
]
}
},
"/api/v1/tbank-connection": {
"get": {
"operationId": "TbankConnectionController_status",
"parameters": [],
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/TbankConnectionStatusDto"
}
}
}
},
"400": {
"description": "Invalid request",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"401": {
"description": "Authentication required",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"404": {
"description": "Resource not found",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"500": {
"description": "Internal server error",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
}
},
"security": [
{
"bearer": []
}
],
"tags": [
"tbank-connection"
]
},
"put": {
"operationId": "TbankConnectionController_put",
"parameters": [],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/PutTbankConnectionDto"
}
}
}
},
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/TbankConnectionStatusDto"
}
}
}
},
"400": {
"description": "Invalid request",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"401": {
"description": "Authentication required",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"404": {
"description": "Resource not found",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"500": {
"description": "Internal server error",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
}
},
"security": [
{
"bearer": []
}
],
"tags": [
"tbank-connection"
]
},
"delete": {
"operationId": "TbankConnectionController_delete",
"parameters": [],
"responses": {
"204": {
"description": ""
},
"400": {
"description": "Invalid request",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"401": {
"description": "Authentication required",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"404": {
"description": "Resource not found",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"500": {
"description": "Internal server error",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
}
},
"security": [
{
"bearer": []
}
],
"tags": [
"tbank-connection"
]
}
},
"/api/v1/brokerage-accounts": {
"get": {
"operationId": "BrokerageAccountsController_list",
"parameters": [],
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/BrokerageAccountDto"
}
}
}
}
},
"400": {
"description": "Invalid request",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"401": {
"description": "Authentication required",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"404": {
"description": "Resource not found",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"500": {
"description": "Internal server error",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
}
},
"security": [
{
"bearer": []
}
],
"tags": [
"brokerage-accounts"
]
}
},
"/api/v1/brokerage-accounts/{accountId}/portfolio": {
"get": {
"operationId": "PortfolioController_get",
"parameters": [
{
"name": "accountId",
"required": true,
"in": "path",
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/PortfolioDto"
}
}
}
},
"400": {
"description": "Invalid request",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"401": {
"description": "Authentication required",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"404": {
"description": "Resource not found",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
},
"500": {
"description": "Internal server error",
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetailsDto"
}
}
}
}
},
"security": [
{
"bearer": []
}
],
"tags": [
"brokerage-accounts"
]
}
}
},
"info": {
"title": "WealthLedger API",
"description": "",
"version": "v1",
"contact": {}
},
"tags": [],
"servers": [],
"components": {
"schemas": {
"ProblemDetailsDto": {
"type": "object",
"properties": {
"status": {
"type": "number",
"example": 401
},
"code": {
"type": "string",
"example": "UNAUTHORIZED"
},
"errors": {
"example": [
{
"pointer": "#/email",
"detail": "must be a valid email address"
}
],
"type": "array",
"items": {
"type": "string"
}
}
},
"required": [
"status",
"code"
]
},
"CredentialsDto": {
"type": "object",
"properties": {
"email": {
"type": "string",
"example": "user@example.com"
},
"password": {
"type": "string",
"minLength": 12,
"maxLength": 72,
"example": "a secure password"
}
},
"required": [
"email",
"password"
]
},
"UserDto": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"email": {
"type": "string"
}
},
"required": [
"id",
"email"
]
},
"AuthenticationDto": {
"type": "object",
"properties": {
"accessToken": {
"type": "string"
},
"user": {
"$ref": "#/components/schemas/UserDto"
}
},
"required": [
"accessToken",
"user"
]
},
"TbankConnectionStatusDto": {
"type": "object",
"properties": {
"connected": {
"type": "boolean"
},
"connectedAt": {
"type": "object",
"nullable": true,
"format": "date-time"
}
},
"required": [
"connected",
"connectedAt"
]
},
"PutTbankConnectionDto": {
"type": "object",
"properties": {
"token": {
"type": "string",
"writeOnly": true
}
},
"required": [
"token"
]
},
"BrokerageAccountDto": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"type": {
"type": "string",
"enum": [
"BROKERAGE",
"INDIVIDUAL_INVESTMENT",
"INVEST_BOX",
"MONEY_MARKET_FUND"
]
},
"openedOn": {
"type": "string",
"nullable": true,
"format": "date"
}
},
"required": [
"id",
"name",
"type",
"openedOn"
]
},
"MoneyDto": {
"type": "object",
"properties": {
"currency": {
"type": "string"
},
"value": {
"type": "string"
}
},
"required": [
"currency",
"value"
]
},
"PortfolioPositionDto": {
"type": "object",
"properties": {
"figi": {
"type": "string",
"nullable": true
},
"displayName": {
"type": "string"
},
"type": {
"type": "string",
"enum": [
"STOCK",
"BOND",
"FUND",
"CURRENCY",
"FUTURE",
"OPTION",
"STRUCTURED_NOTE",
"DIGITAL_FINANCIAL_ASSET",
"OTHER"
]
},
"quantity": {
"type": "string"
},
"currentValue": {
"nullable": true,
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/MoneyDto"
}
]
},
"averagePurchasePrice": {
"nullable": true,
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/MoneyDto"
}
]
},
"absoluteReturn": {
"nullable": true,
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/MoneyDto"
}
]
}
},
"required": [
"figi",
"displayName",
"type",
"quantity",
"currentValue",
"averagePurchasePrice",
"absoluteReturn"
]
},
"PortfolioDto": {
"type": "object",
"properties": {
"lastSuccessfulAt": {
"type": "string",
"format": "date-time"
},
"isStale": {
"type": "boolean"
},
"totalsByCurrency": {
"type": "array",
"items": {
"$ref": "#/components/schemas/MoneyDto"
}
},
"positions": {
"type": "array",
"items": {
"$ref": "#/components/schemas/PortfolioPositionDto"
}
},
"cashBalances": {
"type": "array",
"items": {
"$ref": "#/components/schemas/MoneyDto"
}
}
},
"required": [
"lastSuccessfulAt",
"isStale",
"totalsByCurrency",
"positions",
"cashBalances"
]
}
}
}
}
+2 -3
View File
@@ -1,7 +1,6 @@
import { MantineProvider } from "@mantine/core";
import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it } from "vitest";
import { theme } from "../../app/theme";
import { StateView } from "./StateView";
describe("StateView", () => {
@@ -9,7 +8,7 @@ describe("StateView", () => {
it("renders an accessible loading state with a loader", () => {
render(
<MantineProvider theme={theme}>
<MantineProvider>
<StateView loading text="Пожалуйста, подождите." title="Загрузка" />
</MantineProvider>,
);
@@ -21,7 +20,7 @@ describe("StateView", () => {
it("renders an accessible error state", () => {
render(
<MantineProvider theme={theme}>
<MantineProvider>
<StateView
error
text="Попробуйте обновить страницу позже."
+2
View File
@@ -0,0 +1,2 @@
export { AppShell } from "./AppShell";
export { Button, StateView } from "./StateView";