diff --git a/src/App.test.tsx b/src/App.test.tsx index 31cb23b..417aff0 100644 --- a/src/App.test.tsx +++ b/src/App.test.tsx @@ -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'); @@ -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(); - - 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'); - }); }); diff --git a/src/App.tsx b/src/App.tsx index bff6e06..9501b5d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,7 +2,6 @@ import { useState, useEffect, useCallback, useMemo } from 'react'; import QRCode from 'qrcode'; import { apiService, - mockApiService, updateApiBaseURL, InviteCode, Admin, @@ -22,7 +21,6 @@ import { Sun, Moon, Globe, - Zap, Users, UserPlus, UserMinus, @@ -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(null); const [inviteCount, setInviteCount] = useState(1); @@ -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); @@ -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) { @@ -184,7 +180,7 @@ function App() { } finally { setLoading(false); } - }, [activeService, setAdmins, setError]); + }, [setAdmins, setError]); const handleAddAdmin = async (e: React.FormEvent) => { e.preventDefault(); @@ -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); @@ -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( @@ -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) { @@ -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( @@ -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( @@ -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; @@ -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; @@ -458,38 +452,16 @@ function App() {
-
-
-

Demo Mode

-

- Run locally without a backend -

-
- +
+ + setApiHost(e.target.value)} + />
- {!isDemoMode && ( -
- - setApiHost(e.target.value)} - /> -
- )}
- {isDemoMode && } - {loading ? 'Logging in...' : isDemoMode ? 'Start Demo' : 'Login'} + {loading ? 'Logging in...' : 'Login'}
diff --git a/src/api.test.ts b/src/api.test.ts index b8384cc..816c6bf 100644 --- a/src/api.test.ts +++ b/src/api.test.ts @@ -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 }) => { @@ -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', () => { @@ -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', () => { @@ -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/'); @@ -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'); - }); -}); diff --git a/src/api.ts b/src/api.ts index e228666..a14f8a8 100644 --- a/src/api.ts +++ b/src/api.ts @@ -15,7 +15,6 @@ export const api = axios.create({ export const updateApiBaseURL = (newBaseURL: string) => { api.defaults.baseURL = newBaseURL; }; - export interface InviteCodes { codes: InviteCode[]; cursor?: string | null; @@ -104,171 +103,3 @@ export const apiService = { removeAdmin: (username: string): Promise> => api.delete('/api/admins', { data: { username } }), }; - -export const mockApiService = { - login: async (username: string, _password: string): Promise<{ data: LoginResponse }> => { - await new Promise((resolve) => setTimeout(resolve, 500)); - if (username === 'new-user') { - return { - data: { - username: 'new-user', - otp_enabled: false, - otp_verified: false, - otp_auth_url: 'otpauth://totp/InviteCode:new-user?secret=MOCKSECRET&issuer=InviteCode', - }, - }; - } - return { - data: { - username: 'demo-user', - otp_enabled: true, - otp_verified: true, - otp_auth_url: null, - }, - }; - }, - - 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[] = [ - { - code: 'DEMO-123', - available: 1, - disabled: false, - forAccount: 'demo-user', - createdBy: 'demo-user', - createdAt: new Date().toISOString(), - uses: [], - }, - { - code: 'USED-456', - available: 0, - disabled: false, - forAccount: 'demo-user', - createdBy: 'admin', - createdAt: new Date(Date.now() - 86400000).toISOString(), - uses: [ - { - usedBy: 'did:plc:mockuser', - usedByHandle: 'mockuser.bsky.social', - usedByEmail: null, - usedAt: new Date().toISOString(), - usedByDeactivated: false, - }, - ], - }, - { - code: 'GONE-321', - available: 0, - disabled: false, - forAccount: 'demo-user', - createdBy: 'admin', - createdAt: new Date(Date.now() - 259200000).toISOString(), - uses: [ - { - usedBy: 'did:plc:mockgone', - usedByHandle: 'mockgone.bsky.social', - usedByEmail: null, - usedAt: new Date(Date.now() - 172800000).toISOString(), - usedByDeactivated: true, - }, - ], - }, - { - code: 'DISABLED-789', - available: 1, - disabled: true, - forAccount: 'demo-user', - createdBy: 'admin', - createdAt: new Date(Date.now() - 172800000).toISOString(), - uses: [], - }, - ]; - return { data: { codes, cursor: null } }; - }, - - createInviteCodes: async (count: number): Promise<{ data: void }> => { - await new Promise((resolve) => setTimeout(resolve, 500)); - console.log(`Mock: Created ${count} invite codes`); - return { data: undefined }; - }, - - disableInviteCode: async (code: string): Promise<{ data: void }> => { - await new Promise((resolve) => setTimeout(resolve, 300)); - console.log(`Mock: Disabled code ${code}`); - return { data: undefined }; - }, - - validateOtp: async (_token: string): Promise<{ data: ValidateOtpResponse }> => { - await new Promise((resolve) => setTimeout(resolve, 300)); - return { data: { otp_valid: true } }; - }, - - verifyOtp: async (_token: string): Promise<{ data: VerifyOtpResponse }> => { - await new Promise((resolve) => setTimeout(resolve, 300)); - return { - data: { - otp_verified: true, - user: { - username: 'new-user', - otp_enabled: true, - otp_verified: true, - otp_auth_url: null, - }, - }, - }; - }, - - getAdmins: async (): Promise<{ data: AdminsResponse }> => { - await new Promise((resolve) => setTimeout(resolve, 300)); - const admins: Admin[] = JSON.parse( - localStorage.getItem('mock_admins') || - JSON.stringify([ - { username: 'admin', otp_enabled: true, otp_verified: true }, - { username: 'demo-user', otp_enabled: true, otp_verified: true }, - ]) - ); - return { data: { status: 'success', admins } }; - }, - - addAdmin: async (username: string): Promise<{ data: AddAdminResponse }> => { - await new Promise((resolve) => setTimeout(resolve, 500)); - const admins = JSON.parse( - localStorage.getItem('mock_admins') || - JSON.stringify([ - { username: 'admin', otp_enabled: true, otp_verified: true }, - { username: 'demo-user', otp_enabled: true, otp_verified: true }, - ]) - ); - if (!admins.find((a: Admin) => a.username === username)) { - admins.push({ username, otp_enabled: false, otp_verified: false }); - localStorage.setItem('mock_admins', JSON.stringify(admins)); - } - return { - data: { - status: 'success', - message: 'Admin user created successfully', - password: 'mock-generated-password-' + Math.random().toString(36).substring(7), - }, - }; - }, - - removeAdmin: async (username: string): Promise<{ data: RemoveAdminResponse }> => { - await new Promise((resolve) => setTimeout(resolve, 300)); - const admins = JSON.parse( - localStorage.getItem('mock_admins') || - JSON.stringify([ - { username: 'admin', otp_enabled: true, otp_verified: true }, - { username: 'demo-user', otp_enabled: true, otp_verified: true }, - ]) - ); - const filtered = admins.filter((a: Admin) => a.username !== username); - localStorage.setItem('mock_admins', JSON.stringify(filtered)); - return { data: { status: 'success', message: 'Admin user removed successfully' } }; - }, -};