test-data.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import testUsers from './test-users.json' with { type: 'json' };
  2. import roles from './roles.json' with { type: 'json' };
  3. export interface TestUser {
  4. username: string;
  5. password: string;
  6. email: string;
  7. role: string;
  8. }
  9. export interface TestRole {
  10. name: string;
  11. permissions: string[];
  12. description: string;
  13. }
  14. export class TestData {
  15. static getAdminUser(): TestUser {
  16. return testUsers.admin;
  17. }
  18. static getRegularUser(): TestUser {
  19. return testUsers.regularUser;
  20. }
  21. static getInvalidUser(): TestUser {
  22. return testUsers.invalidUser;
  23. }
  24. static generateTestUser(role: string = 'user'): TestUser {
  25. const timestamp = Date.now();
  26. return {
  27. username: `testuser_${timestamp}`,
  28. password: `Test123!_${timestamp}`,
  29. email: `testuser_${timestamp}@example.com`,
  30. role: role
  31. };
  32. }
  33. static getRole(roleName: string): TestRole | undefined {
  34. return roles[roleName as keyof typeof roles];
  35. }
  36. static getAllRoles(): Record<string, TestRole> {
  37. return roles;
  38. }
  39. static getTestUserCredentials(username: string): TestUser | undefined {
  40. return Object.values(testUsers).find(user => user.username === username);
  41. }
  42. static validatePassword(password: string): boolean {
  43. // 密码强度验证:至少8个字符,包含大小写字母和数字
  44. const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/;
  45. return passwordRegex.test(password);
  46. }
  47. static generateRandomString(length: number = 10): string {
  48. const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  49. let result = '';
  50. for (let i = 0; i < length; i++) {
  51. result += chars.charAt(Math.floor(Math.random() * chars.length));
  52. }
  53. return result;
  54. }
  55. static generateTestEmail(): string {
  56. return `test_${Date.now()}@example.com`;
  57. }
  58. static generateTestPhone(): string {
  59. // 生成随机手机号
  60. const prefix = '138';
  61. const suffix = Math.floor(10000000 + Math.random() * 90000000);
  62. return prefix + suffix.toString().substring(0, 8);
  63. }
  64. }
  65. export default TestData;