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

Commit

Permalink
Refactor getAuth to fetch all of the auth values from a given operation
Browse files Browse the repository at this point in the history
  • Loading branch information
domharrington committed Jan 25, 2019
1 parent ddffc6a commit 305bfab
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
}
],
"paths": {
"/things": {
"/or-security": {
"post": {
"operationId": "things",
"operationId": "orSecurity",
"security": [
{
"oauthScheme": ["write:things"]
Expand Down Expand Up @@ -75,28 +75,6 @@
}
}
},
"/or-security": {
"post": {
"operationId": "orSecurity",
"security": [
{
"oauthScheme": ["write:things"]
},
{
"apiKeyScheme": []
}
],
"summary": "or security",
"description": "",
"operationId": "addOrSecurity",
"parameters": [],
"responses": {
"405": {
"description": "Invalid input"
}
}
}
},
"/single-auth": {
"post": {
"operationId": "singleAuth",
Expand Down Expand Up @@ -158,7 +136,7 @@
"security": [
{
"oauthScheme": ["write:things", "read:things"],
"specialMagicAuth": []
"unknownAuthType": []
},
{
"oauthDiff": ["write:things", "read:things"]
Expand All @@ -181,7 +159,7 @@
"security": [
{
"oauthScheme": ["write:things", "read:things"],
"specialMagicAuth": []
"unknownAuthType": []
},
{
"nonExistentScheme": []
Expand All @@ -203,7 +181,7 @@
"operationId": "unknownAuthType",
"security": [
{
"specialMagicAuth": []
"unknownAuthType": []
}
],
"summary": "unknown auth type",
Expand Down Expand Up @@ -259,7 +237,7 @@
"name": "apiKey",
"in": "header"
},
"specialMagicAuth": {
"unknownAuthType": {
"type": "demigorgon",
"name": "eleven",
"in": "header"
Expand Down
75 changes: 62 additions & 13 deletions packages/api-explorer/__tests__/lib/get-auth.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,54 @@
const multipleSecurities = require('../fixtures/multiple-securities/oas.json');
const Oas = require('../../src/lib/Oas');
const getAuth = require('../../src/lib/get-auth');

const oas = new Oas(multipleSecurities);

it('should work for || security', () => {
expect(
getAuth(
{ oauthScheme: 'oauth', apiKeyScheme: 'apikey' },
oas.operation('/or-security', 'post'),
),
).toEqual({ oauthScheme: 'oauth', apiKeyScheme: 'apikey' });
});

it('should work for && security', () => {
expect(
getAuth(
{ oauthScheme: 'oauth', apiKeyScheme: 'apikey' },
oas.operation('/and-security', 'post'),
),
).toEqual({ oauthScheme: 'oauth', apiKeyScheme: 'apikey' });
});

it('should work for && and || security', () => {
expect(
getAuth(
{ oauthScheme: 'oauth', apiKeyScheme: 'apikey', oauthDiff: 'oauthDiff' },
oas.operation('/and-or-security', 'post'),
),
).toEqual({ oauthScheme: 'oauth', apiKeyScheme: 'apikey', oauthDiff: 'oauthDiff' });
});

it('should work for single auth', () => {
expect(getAuth({ apiKeyScheme: 'apikey' }, oas.operation('/single-auth', 'post'))).toEqual({
apiKeyScheme: 'apikey',
});
});

it('should work for no auth', () => {
expect(getAuth({}, oas.operation('/no-auth', 'post'))).toEqual({});
});

it('should work for unknown auth type', () => {
expect(getAuth({}, oas.operation('/unknown-auth-type', 'post'))).toEqual({
unknownAuthType: null,
});
});

const { getSingle } = getAuth;

const topLevelUser = { apiKey: '123456', user: 'user', pass: 'pass' };
const keysUser = { keys: [{ apiKey: '123456', name: 'app-1' }, { apiKey: '7890', name: 'app-2' }] };
const topLevelSchemeUser = { schemeName: 'scheme-key' };
Expand All @@ -12,46 +61,46 @@ const keysSchemeUser = {
};

it('should return apiKey property for oauth', () => {
expect(getAuth(topLevelUser, { type: 'oauth2' })).toBe('123456');
expect(getSingle(topLevelUser, { type: 'oauth2' })).toBe('123456');
});

it('should return apiKey property for apiKey', () => {
expect(getAuth(topLevelUser, { type: 'oauth2' })).toBe('123456');
expect(getSingle(topLevelUser, { type: 'oauth2' })).toBe('123456');
});

it('should return user/pass properties for basic auth', () => {
expect(getAuth(topLevelUser, { type: 'http', scheme: 'basic' })).toEqual({
expect(getSingle(topLevelUser, { type: 'http', scheme: 'basic' })).toEqual({
user: 'user',
pass: 'pass',
});
});

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

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

it('should return item by scheme name if no apiKey/user/pass', () => {
expect(getAuth(topLevelSchemeUser, { type: 'oauth2', _key: 'schemeName' })).toBe('scheme-key');
expect(getAuth(keysSchemeUser, { type: 'oauth2', _key: 'schemeName' })).toBe('scheme-key-1');
expect(getAuth(keysSchemeUser, { type: 'oauth2', _key: 'schemeName' }, 'app-2')).toBe(
expect(getSingle(topLevelSchemeUser, { type: 'oauth2', _key: 'schemeName' })).toBe('scheme-key');
expect(getSingle(keysSchemeUser, { type: 'oauth2', _key: 'schemeName' })).toBe('scheme-key-1');
expect(getSingle(keysSchemeUser, { type: 'oauth2', _key: 'schemeName' }, 'app-2')).toBe(
'scheme-key-2',
);
expect(getAuth(keysSchemeUser, { type: 'http', _key: 'schemeName' }, 'app-3')).toEqual({
expect(getSingle(keysSchemeUser, { type: 'http', _key: 'schemeName' }, 'app-3')).toEqual({
user: 'user',
pass: 'pass',
});
});

it('should return null for anything else', () => {
expect(getAuth(topLevelUser, { type: 'unknown' })).toBe(null);
expect(getAuth(keysUser, { type: 'unknown' })).toBe(null);
expect(getAuth(keysUser, { type: 'unknown' }, 'app-2')).toBe(null);
expect(getSingle(topLevelUser, { type: 'unknown' })).toBe(null);
expect(getSingle(keysUser, { type: 'unknown' })).toBe(null);
expect(getSingle(keysUser, { type: 'unknown' }, 'app-2')).toBe(null);
});

it('should allow scheme to be undefined', () => {
expect(getAuth(topLevelUser)).toBe(null);
expect(getSingle(topLevelUser)).toBe(null);
});
16 changes: 16 additions & 0 deletions packages/api-explorer/src/lib/get-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,20 @@ function getAuth(user, scheme, selectedApp = false) {
return getKey(user, scheme);
}

function getAuth(user, operation) {
return operation
.getSecurity()
.map(securityRequirement => {
return Object.keys(securityRequirement)
.map(name => {
operation.oas.components.securitySchemes[name]._key = name;
return { [name]: getSingle(user, operation.oas.components.securitySchemes[name]) };
})
.reduce((prev, next) => Object.assign(prev, next));
})
.reduce((prev, next) => Object.assign(prev, next), {});
}

module.exports = getAuth;

module.exports.getSingle = getSingle;

0 comments on commit 305bfab

Please sign in to comment.