/** * ================================================================ * E2E Test Utility Functions * ================================================================ * Provides common assertion helpers and test data for Playwright E2E tests * * Usage: * const { assertApiSuccess, assertApiError, authHeaders, getTestData } = require('./helpers/utils'); * const data = assertApiSuccess(response); * await assertApiError(response, 'AUTH_FAILED', 'Invalid token'); * const headers = authHeaders(auth); * const testData = getTestData(); * ================================================================ */ var BASE_URL = process.env.PW_BASE_URL || 'http://cfc.iwintrue.com'; /** * Assert API response indicates success * @param {APIResponse} response - Playwright APIResponse object * @param {number} expectedStatus - Expected HTTP status code (default: 200) * @returns {Promise} - Parsed response body data * @throws {Error} - If status or code is not as expected */ async function assertApiSuccess(response, expectedStatus) { if (expectedStatus === undefined) { expectedStatus = 200; } var status = response.status(); if (status !== expectedStatus) { var body = await response.text(); throw new Error('Expected HTTP status ' + expectedStatus + ' but got ' + status + '. Body: ' + body); } var body; try { body = await response.json(); } catch (e) { var text = await response.text(); throw new Error('Failed to parse JSON response: ' + text); } if (body.code !== 200) { throw new Error('Expected body.code === 200 but got ' + body.code + '. Message: ' + (body.message || 'N/A')); } return body.data; } /** * Assert API response indicates an error * @param {APIResponse} response - Playwright APIResponse object * @param {string} [expectedCode] - Optional expected error code * @param {string} [expectedMessage] - Optional expected error message substring * @throws {Error} - If response indicates success or doesn't match expected error */ async function assertApiError(response, expectedCode, expectedMessage) { var status = response.status(); var body; try { body = await response.json(); } catch (e) { var text = await response.text(); throw new Error('Expected error response but failed to parse JSON: ' + text); } // Error responses typically have non-200 codes or body.code !== 200 if (body.code === 200) { throw new Error('Expected error response but got success. Body: ' + JSON.stringify(body)); } if (expectedCode !== undefined && body.code !== expectedCode) { throw new Error('Expected error code ' + expectedCode + ' but got ' + body.code + '. Message: ' + (body.message || 'N/A')); } if (expectedMessage !== undefined && body.message) { if (body.message.indexOf(expectedMessage) === -1) { throw new Error('Expected error message containing "' + expectedMessage + '" but got "' + body.message + '"'); } } } /** * Build authorization headers from auth object * @param {Object} auth - Auth object with token property * @returns {Object} - Headers object with Authorization and Content-Type */ function authHeaders(auth) { return { Authorization: 'Bearer ' + auth.token, 'Content-Type': 'application/json' }; } /** * Get test data with all seed data IDs * @returns {Object} - Object containing all test user IDs and family IDs */ function getTestData() { return { parentUserId: 1002, childUserId: 1003, child2UserId: 1004, teacherUserId: 1005, nutritionistUserId: 1006, butlerUserId: 1007, assessorUserId: 1008, adminUserId: 1001, vendorUserId: 1009, familyId: 2001, family2Id: 2002 }; } // ================================================================ // Module exports (CommonJS) // ================================================================ module.exports = { assertApiSuccess: assertApiSuccess, assertApiError: assertApiError, authHeaders: authHeaders, getTestData: getTestData, BASE_URL: BASE_URL };