Skip to content

Commit 5505eb2

Browse files
Copilothotlong
andcommitted
test: add tests for conditional auth behavior
- Add test for AuthProvider with enabled=false (guest mode) - Add test for AuthProvider with enabled=true (normal auth) - Add test for guest session token creation - Fix TypeScript error in AuthProvider (expiresAt Date type) Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent afffa0d commit 5505eb2

2 files changed

Lines changed: 100 additions & 1 deletion

File tree

packages/auth/src/AuthProvider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function AuthProvider({
7575
});
7676
setSession({
7777
token: 'guest-token',
78-
expiresAt: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000).toISOString(), // 1 year
78+
expiresAt: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000), // 1 year
7979
});
8080
setIsLoading(false);
8181
return;
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
import { describe, it, expect, beforeEach } from 'vitest';
10+
import { render, screen, waitFor } from '@testing-library/react';
11+
import { AuthProvider } from '../AuthProvider';
12+
import { useAuth } from '../useAuth';
13+
import { useContext } from 'react';
14+
import { AuthCtx } from '../AuthContext';
15+
16+
// Test component to access auth context
17+
function AuthTestComponent() {
18+
const auth = useAuth();
19+
return (
20+
<div>
21+
<div data-testid="is-authenticated">{String(auth.isAuthenticated)}</div>
22+
<div data-testid="is-loading">{String(auth.isLoading)}</div>
23+
<div data-testid="user-id">{auth.user?.id || 'null'}</div>
24+
<div data-testid="user-name">{auth.user?.name || 'null'}</div>
25+
</div>
26+
);
27+
}
28+
29+
describe('AuthProvider with enabled prop', () => {
30+
it('should bypass authentication when enabled=false', async () => {
31+
render(
32+
<AuthProvider authUrl="/api/auth" enabled={false}>
33+
<AuthTestComponent />
34+
</AuthProvider>
35+
);
36+
37+
// Wait for loading to complete
38+
await waitFor(() => {
39+
expect(screen.getByTestId('is-loading').textContent).toBe('false');
40+
});
41+
42+
// Should be authenticated with guest user
43+
expect(screen.getByTestId('is-authenticated').textContent).toBe('true');
44+
expect(screen.getByTestId('user-id').textContent).toBe('guest');
45+
expect(screen.getByTestId('user-name').textContent).toBe('Guest User');
46+
});
47+
48+
it('should use normal auth flow when enabled=true (default)', async () => {
49+
// Mock client that returns null session
50+
const mockClient = {
51+
getSession: async () => null,
52+
signIn: async () => ({ user: { id: '1', name: 'Test', email: 'test@example.com' }, session: { token: 'token' } }),
53+
signUp: async () => ({ user: { id: '1', name: 'Test', email: 'test@example.com' }, session: { token: 'token' } }),
54+
signOut: async () => {},
55+
updateUser: async () => ({ id: '1', name: 'Test', email: 'test@example.com' }),
56+
forgotPassword: async () => {},
57+
resetPassword: async () => {},
58+
};
59+
60+
render(
61+
<AuthProvider authUrl="/api/auth" client={mockClient}>
62+
<AuthTestComponent />
63+
</AuthProvider>
64+
);
65+
66+
// Wait for loading to complete
67+
await waitFor(() => {
68+
expect(screen.getByTestId('is-loading').textContent).toBe('false');
69+
});
70+
71+
// Should NOT be authenticated (no session)
72+
expect(screen.getByTestId('is-authenticated').textContent).toBe('false');
73+
expect(screen.getByTestId('user-id').textContent).toBe('null');
74+
});
75+
76+
it('should create a guest session with token when auth is disabled', async () => {
77+
function SessionTestComponent() {
78+
const context = useContext(AuthCtx);
79+
return (
80+
<div>
81+
<div data-testid="session-token">{context?.session?.token || 'null'}</div>
82+
<div data-testid="has-expiry">{String(!!context?.session?.expiresAt)}</div>
83+
</div>
84+
);
85+
}
86+
87+
render(
88+
<AuthProvider authUrl="/api/auth" enabled={false}>
89+
<SessionTestComponent />
90+
</AuthProvider>
91+
);
92+
93+
await waitFor(() => {
94+
expect(screen.getByTestId('session-token').textContent).toBe('guest-token');
95+
});
96+
97+
expect(screen.getByTestId('has-expiry').textContent).toBe('true');
98+
});
99+
});

0 commit comments

Comments
 (0)