auth-routes.integration.test.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 { tenantRoutes } from '../../src/routes';
  5. // 设置集成测试钩子
  6. setupIntegrationDatabaseHooksWithEntities([])
  7. describe('租户认证API集成测试', () => {
  8. let client: ReturnType<typeof testClient<typeof tenantRoutes>>;
  9. beforeEach(async () => {
  10. // 创建测试客户端
  11. client = testClient(tenantRoutes);
  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. if (response.status === 200) {
  23. const data = await response.json();
  24. expect(data.token).toBeDefined();
  25. expect(data.userId).toBe(1);
  26. expect(data.username).toBe('superadmin');
  27. expect(data.message).toBe('登录成功');
  28. }
  29. });
  30. it('应该拒绝错误的用户名', async () => {
  31. const response = await client.login.$post({
  32. json: {
  33. username: 'wronguser',
  34. password: 'admin123'
  35. }
  36. });
  37. expect(response.status).toBe(401);
  38. const data = await response.json();
  39. expect(data.message).toBe('用户名或密码错误');
  40. });
  41. it('应该拒绝错误的密码', async () => {
  42. const response = await client.login.$post({
  43. json: {
  44. username: 'superadmin',
  45. password: 'wrongpassword'
  46. }
  47. });
  48. expect(response.status).toBe(401);
  49. const data = await response.json();
  50. expect(data.message).toBe('用户名或密码错误');
  51. });
  52. it('应该拒绝空的用户名', async () => {
  53. const response = await client.login.$post({
  54. json: {
  55. username: '',
  56. password: 'admin123'
  57. }
  58. });
  59. expect(response.status).toBe(400);
  60. });
  61. it('应该拒绝空的密码', async () => {
  62. const response = await client.login.$post({
  63. json: {
  64. username: 'superadmin',
  65. password: ''
  66. }
  67. });
  68. expect(response.status).toBe(400);
  69. });
  70. });
  71. });