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

Commit a1002f5

Browse files
committed
Add fixtures api and acceptance tests
1 parent a62f509 commit a1002f5

5 files changed

Lines changed: 190 additions & 9 deletions

File tree

src/Fixtures.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 FixturesApi {
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.getCollection.bind(this));
26+
this._router.get("/:id", this.getModel.bind(this));
27+
}
28+
29+
_parseModel(fixture) {
30+
return {
31+
id: fixture.id,
32+
requestMatchId: fixture.requestMatchId,
33+
handler: fixture.constructor.displayName,
34+
request: fixture.request,
35+
response: fixture.response
36+
};
37+
}
38+
39+
_parseCollection() {
40+
return this._fixtures.collection.map(this._parseModel);
41+
}
42+
43+
getCollection(req, res) {
44+
this._tracer.verbose(`${PLUGIN_NAME}: Sending fixtures | ${req.id}`);
45+
res.status(200);
46+
res.send(this._parseCollection());
47+
}
48+
49+
getModel(req, res, next) {
50+
const id = req.params.id;
51+
this._tracer.verbose(`${PLUGIN_NAME}: Sending fixture ${id} | ${req.id}`);
52+
const foundFixture = this._fixtures.collection.find(fixture => fixture.id === id);
53+
if (foundFixture) {
54+
res.status(200);
55+
res.send(this._parseModel(foundFixture));
56+
} else {
57+
next(Boom.notFound(`Fixture with id "${id}" was not found`));
58+
}
59+
}
60+
61+
get router() {
62+
return this._router;
63+
}
64+
}
65+
66+
module.exports = FixturesApi;

src/Plugin.js

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

1616
const Settings = require("./Settings");
1717
const Behaviors = require("./Behaviors");
18+
const Fixtures = require("./Fixtures");
1819

1920
const {
2021
ADMIN_API_PATH_OPTION,
@@ -23,6 +24,7 @@ const {
2324
PLUGIN_NAME,
2425
SETTINGS_API_PATH,
2526
BEHAVIORS_API_PATH,
27+
FIXTURES_API_PATH,
2628
DEPRECATED_API_PATH
2729
} = require("./constants");
2830

@@ -34,6 +36,7 @@ class Plugin {
3436
this._deprecatedApi = new DeprecatedApi(core);
3537
this._settingsApi = new Settings(this._core);
3638
this._behaviorsApi = new Behaviors(this._core);
39+
this._fixturesApi = new Fixtures(this._core);
3740
core.addSetting({
3841
name: ADMIN_API_PATH_OPTION,
3942
type: "string",
@@ -63,6 +66,7 @@ class Plugin {
6366
this._router = express.Router();
6467
this._router.use(SETTINGS_API_PATH, this._settingsApi.router);
6568
this._router.use(BEHAVIORS_API_PATH, this._behaviorsApi.router);
69+
this._router.use(FIXTURES_API_PATH, this._fixturesApi.router);
6670
}
6771

6872
_addDeprecatedRouter() {

src/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ module.exports = {
1515
PLUGIN_NAME: "plugin-admin-api",
1616
SETTINGS_API_PATH: "/settings",
1717
BEHAVIORS_API_PATH: "/behaviors",
18+
FIXTURES_API_PATH: "/fixtures",
1819
DEPRECATED_API_PATH: "/mocks"
1920
};

test/acceptance/behaviors-api.spec.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ describe("behaviors api", () => {
2222

2323
describe("get /", () => {
2424
it("should return current behaviors", async () => {
25-
const settingsResponse = await request("/admin/behaviors");
26-
expect(settingsResponse).toEqual([
25+
const response = await request("/admin/behaviors");
26+
expect(response).toEqual([
2727
{
2828
extendedFrom: null,
2929
fixtures: ["12e5f429b92f67d4ec2bf90940ec1135", "0dbc954f9d9c9f3f7996c60e63384c9e"],
@@ -53,8 +53,8 @@ describe("behaviors api", () => {
5353

5454
describe("get /standard", () => {
5555
it("should return standard behavior", async () => {
56-
const settingsResponse = await request("/admin/behaviors/standard");
57-
expect(settingsResponse).toEqual({
56+
const response = await request("/admin/behaviors/standard");
57+
expect(response).toEqual({
5858
extendedFrom: null,
5959
fixtures: ["12e5f429b92f67d4ec2bf90940ec1135", "0dbc954f9d9c9f3f7996c60e63384c9e"],
6060
name: "standard"
@@ -64,8 +64,8 @@ describe("behaviors api", () => {
6464

6565
describe("get /dynamic", () => {
6666
it("should return standard behavior", async () => {
67-
const settingsResponse = await request("/admin/behaviors/dynamic");
68-
expect(settingsResponse).toEqual({
67+
const response = await request("/admin/behaviors/dynamic");
68+
expect(response).toEqual({
6969
extendedFrom: "standard",
7070
fixtures: [
7171
"e82af88532da929b0592925899eb056e",
@@ -79,12 +79,12 @@ describe("behaviors api", () => {
7979

8080
describe("get unexistant behavior", () => {
8181
it("should return a not found error", async () => {
82-
const settingsResponse = await request("/admin/behaviors/foo", {
82+
const response = await request("/admin/behaviors/foo", {
8383
resolveWithFullResponse: true,
8484
simple: false
8585
});
86-
expect(settingsResponse.statusCode).toEqual(404);
87-
expect(settingsResponse.body.message).toEqual('Behavior with name "foo" was not found');
86+
expect(response.statusCode).toEqual(404);
87+
expect(response.body.message).toEqual('Behavior with name "foo" was not found');
8888
});
8989
});
9090
});
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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("fixtures 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 fixtures", async () => {
25+
const response = await request("/admin/fixtures");
26+
expect(response).toEqual([
27+
{
28+
id: "e82af88532da929b0592925899eb056e",
29+
requestMatchId: "9989c8c9766561cd432c625deabca48b",
30+
handler: "mocks-server-fixture",
31+
request: { url: "/api/users/:id", method: "GET" },
32+
response: {
33+
type: "dynamic",
34+
function:
35+
'(req, res) => {\n const userId = req.params.id;\n const user = INITIAL_USERS.find(userData => userData.id === Number(userId));\n\n if (user) {\n res.status(200);\n res.send(user);\n } else {\n res.status(404);\n res.send({\n message: "User not found"\n });\n }\n }'
36+
}
37+
},
38+
{
39+
id: "bd5292849ee3fda9fa8383837bb908e7",
40+
requestMatchId: "9989c8c9766561cd432c625deabca48b",
41+
handler: "mocks-server-fixture",
42+
request: { url: "/api/users/:id", method: "GET" },
43+
response: { type: "static", status: 200, body: { id: 2, name: "Jane Doe" } }
44+
},
45+
{
46+
id: "12e5f429b92f67d4ec2bf90940ec1135",
47+
requestMatchId: "9989c8c9766561cd432c625deabca48b",
48+
handler: "mocks-server-fixture",
49+
request: { url: "/api/users/:id", method: "GET" },
50+
response: { type: "static", status: 200, body: { id: 1, name: "John Doe" } }
51+
},
52+
{
53+
id: "0dbc954f9d9c9f3f7996c60e63384c9e",
54+
requestMatchId: "8b4d07b38f9320be1702c093fd1daa76",
55+
handler: "mocks-server-fixture",
56+
request: { url: "/api/users", method: "GET" },
57+
response: {
58+
type: "static",
59+
status: 200,
60+
body: [
61+
{ id: 1, name: "John Doe" },
62+
{ id: 2, name: "Jane Doe" }
63+
]
64+
}
65+
}
66+
]);
67+
});
68+
});
69+
70+
describe("get /e82af88532da929b0592925899eb056e", () => {
71+
it("should return fixture with id e82af88532da929b0592925899eb056e", async () => {
72+
const response = await request("/admin/fixtures/e82af88532da929b0592925899eb056e");
73+
expect(response).toEqual({
74+
id: "e82af88532da929b0592925899eb056e",
75+
requestMatchId: "9989c8c9766561cd432c625deabca48b",
76+
handler: "mocks-server-fixture",
77+
request: { url: "/api/users/:id", method: "GET" },
78+
response: {
79+
type: "dynamic",
80+
function:
81+
'(req, res) => {\n const userId = req.params.id;\n const user = INITIAL_USERS.find(userData => userData.id === Number(userId));\n\n if (user) {\n res.status(200);\n res.send(user);\n } else {\n res.status(404);\n res.send({\n message: "User not found"\n });\n }\n }'
82+
}
83+
});
84+
});
85+
});
86+
87+
describe("get /bd5292849ee3fda9fa8383837bb908e7", () => {
88+
it("should return fixture with id bd5292849ee3fda9fa8383837bb908e7", async () => {
89+
const response = await request("/admin/fixtures/bd5292849ee3fda9fa8383837bb908e7");
90+
expect(response).toEqual({
91+
id: "bd5292849ee3fda9fa8383837bb908e7",
92+
requestMatchId: "9989c8c9766561cd432c625deabca48b",
93+
handler: "mocks-server-fixture",
94+
request: { url: "/api/users/:id", method: "GET" },
95+
response: { type: "static", status: 200, body: { id: 2, name: "Jane Doe" } }
96+
});
97+
});
98+
});
99+
100+
describe("get unexistant fixture", () => {
101+
it("should return a not found error", async () => {
102+
const response = await request("/admin/fixtures/foo", {
103+
resolveWithFullResponse: true,
104+
simple: false
105+
});
106+
expect(response.statusCode).toEqual(404);
107+
expect(response.body.message).toEqual('Fixture with id "foo" was not found');
108+
});
109+
});
110+
});

0 commit comments

Comments
 (0)