login.spec.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * ================================================================
  3. * Teacher Login E2E Tests
  4. * ================================================================
  5. * Tests for teacher (成长规划师) role authentication and multi-role capability
  6. *
  7. * Seed data: teacher userId=1005 phone=13800000001
  8. * ================================================================
  9. */
  10. const { test, expect } = require('@playwright/test');
  11. const { loginAs, BASE_URL } = require('../../helpers/auth');
  12. test.describe('【规划师】登录与多角色', () => {
  13. var auth;
  14. test.beforeAll(async ({ browser }) => {
  15. auth = await loginAs(browser, 'teacher');
  16. });
  17. test('[场景1] Teacher login - verify token and role', async () => {
  18. // Verify token is obtained
  19. if (!auth.token) {
  20. throw new Error('Failed to obtain token for teacher login');
  21. }
  22. // Verify role is teacher
  23. if (auth.role !== 'teacher') {
  24. throw new Error('Expected role to be teacher but got ' + auth.role);
  25. }
  26. // Verify userId matches seed data
  27. if (auth.userId !== 1005) {
  28. throw new Error('Expected userId 1005 but got ' + auth.userId);
  29. }
  30. console.log('Teacher login successful: token=' + auth.token.substring(0, 20) + '..., role=' + auth.role);
  31. });
  32. test('[场景2] Teacher has multi-role capability - check roles field contains teacher and parent', async ({ page }) => {
  33. // Set token in localStorage
  34. await page.goto(BASE_URL, { waitUntil: 'domcontentloaded', timeout: 30000 });
  35. await page.evaluate(function(token) {
  36. localStorage.setItem('token', token);
  37. localStorage.setItem('access_token', token);
  38. }, auth.token);
  39. // Call user info API to get roles
  40. var response = await page.request.post(BASE_URL + '/api/user/info', {
  41. headers: {
  42. Authorization: 'Bearer ' + auth.token,
  43. 'Content-Type': 'application/json'
  44. }
  45. });
  46. var body = await response.json();
  47. // Verify response structure
  48. if (body.code !== 200) {
  49. throw new Error('Failed to get user info: ' + (body.message || 'Unknown error'));
  50. }
  51. var userData = body.data;
  52. // Check if roles field exists and contains teacher and parent
  53. var roles = userData.roles || userData.roleList || [];
  54. // If roles is a string, convert to array
  55. if (typeof roles === 'string') {
  56. roles = roles.split(',');
  57. }
  58. // Verify teacher role exists
  59. var hasTeacherRole = false;
  60. var hasParentRole = false;
  61. for (var i = 0; i < roles.length; i++) {
  62. if (roles[i] === 'teacher') {
  63. hasTeacherRole = true;
  64. }
  65. if (roles[i] === 'parent') {
  66. hasParentRole = true;
  67. }
  68. }
  69. if (!hasTeacherRole) {
  70. throw new Error('Teacher role not found in roles: ' + JSON.stringify(roles));
  71. }
  72. if (!hasParentRole) {
  73. throw new Error('Parent role not found in roles: ' + JSON.stringify(roles));
  74. }
  75. console.log('Teacher multi-role verified: roles=' + JSON.stringify(roles));
  76. });
  77. });