Skip to content

Commit b81de8f

Browse files
committed
cds v9 wip test 3
1 parent 5403cd4 commit b81de8f

File tree

4 files changed

+153
-151
lines changed

4 files changed

+153
-151
lines changed

test/cds-test-services/__snapshots__/cds-test-services.test.js.snap renamed to test/cds-test-services/__snapshots__/cds-test-feature-service.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`cds-test feature-service state response no change 1`] = `
3+
exports[`cds-test-feature-service state response no change 1`] = `
44
{
55
"test/feature_a": {
66
"config": {
@@ -100,7 +100,7 @@ exports[`cds-test feature-service state response no change 1`] = `
100100
}
101101
`;
102102

103-
exports[`cds-test feature-service state response with changes 1`] = `
103+
exports[`cds-test-feature-service state response with changes 1`] = `
104104
{
105105
"test/feature_a": {
106106
"config": {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"use strict";
2+
3+
const cds = require("@sap/cds");
4+
const toggles = require("../../src");
5+
6+
jest.spyOn(process, "cwd").mockReturnValue(__dirname);
7+
// TODO how do we get our plugin and @sap/cds-mtxs loaded as part of the plugin startup here
8+
const server = cds.test("test/cds-test-services");
9+
const systemCall = { validateStatus: () => true, auth: { username: "system", password: "system" } };
10+
11+
describe("cds-test-check-service", () => {
12+
afterEach(() => {
13+
jest.clearAllMocks();
14+
});
15+
16+
test("priority endpoint with no feature is false", async () => {
17+
const i = toggles.getFeaturesInfos();
18+
expect(toggles.getFeatureValue("/fts/check-service-extension")).toBe(false);
19+
const response = await server.get("/rest/check/priority", systemCall);
20+
expect(response.status).toBe(200);
21+
expect(response.data).toBe(false);
22+
});
23+
24+
test("priority endpoint with feature is true", async () => {
25+
await toggles.changeFeatureValue("/fts/check-service-extension", true);
26+
expect(toggles.getFeatureValue("/fts/check-service-extension")).toBe(true);
27+
const response = await server.get("/rest/check/priority", systemCall);
28+
expect(response.status).toBe(200);
29+
expect(response.data).toBe(true);
30+
});
31+
});
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
"use strict";
2+
3+
const cds = require("@sap/cds");
4+
const toggles = require("../../src");
5+
6+
const { FEATURE, mockConfig: config } = require("../__common__/mockdata");
7+
8+
const server = cds.test("test/cds-test-services");
9+
const systemCall = { validateStatus: () => true, auth: { username: "system", password: "system" } };
10+
11+
describe("cds-test-feature-service", () => {
12+
beforeEach(async () => {
13+
toggles._reset();
14+
await toggles.initializeFeatures({ config });
15+
});
16+
afterEach(() => {
17+
jest.clearAllMocks();
18+
});
19+
20+
const featureBChanges = [
21+
{
22+
key: FEATURE.B,
23+
value: 2,
24+
},
25+
{
26+
key: FEATURE.B,
27+
value: 20,
28+
scope: {
29+
tenant: "a",
30+
},
31+
},
32+
{
33+
key: FEATURE.B,
34+
value: 30,
35+
scope: {
36+
tenant: "b",
37+
},
38+
},
39+
];
40+
41+
test.each(["/rest/feature/redisSendCommand"])("%s is forbidden for system", async (endpoint) => {
42+
const response = await server.post(endpoint, {}, systemCall);
43+
expect(response.status).toBe(403);
44+
});
45+
46+
test("state response no change", async () => {
47+
const response = await server.get("/rest/feature/state", systemCall);
48+
expect(response.status).toBe(200);
49+
expect(response.data).toMatchSnapshot();
50+
});
51+
52+
test("state response with changes", async () => {
53+
await server.post("/rest/feature/redisUpdate", featureBChanges, systemCall);
54+
const response = await server.get("/rest/feature/state", systemCall);
55+
expect(response.status).toBe(200);
56+
expect(response.data).toMatchSnapshot();
57+
});
58+
59+
test("redisRead response no change", async () => {
60+
const response = await server.post("/rest/feature/redisRead", {}, systemCall);
61+
expect(response.status).toBe(200);
62+
expect(response.data).toMatchInlineSnapshot(`{}`);
63+
});
64+
65+
test("redisRead response with changes", async () => {
66+
await server.post("/rest/feature/redisUpdate", featureBChanges, systemCall);
67+
const response = await server.post("/rest/feature/redisRead", {}, systemCall);
68+
expect(response.status).toBe(200);
69+
expect(response.data).toMatchInlineSnapshot(`
70+
{
71+
"test/feature_b": {
72+
"config": {
73+
"SOURCE": "RUNTIME",
74+
"TYPE": "number",
75+
},
76+
"fallbackValue": 1,
77+
"rootValue": 2,
78+
"scopedValues": {
79+
"tenant::a": 20,
80+
"tenant::b": 30,
81+
},
82+
},
83+
}
84+
`);
85+
});
86+
87+
test("redisUpdate response success", async () => {
88+
const response = await server.post(
89+
"/rest/feature/redisUpdate",
90+
{
91+
key: FEATURE.A,
92+
value: true,
93+
},
94+
systemCall
95+
);
96+
expect(response.status).toBe(204);
97+
expect(response.data).toMatchInlineSnapshot(`""`);
98+
});
99+
100+
test("redisUpdate response validation fail", async () => {
101+
const response = await server.post(
102+
"/rest/feature/redisUpdate",
103+
{
104+
key: FEATURE.A,
105+
value: 100,
106+
},
107+
systemCall
108+
);
109+
expect(response.status).toBe(422);
110+
expect(response.data).toMatchInlineSnapshot(`
111+
{
112+
"error": {
113+
"code": "422",
114+
"message": "value "100" has invalid type number, must be boolean",
115+
"target": "test/feature_a",
116+
},
117+
}
118+
`);
119+
});
120+
});

test/cds-test-services/cds-test-services.test.js

Lines changed: 0 additions & 149 deletions
This file was deleted.

0 commit comments

Comments
 (0)