Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
fix(tests): parameter compatibility of reports abilities
Browse files Browse the repository at this point in the history
  • Loading branch information
MitanOmar committed May 12, 2023
1 parent bb3cd83 commit 3fc5648
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
10 changes: 5 additions & 5 deletions app/abilities/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ export default Ability.extend({
function () {
const isEditable =
this.user?.isSuperuser ||
(!this.model.verifiedBy?.id &&
(!this.model?.verifiedBy?.get("id") &&
// eslint-disable-next-line ember/no-get
(this.get("model.user.id") === this.user?.id ||
(this.model?.user?.get("id") === this.user?.get("id") ||
// eslint-disable-next-line ember/no-get
(this.get("model.user.supervisors") ?? [])
(this.model?.user?.get("supervisors") ?? [])
.mapBy("id")
.includes(this.user?.id)));
.includes(this.user?.get("id"))));
const isReviewer =
(this.model?.taskAssignees ?? [])
.concat(
this.model?.projectAssignees ?? [],
this.model?.customerAssignees ?? []
)
.mapBy("user.id")
.includes(this.user?.id) && !this.model.verifiedBy?.id;
.includes(this.user?.get("id")) && !this.model?.verifiedBy?.get("id");
return isEditable || isReviewer;
}
),
Expand Down
38 changes: 22 additions & 16 deletions tests/unit/abilities/report-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import EmberObject from "@ember/object";
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";

Expand All @@ -6,52 +7,57 @@ module("Unit | Ability | report", function (hooks) {

test("can edit when user is superuser", function (assert) {
const ability = this.owner.lookup("ability:report");
ability.set("user", { isSuperuser: true });
ability.set("user", EmberObject.create({ isSuperuser: true }));

assert.true(ability.get("canEdit"));
});

test("can edit when user is superuser and report is verified", function (assert) {
const ability = this.owner.lookup("ability:report");
ability.set("user", { isSuperuser: true });
ability.set("model", { verifiedBy: { id: 1 } });
ability.set("user", EmberObject.create({ isSuperuser: true }));
ability.set("model", { verifiedBy: EmberObject.create({ id: 1 }) });

assert.true(ability.get("canEdit"));
});

test("can edit when user owns report", function (assert) {
const ability = this.owner.lookup("ability:report");
ability.set("user", { id: 1 });
ability.set("model", { user: { id: 1 } });
ability.set("user", EmberObject.create({ id: 1 }));
ability.set("model", { user: EmberObject.create({ id: 1 }) });

assert.true(ability.get("canEdit"));
});

test("can edit when user is supervisor of owner", function (assert) {
const ability = this.owner.lookup("ability:report");
ability.set("user", { id: 1 });
ability.set("model", { user: { supervisors: [{ id: 1 }] } });
ability.set("user", EmberObject.create({ id: 1 }));
ability.set("model", {
user: EmberObject.create({ supervisors: [{ id: 1 }] }),
});

assert.true(ability.get("canEdit"));
});

test("can edit when user reviewer of project", function (assert) {
const ability = this.owner.lookup("ability:report");
const user = { id: 1 };
const user = EmberObject.create({ id: 1 });
const projectAssignee = [{ user }];
ability.set("user", user);
ability.set("model", {
projectAssignees: projectAssignee,
});
ability.set(
"model",
EmberObject.create({
projectAssignees: projectAssignee,
})
);

assert.true(ability.get("canEdit"));
});

test("can not edit when not allowed", function (assert) {
const ability = this.owner.lookup("ability:report");
ability.set("user", { id: 1, isSuperuser: false });
ability.set("user", EmberObject.create({ id: 1, isSuperuser: false }));
ability.set("model", {
user: { id: 2, supervisors: [{ id: 2 }] },
user: EmberObject.create({ id: 2, supervisors: [{ id: 2 }] }),
task: { project: { reviewers: [{ id: 2 }] } },
projectAssignees: [{ id: 2 }],
});
Expand All @@ -61,11 +67,11 @@ module("Unit | Ability | report", function (hooks) {

test("can not edit when report is verified and billed", function (assert) {
const ability = this.owner.lookup("ability:report");
ability.set("user", { id: 1, isSuperuser: false });
ability.set("user", EmberObject.create({ id: 1, isSuperuser: false }));
ability.set("model", {
user: { id: 1, supervisors: [{ id: 1 }] },
user: EmberObject.create({ id: 1, supervisors: [{ id: 1 }] }),
projectAssignees: [{ id: 1 }],
verifiedBy: { id: 1 },
verifiedBy: EmberObject.create({ id: 1 }),
billed: true,
});

Expand Down

0 comments on commit 3fc5648

Please sign in to comment.