1+ const multipleSecurities = require ( '../fixtures/multiple-securities/oas.json' ) ;
2+ const Oas = require ( '../../src/lib/Oas' ) ;
13const 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+
352const topLevelUser = { apiKey : '123456' , user : 'user' , pass : 'pass' } ;
453const keysUser = { keys : [ { apiKey : '123456' , name : 'app-1' } , { apiKey : '7890' , name : 'app-2' } ] } ;
554const topLevelSchemeUser = { schemeName : 'scheme-key' } ;
@@ -12,46 +61,46 @@ const keysSchemeUser = {
1261} ;
1362
1463it ( 'should return apiKey property for oauth' , ( ) => {
15- expect ( getAuth ( topLevelUser , { type : 'oauth2' } ) ) . toBe ( '123456' ) ;
64+ expect ( getSingle ( topLevelUser , { type : 'oauth2' } ) ) . toBe ( '123456' ) ;
1665} ) ;
1766
1867it ( 'should return apiKey property for apiKey' , ( ) => {
19- expect ( getAuth ( topLevelUser , { type : 'oauth2' } ) ) . toBe ( '123456' ) ;
68+ expect ( getSingle ( topLevelUser , { type : 'oauth2' } ) ) . toBe ( '123456' ) ;
2069} ) ;
2170
2271it ( '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
2978it ( '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
3382it ( '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
3786it ( '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
4998it ( '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
55104it ( 'should allow scheme to be undefined' , ( ) => {
56- expect ( getAuth ( topLevelUser ) ) . toBe ( null ) ;
105+ expect ( getSingle ( topLevelUser ) ) . toBe ( null ) ;
57106} ) ;
0 commit comments