login.spec.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. var BASE_URL = process.env.PW_BASE_URL || 'http://cfc.iwintrue.com';
  11. var test = require('../../../helpers/auth');
  12. test.describe('Teacher Login Tests', function() {
  13. var auth;
  14. test.beforeAll(async function() {
  15. // Login as teacher before all tests
  16. auth = await test.loginAs(browser, 'teacher');
  17. });
  18. test('Scene 1: Teacher login - verify token and role', async function() {
  19. // Verify token is obtained
  20. if (!auth.token) {
  21. throw new Error('Failed to obtain token for teacher login');
  22. }
  23. // Verify role is teacher
  24. if (auth.role !== 'teacher') {
  25. throw new Error('Expected role to be teacher but got ' + auth.role);
  26. }
  27. // Verify userId matches seed data
  28. if (auth.userId !== 1005) {
  29. throw new Error('Expected userId 1005 but got ' + auth.userId);
  30. }
  31. console.log('Teacher login successful: token=' + auth.token.substring(0, 20) + '..., role=' + auth.role);
  32. });
  33. test('Scene 2: Teacher has multi-role capability - check roles field contains teacher and parent', async function() {
  34. // Make API call to get user info with roles
  35. var context = await browser.newContext();
  36. var page = await context.newPage();
  37. // Set token in localStorage
  38. await page.goto(BASE_URL, { waitUntil: 'networkidle', timeout: 30000 });
  39. await page.evaluate(function(token) {
  40. localStorage.setItem('token', token);
  41. localStorage.setItem('access_token', token);
  42. }, auth.token);
  43. // Call user info API to get roles
  44. var response = await page.request.get(BASE_URL + '/api/user/info', {
  45. headers: {
  46. Authorization: 'Bearer ' + auth.token,
  47. 'Content-Type': 'application/json'
  48. }
  49. });
  50. var body = await response.json();
  51. // Verify response structure
  52. if (body.code !== 200) {
  53. throw new Error('Failed to get user info: ' + (body.message || 'Unknown error'));
  54. }
  55. var userData = body.data;
  56. // Check if roles field exists and contains teacher and parent
  57. var roles = userData.roles || userData.roleList || [];
  58. // If roles is a string, convert to array
  59. if (typeof roles === 'string') {
  60. roles = roles.split(',');
  61. }
  62. // Verify teacher role exists
  63. var hasTeacherRole = false;
  64. var hasParentRole = false;
  65. for (var i = 0; i < roles.length; i++) {
  66. if (roles[i] === 'teacher') {
  67. hasTeacherRole = true;
  68. }
  69. if (roles[i] === 'parent') {
  70. hasParentRole = true;
  71. }
  72. }
  73. if (!hasTeacherRole) {
  74. throw new Error('Teacher role not found in roles: ' + JSON.stringify(roles));
  75. }
  76. if (!hasParentRole) {
  77. throw new Error('Parent role not found in roles: ' + JSON.stringify(roles));
  78. }
  79. console.log('Teacher multi-role verified: roles=' + JSON.stringify(roles));
  80. await context.close();
  81. });
  82. });
  83. module.exports = {};