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

Commit 8f2da8d

Browse files
authored
Merge pull request #79 from mocks-server/feat-78-alerts
Add alerts
2 parents e9eabc3 + 2f36704 commit 8f2da8d

28 files changed

Lines changed: 649 additions & 63 deletions

.eslintrc.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"parserOptions": {
77
"ecmaVersion": "2018"
88
},
9-
"plugins": ["prettier"],
9+
"plugins": ["prettier", "no-only-tests"],
1010
"rules": {
1111
"prettier/prettier": [
1212
"error",
@@ -16,7 +16,8 @@
1616
}
1717
],
1818
"no-undef": "error",
19-
"no-unused-vars": ["error", { "vars": "all", "args": "after-used", "ignoreRestSiblings": false }]
19+
"no-unused-vars": ["error", { "vars": "all", "args": "after-used", "ignoreRestSiblings": false }],
20+
"no-only-tests/no-only-tests": [2]
2021
},
2122
"extends": ["prettier"],
2223
"root": true

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010

1111
## [unreleased]
1212
### Added
13+
- feat(#78): Add alerts router
14+
- chore(lint): Add eslint plugin to avoid only in tests
1315
### Changed
16+
- chore(deps): Update dependencies
17+
- test(e2e): Move utils to a support folder
18+
- feat: Add/remove routers on start/stop plugin methods, not in init method.
1419
### Fixed
1520
### Removed
1621

jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ module.exports = {
2525
},
2626

2727
// The glob patterns Jest uses to detect test files
28-
testMatch: ["**/test/unit/**/?(*.)+(spec|test).js?(x)"],
28+
testMatch: ["<rootDir>/test/unit/**/*.spec.js"],
29+
// testMatch: ["<rootDir>/test/unit/**/Plugin.spec.js"],
2930

3031
// The test environment that will be used for testing
3132
testEnvironment: "node",

jest.e2e.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ module.exports = {
55
// Automatically clear mock calls and instances between every test
66
clearMocks: true,
77

8-
testMatch: ["**/test/e2e/**/?(*.)+(spec|test).js?(x)"],
8+
testMatch: ["<rootDir>/test/e2e/**/*.spec.js"],
9+
// testMatch: ["<rootDir>/test/e2e/**/stop-plugin.spec.js"],
910

1011
// Indicates whether the coverage information should be collected while executing the test
1112
collectCoverage: false,

package-lock.json

Lines changed: 12 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@
3434
"@mocks-server/core": ">=1.3.0"
3535
},
3636
"dependencies": {
37-
"@mocks-server/admin-api-paths": "1.0.8",
37+
"@mocks-server/admin-api-paths": "1.1.0",
3838
"@hapi/boom": "9.1.0",
3939
"express": "4.17.1"
4040
},
4141
"devDependencies": {
42-
"@mocks-server/core": "1.5.1",
42+
"@mocks-server/core": "1.6.0",
4343
"eslint": "7.12.1",
44+
"eslint-plugin-no-only-tests": "2.4.0",
4445
"eslint-config-prettier": "6.15.0",
4546
"eslint-plugin-prettier": "3.1.4",
4647
"fs-extra": "9.0.1",

src/Alerts.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright 2020 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+
const Boom = require("@hapi/boom");
16+
17+
const { PLUGIN_NAME } = require("./constants");
18+
19+
class AlertsApi {
20+
constructor(core) {
21+
this._core = core;
22+
this._tracer = core.tracer;
23+
this._router = express.Router();
24+
this._router.get("/", this.getCollection.bind(this));
25+
this._router.get("/:id", this.getModel.bind(this));
26+
}
27+
28+
_parseModel(alert) {
29+
return {
30+
id: alert.context,
31+
context: alert.context,
32+
message: alert.message,
33+
error: alert.error
34+
? {
35+
name: alert.error.name,
36+
message: alert.error.message,
37+
stack: alert.error.stack,
38+
}
39+
: null,
40+
};
41+
}
42+
43+
_parseCollection() {
44+
return this._core.alerts.map(this._parseModel);
45+
}
46+
47+
getCollection(req, res) {
48+
this._tracer.verbose(`${PLUGIN_NAME}: Sending alerts | ${req.id}`);
49+
res.status(200);
50+
res.send(this._parseCollection());
51+
}
52+
53+
getModel(req, res, next) {
54+
const id = req.params.id;
55+
this._tracer.verbose(`${PLUGIN_NAME}: Sending alert ${id} | ${req.id}`);
56+
const foundAlert = this._core.alerts.find((alert) => alert.context === id);
57+
if (foundAlert) {
58+
res.status(200);
59+
res.send(this._parseModel(foundAlert));
60+
} else {
61+
next(Boom.notFound(`Alert with id "${id}" was not found`));
62+
}
63+
}
64+
65+
get router() {
66+
return this._router;
67+
}
68+
}
69+
70+
module.exports = AlertsApi;

src/Plugin.js

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ const {
1616
BEHAVIORS,
1717
ABOUT,
1818
FIXTURES,
19+
ALERTS,
1920
} = require("@mocks-server/admin-api-paths");
2021

2122
const packageInfo = require("../package.json");
2223
const DeprecatedApi = require("./deprecated/Api");
2324

2425
const Settings = require("./Settings");
2526
const Behaviors = require("./Behaviors");
27+
const Alerts = require("./Alerts");
2628
const Fixtures = require("./Fixtures");
2729
const About = require("./About");
2830

@@ -41,6 +43,7 @@ class Plugin {
4143
this._deprecatedApi = new DeprecatedApi(core);
4244
this._settingsApi = new Settings(this._core);
4345
this._behaviorsApi = new Behaviors(this._core);
46+
this._alertsApi = new Alerts(this._core);
4447
this._aboutApi = new About(this._core);
4548
this._fixturesApi = new Fixtures(this._core);
4649
core.addSetting({
@@ -66,28 +69,34 @@ class Plugin {
6669

6770
async init() {
6871
await this._deprecatedApi.init();
69-
this._core.onChangeSettings(this._onChangeSettings);
7072
this._initRouter();
73+
}
74+
75+
start() {
76+
this._stopListeningOnChangeSettings = this._core.onChangeSettings(this._onChangeSettings);
7177
this._addDeprecatedRouter();
7278
this._addRouter();
7379
}
7480

81+
stop() {
82+
if (this._stopListeningOnChangeSettings) {
83+
this._stopListeningOnChangeSettings();
84+
}
85+
this._removeDeprecatedRouter();
86+
this._removeRouter();
87+
}
88+
7589
_initRouter() {
7690
this._router = express.Router();
7791
this._router.use(SETTINGS, this._settingsApi.router);
7892
this._router.use(BEHAVIORS, this._behaviorsApi.router);
7993
this._router.use(ABOUT, this._aboutApi.router);
8094
this._router.use(FIXTURES, this._fixturesApi.router);
95+
this._router.use(ALERTS, this._alertsApi.router);
8196
}
8297

8398
_addDeprecatedRouter() {
84-
if (
85-
this._settings.get(ADMIN_API_DEPRECATED_PATHS_OPTION) === false &&
86-
this._addedDeprecatedRouter
87-
) {
88-
this._core.removeRouter(DEPRECATED_API_PATH, this._deprecatedApi.router);
89-
this._addedDeprecatedRouter = false;
90-
}
99+
this._removeDeprecatedRouter();
91100
if (
92101
this._settings.get(ADMIN_API_DEPRECATED_PATHS_OPTION) === true &&
93102
!this._addedDeprecatedRouter
@@ -97,12 +106,24 @@ class Plugin {
97106
}
98107
}
99108

109+
_removeDeprecatedRouter() {
110+
if (this._addedDeprecatedRouter) {
111+
this._core.removeRouter(DEPRECATED_API_PATH, this._deprecatedApi.router);
112+
this._addedDeprecatedRouter = false;
113+
}
114+
}
115+
100116
_addRouter() {
101-
if (this._previousRoutersPath) {
102-
this._core.removeRouter(this._previousRoutersPath, this._router);
117+
this._removeRouter();
118+
this._routersPath = this._settings.get(ADMIN_API_PATH_OPTION);
119+
this._core.addRouter(this._routersPath, this._router);
120+
}
121+
122+
_removeRouter() {
123+
if (this._routersPath) {
124+
this._core.removeRouter(this._routersPath, this._router);
125+
this._routersPath = null;
103126
}
104-
this._previousRoutersPath = this._settings.get(ADMIN_API_PATH_OPTION);
105-
this._core.addRouter(this._previousRoutersPath, this._router);
106127
}
107128

108129
_onChangeSettings(newSettings) {

test/e2e/about-api.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0
88
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.
99
*/
1010

11-
const { startServer, stopServer, request } = require("./utils");
11+
const { startServer, stopServer, request } = require("./support/utils");
1212
const { version } = require("../../package.json");
1313

1414
describe("about api", () => {
@@ -17,8 +17,8 @@ describe("about api", () => {
1717
server = await startServer("web-tutorial");
1818
});
1919

20-
afterAll(() => {
21-
stopServer(server);
20+
afterAll(async () => {
21+
await stopServer(server);
2222
});
2323

2424
describe("get /", () => {

0 commit comments

Comments
 (0)