1
+ const multipleSecurities = require ( '../fixtures/multiple-securities/oas.json' ) ;
2
+ const Oas = require ( '../../src/lib/Oas' ) ;
1
3
const getAuth = require ( '../../src/lib/get-auth' ) ;
2
4
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
+
3
52
const topLevelUser = { apiKey : '123456' , user : 'user' , pass : 'pass' } ;
4
53
const keysUser = { keys : [ { apiKey : '123456' , name : 'app-1' } , { apiKey : '7890' , name : 'app-2' } ] } ;
5
54
const topLevelSchemeUser = { schemeName : 'scheme-key' } ;
@@ -12,46 +61,46 @@ const keysSchemeUser = {
12
61
} ;
13
62
14
63
it ( 'should return apiKey property for oauth' , ( ) => {
15
- expect ( getAuth ( topLevelUser , { type : 'oauth2' } ) ) . toBe ( '123456' ) ;
64
+ expect ( getSingle ( topLevelUser , { type : 'oauth2' } ) ) . toBe ( '123456' ) ;
16
65
} ) ;
17
66
18
67
it ( 'should return apiKey property for apiKey' , ( ) => {
19
- expect ( getAuth ( topLevelUser , { type : 'oauth2' } ) ) . toBe ( '123456' ) ;
68
+ expect ( getSingle ( topLevelUser , { type : 'oauth2' } ) ) . toBe ( '123456' ) ;
20
69
} ) ;
21
70
22
71
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 ( {
24
73
user : 'user' ,
25
74
pass : 'pass' ,
26
75
} ) ;
27
76
} ) ;
28
77
29
78
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' ) ;
31
80
} ) ;
32
81
33
82
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' ) ;
35
84
} ) ;
36
85
37
86
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 (
41
90
'scheme-key-2' ,
42
91
) ;
43
- expect ( getAuth ( keysSchemeUser , { type : 'http' , _key : 'schemeName' } , 'app-3' ) ) . toEqual ( {
92
+ expect ( getSingle ( keysSchemeUser , { type : 'http' , _key : 'schemeName' } , 'app-3' ) ) . toEqual ( {
44
93
user : 'user' ,
45
94
pass : 'pass' ,
46
95
} ) ;
47
96
} ) ;
48
97
49
98
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 ) ;
53
102
} ) ;
54
103
55
104
it ( 'should allow scheme to be undefined' , ( ) => {
56
- expect ( getAuth ( topLevelUser ) ) . toBe ( null ) ;
105
+ expect ( getSingle ( topLevelUser ) ) . toBe ( null ) ;
57
106
} ) ;
0 commit comments