Skip to content
This repository was archived by the owner on Mar 28, 2022. It is now read-only.

Commit 213b05f

Browse files
committed
Add about api
1 parent 45b9cf3 commit 213b05f

7 files changed

Lines changed: 197 additions & 2 deletions

File tree

src/About.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2019 Javier Brea
3+
Copyright 2019 XbyOrange
4+
5+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
10+
*/
11+
12+
"use strict";
13+
14+
const express = require("express");
15+
16+
const { PLUGIN_NAME } = require("./constants");
17+
const { version } = require("../package.json");
18+
19+
class AboutApi {
20+
constructor(core) {
21+
this._core = core;
22+
this._tracer = core.tracer;
23+
this._fixtures = this._core.fixtures;
24+
this._router = express.Router();
25+
this._router.get("/", this.getAbout.bind(this));
26+
}
27+
28+
getAbout(req, res) {
29+
this._tracer.verbose(`${PLUGIN_NAME}: Sending about | ${req.id}`);
30+
res.status(200);
31+
res.send({
32+
version
33+
});
34+
}
35+
36+
get router() {
37+
return this._router;
38+
}
39+
}
40+
41+
module.exports = AboutApi;

src/Plugin.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const DeprecatedApi = require("./deprecated/Api");
1616
const Settings = require("./Settings");
1717
const Behaviors = require("./Behaviors");
1818
const Fixtures = require("./Fixtures");
19+
const About = require("./About");
1920

2021
const {
2122
ADMIN_API_PATH_OPTION,
@@ -25,7 +26,8 @@ const {
2526
SETTINGS_API_PATH,
2627
BEHAVIORS_API_PATH,
2728
FIXTURES_API_PATH,
28-
DEPRECATED_API_PATH
29+
DEPRECATED_API_PATH,
30+
ABOUT_API_PATH
2931
} = require("./constants");
3032

3133
class Plugin {
@@ -36,6 +38,7 @@ class Plugin {
3638
this._deprecatedApi = new DeprecatedApi(core);
3739
this._settingsApi = new Settings(this._core);
3840
this._behaviorsApi = new Behaviors(this._core);
41+
this._aboutApi = new About(this._core);
3942
this._fixturesApi = new Fixtures(this._core);
4043
core.addSetting({
4144
name: ADMIN_API_PATH_OPTION,
@@ -66,6 +69,7 @@ class Plugin {
6669
this._router = express.Router();
6770
this._router.use(SETTINGS_API_PATH, this._settingsApi.router);
6871
this._router.use(BEHAVIORS_API_PATH, this._behaviorsApi.router);
72+
this._router.use(ABOUT_API_PATH, this._aboutApi.router);
6973
this._router.use(FIXTURES_API_PATH, this._fixturesApi.router);
7074
}
7175

src/constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ module.exports = {
1616
SETTINGS_API_PATH: "/settings",
1717
BEHAVIORS_API_PATH: "/behaviors",
1818
FIXTURES_API_PATH: "/fixtures",
19-
DEPRECATED_API_PATH: "/mocks"
19+
DEPRECATED_API_PATH: "/mocks",
20+
ABOUT_API_PATH: "/about"
2021
};

test/acceptance/about-api.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright 2019 Javier Brea
3+
4+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
9+
*/
10+
11+
const { startServer, stopServer, request } = require("./utils");
12+
const { version } = require("../../package.json");
13+
14+
describe("about api", () => {
15+
let server;
16+
beforeAll(async () => {
17+
server = await startServer("web-tutorial");
18+
});
19+
20+
afterAll(() => {
21+
stopServer(server);
22+
});
23+
24+
describe("get /", () => {
25+
it("should return current version", async () => {
26+
const response = await request("/admin/about");
27+
expect(response).toEqual({
28+
version
29+
});
30+
});
31+
});
32+
});

test/unit/src/About.mocks.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2019 Javier Brea
3+
Copyright 2019 XbyOrange
4+
5+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
10+
*/
11+
12+
const sinon = require("sinon");
13+
14+
jest.mock("../../../src/About");
15+
16+
const About = require("../../../src/About");
17+
18+
const Mock = class Mock {
19+
constructor() {
20+
this._sandbox = sinon.createSandbox();
21+
22+
this._stubs = {
23+
init: this._sandbox.stub()
24+
};
25+
26+
About.mockImplementation(() => this._stubs);
27+
}
28+
29+
get stubs() {
30+
return {
31+
Constructor: About,
32+
instance: this._stubs
33+
};
34+
}
35+
36+
restore() {
37+
this._sandbox.restore();
38+
}
39+
};
40+
41+
module.exports = Mock;

test/unit/src/About.spec.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2019 Javier Brea
3+
Copyright 2019 XbyOrange
4+
5+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
10+
*/
11+
12+
const express = require("express");
13+
const sinon = require("sinon");
14+
15+
const LibMocks = require("../Libs.mocks");
16+
const CoreMocks = require("../Core.mocks");
17+
18+
const About = require("../../../src/About");
19+
const { version } = require("../../../package.json");
20+
21+
describe("About", () => {
22+
let sandbox;
23+
let libMocks;
24+
let coreMock;
25+
let coreInstance;
26+
let resMock;
27+
let about;
28+
29+
beforeEach(() => {
30+
sandbox = sinon.createSandbox();
31+
resMock = {
32+
status: sandbox.stub(),
33+
send: sandbox.stub()
34+
};
35+
libMocks = new LibMocks();
36+
coreMock = new CoreMocks();
37+
coreInstance = coreMock.stubs.instance;
38+
about = new About(coreInstance);
39+
expect.assertions(1);
40+
});
41+
42+
afterEach(() => {
43+
sandbox.restore();
44+
libMocks.restore();
45+
coreMock.restore();
46+
});
47+
48+
describe("when created", () => {
49+
it("should create an express Router", async () => {
50+
expect(express.Router.calledOnce).toEqual(true);
51+
});
52+
53+
it("should have added get router at /", async () => {
54+
expect(libMocks.stubs.express.get.getCall(0).args[0]).toEqual("/");
55+
});
56+
});
57+
58+
describe("getAbout router", () => {
59+
it("should return current package version", () => {
60+
about.getAbout({}, resMock);
61+
expect(resMock.send.getCall(0).args[0]).toEqual({
62+
version
63+
});
64+
});
65+
});
66+
67+
describe("router getter", () => {
68+
it("should return express created router", async () => {
69+
expect(about.router).toEqual(libMocks.stubs.express);
70+
});
71+
});
72+
});

test/unit/src/Plugin.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const DeprecatedApiMocks = require("./deprecated/Api.mocks");
1818
const SettingsMocks = require("./Settings.mocks");
1919
const BehaviorsMocks = require("./Behaviors.mocks");
2020
const FixturesMock = require("./Fixtures.mocks");
21+
const AboutMock = require("./About.mocks");
2122

2223
const Plugin = require("../../../src/Plugin");
2324

@@ -31,13 +32,15 @@ describe("Plugin", () => {
3132
let deprecatedApiInstance;
3233
let coreMock;
3334
let coreInstance;
35+
let aboutMock;
3436
let plugin;
3537

3638
beforeEach(() => {
3739
sandbox = sinon.createSandbox();
3840
libMocks = new LibMocks();
3941
settingsMocks = new SettingsMocks();
4042
behaviorsMocks = new BehaviorsMocks();
43+
aboutMock = new AboutMock();
4144
fixturesMock = new FixturesMock();
4245
deprecatedApiMock = new DeprecatedApiMocks();
4346
deprecatedApiInstance = deprecatedApiMock.stubs.instance;
@@ -54,6 +57,7 @@ describe("Plugin", () => {
5457
behaviorsMocks.restore();
5558
fixturesMock.restore();
5659
coreMock.restore();
60+
aboutMock.restore();
5761
deprecatedApiMock.restore();
5862
});
5963

0 commit comments

Comments
 (0)