Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #13 from samkelleher/dont-fail-on-mongodb
Browse files Browse the repository at this point in the history
fix: Don't totally fail if cannot load MongoDB version.
  • Loading branch information
Akarshit authored May 24, 2021
2 parents a34ee4a + 1c182a6 commit 60d5f1d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"sideEffects": false,
"dependencies": {
"@reactioncommerce/api-utils": "^1.14.2",
"@reactioncommerce/logger": "^1.1.3",
"@reactioncommerce/reaction-error": "^1.0.1"
},
"devDependencies": {
Expand Down
16 changes: 13 additions & 3 deletions src/queries/systemInformation.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Logger from "@reactioncommerce/logger";

/**
* @name queries.systemInformation
* @method
Expand All @@ -12,12 +14,20 @@ export default async function systemInformation(context, shopId) {

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

const mongoAdmin = await db.admin();
const mongoInfo = await mongoAdmin.serverStatus();
let mongoVersion = "";

try {
const mongoAdmin = await db.admin();
const mongoInfo = await mongoAdmin.serverStatus();
mongoVersion = mongoInfo.version;
} catch (error) {
Logger.error(error);
}

const plugins = Object.values(context.app.registeredPlugins).filter((plugin) => plugin.version);
return {
apiVersion: context.appVersion,
mongoVersion: { version: mongoInfo.version },
mongoVersion: { version: mongoVersion },
plugins: plugins.map(({ name, version }) => ({ name, version }))
};
}
28 changes: 28 additions & 0 deletions src/queries/systemInformation.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import mockContext from "@reactioncommerce/api-utils/tests/mockContext.js";
import ReactionError from "@reactioncommerce/reaction-error";
import Logger from "@reactioncommerce/logger";
import systemInformation from "./systemInformation.js";

jest.mock("@reactioncommerce/logger", () => ({
error: jest.fn().mockImplementationOnce(() => {})
}));

const fakeShopId = "FAKE_SHOP_ID";
mockContext.appVersion = "1.2.3";
mockContext.validatePermissions = jest.fn("validatePermissions");
Expand Down Expand Up @@ -65,3 +70,26 @@ test("returns system info if user has permission", async () => {

return expect(result).toEqual(expectedResult);
});

test("returns plugins even when mongoVersion fails to load", async () => {
const expectedResult = {
apiVersion: "1.2.3",
mongoVersion: { version: "" },
plugins: [
{ name: "plugin-a", version: "1.0.0" },
{ name: "plugin-b", version: "1.1.0" },
{ name: "plugin-c", version: "1.0.2" },
{ name: "plugin-d", version: "2.0.0" }
]
};

mockContext.validatePermissions.mockReturnValueOnce(Promise.resolve(undefined));
mockContext.app.db.admin.mockReturnValueOnce(Promise.resolve(mongoAdmin));
mongoAdmin.serverStatus.mockRejectedValueOnce(new Error("Simulate serverStatus() error."));
const simulatedError = new Error("Simulate serverStatus() error.");

const result = await systemInformation(mockContext, fakeShopId);

expect(result).toEqual(expectedResult);
expect(Logger.error).toHaveBeenCalledWith(simulatedError);
});

0 comments on commit 60d5f1d

Please sign in to comment.