Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions user-interface/authentication/jest.config.js

This file was deleted.

7,684 changes: 1,345 additions & 6,339 deletions user-interface/authentication/package-lock.json

Large diffs are not rendered by default.

11 changes: 4 additions & 7 deletions user-interface/authentication/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"build": "rsbuild build",
"preview": "rsbuild preview",
"compile": "tsc",
"test": "jest"
"test": "rstest"
},
"dependencies": {
"react": "^19.2.3",
Expand All @@ -25,17 +25,14 @@
"@rsbuild/core": "1.7.3",
"@rsbuild/plugin-react": "^1.4.6",
"@rsbuild/plugin-type-check": "^1.3.4",
"@rstest/core": "^0.9.7",
"@testing-library/dom": "^10.4.1",
"@testing-library/jest-dom": "^6.9.1",
"@testing-library/react": "^16.3.2",
"@types/jest": "^30.0.0",
"@types/node": "^25.3.2",
"@types/react": "^19.2.14",
"@types/react-dom": "^19.2.3",
"jest": "^30.3.0",
"jest-environment-jsdom": "^30.3.0",
"jest-fetch-mock": "^3.0.3",
"ts-jest": "^29.4.9",
"typescript": "^5.9.3"
"happy-dom": "^20.8.9",
"typescript": "^6.0.2"
}
}
8 changes: 8 additions & 0 deletions user-interface/authentication/rstest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { pluginReact } from '@rsbuild/plugin-react';
import { defineConfig } from '@rstest/core';

export default defineConfig({
plugins: [pluginReact()],
testEnvironment: 'happy-dom',
setupFiles: ['./rstest.setup.mts'],
});
10 changes: 10 additions & 0 deletions user-interface/authentication/rstest.setup.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { afterEach, expect } from '@rstest/core';
import { cleanup } from '@testing-library/react';
import * as jestDomMatchers from '@testing-library/jest-dom/matchers';

expect.extend(jestDomMatchers);

// Clean up after each test to prevent test pollution
afterEach(() => {
cleanup();
});
19 changes: 8 additions & 11 deletions user-interface/authentication/src/LoginForm.test.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import React, {act} from 'react';
import {expect, rs, test} from '@rstest/core';
import {render, screen} from '@testing-library/react';
import LoginForm from './LoginForm';
import fetchMock from "jest-fetch-mock";
import {CsrfToken} from "./api";

test('renders login form', async () => {

fetchMock.mockResponse(JSON.stringify({csrfToken: 'aardvark'}));
rs.mock('./api', () => ({
fetchCsrfToken: (): Promise<CsrfToken> => Promise.resolve({csrfToken: 'aardvark'}),
}));

await act(async () => {
render(<LoginForm />);
});

const loginButton = screen.getByText(/Login/i);
expect(loginButton).toBeInTheDocument();
test('renders login form', async () => {
render(<LoginForm/>);
expect(await screen.findByText(/Login/i)).toBeDefined();
});
25 changes: 5 additions & 20 deletions user-interface/authentication/src/LoginForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {ChangeEvent, FormEvent, MouseEvent} from "react";
import {fetchAuthentication, fetchCsrfToken} from "./api";

declare namespace LoginForm {
export declare namespace LoginForm {

interface Props {
authenticationEndpoint: string,
Expand Down Expand Up @@ -42,13 +43,7 @@ export class LoginForm extends React.Component<LoginForm.Props, LoginForm.State>
}

async refreshCsrfToken() {
return window
.fetch(this.props.sessionEndpoint, {
method: "GET",
headers: {'accept': 'application/json'},
credentials: "include"
})
.then(response => response.json())
return fetchCsrfToken(this.props.sessionEndpoint)
.then(result => this.setState({csrfToken: result.csrfToken}))
.catch(error => console.error("Get CSRF token error:", error));
}
Expand All @@ -75,18 +70,8 @@ export class LoginForm extends React.Component<LoginForm.Props, LoginForm.State>

// TODO - What if we know we have no CSRF token, now we're doing via extra fetch?

await fetch((this.props.authenticationEndpoint), {
method: "POST",
headers: {'content-type': 'application/json'},
credentials: "include",
body: JSON.stringify({
username: this.state.username,
password: this.state.password.split(""),
csrfToken: this.state.csrfToken
})
})
.then(response => response.json())
.then(result => window.location.reload())
await fetchAuthentication(this.state, this.props.authenticationEndpoint)
.then(_ => window.location.reload())
.catch(exception => console.error("Authentication error:", exception));

// TODO - Handle invalid CSRF token by refreshing the page.
Expand Down
35 changes: 35 additions & 0 deletions user-interface/authentication/src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import LoginForm from "./LoginForm";

export interface CsrfToken {
csrfToken: string
}

export async function fetchCsrfToken(endpoint: string): Promise<CsrfToken> {
return window
.fetch(endpoint, {
method: "GET",
headers: {'accept': 'application/json'},
credentials: "include"
})
.then(response => response.json());
}

export interface AuthenticationResult {
type: "success" | "failure";
username?: string;
}

export async function fetchAuthentication(state: LoginForm.State, endpoint: string): Promise<AuthenticationResult> {
return window
.fetch(endpoint, {
method: "POST",
headers: {'content-type': 'application/json'},
credentials: "include",
body: JSON.stringify({
username: state.username,
password: state.password.split(""),
csrfToken: state.csrfToken
})
})
.then(response => response.json())
}
8 changes: 0 additions & 8 deletions user-interface/authentication/src/setupTests.ts

This file was deleted.

3 changes: 2 additions & 1 deletion user-interface/authentication/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"rootDir": "src",
},
"include": [
"src"
]
],
}
Loading