You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.9 KiB

4 years ago
  1. /*
  2. * A tool for generating a test user session which can be used for debugging configs
  3. * that require sessions.
  4. */
  5. const m = (f) => `${__dirname}/../${f}`;
  6. const fs = require('fs');
  7. const path = require('path');
  8. const querystring = require('querystring');
  9. const request = require(m('src/node_modules/request'));
  10. const settings = require(m('src/node/utils/Settings'));
  11. const supertest = require(m('src/node_modules/supertest'));
  12. (async () => {
  13. const api = supertest(`http://${settings.ip}:${settings.port}`);
  14. const filePath = path.join(__dirname, '../APIKEY.txt');
  15. const apikey = fs.readFileSync(filePath, {encoding: 'utf-8'});
  16. let res;
  17. res = await api.get('/api/');
  18. const apiVersion = res.body.currentVersion;
  19. if (!apiVersion) throw new Error('No version set in API');
  20. const uri = (cmd, args) => `/api/${apiVersion}/${cmd}?${querystring.stringify(args)}`;
  21. res = await api.post(uri('createGroup', {apikey}));
  22. if (res.body.code === 1) throw new Error(`Error creating group: ${res.body}`);
  23. const groupID = res.body.data.groupID;
  24. console.log('groupID', groupID);
  25. res = await api.post(uri('createGroupPad', {apikey, groupID}));
  26. if (res.body.code === 1) throw new Error(`Error creating group pad: ${res.body}`);
  27. console.log('Test Pad ID ====> ', res.body.data.padID);
  28. res = await api.post(uri('createAuthor', {apikey}));
  29. if (res.body.code === 1) throw new Error(`Error creating author: ${res.body}`);
  30. const authorID = res.body.data.authorID;
  31. console.log('authorID', authorID);
  32. const validUntil = Math.floor(new Date() / 1000) + 60000;
  33. console.log('validUntil', validUntil);
  34. res = await api.post(uri('createSession', {apikey, groupID, authorID, validUntil}));
  35. if (res.body.code === 1) throw new Error(`Error creating session: ${res.body}`);
  36. console.log('Session made: ====> create a cookie named sessionID and set the value to',
  37. res.body.data.sessionID);
  38. })();