forked from cap-js-community/odata-v2-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.test.js
124 lines (108 loc) · 4.4 KB
/
auth.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"use strict";
const cds = require("@sap/cds");
const supertest = require("supertest");
const util = require("./_env/util/request");
process.env.CDS_LOG_LEVELS_COV2AP = "debug";
const consoleDebugSpy = jest.spyOn(global.console, "debug");
consoleDebugSpy.mockImplementation(() => {});
cds.test(__dirname + "/_env");
let request;
const validAuth = `Basic ${Buffer.from(
`${cds.requires.auth.users.alice.id}:${cds.requires.auth.users.alice.password}`,
).toString("base64")}`;
const invalidAuth = `Basic ${Buffer.from(
`${cds.requires.auth.users.bob.id}:${cds.requires.auth.users.bob.password}`,
).toString("base64")}`;
describe("auth", () => {
beforeAll(async () => {
await global._init;
request = supertest(cds.app.server);
});
it("GET $metadata auth", async () => {
let response = await util.callRead(request, "/odata/v2/auth/$metadata", {
accept: "application/xml",
});
expect(response.status).toEqual(401);
expect(response.headers["www-authenticate"]).toEqual('Basic realm="Users"');
response = await util.callRead(request, "/odata/v2/auth/$metadata", {
accept: "application/xml",
Authorization: invalidAuth,
});
expect(response.status).toEqual(403);
response = await util.callRead(request, "/odata/v2/auth/$metadata", {
accept: "application/xml",
Authorization: validAuth,
});
expect(response.status).toEqual(200);
response = await util.callRead(request, "/odata/v2/auth/$metadata", {
accept: "application/xml",
Authorization:
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
});
expect(response.status).toEqual(401);
});
it("GET $metadata invalid auth", async () => {
const consoleSpy = jest.spyOn(console, "error");
let response = await util.callRead(request, "/odata/v2/auth/$metadata", {
accept: "application/xml",
Authorization: "Bearer xyz",
});
expect(response.status).toEqual(401);
expect(consoleSpy).toHaveBeenCalledWith(
"[cov2ap] -",
"Authorization:",
expect.objectContaining(new Error("Invalid JWT token")),
);
response = await util.callRead(request, "/odata/v2/auth/$metadata", {
accept: "application/xml",
Authorization: validAuth,
});
expect(response.status).toEqual(200);
});
it("GET service root invalid auth", async () => {
const consoleSpy = jest.spyOn(console, "error");
let response = await util.callRead(request, "/odata/v2/auth/", {
accept: "application/xml",
Authorization: "Bearer xyz",
});
expect(response.status).toEqual(401);
expect(consoleSpy).toHaveBeenCalledWith(
"[cov2ap] -",
"Authorization:",
expect.objectContaining(new Error("Invalid JWT token")),
);
response = await util.callRead(request, "/odata/v2/auth/", {
accept: "application/xml",
Authorization: validAuth,
});
expect(response.status).toEqual(200);
});
it("GET $metadata check response correlation", async () => {
const response = await util.callRead(request, "/odata/v2/auth/$metadata", {
accept: "application/xml",
Authorization: validAuth,
});
expect(response.status).toEqual(200);
expect(response.headers["x-request-id"]).toBeDefined();
expect(response.headers["x-correlation-id"]).toBeDefined();
expect(response.headers["x-correlationid"]).toBeDefined();
});
it("GET sanitize authorization header for debug traces", async () => {
consoleDebugSpy.mockReset();
const response = await util.callRead(request, "/odata/v2/auth/Header", {
accept: "application/xml",
Authorization: validAuth,
});
expect(response.status).toEqual(200);
const traceRequest = consoleDebugSpy.mock.calls.find((call) => call[1] === "Request:");
expect(traceRequest).toBeDefined();
expect(traceRequest[2]).toMatch(/Basic \*\*\*/);
const traceProxyRequest = consoleDebugSpy.mock.calls.find((call) => call[1] === "ProxyRequest:");
expect(traceProxyRequest).toBeDefined();
expect(traceProxyRequest[2]).toMatch(/Basic \*\*\*/);
const traceResponse = consoleDebugSpy.mock.calls.find((call) => call[1] === "Response:");
expect(traceResponse).toBeDefined();
const traceProxyResponse = consoleDebugSpy.mock.calls.find((call) => call[1] === "ProxyResponse:");
expect(traceProxyResponse).toBeDefined();
});
});