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
32 changes: 26 additions & 6 deletions src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,18 @@ const fillLogin = (username: string) => {
};

describe('login flow', () => {
it('stores the authenticated session and shows Home on a successful login', async () => {
it('requires OTP validation before loading protected data', async () => {
let inviteRequests = 0;
let adminRequests = 0;
server.use(
http.get(`${API_HOST}/api/invite-codes`, () => {
inviteRequests += 1;
return HttpResponse.json({ codes: invites });
}),
http.get(`${API_HOST}/api/admins`, () => {
adminRequests += 1;
return HttpResponse.json({ admins: [] });
}),
http.post(`${API_HOST}/api/auth/login`, () =>
HttpResponse.json({
otp_enabled: true,
Expand All @@ -195,8 +205,12 @@ describe('login flow', () => {
render(<App />);
fillLogin('admin');

await waitFor(() => expect(localStorage.getItem('authenticated')).toBe('true'));
await expectCodeVisible('UNUSED-CODE');
await waitFor(() =>
expect(screen.getByText('Two-Factor Authentication')).toBeInTheDocument()
);
expect(localStorage.getItem('authenticated')).toBeNull();
expect(inviteRequests).toBe(0);
expect(adminRequests).toBe(0);
});

it('routes to the OTP validation screen when 2FA is required', async () => {
Expand All @@ -205,7 +219,7 @@ describe('login flow', () => {
HttpResponse.json({
username: 'twofa',
otp_enabled: true,
otp_verified: false,
otp_verified: true,
otp_auth_url: null,
})
)
Expand Down Expand Up @@ -452,7 +466,7 @@ describe('OTP verification and validation', () => {
HttpResponse.json({
username: 'twofa',
otp_enabled: true,
otp_verified: false,
otp_verified: true,
otp_auth_url: null,
})
)
Expand All @@ -478,7 +492,7 @@ describe('OTP verification and validation', () => {
HttpResponse.json({
username: 'twofa',
otp_enabled: true,
otp_verified: false,
otp_verified: true,
otp_auth_url: null,
})
),
Expand Down Expand Up @@ -608,6 +622,12 @@ describe('theme and demo mode', () => {
fireEvent.click(screen.getByRole('switch'));
fireEvent.click(screen.getByRole('button', { name: /Start Demo/ }));

await waitFor(() =>
expect(screen.getByText('Two-Factor Authentication')).toBeInTheDocument()
);
fireEvent.change(screen.getByPlaceholderText('000000'), { target: { value: '123456' } });
fireEvent.click(screen.getByRole('button', { name: 'Validate' }));

await waitFor(
() => expect(screen.getAllByText('DEMO-123').length).toBeGreaterThan(0),
{ timeout: 3000 }
Expand Down
18 changes: 8 additions & 10 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,15 @@ function App() {

try {
const response = await activeService.login(username, password);
if (response.data.otp_enabled && !response.data.otp_verified) {
if (response.data.otp_verified) {
setPage('QrValidate');
} else if (!response.data.otp_enabled && !response.data.otp_verified) {
if (response.data.otp_auth_url) {
const qrCodeUrl = await QRCode.toDataURL(response.data.otp_auth_url, {
width: 200,
margin: 1,
});
setQrCode(qrCodeUrl);
setPage('QrVerify');
}
} else if (response.data.otp_auth_url) {
const qrCodeUrl = await QRCode.toDataURL(response.data.otp_auth_url, {
width: 200,
margin: 1,
});
setQrCode(qrCodeUrl);
setPage('QrVerify');
} else {
localStorage.setItem('authenticated', 'true');
setPage('Home');
Expand Down
Loading