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
20 changes: 1 addition & 19 deletions src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ describe('CSV export', () => {
});
});

describe('theme and demo mode', () => {
describe('theme', () => {
it('toggles dark mode and persists the choice', async () => {
renderLoggedIn();
await expectCodeVisible('UNUSED-CODE');
Expand All @@ -616,23 +616,5 @@ describe('theme and demo mode', () => {
expect(localStorage.getItem('theme')).toBe('dark');
});

it('runs against the mock backend when demo mode is enabled', async () => {
render(<App />);

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 }
);
expect(localStorage.getItem('demo_mode')).toBe('true');
});
});

75 changes: 23 additions & 52 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useState, useEffect, useCallback, useMemo } from 'react';
import QRCode from 'qrcode';
import {
apiService,
mockApiService,
updateApiBaseURL,
InviteCode,
Admin,
Expand All @@ -22,7 +21,6 @@ import {
Sun,
Moon,
Globe,
Zap,
Users,
UserPlus,
UserMinus,
Expand Down Expand Up @@ -84,8 +82,6 @@ function App() {
import.meta.env.VITE_API_HOST ||
'https://frontend.myapp.local/'
);
const [isDemoMode, setIsDemoMode] = useState(localStorage.getItem('demo_mode') === 'true');
const activeService = isDemoMode ? mockApiService : apiService;
const [otpToken, setOtpToken] = useState('');
const [qrCode, setQrCode] = useState<string | null>(null);
const [inviteCount, setInviteCount] = useState(1);
Expand Down Expand Up @@ -139,18 +135,18 @@ function App() {
}, [invites, filter]);

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

const fetchInvites = useCallback(async () => {
setLoading(true);
try {
const response = await activeService.getInviteCodes();
const response = await apiService.getInviteCodes();
const data = response.data?.codes || [];
setInvites(Array.isArray(data) ? data : []);
setError(null);
Expand All @@ -166,12 +162,12 @@ function App() {
} finally {
setLoading(false);
}
}, [activeService, handleLogout]);
}, [handleLogout]);

const fetchAdmins = useCallback(async () => {
setLoading(true);
try {
const response = await activeService.getAdmins();
const response = await apiService.getAdmins();
setAdmins(response.data?.admins || []);
setError(null);
} catch (err: unknown) {
Expand All @@ -184,7 +180,7 @@ function App() {
} finally {
setLoading(false);
}
}, [activeService, setAdmins, setError]);
}, [setAdmins, setError]);

const handleAddAdmin = async (e: React.FormEvent) => {
e.preventDefault();
Expand All @@ -196,7 +192,7 @@ function App() {
setError(null);
setNewAdminPassword(null);
try {
const response = await activeService.addAdmin(trimmedUsername);
const response = await apiService.addAdmin(trimmedUsername);
setNewAdminUsername('');
if (response.data.password) {
setNewAdminPassword(response.data.password);
Expand All @@ -218,7 +214,7 @@ function App() {
setLoading(true);
setError(null);
try {
await activeService.removeAdmin(username);
await apiService.removeAdmin(username);
await fetchAdmins();
} catch (err: unknown) {
setError(
Expand Down Expand Up @@ -248,10 +244,8 @@ function App() {
updateApiBaseURL(normalizedHost);
}

localStorage.setItem('demo_mode', isDemoMode.toString());

try {
const response = await activeService.login(username, password);
const response = await apiService.login(username, password);
if (response.data.otp_verified) {
setPage('QrValidate');
} else if (response.data.otp_auth_url) {
Expand All @@ -278,7 +272,7 @@ function App() {
setLoading(true);
setError(null);
try {
await activeService.createInviteCodes(inviteCount);
await apiService.createInviteCodes(inviteCount);
await fetchInvites();
} catch (err: unknown) {
setError(
Expand All @@ -294,7 +288,7 @@ function App() {
const handleDisableInvite = async (code: string) => {
setError(null);
try {
await activeService.disableInviteCode(code);
await apiService.disableInviteCode(code);
await fetchInvites();
} catch (err: unknown) {
setError(
Expand All @@ -309,7 +303,7 @@ function App() {
setLoading(true);
setError(null);
try {
const response = await activeService.verifyOtp(otpToken);
const response = await apiService.verifyOtp(otpToken);
if (!response.data.otp_verified) {
setError('OTP Verification failed');
return;
Expand All @@ -332,7 +326,7 @@ function App() {
setLoading(true);
setError(null);
try {
const response = await activeService.validateOtp(otpToken);
const response = await apiService.validateOtp(otpToken);
if (!response.data.otp_valid) {
setError('OTP Validation failed');
return;
Expand Down Expand Up @@ -458,38 +452,16 @@ function App() {
</div>

<form onSubmit={handleLogin} className="space-y-4">
<div className="flex items-center gap-3 p-3 bg-blue-50 dark:bg-blue-900/20 border border-blue-100 dark:border-blue-900/30 rounded-lg mb-2">
<div className="flex-1">
<p className="text-sm font-medium text-blue-800 dark:text-blue-300">Demo Mode</p>
<p className="text-xs text-blue-600 dark:text-blue-400">
Run locally without a backend
</p>
</div>
<button
type="button"
onClick={() => setIsDemoMode(!isDemoMode)}
className={`relative inline-flex h-6 w-11 items-center rounded-full transition-colors focus:outline-hidden ring-2 ring-offset-2 ring-transparent focus:ring-blue-500 ${isDemoMode ? 'bg-blue-600' : 'bg-gray-300 dark:bg-gray-600'}`}
role="switch"
aria-checked={isDemoMode}
aria-label="Demo Mode"
>
<span
className={`inline-block h-4 w-4 transform rounded-full bg-white transition-transform ${isDemoMode ? 'translate-x-6' : 'translate-x-1'}`}
/>
</button>
<div className="relative">
<Globe className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-400 w-5 h-5" />
<input
type="text"
placeholder="Backend API Host (e.g. https://api.example.com)"
className="w-full p-3.5 pl-10 border border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-white rounded-lg focus:ring-2 focus:ring-blue-500 outline-hidden transition-all"
value={apiHost}
onChange={(e) => setApiHost(e.target.value)}
/>
</div>
{!isDemoMode && (
<div className="relative">
<Globe className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-400 w-5 h-5" />
<input
type="text"
placeholder="Backend API Host (e.g. https://api.example.com)"
className="w-full p-3.5 pl-10 border border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-white rounded-lg focus:ring-2 focus:ring-blue-500 outline-hidden transition-all"
value={apiHost}
onChange={(e) => setApiHost(e.target.value)}
/>
</div>
)}
<div>
<input
type="text"
Expand Down Expand Up @@ -519,8 +491,7 @@ function App() {
disabled={loading}
className="w-full bg-blue-600 text-white p-3.5 rounded-lg font-bold hover:bg-blue-700 transition shadow-lg shadow-blue-500/20 disabled:opacity-50 active:scale-[0.98] flex items-center justify-center gap-2"
>
{isDemoMode && <Zap className="w-5 h-5" />}
{loading ? 'Logging in...' : isDemoMode ? 'Start Demo' : 'Login'}
{loading ? 'Logging in...' : 'Login'}
</button>
</form>
</div>
Expand Down
53 changes: 10 additions & 43 deletions src/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it, expect, beforeAll, afterEach, afterAll } from 'vitest';
import { setupServer } from 'msw/node';
import { http, HttpResponse } from 'msw';
import { apiService, api, updateApiBaseURL, getBaseURL, mockApiService } from './api';
import { apiService, api, updateApiBaseURL, getBaseURL } from './api';

const handlers = [
http.post('https://frontend.myapp.local/api/auth/login', async ({ request }) => {
Expand All @@ -17,8 +17,9 @@ const handlers = [
return new HttpResponse(null, { status: 401 });
}),

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

http.get('https://frontend.myapp.local/api/invite-codes', () => {
Expand Down Expand Up @@ -55,12 +56,14 @@ const handlers = [
});
}),

http.post('https://frontend.myapp.local/api/create-invite-codes', () =>
new HttpResponse(null, { status: 200 })
http.post(
'https://frontend.myapp.local/api/create-invite-codes',
() => new HttpResponse(null, { status: 200 })
),

http.post('https://frontend.myapp.local/api/disable-invite-codes', () =>
new HttpResponse(null, { status: 200 })
http.post(
'https://frontend.myapp.local/api/disable-invite-codes',
() => new HttpResponse(null, { status: 200 })
),

http.post('https://frontend.myapp.local/api/auth/otp/validate', () => {
Expand Down Expand Up @@ -181,7 +184,6 @@ describe('apiService', () => {
expect(requestedBody).toEqual({ username: 'gone' });
});
});

describe('base URL resolution', () => {
it('getBaseURL falls back to the default host when nothing is configured', () => {
expect(getBaseURL()).toBe('https://frontend.myapp.local/');
Expand All @@ -202,38 +204,3 @@ describe('base URL resolution', () => {
}
});
});

describe('mockApiService', () => {
it('login returns verified admin state for a regular user', async () => {
const response = await mockApiService.login('demo-user', 'pw');
expect(response.data.username).toBe('demo-user');
expect(response.data.otp_verified).toBe(true);
});

it('login returns OTP setup fields for a new user', async () => {
const response = await mockApiService.login('new-user', 'pw');
expect(response.data.otp_enabled).toBe(false);
expect(response.data.otp_auth_url).toContain('otpauth://');
});

it('getInviteCodes returns the demo fixtures', async () => {
const response = await mockApiService.getInviteCodes();
expect(response.data.codes).toHaveLength(4);
expect(response.data.codes.map((c) => c.code)).toContain('DEMO-123');
});

it('addAdmin persists the new admin and returns a password', async () => {
const response = await mockApiService.addAdmin('grace');
expect(response.data.status).toBe('success');
expect(response.data.password).toContain('mock-generated-password-');

const after = await mockApiService.getAdmins();
expect(after.data.admins.map((a) => a.username)).toContain('grace');
});

it('removeAdmin drops the admin from the persisted list', async () => {
await mockApiService.removeAdmin('admin');
const after = await mockApiService.getAdmins();
expect(after.data.admins.map((a) => a.username)).not.toContain('admin');
});
});
Loading
Loading