utils.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * ================================================================
  3. * E2E Test Utility Functions
  4. * ================================================================
  5. * Provides common assertion helpers and test data for Playwright E2E tests
  6. *
  7. * Usage:
  8. * const { assertApiSuccess, assertApiError, authHeaders, getTestData } = require('./helpers/utils');
  9. * const data = assertApiSuccess(response);
  10. * await assertApiError(response, 'AUTH_FAILED', 'Invalid token');
  11. * const headers = authHeaders(auth);
  12. * const testData = getTestData();
  13. * ================================================================
  14. */
  15. var BASE_URL = process.env.PW_BASE_URL || 'http://cfc.iwintrue.com';
  16. /**
  17. * Assert API response indicates success
  18. * @param {APIResponse} response - Playwright APIResponse object
  19. * @param {number} expectedStatus - Expected HTTP status code (default: 200)
  20. * @returns {Promise<Object>} - Parsed response body data
  21. * @throws {Error} - If status or code is not as expected
  22. */
  23. async function assertApiSuccess(response, expectedStatus) {
  24. if (expectedStatus === undefined) {
  25. expectedStatus = 200;
  26. }
  27. var status = response.status();
  28. if (status !== expectedStatus) {
  29. var body = await response.text();
  30. throw new Error('Expected HTTP status ' + expectedStatus + ' but got ' + status + '. Body: ' + body);
  31. }
  32. var body;
  33. try {
  34. body = await response.json();
  35. } catch (e) {
  36. var text = await response.text();
  37. throw new Error('Failed to parse JSON response: ' + text);
  38. }
  39. if (body.code !== 200) {
  40. throw new Error('Expected body.code === 200 but got ' + body.code + '. Message: ' + (body.message || 'N/A'));
  41. }
  42. return body.data;
  43. }
  44. /**
  45. * Assert API response indicates an error
  46. * @param {APIResponse} response - Playwright APIResponse object
  47. * @param {string} [expectedCode] - Optional expected error code
  48. * @param {string} [expectedMessage] - Optional expected error message substring
  49. * @throws {Error} - If response indicates success or doesn't match expected error
  50. */
  51. async function assertApiError(response, expectedCode, expectedMessage) {
  52. var status = response.status();
  53. var body;
  54. try {
  55. body = await response.json();
  56. } catch (e) {
  57. var text = await response.text();
  58. throw new Error('Expected error response but failed to parse JSON: ' + text);
  59. }
  60. // Error responses typically have non-200 codes or body.code !== 200
  61. if (body.code === 200) {
  62. throw new Error('Expected error response but got success. Body: ' + JSON.stringify(body));
  63. }
  64. if (expectedCode !== undefined && body.code !== expectedCode) {
  65. throw new Error('Expected error code ' + expectedCode + ' but got ' + body.code + '. Message: ' + (body.message || 'N/A'));
  66. }
  67. if (expectedMessage !== undefined && body.message) {
  68. if (body.message.indexOf(expectedMessage) === -1) {
  69. throw new Error('Expected error message containing "' + expectedMessage + '" but got "' + body.message + '"');
  70. }
  71. }
  72. }
  73. /**
  74. * Build authorization headers from auth object
  75. * @param {Object} auth - Auth object with token property
  76. * @returns {Object} - Headers object with Authorization and Content-Type
  77. */
  78. function authHeaders(auth) {
  79. return {
  80. Authorization: 'Bearer ' + auth.token,
  81. 'Content-Type': 'application/json'
  82. };
  83. }
  84. /**
  85. * Get test data with all seed data IDs
  86. * @returns {Object} - Object containing all test user IDs and family IDs
  87. */
  88. function getTestData() {
  89. return {
  90. parentUserId: 1002,
  91. childUserId: 1003,
  92. child2UserId: 1004,
  93. teacherUserId: 1005,
  94. nutritionistUserId: 1006,
  95. butlerUserId: 1007,
  96. assessorUserId: 1008,
  97. adminUserId: 1001,
  98. vendorUserId: 1009,
  99. familyId: 2001,
  100. family2Id: 2002
  101. };
  102. }
  103. // ================================================================
  104. // Module exports (CommonJS)
  105. // ================================================================
  106. module.exports = {
  107. assertApiSuccess: assertApiSuccess,
  108. assertApiError: assertApiError,
  109. authHeaders: authHeaders,
  110. getTestData: getTestData,
  111. BASE_URL: BASE_URL
  112. };