Skip to content

Commit 80277b8

Browse files
committed
Unskip & polish off OAuth2 tests
1 parent 06cf0d8 commit 80277b8

File tree

2 files changed

+132
-97
lines changed

2 files changed

+132
-97
lines changed

src/execute/oas3/build-request.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ export default function (options, req) {
7676
// Adapted from the Swagger2 implementation
7777
export function applySecurities({request, securities = {}, operation = {}, spec}) {
7878
const result = assign({}, request)
79-
const {authorized = {}, specSecurity = []} = securities
80-
const security = operation.security || specSecurity
79+
const {authorized = {}} = securities
80+
const security = operation.security || spec.security || []
8181
const isAuthorized = authorized && !!Object.keys(authorized).length
8282
const securityDef = get(spec, ['components', 'securitySchemes']) || {}
8383

test/oas3/execute/authorization.js

Lines changed: 130 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@ import {execute, buildRequest, baseUrl, applySecurities, self as stubs} from '..
99

1010

1111
// OAS 3.0 Authorization
12-
//
13-
// Testing TODO:
14-
// - [ ] Ignore `Authorization` header parameters
15-
// - [ ] OAuth2 with credentials in constructor
16-
// - [ ] OAuth2 with credentials through buildRequest/execute
17-
// - [ ] HTTP Basic with credentials in constructor
18-
// - [ ] HTTP Basic with credentials through buildRequest/execute
19-
// - [ ] HTTP Bearer with credentials in constructor
20-
// - [ ] HTTP Bearer with credentials through buildRequest/execute
2112

2213
describe('Authorization - OpenAPI Specification 3.0', function () {
2314
it('should ignore a header parameter named `Authorization`', () => {
@@ -435,119 +426,163 @@ describe('Authorization - OpenAPI Specification 3.0', function () {
435426
})
436427
})
437428

438-
describe.skip('OAuth2', () => {
439-
describe('implicit', () => {
440-
it('should build a request with constructor credentials', () => {
441-
const spec = {
442-
openapi: '3.0.0',
443-
paths: {
444-
'/': {
445-
get: {
446-
operationId: 'myOperation',
447-
parameters: [
448-
{
449-
name: 'Authorization',
450-
in: 'header'
429+
describe('OAuth2', () => {
430+
it('should build a request with an operation security', () => {
431+
const spec = {
432+
openapi: '3.0.0',
433+
components: {
434+
securitySchemes: {
435+
myOAuth2Implicit: {
436+
type: 'oauth2',
437+
flows: {
438+
implicit: {
439+
authorizationUrl: 'http://google.com/',
440+
scopes: {
441+
myScope: 'blah blah blah'
451442
}
452-
]
443+
}
453444
}
454445
}
455446
}
447+
},
448+
paths: {
449+
'/': {
450+
get: {
451+
operationId: 'myOperation',
452+
security: [
453+
{myOAuth2Implicit: []}
454+
]
455+
}
456+
}
456457
}
458+
}
457459

458-
// when
459-
const req = buildRequest({
460-
spec,
461-
operationId: 'myOperation',
462-
parameters: {
463-
Authorization: 'myAuthValue'
460+
// when
461+
const req = buildRequest({
462+
spec,
463+
operationId: 'myOperation',
464+
securities: {
465+
authorized: {
466+
myOAuth2Implicit: {
467+
token: {
468+
access_token: 'myTokenValue'
469+
}
470+
}
464471
}
465-
})
472+
}
473+
})
466474

467-
expect(req).toEqual({
468-
method: 'GET',
469-
url: '/',
470-
credentials: 'same-origin',
471-
headers: {},
472-
})
475+
expect(req).toEqual({
476+
method: 'GET',
477+
url: '/',
478+
credentials: 'same-origin',
479+
headers: {
480+
Authorization: 'Bearer myTokenValue'
481+
},
473482
})
474-
it('should build a request with buildRequest credentials', () => {
475-
const spec = {
476-
openapi: '3.0.0',
477-
paths: {
478-
'/': {
479-
get: {
480-
operationId: 'myOperation',
481-
parameters: [
482-
{
483-
name: 'Authorization',
484-
in: 'header'
483+
})
484+
it('should build a request with a global security', () => {
485+
const spec = {
486+
openapi: '3.0.0',
487+
security: [
488+
{myOAuth2Implicit: []}
489+
],
490+
components: {
491+
securitySchemes: {
492+
myOAuth2Implicit: {
493+
type: 'oauth2',
494+
flows: {
495+
implicit: {
496+
authorizationUrl: 'http://google.com/',
497+
scopes: {
498+
myScope: 'blah blah blah'
485499
}
486-
]
500+
}
487501
}
488502
}
489503
}
504+
},
505+
paths: {
506+
'/': {
507+
get: {
508+
operationId: 'myOperation'
509+
}
510+
}
490511
}
512+
}
491513

492-
// when
493-
const req = buildRequest({
494-
spec,
495-
operationId: 'myOperation',
496-
parameters: {
497-
Authorization: 'myAuthValue'
514+
// when
515+
const req = buildRequest({
516+
spec,
517+
operationId: 'myOperation',
518+
securities: {
519+
authorized: {
520+
myOAuth2Implicit: {
521+
token: {
522+
access_token: 'myTokenValue'
523+
}
524+
}
498525
}
499-
})
526+
}
527+
})
500528

501-
expect(req).toEqual({
502-
method: 'GET',
503-
url: '/',
504-
credentials: 'same-origin',
505-
headers: {},
506-
})
529+
expect(req).toEqual({
530+
method: 'GET',
531+
url: '/',
532+
credentials: 'same-origin',
533+
headers: {
534+
Authorization: 'Bearer myTokenValue'
535+
},
507536
})
508-
it('should set buildRequest credentials over constructor', () => {
509-
const spec = {
510-
openapi: '3.0.0',
511-
paths: {
512-
'/': {
513-
get: {
514-
operationId: 'myOperation',
515-
parameters: [
516-
{
517-
name: 'Authorization',
518-
in: 'header'
537+
})
538+
it('should build a request without authorization when spec does not require it', () => {
539+
const spec = {
540+
openapi: '3.0.0',
541+
components: {
542+
securitySchemes: {
543+
myOAuth2Implicit: {
544+
type: 'oauth2',
545+
flows: {
546+
implicit: {
547+
authorizationUrl: 'http://google.com/',
548+
scopes: {
549+
myScope: 'blah blah blah'
519550
}
520-
]
551+
}
521552
}
522553
}
523554
}
555+
},
556+
paths: {
557+
'/': {
558+
get: {
559+
operationId: 'myOperation'
560+
}
561+
}
524562
}
563+
}
525564

526-
// when
527-
const req = buildRequest({
528-
spec,
529-
operationId: 'myOperation',
530-
parameters: {
531-
Authorization: 'myAuthValue'
565+
// when
566+
const req = buildRequest({
567+
spec,
568+
operationId: 'myOperation',
569+
securities: {
570+
authorized: {
571+
myOAuth2Implicit: {
572+
token: {
573+
access_token: 'myTokenValue'
574+
}
575+
}
532576
}
533-
})
534-
535-
expect(req).toEqual({
536-
method: 'GET',
537-
url: '/',
538-
credentials: 'same-origin',
539-
headers: {},
540-
})
577+
}
541578
})
542-
})
543-
describe('password', () => {
544-
545-
})
546-
describe('application', () => {
547-
548-
})
549-
describe('access code', () => {
550579

580+
expect(req).toEqual({
581+
method: 'GET',
582+
url: '/',
583+
credentials: 'same-origin',
584+
headers: {},
585+
})
551586
})
552587
})
553588
})

0 commit comments

Comments
 (0)