setup.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. const {Before, After} = require('cucumber');
  2. const {client} = require('nightwatch-api');
  3. const fetch = require('node-fetch');
  4. let initialUsers = {};
  5. let dolApiKey = '';
  6. const getUsers = async function (api_key = null) {
  7. const header = {};
  8. let dolApiKey;
  9. const url = client.globals.backend_url + 'api/index.php/users';
  10. if (api_key === null) {
  11. dolApiKey = client.globals.dolApiKey;
  12. } else {
  13. dolApiKey = api_key;
  14. }
  15. header['Accept'] = 'application/json';
  16. header['DOLAPIKEY'] = dolApiKey;
  17. await fetch(url, {
  18. method: 'GET',
  19. headers: header
  20. })
  21. .then(async (response) => {
  22. client.globals.response = response;
  23. });
  24. };
  25. const getUsersId = async function () {
  26. const users = {};
  27. await getUsers();
  28. const json_response = await client.globals.response.json();
  29. for (const user of json_response) {
  30. users[user.id] = user.id;
  31. }
  32. return users;
  33. };
  34. const getDolApiKey = async function (login = null, password = null) {
  35. const header = {};
  36. if (login === null && password === null) {
  37. login = client.globals.adminUsername;
  38. password = client.globals.adminPassword;
  39. }
  40. const params = new URLSearchParams();
  41. params.set('login', login);
  42. params.set('password', password);
  43. const apiKey = client.globals.backend_url + `api/index.php/login?${params.toString()}`;
  44. header['Accept'] = 'application/json';
  45. await fetch(apiKey, {
  46. method: 'GET',
  47. headers: header
  48. })
  49. .then(async (response) => {
  50. const jsonResponse = await response.json();
  51. dolApiKey = jsonResponse['success']['token'];
  52. if (login === client.globals.adminUsername && password === client.globals.adminPassword) {
  53. client.globals.dolApiKey = dolApiKey;
  54. }
  55. });
  56. return dolApiKey;
  57. };
  58. Before(async function getAdminDolApiKey() {
  59. await getDolApiKey();
  60. });
  61. Before(async () => {
  62. initialUsers = await getUsersId();
  63. });
  64. After(async () => {
  65. const finalUsers = await getUsersId();
  66. const header = {};
  67. const url = client.globals.backend_url + 'api/index.php/users/';
  68. header['Accept'] = 'application/json';
  69. header['DOLAPIKEY'] = client.globals.dolApiKey;
  70. let found;
  71. for (const finaluser in finalUsers) {
  72. for (const initialuser in initialUsers) {
  73. found = false;
  74. if (initialuser === finaluser) {
  75. found = true;
  76. break;
  77. }
  78. }
  79. if (!found) {
  80. await fetch(url + finaluser, {
  81. method: 'DELETE',
  82. headers: header
  83. })
  84. .then(res => {
  85. if (res.status < 200 || res.status >= 400) {
  86. throw new Error("Failed to delete user: " + res.statusText);
  87. }
  88. });
  89. }
  90. }
  91. });
  92. module.exports = {getDolApiKey, getUsers};