Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added validatePermissions for Surcharges query endpoints #6716

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spotty-turkeys-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@reactioncommerce/api-plugin-surcharges": patch
---

Added validatePermissions for the query endpoints
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const opaqueSurchargeId = encodeOpaqueId("reaction/surcharge", internalSurcharge
const shopName = "Test Shop";
let testApp;
let surchargeById;
let mockAdminAccount;

const mockSurcharge = Factory.Surcharge.makeOne({
_id: internalSurchargeId,
Expand All @@ -35,6 +36,23 @@ beforeAll(async () => {

await insertPrimaryShop(testApp.context, { _id: internalShopId, name: shopName });
await testApp.collections.Surcharges.insertOne(mockSurcharge);

const adminGroup = Factory.Group.makeOne({
_id: "adminGroup",
createdBy: null,
name: "admin",
permissions: ["reaction:legacy:surcharges/read"],
slug: "admin",
shopId: internalShopId
});
await testApp.collections.Groups.insertOne(adminGroup);

mockAdminAccount = Factory.Account.makeOne({
groups: [adminGroup._id]
});
await testApp.createUserAndAccount(mockAdminAccount);
await testApp.setLoggedInUser(mockAdminAccount);

surchargeById = testApp.query(SurchargeByIdQuery);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const shopName = "Test Shop";
let testApp;
let surcharges;
let mockSurcharges;
let mockAdminAccount;

beforeAll(async () => {
testApp = new ReactionTestAPICore();
Expand All @@ -32,6 +33,22 @@ beforeAll(async () => {
amount: 10
});
await testApp.collections.Surcharges.insertMany(mockSurcharges);

const adminGroup = Factory.Group.makeOne({
_id: "adminGroup",
createdBy: null,
name: "admin",
permissions: ["reaction:legacy:surcharges/read"],
slug: "admin",
shopId: internalShopId
});
await testApp.collections.Groups.insertOne(adminGroup);

mockAdminAccount = Factory.Account.makeOne({
groups: [adminGroup._id]
});
await testApp.createUserAndAccount(mockAdminAccount);
await testApp.setLoggedInUser(mockAdminAccount);
surcharges = testApp.query(SurchargesQuery);
});

Expand Down
2 changes: 2 additions & 0 deletions packages/api-plugin-surcharges/src/queries/surchargeById.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export default async function surchargeById(context, { surchargeId, shopId } = {
const { collections } = context;
const { Surcharges } = collections;

await context.validatePermissions("reaction:legacy:surcharges", "read", { shopId });

const surcharge = await Surcharges.findOne({
_id: surchargeId,
shopId
Expand Down
39 changes: 39 additions & 0 deletions packages/api-plugin-surcharges/src/queries/surchargeById.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import mockContext from "@reactioncommerce/api-utils/tests/mockContext.js";
import mockCollection from "@reactioncommerce/api-utils/tests/mockCollection.js";
import ReactionError from "@reactioncommerce/reaction-error";
import surchargeByIdQuery from "./surchargeById.js";

const fakeShopId = "FAKE_SHOP_ID";
const fakeSurchargeId = "FAKE_SURCHARGE_ID";
const mockSurcharge = { _id: fakeSurchargeId };
mockContext.validatePermissions = jest.fn("validatePermissions");
mockContext.collections.Surcharges = mockCollection("Surcharges");

beforeEach(() => {
jest.resetAllMocks();
});

test("returns the surcharge if validatePermission returns true", async () => {
mockContext.collections.Surcharges.findOne.mockReturnValueOnce(Promise.resolve(mockSurcharge));
mockContext.validatePermissions.mockReturnValueOnce(Promise.resolve(undefined));
const result = await surchargeByIdQuery(mockContext, { surchargeId: fakeSurchargeId, shopId: fakeShopId });

expect(mockContext.validatePermissions).toHaveBeenCalledWith(
"reaction:legacy:surcharges",
"read",
{ shopId: fakeShopId }
);
expect(mockContext.collections.Surcharges.findOne).toHaveBeenCalledWith({ _id: fakeSurchargeId, shopId: fakeShopId });
await expect(result).toBe(mockSurcharge);
});


test("throws access-denied if not allowed", async () => {
mockContext.validatePermissions.mockImplementation(() => {
throw new ReactionError("access-denied", "Access Denied");
});
mockContext.collections.Accounts.findOne.mockReturnValueOnce(undefined);
const result = surchargeByIdQuery(mockContext, { surchargeId: fakeSurchargeId, shopId: fakeShopId });
const expectedResult = new ReactionError("access-denied", "Access Denied");
await expect(result).rejects.toThrow(expectedResult);
});
2 changes: 2 additions & 0 deletions packages/api-plugin-surcharges/src/queries/surcharges.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export default async function surcharges(context, { shopId } = {}) {
const { collections } = context;
const { Surcharges } = collections;

await context.validatePermissions("reaction:legacy:surcharges", "read", { shopId });

return Surcharges.find({
shopId
});
Expand Down
37 changes: 37 additions & 0 deletions packages/api-plugin-surcharges/src/queries/surcharges.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import mockContext from "@reactioncommerce/api-utils/tests/mockContext.js";
import mockCollection from "@reactioncommerce/api-utils/tests/mockCollection.js";
import ReactionError from "@reactioncommerce/reaction-error";
import surchargesQuery from "./surcharges.js";

const fakeShopId = "FAKE_SHOP_ID";
mockContext.validatePermissions = jest.fn("validatePermissions");
mockContext.collections.Surcharges = mockCollection("Surcharges");

beforeEach(() => {
jest.resetAllMocks();
});

test("returns the surcharges cursor if validatePermission returns true", async () => {
mockContext.collections.Surcharges.find.mockReturnValueOnce(Promise.resolve("CURSOR"));
mockContext.validatePermissions.mockReturnValueOnce(Promise.resolve(undefined));
const result = await surchargesQuery(mockContext, { shopId: fakeShopId });

expect(mockContext.collections.Surcharges.find).toHaveBeenCalledWith({ shopId: fakeShopId });
expect(mockContext.validatePermissions).toHaveBeenCalledWith(
"reaction:legacy:surcharges",
"read",
{ shopId: fakeShopId }
);
await expect(result).toBe("CURSOR");
});


test("throws access-denied if not allowed", async () => {
mockContext.validatePermissions.mockImplementation(() => {
throw new ReactionError("access-denied", "Access Denied");
});
mockContext.collections.Accounts.findOne.mockReturnValueOnce(undefined);
const result = surchargesQuery(mockContext, { shopId: fakeShopId });
const expectedResult = new ReactionError("access-denied", "Access Denied");
await expect(result).rejects.toThrow(expectedResult);
});