Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/plugins/navigation/mutations/updateNavigationTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ export default async function updateNavigationTree(context, input) {
const { NavigationTrees } = collections;
const { navigationTreeId, shopId, navigationTree } = input;

const treeSelector = { _id: navigationTreeId, shopId };
const existingNavigationTree = await NavigationTrees.findOne(treeSelector);
if (!existingNavigationTree) {
throw new ReactionError("navigation-tree-not-found", "No navigation tree was found");
}

const {
shouldNavigationTreeItemsBeAdminOnly,
shouldNavigationTreeItemsBePubliclyVisible,
Expand Down Expand Up @@ -46,12 +52,6 @@ export default async function updateNavigationTree(context, input) {

await context.validatePermissions(`reaction:navigationTrees:${navigationTreeId}`, "update", { shopId, legacyRoles: ["core"] });

const treeSelector = { _id: navigationTreeId, shopId };
const existingNavigationTree = await NavigationTrees.findOne(treeSelector);
if (!existingNavigationTree) {
throw new ReactionError("navigation-tree-not-found", "No navigation tree was found");
}

const update = {};

if (draftItems) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
mutation UpdateNavigationTree($updateNavigationTreeInput: UpdateNavigationTreeInput!) {
updateNavigationTree(input: $updateNavigationTreeInput) {
navigationTree {
...NavigationTree
}
}
}

fragment NavigationTree on NavigationTree {
_id
draftItems {
expanded
isPrivate
isSecondary
isVisible
navigationItem {
_id
}
}
hasUnpublishedChanges
name
shopId
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`an unauthorized user should not be able to update the navigation tree 1`] = `
Array [
Object {
"extensions": Object {
"code": "FORBIDDEN",
"exception": Object {
"details": Object {},
"error": "access-denied",
"eventData": Object {},
"isClientSafe": true,
"reason": "Access Denied",
},
},
"locations": Array [
Object {
"column": 3,
"line": 2,
},
],
"message": "Access Denied",
"path": Array [
"updateNavigationTree",
],
},
]
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import encodeOpaqueId from "@reactioncommerce/api-utils/encodeOpaqueId.js";
import importAsString from "@reactioncommerce/api-utils/importAsString.js";
import Factory from "/tests/util/factory.js";
import TestApp from "/tests/util/TestApp.js";

const UpdateNavigationTreeMutation = importAsString("./UpdateNavigationTreeMutation.graphql");

jest.setTimeout(300000);

const internalShopId = "123";
const opaqueShopId = "cmVhY3Rpb24vc2hvcDoxMjM="; // reaction/shop:123
const shopName = "Test Shop";
const encodeNavigationTreeOpaqueId = encodeOpaqueId("reaction/navigationTree");
const encodeNavigationItemOpaqueId = encodeOpaqueId("reaction/navigationItem");

const mockAdminAccount = Factory.Account.makeOne({
roles: {
[internalShopId]: ["admin", "core"]
}
});

const mockNavigationTree = Factory.NavigationTree.makeOne({
_id: "1001",
name: "Default Navigation",
shopId: internalShopId,
draftItems: Factory.NavigationTreeItem.makeMany(2, {
isPrivate: false,
isSecondary: false,
isVisible: false
})
});

const mockNavigationItem = Factory.NavigationItem.makeOne({
shopId: internalShopId
});

const updateNavigationTreeInput = {
shopId: opaqueShopId,
navigationTree: {
name: "Not Default Navigation",
draftItems: [
{
navigationItemId: encodeNavigationItemOpaqueId(mockNavigationItem._id),
isPrivate: false,
isSecondary: false,
isVisible: true
}
]
},
id: encodeNavigationTreeOpaqueId(mockNavigationTree._id)
};

let testApp;
let updateNavigationTree;

beforeAll(async () => {
testApp = new TestApp();
await testApp.start();
await testApp.insertPrimaryShop({ _id: internalShopId, name: shopName });
await testApp.createUserAndAccount(mockAdminAccount);
await testApp.setLoggedInUser(mockAdminAccount);
await testApp.collections.NavigationTrees.insertOne(mockNavigationTree);
await testApp.collections.NavigationItems.insertOne(mockNavigationItem);

updateNavigationTree = await testApp.mutate(UpdateNavigationTreeMutation);
});

beforeEach(async () => {
await testApp.clearLoggedInUser();
});

afterAll(async () => {
await testApp.collections.NavigationItems.deleteMany({});
await testApp.collections.Shops.deleteMany({});
await testApp.stop();
});

test("an authorized user should be able to update the navigation tree", async () => {
let result;
try {
await testApp.setLoggedInUser(mockAdminAccount);
result = await updateNavigationTree({
updateNavigationTreeInput
});
} catch (error) {
expect(error).toBeUndefined();
}

expect(result.updateNavigationTree.navigationTree).toEqual({
_id: encodeNavigationTreeOpaqueId(mockNavigationTree._id),
draftItems: [
{
expanded: null,
isPrivate: false,
isSecondary: false,
isVisible: true,
navigationItem: {
_id: encodeNavigationItemOpaqueId(mockNavigationItem._id)
}
}
],
hasUnpublishedChanges: true,
name: "Not Default Navigation",
shopId: opaqueShopId
});
});

test("an unauthorized user should not be able to update the navigation tree", async () => {
try {
await updateNavigationTree({
updateNavigationTreeInput
});
} catch (error) {
expect(error).toMatchSnapshot();
}
});
9 changes: 9 additions & 0 deletions tests/util/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ import {
extendSimplePricingSchemas
} from "../../src/plugins/simple-pricing/simpleSchemas.js";

import {
NavigationItem,
NavigationTree,
NavigationTreeItem
} from "../../src/plugins/navigation/simpleSchemas.js";

import { AddressValidationRule } from "../../src/core-services/address/simpleSchemas.js";

const schemasToAddToFactory = {
Expand All @@ -102,6 +108,9 @@ const schemasToAddToFactory = {
Email,
EmailTemplates,
Group,
NavigationItem,
NavigationTree,
NavigationTreeItem,
Order,
OrderAddress,
OrderFulfillmentGroup,
Expand Down