auth-routes.integration.test.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { describe, it, expect, beforeEach } from 'vitest';
  2. import { testClient } from 'hono/testing';
  3. import { IntegrationTestDatabase, setupIntegrationDatabaseHooksWithEntities } from '@d8d/shared-test-util';
  4. import { authRoutes } from '../../src/routes';
  5. // 设置集成测试钩子
  6. setupIntegrationDatabaseHooksWithEntities([])
  7. describe('租户认证API集成测试', () => {
  8. let client: ReturnType<typeof testClient<typeof authRoutes>>;
  9. beforeEach(async () => {
  10. // 创建测试客户端
  11. client = testClient(authRoutes);
  12. });
  13. describe('POST /login - 登录接口', () => {
  14. it('应该使用正确的账号密码成功登录', async () => {
  15. const response = await client.login.$post({
  16. json: {
  17. username: 'superadmin',
  18. password: 'admin123'
  19. }
  20. });
  21. expect(response.status).toBe(200);
  22. const data = await response.json();
  23. expect(data.token).toBeDefined();
  24. expect(data.userId).toBe(1);
  25. expect(data.username).toBe('superadmin');
  26. expect(data.message).toBe('登录成功');
  27. });
  28. it('应该拒绝错误的用户名', async () => {
  29. const response = await client.login.$post({
  30. json: {
  31. username: 'wronguser',
  32. password: 'admin123'
  33. }
  34. });
  35. expect(response.status).toBe(401);
  36. const data = await response.json();
  37. expect(data.message).toBe('用户名或密码错误');
  38. });
  39. it('应该拒绝错误的密码', async () => {
  40. const response = await client.login.$post({
  41. json: {
  42. username: 'superadmin',
  43. password: 'wrongpassword'
  44. }
  45. });
  46. expect(response.status).toBe(401);
  47. const data = await response.json();
  48. expect(data.message).toBe('用户名或密码错误');
  49. });
  50. it('应该拒绝空的用户名', async () => {
  51. const response = await client.login.$post({
  52. json: {
  53. username: '',
  54. password: 'admin123'
  55. }
  56. });
  57. expect(response.status).toBe(400);
  58. });
  59. it('应该拒绝空的密码', async () => {
  60. const response = await client.login.$post({
  61. json: {
  62. username: 'superadmin',
  63. password: ''
  64. }
  65. });
  66. expect(response.status).toBe(400);
  67. });
  68. });
  69. });