auth.test.ts
1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { describe, it, expect } from 'vitest';
import { authApi } from './auth';
import { BizError } from './errors';
describe('authApi.login', () => {
it('returns LoginVo on success', async () => {
const vo = await authApi.login({
username: 'alice',
password: 'Password1!',
companyCode: 'HQ',
});
expect(vo.accessToken).toBe('fake-jwt');
expect(vo.userInfo.username).toBe('alice');
expect(vo.userInfo.employeeName).toBe('张三');
expect(vo.expiresInSec).toBe(7200);
});
it('throws BizError 40101 on bad credentials', async () => {
await expect(
authApi.login({ username: 'alice', password: 'WRONG', companyCode: 'HQ' }),
).rejects.toMatchObject({ code: 40101 });
});
it('throws BizError 42301 with lockUntil data on locked account', async () => {
try {
await authApi.login({ username: 'locked', password: 'X', companyCode: 'HQ' });
throw new Error('expected throw');
} catch (e) {
expect(e).toBeInstanceOf(BizError);
const be = e as BizError;
expect(be.code).toBe(42301);
expect((be.data as { lockUntil: string }).lockUntil).toBe('2030-01-01T12:00:00');
}
});
it('throws BizError 40103 on deleted account', async () => {
await expect(
authApi.login({ username: 'deleted', password: 'X', companyCode: 'HQ' }),
).rejects.toMatchObject({ code: 40103 });
});
it('throws BizError 40004 on unknown company', async () => {
await expect(
authApi.login({ username: 'alice', password: 'Password1!', companyCode: 'NOPE' }),
).rejects.toMatchObject({ code: 40004 });
});
});