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
43 changes: 43 additions & 0 deletions src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const server = setupServer(
HttpResponse.json({ status: 'success', message: 'ok', password: 'generated-pass' })
),
http.delete(`${API_HOST}/api/admins`, () => HttpResponse.json({ success: true })),
http.post(`${API_HOST}/api/auth/logout`, () => new HttpResponse(null, { status: 204 })),
http.post(`${API_HOST}/api/auth/otp/verify`, () =>
HttpResponse.json({
otp_verified: true,
Expand Down Expand Up @@ -135,6 +136,23 @@ describe('App', () => {
expect(localStorage.getItem('authenticated')).toBeNull();
});

it('invalidates the server session when logging out', async () => {
let logoutRequests = 0;
server.use(
http.post(`${API_HOST}/api/auth/logout`, () => {
logoutRequests += 1;
return new HttpResponse(null, { status: 204 });
})
);

renderLoggedIn();
await expectCodeVisible('UNUSED-CODE');

fireEvent.click(screen.getByTitle('Logout'));

await waitFor(() => expect(logoutRequests).toBe(1));
});

it('fetches invites once on mount and does not refetch when filtering', async () => {
let inviteRequests = 0;
server.use(
Expand Down Expand Up @@ -225,6 +243,31 @@ describe('login flow', () => {
);
});

it('returns to login when OTP setup is cancelled', async () => {
server.use(
http.post(`${API_HOST}/api/auth/login`, () =>
HttpResponse.json({
otp_enabled: false,
otp_verified: false,
username: 'newuser',
otp_auth_url: 'otpauth://totp/InviteCode:x?secret=S',
})
)
);

render(<App />);
fillLogin('newuser');

await waitFor(() =>
expect(screen.getByText('Setup Multi-Factor Authentication')).toBeInTheDocument()
);

fireEvent.click(screen.getByRole('button', { name: 'Cancel' }));

await waitFor(() => expect(screen.getByPlaceholderText('Username')).toBeInTheDocument());
expect(localStorage.getItem('authenticated')).toBeNull();
});

it('shows an error message when login fails', async () => {
server.use(
http.post(`${API_HOST}/api/auth/login`, () => new HttpResponse(null, { status: 401 }))
Expand Down
8 changes: 3 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ function App() {
}, [invites, filter]);

const handleLogout = useCallback(() => {
void activeService.logout().catch(() => undefined);
localStorage.removeItem('authenticated');
setQrCode(null);
setOtpToken('');
setError(null);
setPage('Login');
}, []);
}, [activeService]);

const fetchInvites = useCallback(async () => {
setLoading(true);
Expand Down Expand Up @@ -1054,10 +1055,7 @@ function App() {
{error && <p className="text-red-500 text-sm mb-4">{error}</p>}
<div className="flex flex-col sm:flex-row gap-3">
<button
onClick={() => {
setError(null);
setPage('Home');
}}
onClick={handleLogout}
className="flex-1 p-4 border dark:border-gray-600 dark:text-white rounded-xl font-bold hover:bg-gray-100 dark:hover:bg-gray-700 transition"
>
Cancel
Expand Down
9 changes: 9 additions & 0 deletions src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const handlers = [
return new HttpResponse(null, { status: 401 });
}),

http.post('https://frontend.myapp.local/api/auth/logout', () =>
new HttpResponse(null, { status: 204 })
),

http.get('https://frontend.myapp.local/api/invite-codes', () => {
return HttpResponse.json({
codes: [
Expand Down Expand Up @@ -109,6 +113,11 @@ describe('apiService', () => {
expect(response.data.otp_verified).toBe(true);
});

it('logout should invalidate the session', async () => {
const response = await apiService.logout();
expect(response.status).toBe(204);
});

it('getInviteCodes should return list of invite codes', async () => {
const response = await apiService.getInviteCodes();
expect(response.data.codes).toHaveLength(2);
Expand Down
7 changes: 7 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export const apiService = {
login: (username: string, password: string): Promise<AxiosResponse<LoginResponse>> =>
api.post<LoginResponse>('/api/auth/login', { username, password }),

logout: (): Promise<AxiosResponse<void>> => api.post<void>('/api/auth/logout'),

getInviteCodes: (): Promise<AxiosResponse<InviteCodes>> =>
api.get<InviteCodes>('/api/invite-codes'),

Expand Down Expand Up @@ -126,6 +128,11 @@ export const mockApiService = {
};
},

logout: async (): Promise<{ data: void }> => {
await new Promise((resolve) => setTimeout(resolve, 100));
return { data: undefined };
},

getInviteCodes: async (): Promise<{ data: InviteCodes }> => {
await new Promise((resolve) => setTimeout(resolve, 300));
const codes: InviteCode[] = [
Expand Down
Loading