-
Notifications
You must be signed in to change notification settings - Fork 0
Revert "fix: construct User object from flat AuthResponse fields in auth store" #79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -28,10 +28,7 @@ export interface RegisterRequest { | |||||||||||
|
|
||||||||||||
| export interface AuthResponse { | ||||||||||||
| token: string | ||||||||||||
| userId: number | ||||||||||||
| username: string | ||||||||||||
| email: string | ||||||||||||
| role: UserRole | ||||||||||||
| user: User | ||||||||||||
|
||||||||||||
| user: User | |
| userId: number | |
| username: string | |
| email: string | |
| role: UserRole |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,108 +94,6 @@ describe('useAuthStore – SAML session initialisation', () => { | |
| }) | ||
| }) | ||
|
|
||
| describe('useAuthStore – login / register with flat AuthResponse', () => { | ||
| beforeEach(() => { | ||
| setActivePinia(createPinia()) | ||
| localStorage.clear() | ||
| vi.resetAllMocks() | ||
| }) | ||
|
|
||
| const flatLoginResponse = { | ||
| token: 'jwt.login.token', | ||
| userId: 42, | ||
| username: 'test-user', | ||
| email: '[email protected]', | ||
| role: UserRole.USER | ||
| } | ||
|
|
||
| const flatRegisterResponse = { | ||
| token: 'jwt.register.token', | ||
| userId: 99, | ||
| username: 'new-user', | ||
| email: '[email protected]', | ||
| role: UserRole.ADMIN | ||
| } | ||
|
|
||
| it('populates user correctly after login with flat AuthResponse', async () => { | ||
| vi.mocked(authApi.login).mockResolvedValue(flatLoginResponse) | ||
|
|
||
| const auth = useAuthStore() | ||
| await auth.login({ username: 'test-user', password: 'secret' }) | ||
|
|
||
| expect(auth.user).not.toBeNull() | ||
| expect(auth.user?.id).toBe(42) | ||
| expect(auth.user?.username).toBe('test-user') | ||
| expect(auth.user?.email).toBe('[email protected]') | ||
| expect(auth.user?.role).toBe(UserRole.USER) | ||
| expect(auth.token).toBe('jwt.login.token') | ||
| }) | ||
|
|
||
| it('sets isAuthenticated to true after login', async () => { | ||
| vi.mocked(authApi.login).mockResolvedValue(flatLoginResponse) | ||
|
|
||
| const auth = useAuthStore() | ||
| expect(auth.isAuthenticated).toBe(false) | ||
|
|
||
| await auth.login({ username: 'test-user', password: 'secret' }) | ||
|
|
||
| expect(auth.isAuthenticated).toBe(true) | ||
| }) | ||
|
|
||
| it('persists constructed User to localStorage after login', async () => { | ||
| vi.mocked(authApi.login).mockResolvedValue(flatLoginResponse) | ||
|
|
||
| const auth = useAuthStore() | ||
| await auth.login({ username: 'test-user', password: 'secret' }) | ||
|
|
||
| expect(localStorage.getItem('token')).toBe('jwt.login.token') | ||
| const storedUser = JSON.parse(localStorage.getItem('user')!) | ||
| expect(storedUser.id).toBe(42) | ||
| expect(storedUser.username).toBe('test-user') | ||
| expect(storedUser.email).toBe('[email protected]') | ||
| expect(storedUser.role).toBe(UserRole.USER) | ||
| }) | ||
|
|
||
| it('populates user correctly after register with flat AuthResponse', async () => { | ||
| vi.mocked(authApi.register).mockResolvedValue(flatRegisterResponse) | ||
|
|
||
| const auth = useAuthStore() | ||
| await auth.register({ username: 'new-user', email: '[email protected]', password: 'secret123' }) | ||
|
|
||
| expect(auth.user).not.toBeNull() | ||
| expect(auth.user?.id).toBe(99) | ||
| expect(auth.user?.username).toBe('new-user') | ||
| expect(auth.user?.email).toBe('[email protected]') | ||
| expect(auth.user?.role).toBe(UserRole.ADMIN) | ||
| expect(auth.token).toBe('jwt.register.token') | ||
| }) | ||
|
|
||
| it('sets isAuthenticated to true after register', async () => { | ||
| vi.mocked(authApi.register).mockResolvedValue(flatRegisterResponse) | ||
|
|
||
| const auth = useAuthStore() | ||
| expect(auth.isAuthenticated).toBe(false) | ||
|
|
||
| await auth.register({ username: 'new-user', email: '[email protected]', password: 'secret123' }) | ||
|
|
||
| expect(auth.isAuthenticated).toBe(true) | ||
| }) | ||
|
|
||
| it('persists constructed User to localStorage after register', async () => { | ||
| vi.mocked(authApi.register).mockResolvedValue(flatRegisterResponse) | ||
|
|
||
| const auth = useAuthStore() | ||
| await auth.register({ username: 'new-user', email: '[email protected]', password: 'secret123' }) | ||
|
|
||
| expect(localStorage.getItem('token')).toBe('jwt.register.token') | ||
| const storedUser = JSON.parse(localStorage.getItem('user')!) | ||
| expect(storedUser.id).toBe(99) | ||
| expect(storedUser.username).toBe('new-user') | ||
| expect(storedUser.email).toBe('[email protected]') | ||
| expect(storedUser.role).toBe(UserRole.ADMIN) | ||
| }) | ||
| }) | ||
|
|
||
| describe('useAuthStore – mocking the SAML user directly (unit-test style)', () => { | ||
| beforeEach(() => { | ||
| setActivePinia(createPinia()) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
login()andregister()now readresponse.userand persist it. Given the backend auth endpoints currently return flat fields (nouserobject), this will setuser.valuetoundefined, store invalid JSON, and leaveisAuthenticatedfalse after a successful login/register. Consider mapping the backend response into aUser(e.g., reintroduce auserFromAuthResponsetransform or do the mapping in@/api/auth) or change the backend response to includeuser.