/** * ================================================================ * Teacher Login E2E Tests * ================================================================ * Tests for teacher (成长规划师) role authentication and multi-role capability * * Seed data: teacher userId=1005 phone=13800000001 * ================================================================ */ var BASE_URL = process.env.PW_BASE_URL || 'http://cfc.iwintrue.com'; var test = require('../../../helpers/auth'); test.describe('Teacher Login Tests', function() { var auth; test.beforeAll(async function() { // Login as teacher before all tests auth = await test.loginAs(browser, 'teacher'); }); test('Scene 1: Teacher login - verify token and role', async function() { // Verify token is obtained if (!auth.token) { throw new Error('Failed to obtain token for teacher login'); } // Verify role is teacher if (auth.role !== 'teacher') { throw new Error('Expected role to be teacher but got ' + auth.role); } // Verify userId matches seed data if (auth.userId !== 1005) { throw new Error('Expected userId 1005 but got ' + auth.userId); } console.log('Teacher login successful: token=' + auth.token.substring(0, 20) + '..., role=' + auth.role); }); test('Scene 2: Teacher has multi-role capability - check roles field contains teacher and parent', async function() { // Make API call to get user info with roles var context = await browser.newContext(); var page = await context.newPage(); // Set token in localStorage await page.goto(BASE_URL, { waitUntil: 'networkidle', timeout: 30000 }); await page.evaluate(function(token) { localStorage.setItem('token', token); localStorage.setItem('access_token', token); }, auth.token); // Call user info API to get roles var response = await page.request.get(BASE_URL + '/api/user/info', { headers: { Authorization: 'Bearer ' + auth.token, 'Content-Type': 'application/json' } }); var body = await response.json(); // Verify response structure if (body.code !== 200) { throw new Error('Failed to get user info: ' + (body.message || 'Unknown error')); } var userData = body.data; // Check if roles field exists and contains teacher and parent var roles = userData.roles || userData.roleList || []; // If roles is a string, convert to array if (typeof roles === 'string') { roles = roles.split(','); } // Verify teacher role exists var hasTeacherRole = false; var hasParentRole = false; for (var i = 0; i < roles.length; i++) { if (roles[i] === 'teacher') { hasTeacherRole = true; } if (roles[i] === 'parent') { hasParentRole = true; } } if (!hasTeacherRole) { throw new Error('Teacher role not found in roles: ' + JSON.stringify(roles)); } if (!hasParentRole) { throw new Error('Parent role not found in roles: ' + JSON.stringify(roles)); } console.log('Teacher multi-role verified: roles=' + JSON.stringify(roles)); await context.close(); }); }); module.exports = {};