auth.test.ts 1.58 KB
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 });
  });
});