暂无描述

auth.setup.js 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { test as setup } from './restFixture.js';
  2. import { expect } from '@playwright/test';
  3. import fs from 'node:fs';
  4. import dotenv from 'dotenv';
  5. const _PERMISSION_CUSTOMERS_READ = 0x40;
  6. const _ADMINISTRATOR_USERNAME = 'administrator';
  7. let administrator_password;
  8. setup.beforeAll(async ({ playwright }) => {
  9. const envFile = fs.readFileSync('../.env');
  10. const env = dotenv.parse(envFile);
  11. administrator_password = env.IRIS_ADM_PASSWORD;
  12. });
  13. async function authenticate(page, login, password) {
  14. await page.goto('/');
  15. await page.getByRole('textbox', { name: 'Username' }).fill(login);
  16. await page.getByRole('textbox', { name: 'Password' }).fill(password);
  17. await page.getByRole('button', { name: 'Sign in' }).click();
  18. // FIXME: It should be: await page.waitForURL('/dashboard'); No wildcard.
  19. // Wait until the page receives the cookies.
  20. await page.waitForURL('/dashboard*');
  21. const authFile = `./playwright/.auth/${login}.json`;
  22. await page.context().storageState({ path: authFile });
  23. }
  24. setup('authenticate as administrator', async ({ page }) => {
  25. await authenticate(page, _ADMINISTRATOR_USERNAME, administrator_password);
  26. });
  27. setup('authenticate as user with customers read rights', async ({ page, rest }) => {
  28. // TODO when this method is called a second time, all these request will fail
  29. // think about a better ways of doing things, some possible strategies
  30. // - find a way to create a new valid database before and empty the database after
  31. // - find a way to remove elements from the database to roughly get back to the initial state
  32. // - code so that these requests are robust (check the group exists, user exists, link between the two is set...)
  33. // - global setup and teardown? https://playwright.dev/docs/test-global-setup-teardown
  34. let response = await rest.post('/manage/groups/add', {
  35. data: {
  36. group_name: 'group_customers_r',
  37. group_description: 'Group with rights: customers_read',
  38. group_permissions: [_PERMISSION_CUSTOMERS_READ]
  39. }
  40. });
  41. const groupIdentifier = (await response.json()).data.group_id;
  42. const login = 'user_customers_r';
  43. const password = 'aA.1234567890';
  44. response = await rest.post('/manage/users/add', {
  45. data: {
  46. user_name: login,
  47. user_login: login,
  48. user_email: `${login}@eu`,
  49. user_password: password
  50. }
  51. });
  52. const userIdentifier = (await response.json()).data.id;
  53. response = await rest.post(`/manage/users/${userIdentifier}/groups/update`, {
  54. data: {
  55. groups_membership: [groupIdentifier]
  56. }
  57. });
  58. await authenticate(page, login, password);
  59. });