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

adds integration test updateGlobalSettings mutation #5970

Merged
merged 11 commits into from
Jan 7, 2020
7 changes: 6 additions & 1 deletion src/core-services/settings/mutations/updateAppSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ export default async function updateAppSettings(context, settingsUpdates, shopId
if (allowedRoles.length === 0) {
throw new ReactionError("access-denied", `You are not allowed to edit the "${updateKey}" setting`);
}
await context.validatePermissions(`reaction:shops:${shopId}`, "update", { shopId, legacyRoles: allowedRoles }); // eslint-disable-line no-await-in-loop

if (shopId) {
await context.validatePermissions(`reaction:shops:${shopId}`, "update", { shopId, legacyRoles: allowedRoles }); // eslint-disable-line no-await-in-loop
} else {
await context.validatePermissions("reaction:shops", "update", { shopId: null, legacyRoles: allowedRoles }); // eslint-disable-line no-await-in-loop
}
}

const { value: updatedDoc } = await AppSettings.findOneAndUpdate(
Expand Down
2 changes: 1 addition & 1 deletion src/core-services/settings/util/settingsConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function addShopSettingDefaults(settings) {
* @returns {String[]} List of roles that can edit this setting.
*/
export function rolesThatCanEditGlobalSetting(field) {
const config = globalSettingsSchema[field];
const config = globalSettingsConfig[field];
if (!config) return [];

return config.rolesThatCanEdit || [];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`an anonymous user cannot update global settings 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 [
"updateGlobalSettings",
],
},
]
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mutation updateGlobalSettings($input: UpdateGlobalSettingsInput!) {
updateGlobalSettings(input: $input) {
globalSettings {
canSellDigitalProducts
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import importAsString from "@reactioncommerce/api-utils/importAsString.js";
import Factory from "/tests/util/factory.js";
import TestApp from "/tests/util/TestApp.js";

const updateGlobalSettings = importAsString("./updateGlobalSettings.graphql");
const TestGlobalSettingSchema = `
extend type GlobalSettings {
canSellDigitalProducts: Boolean
}

extend input GlobalSettingsUpdates {
canSellDigitalProducts: Boolean
}

extend input UpdateGlobalSettingsInput {
"""
If true a shop can sell digital products
"""
canSellDigitalProducts: Boolean
}
`;

jest.setTimeout(300000);

const shopId = "123";
const shopName = "Test Shop";
let testApp;
let globalSettingsMutation;

const mockGlobalSetting = {
canSellDigitalProducts: false
};

const mockAdminAccount = Factory.Account.makeOne({
roles: {
__global_roles__: ["admin"] // eslint-disable-line camelcase
}
});

beforeAll(async () => {
testApp = new TestApp();

testApp.registerPlugin({
name: "testGlobalSetting",
graphQL: {
schemas: [TestGlobalSettingSchema]
},
globalSettingsConfig: {
canSellDigitalProducts: {
rolesThatCanEdit: ["admin"],
simpleSchema: {
type: Boolean
}
}
}
});

await testApp.start();
await testApp.insertPrimaryShop({ _id: shopId, name: shopName });
await testApp.createUserAndAccount(mockAdminAccount);
await testApp.collections.AppSettings.insertOne(mockGlobalSetting);
globalSettingsMutation = testApp.query(updateGlobalSettings);
});

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

test("an anonymous user cannot update global settings", async () => {
try {
await globalSettingsMutation({
input: {
settingsUpdates: {
canSellDigitalProducts: true
}
}
});
} catch (error) {
expect(error).toMatchSnapshot();
}
});

test("an admin user can update global settings", async () => {
let result;
await testApp.setLoggedInUser(mockAdminAccount);
const settings = await testApp.collections.AppSettings.findOne({
canSellDigitalProducts: false
});
expect(settings.canSellDigitalProducts).toEqual(false);

try {
result = await globalSettingsMutation({
input: {
settingsUpdates: {
canSellDigitalProducts: true
}
}
});
} catch (error) {
expect(error).toBeUndefined();
return;
}

expect(result.updateGlobalSettings.globalSettings.canSellDigitalProducts).toEqual(true);
});

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import importAsString from "@reactioncommerce/api-utils/importAsString.js";
import TestApp from "/tests/util/TestApp.js";

const GlobalSettingsQuery = importAsString("./GlobalSettingsQuery.graphql");
const TestGlobalSettingSchema = importAsString("./TestGlobalSettingSchema.graphql");
const TestGlobalSettingSchema = `
extend type GlobalSettings {
canSellVariantWithoutInventory: Boolean
}
`;

jest.setTimeout(300000);

Expand Down
17 changes: 10 additions & 7 deletions tests/util/TestApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class TestApp {
...user,
roles: {
...(user.roles || {}),
__global_roles__: globalRoles || [] // eslint-disable-line camelcase
__global_roles__: ((user.roles || {}).__global_roles__ || []).concat(globalRoles || []) // eslint-disable-line camelcase
},
services: {
resume: {
Expand All @@ -66,14 +66,17 @@ class TestApp {
}

// Set the hashed login token on the users document
await users.updateOne({ _id: user._id }, {
$push: {
"services.resume.loginTokens": {
hashedToken,
when: new Date()
await users.updateOne(
{ _id: user._id },
{
$push: {
"services.resume.loginTokens": {
hashedToken,
when: new Date()
}
}
}
});
);

this.userId = user._id;

Expand Down