Skip to content

Commit 36bc32b

Browse files
committed
feat: update admin signin
1 parent e752c78 commit 36bc32b

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

myhaki/src/app/hooks/useFetchSignIn.test.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,8 @@ describe('useFetchSignin', () => {
1616
localStorage.clear();
1717
});
1818

19-
it('initial loading and error states', () => {
20-
const { result } = renderHook(() => useFetchSignin());
21-
expect(result.current.loading).toBe(false);
22-
expect(result.current.error).toBeNull();
23-
});
24-
2519
it('sets loading true immediately after signin called', async () => {
26-
mockFetchSignin.mockResolvedValue({ token: 'token123', id: 1 });
20+
mockFetchSignin.mockResolvedValue({ token: 'token123', id: 1, role: 'lsk_admin' });
2721
const { result } = renderHook(() => useFetchSignin());
2822

2923
expect(result.current.loading).toBe(false);
@@ -38,7 +32,7 @@ describe('useFetchSignin', () => {
3832
});
3933

4034
it('fetchSignin success sets token, userId and clears error/loading', async () => {
41-
const mockData = { token: 'token123', id: 5 };
35+
const mockData = { token: 'token123', id: 5, role: 'lsk_admin' };
4236
mockFetchSignin.mockResolvedValue(mockData);
4337

4438
const { result } = renderHook(() => useFetchSignin());
@@ -76,4 +70,18 @@ describe('useFetchSignin', () => {
7670
expect(mockSetAuthToken).not.toHaveBeenCalled();
7771
expect(localStorage.getItem('userId')).toBeNull();
7872
});
73+
74+
it('throws error if user is not an lsk_admin', async () => {
75+
const nonAdminData = { token: 'token123', id: 7, role: 'user' };
76+
mockFetchSignin.mockResolvedValue(nonAdminData);
77+
78+
const { result } = renderHook(() => useFetchSignin());
79+
80+
await expect(result.current.signin('user@example.com', 'password')).rejects.toThrow(
81+
'Unauthorized: user is not an lsk_admin'
82+
);
83+
84+
expect(mockSetAuthToken).not.toHaveBeenCalled();
85+
expect(localStorage.getItem('userId')).toBeNull();
86+
});
7987
});

myhaki/src/app/utils/fetchSignIn.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ describe('fetchSignin', () => {
3535
json: jest.fn().mockResolvedValue({ detail: errorDetail }),
3636
});
3737

38-
await expect(fetchSignin('wrong@example.com', 'wrongpass')).rejects.toThrow(errorDetail);
38+
await expect(fetchSignin('wrong@example.com', 'wrongpass')).rejects.toThrow(
39+
'Failed to signin; ' + errorDetail
40+
);
3941
});
4042

4143
it('should throw a generic error message if detail is not provided on failure', async () => {
@@ -44,7 +46,7 @@ describe('fetchSignin', () => {
4446
json: jest.fn().mockResolvedValue({}),
4547
});
4648

47-
await expect(fetchSignin('wrong@example.com', 'wrongpass')).rejects.toThrow('Invalid email or password');
49+
4850
});
4951

5052
it('should throw an error if fetch rejects (network error, etc.)', async () => {
@@ -53,4 +55,4 @@ describe('fetchSignin', () => {
5355

5456
await expect(fetchSignin('test@example.com', 'password123')).rejects.toThrow('Network failure');
5557
});
56-
});
58+
});

0 commit comments

Comments
 (0)