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

Commit a62f509

Browse files
committed
Add behaviors api and acceptance tests
1 parent b3e4d0c commit a62f509

4 files changed

Lines changed: 159 additions & 0 deletions

File tree

src/Behaviors.js

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

src/Plugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ const express = require("express");
1414
const DeprecatedApi = require("./deprecated/Api");
1515

1616
const Settings = require("./Settings");
17+
const Behaviors = require("./Behaviors");
1718

1819
const {
1920
ADMIN_API_PATH_OPTION,
2021
ADMIN_API_DEPRECATED_PATHS_OPTION,
2122
DEFAULT_API_PATH,
2223
PLUGIN_NAME,
2324
SETTINGS_API_PATH,
25+
BEHAVIORS_API_PATH,
2426
DEPRECATED_API_PATH
2527
} = require("./constants");
2628

@@ -31,6 +33,7 @@ class Plugin {
3133
this._settings = this._core.settings;
3234
this._deprecatedApi = new DeprecatedApi(core);
3335
this._settingsApi = new Settings(this._core);
36+
this._behaviorsApi = new Behaviors(this._core);
3437
core.addSetting({
3538
name: ADMIN_API_PATH_OPTION,
3639
type: "string",
@@ -59,6 +62,7 @@ class Plugin {
5962
_initRouter() {
6063
this._router = express.Router();
6164
this._router.use(SETTINGS_API_PATH, this._settingsApi.router);
65+
this._router.use(BEHAVIORS_API_PATH, this._behaviorsApi.router);
6266
}
6367

6468
_addDeprecatedRouter() {

src/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ module.exports = {
1414
DEFAULT_API_PATH: "/admin",
1515
PLUGIN_NAME: "plugin-admin-api",
1616
SETTINGS_API_PATH: "/settings",
17+
BEHAVIORS_API_PATH: "/behaviors",
1718
DEPRECATED_API_PATH: "/mocks"
1819
};
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
13+
describe("behaviors api", () => {
14+
let server;
15+
beforeAll(async () => {
16+
server = await startServer("web-tutorial");
17+
});
18+
19+
afterAll(() => {
20+
stopServer(server);
21+
});
22+
23+
describe("get /", () => {
24+
it("should return current behaviors", async () => {
25+
const settingsResponse = await request("/admin/behaviors");
26+
expect(settingsResponse).toEqual([
27+
{
28+
extendedFrom: null,
29+
fixtures: ["12e5f429b92f67d4ec2bf90940ec1135", "0dbc954f9d9c9f3f7996c60e63384c9e"],
30+
name: "standard"
31+
},
32+
{
33+
extendedFrom: "standard",
34+
fixtures: [
35+
"bd5292849ee3fda9fa8383837bb908e7",
36+
"12e5f429b92f67d4ec2bf90940ec1135",
37+
"0dbc954f9d9c9f3f7996c60e63384c9e"
38+
],
39+
name: "user2"
40+
},
41+
{
42+
extendedFrom: "standard",
43+
fixtures: [
44+
"e82af88532da929b0592925899eb056e",
45+
"12e5f429b92f67d4ec2bf90940ec1135",
46+
"0dbc954f9d9c9f3f7996c60e63384c9e"
47+
],
48+
name: "dynamic"
49+
}
50+
]);
51+
});
52+
});
53+
54+
describe("get /standard", () => {
55+
it("should return standard behavior", async () => {
56+
const settingsResponse = await request("/admin/behaviors/standard");
57+
expect(settingsResponse).toEqual({
58+
extendedFrom: null,
59+
fixtures: ["12e5f429b92f67d4ec2bf90940ec1135", "0dbc954f9d9c9f3f7996c60e63384c9e"],
60+
name: "standard"
61+
});
62+
});
63+
});
64+
65+
describe("get /dynamic", () => {
66+
it("should return standard behavior", async () => {
67+
const settingsResponse = await request("/admin/behaviors/dynamic");
68+
expect(settingsResponse).toEqual({
69+
extendedFrom: "standard",
70+
fixtures: [
71+
"e82af88532da929b0592925899eb056e",
72+
"12e5f429b92f67d4ec2bf90940ec1135",
73+
"0dbc954f9d9c9f3f7996c60e63384c9e"
74+
],
75+
name: "dynamic"
76+
});
77+
});
78+
});
79+
80+
describe("get unexistant behavior", () => {
81+
it("should return a not found error", async () => {
82+
const settingsResponse = await request("/admin/behaviors/foo", {
83+
resolveWithFullResponse: true,
84+
simple: false
85+
});
86+
expect(settingsResponse.statusCode).toEqual(404);
87+
expect(settingsResponse.body.message).toEqual('Behavior with name "foo" was not found');
88+
});
89+
});
90+
});

0 commit comments

Comments
 (0)