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

Commit 9654df4

Browse files
committed
Add acceptance tests for all options in settings api
1 parent 6f9164b commit 9654df4

6 files changed

Lines changed: 278 additions & 21 deletions

File tree

src/Plugin.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ const {
2020
ADMIN_API_DEPRECATED_PATHS_OPTION,
2121
DEFAULT_API_PATH,
2222
PLUGIN_NAME,
23-
SETTINGS_API_PATH
23+
SETTINGS_API_PATH,
24+
DEPRECATED_API_PATH
2425
} = require("./constants");
2526

2627
class Plugin {
@@ -43,21 +44,55 @@ class Plugin {
4344
description: `Disable deprecated paths of ${PLUGIN_NAME}`,
4445
default: true
4546
});
47+
48+
this._onChangeSettings = this._onChangeSettings.bind(this);
4649
}
4750

4851
async init() {
49-
if (this._settings.get(ADMIN_API_DEPRECATED_PATHS_OPTION) === true) {
50-
await this._deprecatedApi.init();
51-
}
52+
await this._deprecatedApi.init();
53+
this._core.onChangeSettings(this._onChangeSettings);
54+
this._initRouter();
55+
this._addDeprecatedRouter();
5256
this._addRouter();
5357
}
5458

55-
_addRouter() {
59+
_initRouter() {
5660
this._router = express.Router();
5761
this._router.use(SETTINGS_API_PATH, this._settingsApi.router);
62+
}
63+
64+
_addDeprecatedRouter() {
65+
if (
66+
this._settings.get(ADMIN_API_DEPRECATED_PATHS_OPTION) === false &&
67+
this._addedDeprecatedRouter
68+
) {
69+
this._core.removeRouter(DEPRECATED_API_PATH, this._deprecatedApi.router);
70+
this._addedDeprecatedRouter = false;
71+
}
72+
if (
73+
this._settings.get(ADMIN_API_DEPRECATED_PATHS_OPTION) === true &&
74+
!this._addedDeprecatedRouter
75+
) {
76+
this._core.addRouter(DEPRECATED_API_PATH, this._deprecatedApi.router);
77+
this._addedDeprecatedRouter = true;
78+
}
79+
}
5880

59-
this._core.addRouter(this._settings.get(ADMIN_API_PATH_OPTION), this._router);
60-
return Promise.resolve();
81+
_addRouter() {
82+
if (this._previousRoutersPath) {
83+
this._core.removeRouter(this._previousRoutersPath, this._router);
84+
}
85+
this._previousRoutersPath = this._settings.get(ADMIN_API_PATH_OPTION);
86+
this._core.addRouter(this._previousRoutersPath, this._router);
87+
}
88+
89+
_onChangeSettings(newSettings) {
90+
if (newSettings.hasOwnProperty(ADMIN_API_DEPRECATED_PATHS_OPTION)) {
91+
this._addDeprecatedRouter();
92+
}
93+
if (newSettings.hasOwnProperty(ADMIN_API_PATH_OPTION)) {
94+
this._addRouter();
95+
}
6196
}
6297
}
6398

src/constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ module.exports = {
1313
ADMIN_API_DEPRECATED_PATHS_OPTION: "adminApiDeprecatedPaths",
1414
DEFAULT_API_PATH: "/admin",
1515
PLUGIN_NAME: "plugin-admin-api",
16-
SETTINGS_API_PATH: "/settings"
16+
SETTINGS_API_PATH: "/settings",
17+
DEPRECATED_API_PATH: "/mocks"
1718
};

src/deprecated/Api.js

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

12-
"use strict";
13-
1412
const express = require("express");
1513

1614
const Behaviors = require("./Behaviors");
1715
const Settings = require("./Settings");
1816

19-
const MOCKS_PATH = "/mocks";
20-
21-
const { DEFAULT_API_PATH, PLUGIN_NAME } = require("../constants");
17+
const { DEFAULT_API_PATH, DEPRECATED_API_PATH, PLUGIN_NAME } = require("../constants");
2218

2319
// TODO, deprecate mocks router
2420

@@ -32,16 +28,20 @@ class Api {
3228
const behaviorsRouter = new Behaviors(this._core).router;
3329
this._router = express.Router();
3430
this._router.use((req, res, next) => {
35-
this._core.tracer.deprecationWarn(`"${MOCKS_PATH}" ${PLUGIN_NAME} path`, DEFAULT_API_PATH);
31+
this._core.tracer.deprecationWarn(
32+
`"${DEPRECATED_API_PATH}" ${PLUGIN_NAME} path`,
33+
DEFAULT_API_PATH
34+
);
3635
next();
3736
});
3837

3938
this._router.use("/features", behaviorsRouter);
4039
this._router.use("/behaviors", behaviorsRouter);
4140
this._router.use("/settings", new Settings(this._core.settings, this._tracer).router);
41+
}
4242

43-
this._core.addCustomRouter(MOCKS_PATH, this._router);
44-
return Promise.resolve();
43+
get router() {
44+
return this._router;
4545
}
4646
}
4747

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,35 +153,100 @@ describe("settings api", () => {
153153
await request("/admin/settings", {
154154
method: "PATCH",
155155
body: {
156-
port: 3001
156+
port: 3101
157157
}
158158
});
159159
await wait(1000);
160160
});
161161

162162
afterAll(async () => {
163163
await request("/admin/settings", {
164+
port: 3101,
164165
method: "PATCH",
165166
body: {
166-
port: 3000
167+
port: 3100
167168
}
168169
});
169170
await wait(1000);
170171
});
171172

172173
it("should return new port option when getting settings, using new port", async () => {
173174
const settingsResponse = await request("/admin/settings", {
174-
port: 3001
175+
port: 3101
175176
});
176-
expect(settingsResponse.port).toEqual(3001);
177+
expect(settingsResponse.port).toEqual(3101);
177178
});
178179

179180
it("should serve user 2 under the /api/users/1 path using new port", async () => {
180181
const users = await request("/api/users/1", {
181-
port: 3001
182+
port: 3101
182183
});
183184
expect(users).toEqual({ id: 2, name: "Jane Doe" });
184185
});
185186
});
187+
188+
describe("when changing adminApiPath option", () => {
189+
beforeAll(async () => {
190+
await request("/admin/settings", {
191+
method: "PATCH",
192+
body: {
193+
adminApiPath: "/administration"
194+
}
195+
});
196+
await wait(1000);
197+
});
198+
199+
afterAll(async () => {
200+
await request("/administration/settings", {
201+
method: "PATCH",
202+
body: {
203+
adminApiPath: "/admin"
204+
}
205+
});
206+
await wait(1000);
207+
});
208+
209+
it("should return new port adminApiPath when getting settings, using new admin api path", async () => {
210+
const settingsResponse = await request("/administration/settings");
211+
expect(settingsResponse.adminApiPath).toEqual("/administration");
212+
});
213+
214+
it("should return not found adminApiPath when getting settings in old admin api path", async () => {
215+
const settingsResponse = await request("/admin/settings", {
216+
resolveWithFullResponse: true,
217+
simple: false
218+
});
219+
expect(settingsResponse.statusCode).toEqual(404);
220+
});
221+
});
222+
223+
describe("without changing adminApiDeprecatedPaths option", () => {
224+
it("should return current delay option in deprecated api path", async () => {
225+
const settingsResponse = await request("/mocks/settings");
226+
expect(settingsResponse).toEqual({
227+
delay: 0
228+
});
229+
});
230+
});
231+
232+
describe("when changing adminApiDeprecatedPaths option", () => {
233+
beforeAll(async () => {
234+
await request("/admin/settings", {
235+
method: "PATCH",
236+
body: {
237+
adminApiDeprecatedPaths: false
238+
}
239+
});
240+
await wait(1000);
241+
});
242+
243+
it("should return not found when getting settings in deprecated admin api path", async () => {
244+
const settingsResponse = await request("/mocks/settings", {
245+
resolveWithFullResponse: true,
246+
simple: false
247+
});
248+
expect(settingsResponse.statusCode).toEqual(404);
249+
});
250+
});
186251
});
187252
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
const path = require("path");
11+
const fsExtra = require("fs-extra");
12+
13+
const { CliRunner, request, fixturesFolder, wait } = require("./utils");
14+
15+
describe("log option modified through api", () => {
16+
let cli;
17+
beforeAll(async () => {
18+
cli = new CliRunner(["node", "start.js", "--path=web-tutorial", "--log=info"], {
19+
cwd: path.resolve(__dirname, "fixtures")
20+
});
21+
await wait(1000);
22+
});
23+
24+
afterAll(async () => {
25+
await cli.kill();
26+
});
27+
28+
describe("when started", () => {
29+
it("should return log level when getting settings", async () => {
30+
const settingsResponse = await request("/admin/settings");
31+
expect(settingsResponse.log).toEqual("info");
32+
});
33+
34+
it("should not have logged any log with verbose level", async () => {
35+
expect(cli.logs).toEqual(expect.not.stringContaining("Mocks verbose"));
36+
});
37+
});
38+
39+
describe("when log level is changed to verbose", () => {
40+
beforeAll(async () => {
41+
await request("/admin/settings", {
42+
method: "PATCH",
43+
body: {
44+
log: "verbose"
45+
}
46+
});
47+
fsExtra.copySync(fixturesFolder("files-modification"), fixturesFolder("files-watch"));
48+
await wait(1000);
49+
});
50+
51+
it("should return new log level when getting settings", async () => {
52+
const settingsResponse = await request("/admin/settings");
53+
expect(settingsResponse.log).toEqual("verbose");
54+
});
55+
56+
it("should have logged logs with verbose level", async () => {
57+
expect(cli.logs).toEqual(expect.stringContaining("Mocks verbose"));
58+
});
59+
});
60+
});
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
const path = require("path");
11+
const fsExtra = require("fs-extra");
12+
13+
const { CliRunner, request, fixturesFolder, wait } = require("./utils");
14+
15+
describe("watch option modified through api", () => {
16+
let cli;
17+
beforeAll(async () => {
18+
fsExtra.removeSync(fixturesFolder("files-watch"));
19+
fsExtra.copySync(fixturesFolder("web-tutorial"), fixturesFolder("files-watch"));
20+
cli = new CliRunner(["node", "start.js", "--path=files-watch", "--watch=false"], {
21+
cwd: path.resolve(__dirname, "fixtures")
22+
});
23+
await wait(1000);
24+
});
25+
26+
afterAll(async () => {
27+
await cli.kill();
28+
});
29+
30+
describe("when started", () => {
31+
it("should return watch disabled when getting settings", async () => {
32+
const settingsResponse = await request("/admin/settings");
33+
expect(settingsResponse.watch).toEqual(false);
34+
});
35+
36+
it("should serve users collection mock under the /api/users path", async () => {
37+
const users = await request("/api/users");
38+
expect(users).toEqual([
39+
{ id: 1, name: "John Doe" },
40+
{ id: 2, name: "Jane Doe" }
41+
]);
42+
});
43+
});
44+
45+
describe("when watch is enabled and files are modified", () => {
46+
beforeAll(async () => {
47+
await request("/admin/settings", {
48+
method: "PATCH",
49+
body: {
50+
watch: true
51+
}
52+
});
53+
fsExtra.copySync(fixturesFolder("files-modification"), fixturesFolder("files-watch"));
54+
await wait(1000);
55+
});
56+
57+
it("should return watch enabled when getting settings", async () => {
58+
const settingsResponse = await request("/admin/settings");
59+
expect(settingsResponse.watch).toEqual(true);
60+
});
61+
62+
it("should serve new users collection mock under the /api/users path", async () => {
63+
const users = await request("/api/users");
64+
expect(users).toEqual([
65+
{ id: 1, name: "John Doe modified" },
66+
{ id: 2, name: "Jane Doe modified" }
67+
]);
68+
});
69+
});
70+
71+
describe("when watch is disabled and files are modified", () => {
72+
beforeAll(async () => {
73+
await request("/admin/settings", {
74+
method: "PATCH",
75+
body: {
76+
watch: false
77+
}
78+
});
79+
fsExtra.copySync(fixturesFolder("web-tutorial"), fixturesFolder("files-watch"));
80+
await wait(1000);
81+
});
82+
83+
it("should return watch disabled when getting settings", async () => {
84+
const settingsResponse = await request("/admin/settings");
85+
expect(settingsResponse.watch).toEqual(false);
86+
});
87+
88+
it("should serve new users collection mock under the /api/users path", async () => {
89+
const users = await request("/api/users");
90+
expect(users).toEqual([
91+
{ id: 1, name: "John Doe modified" },
92+
{ id: 2, name: "Jane Doe modified" }
93+
]);
94+
});
95+
});
96+
});

0 commit comments

Comments
 (0)