| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- /**
- * ================================================================
- * Teacher Login E2E Tests
- * ================================================================
- * Tests for teacher (成长规划师) role authentication and multi-role capability
- *
- * Seed data: teacher userId=1005 phone=13800000001
- * ================================================================
- */
- const { test, expect } = require('@playwright/test');
- const { loginAs, BASE_URL } = require('../../helpers/auth');
- test.describe('【规划师】登录与多角色', () => {
- var auth;
- test.beforeAll(async ({ browser }) => {
- auth = await loginAs(browser, 'teacher');
- });
- test('[场景1] Teacher login - verify token and role', async () => {
- // 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('[场景2] Teacher has multi-role capability - check roles field contains teacher and parent', async ({ page }) => {
- // Set token in localStorage
- await page.goto(BASE_URL, { waitUntil: 'domcontentloaded', 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.post(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));
- });
- });
|