050-users-permissions.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. Critical path: user management and permission control.
  3. As administrator: create a limited permission group, create a user in
  4. it, then prove from a second browser context that the restricted user
  5. can sign in, sees only permitted modules and is refused by admin-only
  6. endpoints. Finally remove the user and the group and prove both are
  7. really gone (including that the removed user can no longer sign in).
  8. */
  9. "use strict";
  10. const h = require("../lib/system-harness");
  11. const GROUP = "e2etesters";
  12. const USER = "e2euser";
  13. const PASS = "e2e-User-Passw0rd";
  14. h.run("USERS-PERMISSIONS", async function (env) {
  15. const base = env.baseURL;
  16. const adminPage = await h.newPage(env.browser);
  17. await h.loginViaAPI(adminPage, base, env.admin.username, env.admin.password);
  18. // ── 1. Create a limited permission group ──
  19. let resp = await h.postForm(adminPage, base + "/system/permission/newgroup", {
  20. groupname: GROUP,
  21. permission: JSON.stringify(["Desktop", "File Manager"]),
  22. isAdmin: "false",
  23. defaultQuota: "1073741824",
  24. interfaceModule: "Desktop"
  25. });
  26. if (resp.toLowerCase().indexOf("ok") === -1) h.fail("newgroup failed: " + resp);
  27. let groups = await h.getJSON(adminPage, base + "/system/permission/listgroup");
  28. if (JSON.stringify(groups).indexOf(GROUP) === -1) h.fail("new group missing from listgroup");
  29. h.ok("admin can create a limited permission group");
  30. // ── 2. Create a user inside that group ──
  31. resp = await h.postForm(adminPage, base + "/system/auth/register", {
  32. username: USER, password: PASS, group: GROUP
  33. });
  34. if (resp.toLowerCase().indexOf("ok") === -1) h.fail("user creation failed: " + resp);
  35. let users = await h.getJSON(adminPage, base + "/system/users/list?noicon=true");
  36. const created = users.find(function (u) { return u[0] === USER; });
  37. if (!created) h.fail("new user missing from users list: " + JSON.stringify(users));
  38. if (created[1].indexOf(GROUP) === -1) h.fail("new user not in expected group: " + JSON.stringify(created));
  39. h.ok("admin can create a user in the limited group");
  40. // ── 3. The restricted user can sign in through the real form ──
  41. const userPage = await h.newPage(env.browser);
  42. await h.loginViaForm(userPage, base, USER, PASS);
  43. if (!(await h.isLoggedIn(userPage, base))) h.fail("restricted user form login failed");
  44. await userPage.goto(base + "/", { waitUntil: "domcontentloaded" });
  45. if (userPage.url().indexOf("desktop.html") === -1) {
  46. h.fail("restricted user did not land on the desktop: " + userPage.url());
  47. }
  48. h.ok("restricted user signs in and lands on the desktop");
  49. // ── 4. Module visibility is filtered by group permission ──
  50. const userModules = await h.getJSON(userPage, base + "/system/modules/list");
  51. const userModuleNames = userModules.map(function (m) { return m.Name; });
  52. if (userModuleNames.indexOf("File Manager") === -1) {
  53. h.fail("restricted user should see File Manager. Got: " + userModuleNames.join(", "));
  54. }
  55. if (userModuleNames.indexOf("System Setting") !== -1) {
  56. h.fail("restricted user must NOT see System Setting. Got: " + userModuleNames.join(", "));
  57. }
  58. h.ok("restricted user sees permitted modules only (no System Setting)");
  59. // ── 5. Admin-only endpoints refuse the restricted user ──
  60. resp = await h.postForm(userPage, base + "/system/permission/newgroup", {
  61. groupname: "should-not-exist",
  62. permission: JSON.stringify(["Desktop"]),
  63. isAdmin: "false",
  64. defaultQuota: "0",
  65. interfaceModule: "Desktop"
  66. });
  67. if (resp.toLowerCase().indexOf("ok") !== -1) h.fail("restricted user was allowed to create a group");
  68. resp = await h.postForm(userPage, base + "/system/users/removeUser", { username: env.admin.username });
  69. if (resp.toLowerCase().indexOf("ok") !== -1) h.fail("restricted user was allowed to remove a user");
  70. groups = await h.getJSON(adminPage, base + "/system/permission/listgroup");
  71. if (JSON.stringify(groups).indexOf("should-not-exist") !== -1) {
  72. h.fail("group created despite permission denial");
  73. }
  74. h.ok("admin-only endpoints refuse the restricted user");
  75. // ── 6. The restricted user cannot even list permission groups ──
  76. const userGroupsResp = await h.postForm(userPage, base + "/system/permission/listgroup", {});
  77. if (userGroupsResp.indexOf("administrator") !== -1) {
  78. h.fail("restricted user could read the permission group list");
  79. }
  80. h.ok("permission group listing is admin-only");
  81. await userPage.close();
  82. // ── 7. Admin removes the user; their login stops working ──
  83. resp = await h.postForm(adminPage, base + "/system/users/removeUser", { username: USER });
  84. if (resp.toLowerCase().indexOf("ok") === -1) h.fail("removeUser failed: " + resp);
  85. users = await h.getJSON(adminPage, base + "/system/users/list?noicon=true");
  86. if (users && users.find && users.find(function (u) { return u[0] === USER; })) {
  87. h.fail("removed user still present in users list");
  88. }
  89. const ghostPage = await h.newPage(env.browser);
  90. const loginResp = await ghostPage.request.post(base + "/system/auth/login", {
  91. form: { username: USER, password: PASS, rmbme: "false" }
  92. });
  93. const loginBody = (await loginResp.text()).trim();
  94. if (await h.isLoggedIn(ghostPage, base)) h.fail("removed user can still sign in: " + loginBody);
  95. await ghostPage.close();
  96. h.ok("removed user disappears from the list and can no longer sign in");
  97. // ── 8. Admin deletes the permission group ──
  98. resp = await h.postForm(adminPage, base + "/system/permission/delgroup", { groupname: GROUP });
  99. if (resp.toLowerCase().indexOf("ok") === -1) h.fail("delgroup failed: " + resp);
  100. groups = await h.getJSON(adminPage, base + "/system/permission/listgroup");
  101. if (JSON.stringify(groups).indexOf(GROUP) !== -1) h.fail("group still present after delgroup");
  102. h.ok("admin can delete the permission group");
  103. // ── 9. Administrator group is protected from deletion ──
  104. resp = await h.postForm(adminPage, base + "/system/permission/delgroup", { groupname: "administrator" });
  105. if (resp.toLowerCase().indexOf("ok") !== -1) h.fail("administrator group deletion was allowed!");
  106. h.ok("administrator group cannot be deleted");
  107. await adminPage.close();
  108. });