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

Commit 305bfab

Browse files
committed
Refactor getAuth to fetch all of the auth values from a given operation
1 parent ddffc6a commit 305bfab

File tree

3 files changed

+84
-41
lines changed

3 files changed

+84
-41
lines changed

packages/api-explorer/__tests__/fixtures/multiple-securities/oas.json

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
}
1111
],
1212
"paths": {
13-
"/things": {
13+
"/or-security": {
1414
"post": {
15-
"operationId": "things",
15+
"operationId": "orSecurity",
1616
"security": [
1717
{
1818
"oauthScheme": ["write:things"]
@@ -75,28 +75,6 @@
7575
}
7676
}
7777
},
78-
"/or-security": {
79-
"post": {
80-
"operationId": "orSecurity",
81-
"security": [
82-
{
83-
"oauthScheme": ["write:things"]
84-
},
85-
{
86-
"apiKeyScheme": []
87-
}
88-
],
89-
"summary": "or security",
90-
"description": "",
91-
"operationId": "addOrSecurity",
92-
"parameters": [],
93-
"responses": {
94-
"405": {
95-
"description": "Invalid input"
96-
}
97-
}
98-
}
99-
},
10078
"/single-auth": {
10179
"post": {
10280
"operationId": "singleAuth",
@@ -158,7 +136,7 @@
158136
"security": [
159137
{
160138
"oauthScheme": ["write:things", "read:things"],
161-
"specialMagicAuth": []
139+
"unknownAuthType": []
162140
},
163141
{
164142
"oauthDiff": ["write:things", "read:things"]
@@ -181,7 +159,7 @@
181159
"security": [
182160
{
183161
"oauthScheme": ["write:things", "read:things"],
184-
"specialMagicAuth": []
162+
"unknownAuthType": []
185163
},
186164
{
187165
"nonExistentScheme": []
@@ -203,7 +181,7 @@
203181
"operationId": "unknownAuthType",
204182
"security": [
205183
{
206-
"specialMagicAuth": []
184+
"unknownAuthType": []
207185
}
208186
],
209187
"summary": "unknown auth type",
@@ -259,7 +237,7 @@
259237
"name": "apiKey",
260238
"in": "header"
261239
},
262-
"specialMagicAuth": {
240+
"unknownAuthType": {
263241
"type": "demigorgon",
264242
"name": "eleven",
265243
"in": "header"
Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,54 @@
1+
const multipleSecurities = require('../fixtures/multiple-securities/oas.json');
2+
const Oas = require('../../src/lib/Oas');
13
const getAuth = require('../../src/lib/get-auth');
24

5+
const oas = new Oas(multipleSecurities);
6+
7+
it('should work for || security', () => {
8+
expect(
9+
getAuth(
10+
{ oauthScheme: 'oauth', apiKeyScheme: 'apikey' },
11+
oas.operation('/or-security', 'post'),
12+
),
13+
).toEqual({ oauthScheme: 'oauth', apiKeyScheme: 'apikey' });
14+
});
15+
16+
it('should work for && security', () => {
17+
expect(
18+
getAuth(
19+
{ oauthScheme: 'oauth', apiKeyScheme: 'apikey' },
20+
oas.operation('/and-security', 'post'),
21+
),
22+
).toEqual({ oauthScheme: 'oauth', apiKeyScheme: 'apikey' });
23+
});
24+
25+
it('should work for && and || security', () => {
26+
expect(
27+
getAuth(
28+
{ oauthScheme: 'oauth', apiKeyScheme: 'apikey', oauthDiff: 'oauthDiff' },
29+
oas.operation('/and-or-security', 'post'),
30+
),
31+
).toEqual({ oauthScheme: 'oauth', apiKeyScheme: 'apikey', oauthDiff: 'oauthDiff' });
32+
});
33+
34+
it('should work for single auth', () => {
35+
expect(getAuth({ apiKeyScheme: 'apikey' }, oas.operation('/single-auth', 'post'))).toEqual({
36+
apiKeyScheme: 'apikey',
37+
});
38+
});
39+
40+
it('should work for no auth', () => {
41+
expect(getAuth({}, oas.operation('/no-auth', 'post'))).toEqual({});
42+
});
43+
44+
it('should work for unknown auth type', () => {
45+
expect(getAuth({}, oas.operation('/unknown-auth-type', 'post'))).toEqual({
46+
unknownAuthType: null,
47+
});
48+
});
49+
50+
const { getSingle } = getAuth;
51+
352
const topLevelUser = { apiKey: '123456', user: 'user', pass: 'pass' };
453
const keysUser = { keys: [{ apiKey: '123456', name: 'app-1' }, { apiKey: '7890', name: 'app-2' }] };
554
const topLevelSchemeUser = { schemeName: 'scheme-key' };
@@ -12,46 +61,46 @@ const keysSchemeUser = {
1261
};
1362

1463
it('should return apiKey property for oauth', () => {
15-
expect(getAuth(topLevelUser, { type: 'oauth2' })).toBe('123456');
64+
expect(getSingle(topLevelUser, { type: 'oauth2' })).toBe('123456');
1665
});
1766

1867
it('should return apiKey property for apiKey', () => {
19-
expect(getAuth(topLevelUser, { type: 'oauth2' })).toBe('123456');
68+
expect(getSingle(topLevelUser, { type: 'oauth2' })).toBe('123456');
2069
});
2170

2271
it('should return user/pass properties for basic auth', () => {
23-
expect(getAuth(topLevelUser, { type: 'http', scheme: 'basic' })).toEqual({
72+
expect(getSingle(topLevelUser, { type: 'http', scheme: 'basic' })).toEqual({
2473
user: 'user',
2574
pass: 'pass',
2675
});
2776
});
2877

2978
it('should return first item from keys array if no app selected', () => {
30-
expect(getAuth(keysUser, { type: 'oauth2' })).toBe('123456');
79+
expect(getSingle(keysUser, { type: 'oauth2' })).toBe('123456');
3180
});
3281

3382
it('should return selected app from keys array if app provided', () => {
34-
expect(getAuth(keysUser, { type: 'oauth2' }, 'app-2')).toBe('7890');
83+
expect(getSingle(keysUser, { type: 'oauth2' }, 'app-2')).toBe('7890');
3584
});
3685

3786
it('should return item by scheme name if no apiKey/user/pass', () => {
38-
expect(getAuth(topLevelSchemeUser, { type: 'oauth2', _key: 'schemeName' })).toBe('scheme-key');
39-
expect(getAuth(keysSchemeUser, { type: 'oauth2', _key: 'schemeName' })).toBe('scheme-key-1');
40-
expect(getAuth(keysSchemeUser, { type: 'oauth2', _key: 'schemeName' }, 'app-2')).toBe(
87+
expect(getSingle(topLevelSchemeUser, { type: 'oauth2', _key: 'schemeName' })).toBe('scheme-key');
88+
expect(getSingle(keysSchemeUser, { type: 'oauth2', _key: 'schemeName' })).toBe('scheme-key-1');
89+
expect(getSingle(keysSchemeUser, { type: 'oauth2', _key: 'schemeName' }, 'app-2')).toBe(
4190
'scheme-key-2',
4291
);
43-
expect(getAuth(keysSchemeUser, { type: 'http', _key: 'schemeName' }, 'app-3')).toEqual({
92+
expect(getSingle(keysSchemeUser, { type: 'http', _key: 'schemeName' }, 'app-3')).toEqual({
4493
user: 'user',
4594
pass: 'pass',
4695
});
4796
});
4897

4998
it('should return null for anything else', () => {
50-
expect(getAuth(topLevelUser, { type: 'unknown' })).toBe(null);
51-
expect(getAuth(keysUser, { type: 'unknown' })).toBe(null);
52-
expect(getAuth(keysUser, { type: 'unknown' }, 'app-2')).toBe(null);
99+
expect(getSingle(topLevelUser, { type: 'unknown' })).toBe(null);
100+
expect(getSingle(keysUser, { type: 'unknown' })).toBe(null);
101+
expect(getSingle(keysUser, { type: 'unknown' }, 'app-2')).toBe(null);
53102
});
54103

55104
it('should allow scheme to be undefined', () => {
56-
expect(getAuth(topLevelUser)).toBe(null);
105+
expect(getSingle(topLevelUser)).toBe(null);
57106
});

packages/api-explorer/src/lib/get-auth.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,20 @@ function getAuth(user, scheme, selectedApp = false) {
1919
return getKey(user, scheme);
2020
}
2121

22+
function getAuth(user, operation) {
23+
return operation
24+
.getSecurity()
25+
.map(securityRequirement => {
26+
return Object.keys(securityRequirement)
27+
.map(name => {
28+
operation.oas.components.securitySchemes[name]._key = name;
29+
return { [name]: getSingle(user, operation.oas.components.securitySchemes[name]) };
30+
})
31+
.reduce((prev, next) => Object.assign(prev, next));
32+
})
33+
.reduce((prev, next) => Object.assign(prev, next), {});
34+
}
35+
2236
module.exports = getAuth;
37+
38+
module.exports.getSingle = getSingle;

0 commit comments

Comments
 (0)