legacy-user-import-password.test.mjs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { test } from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { readFileSync } from 'node:fs';
  4. import { randomBytes } from 'node:crypto';
  5. import vm from 'node:vm';
  6. const source = readFileSync(new URL('../backfill-legacy-users.mjs', import.meta.url), 'utf8');
  7. function plan(rows, existing = []) {
  8. const context = vm.createContext({
  9. company: { __type: 'Pointer', className: 'Company', objectId: 'co' },
  10. source: { rows }, byLegacyId: new Map(existing.map(row => [row.legacyUserId, row])),
  11. byUsername: new Map(existing.map(row => [row.username, row])), refreshExisting: false, randomBytes,
  12. });
  13. const helpers = source.slice(source.indexOf('function text(value)'), source.indexOf('function requestGroups('));
  14. const planner = source.slice(source.indexOf('const requests = [];'), source.indexOf('const groups = requestGroups(requests);'));
  15. vm.runInContext(helpers + planner + '\nglobalThis.planned = requests;', context);
  16. return context.planned;
  17. }
  18. test('manual legacy import initializes new students consistently and leaves existing credentials alone', () => {
  19. const requests = plan([
  20. { UserID: 20, UserName: 'existing', GroupID: 1 },
  21. { UserID: 21, UserName: 'new-student', GroupID: 1, State: 1, Password: 'ignored-source-secret' },
  22. { UserID: 22, UserName: 'disabled-student', GroupID: 1, State: 0 },
  23. ], [{ objectId: 'existing', username: 'existing', legacyUserId: 20, identityType: 'member' }]);
  24. assert.equal(requests.length, 2);
  25. for (const request of requests) {
  26. assert.equal(request.method, 'POST');
  27. assert.equal(request.body.password, '12345678');
  28. assert.equal(request.body.passwordResetRequired, false);
  29. assert.equal(request.body.legacyPasswordMigrationAllowed, undefined);
  30. assert.equal(request.body.legacyUserData.Password, undefined);
  31. }
  32. assert.equal(requests[0].body.isDisabled, false);
  33. assert.equal(requests[1].body.isDisabled, true);
  34. });
  35. test('manual import keeps distinct random credentials for nonstudent roles', () => {
  36. const requests = plan([
  37. { UserID: 31, UserName: 'store', GroupID: 2 },
  38. { UserID: 32, UserName: 'coach', GroupID: 3 },
  39. ]);
  40. assert.equal(requests.length, 2);
  41. assert.ok(requests.every(row => row.body.password.length > 30));
  42. assert.notEqual(requests[0].body.password, requests[1].body.password);
  43. });