1
1
2
- import win from "core/window"
3
2
import Im from "immutable"
4
3
import oauth2Authorize from "core/oauth2-authorize"
5
4
import * as utils from "core/utils"
@@ -13,75 +12,76 @@ describe("oauth2", () => {
13
12
14
13
let authConfig = {
15
14
auth : { schema : { get : ( key ) => mockSchema [ key ] } , scopes : [ "scope1" , "scope2" ] } ,
16
- authActions : { } ,
15
+ authActions : {
16
+ authPopup : jest . fn ( )
17
+ } ,
17
18
errActions : { } ,
18
19
configs : { oauth2RedirectUrl : "" } ,
19
20
authConfigs : { }
20
21
}
21
22
22
23
let authConfig2 = {
23
24
auth : { schema : { get : ( key ) => mockSchema [ key ] } , scopes : Im . List ( [ "scope2" , "scope3" ] ) } ,
24
- authActions : { } ,
25
+ authActions : {
26
+ authPopup : jest . fn ( )
27
+ } ,
25
28
errActions : { } ,
26
29
configs : { oauth2RedirectUrl : "" } ,
27
30
authConfigs : { }
28
31
}
29
32
30
33
let authConfig3 = {
31
34
auth : { schema : { get : ( key ) => mockSchema [ key ] } , scopes : Im . List ( [ "scope4" , "scope5" ] ) } ,
32
- authActions : { } ,
35
+ authActions : {
36
+ authPopup : jest . fn ( )
37
+ } ,
33
38
errActions : { } ,
34
39
configs : { oauth2RedirectUrl : "" } ,
35
40
authConfigs : { }
36
41
}
37
42
38
- beforeEach ( ( ) => {
39
- win . open = jest . fn ( )
40
- } )
41
-
42
43
describe ( "authorize redirect" , ( ) => {
43
44
it ( "should build authorize url" , ( ) => {
44
- const windowOpenSpy = jest . spyOn ( win , "open" )
45
45
oauth2Authorize ( authConfig )
46
- expect ( windowOpenSpy . mock . calls . length ) . toEqual ( 1 )
47
- expect ( windowOpenSpy . mock . calls [ 0 ] [ 0 ] ) . toMatch ( "https://testAuthorizationUrl?response_type=code&redirect_uri=&scope=scope1%20scope2&state=" )
46
+ expect ( authConfig . authActions . authPopup . mock . calls . length ) . toEqual ( 1 )
47
+ expect ( authConfig . authActions . authPopup . mock . calls [ 0 ] [ 0 ] ) . toMatch ( "https://testAuthorizationUrl?response_type=code&redirect_uri=&scope=scope1%20scope2&state=" )
48
48
49
- windowOpenSpy . mockReset ( )
49
+ authConfig . authActions . authPopup . mockReset ( )
50
50
} )
51
51
52
52
it ( "should build authorize url relative" , function ( ) {
53
- const windowOpenSpy = jest . spyOn ( win , "open" )
54
53
let relativeMockSchema = {
55
54
flow : "accessCode" ,
56
55
authorizationUrl : "/testAuthorizationUrl"
57
56
}
58
57
let relativeAuthConfig = {
59
58
auth : { schema : { get : ( key ) => relativeMockSchema [ key ] } } ,
60
- authActions : { } ,
59
+ authActions : {
60
+ authPopup : jest . fn ( )
61
+ } ,
61
62
errActions : { } ,
62
63
configs : { oauth2RedirectUrl : "" } ,
63
64
authConfigs : { } ,
64
65
currentServer : "https://currentserver"
65
66
}
66
67
oauth2Authorize ( relativeAuthConfig )
67
- expect ( windowOpenSpy . mock . calls . length ) . toEqual ( 1 )
68
- expect ( windowOpenSpy . mock . calls [ 0 ] [ 0 ] ) . toMatch ( "https://currentserver/testAuthorizationUrl?response_type=code&redirect_uri=&state=" )
68
+ expect ( relativeAuthConfig . authActions . authPopup . mock . calls . length ) . toEqual ( 1 )
69
+ expect ( relativeAuthConfig . authActions . authPopup . mock . calls [ 0 ] [ 0 ] ) . toMatch ( "https://currentserver/testAuthorizationUrl?response_type=code&redirect_uri=&state=" )
69
70
70
- windowOpenSpy . mockReset ( )
71
+ relativeAuthConfig . authActions . authPopup . mockReset ( )
71
72
} )
72
73
73
74
it ( "should append query parameters to authorizeUrl with query parameters" , ( ) => {
74
- const windowOpenSpy = jest . spyOn ( win , "open" )
75
75
mockSchema . authorizationUrl = "https://testAuthorizationUrl?param=1"
76
76
oauth2Authorize ( authConfig )
77
- expect ( windowOpenSpy . mock . calls . length ) . toEqual ( 1 )
78
- expect ( windowOpenSpy . mock . calls [ 0 ] [ 0 ] ) . toMatch ( "https://testAuthorizationUrl?param=1&response_type=code&redirect_uri=&scope=scope1%20scope2&state=" )
79
77
80
- windowOpenSpy . mockReset ( )
78
+ expect ( authConfig . authActions . authPopup . mock . calls . length ) . toEqual ( 1 )
79
+ expect ( authConfig . authActions . authPopup . mock . calls [ 0 ] [ 0 ] ) . toMatch ( "https://testAuthorizationUrl?param=1&response_type=code&redirect_uri=&scope=scope1%20scope2&state=" )
80
+
81
+ authConfig . authActions . authPopup . mockReset ( )
81
82
} )
82
83
83
84
it ( "should send code_challenge when using authorizationCode flow with usePkceWithAuthorizationCodeGrant enabled" , ( ) => {
84
- const windowOpenSpy = jest . spyOn ( win , "open" )
85
85
mockSchema . flow = "authorizationCode"
86
86
87
87
const expectedCodeVerifier = "mock_code_verifier"
@@ -93,9 +93,9 @@ describe("oauth2", () => {
93
93
authConfig . authConfigs . usePkceWithAuthorizationCodeGrant = true
94
94
95
95
oauth2Authorize ( authConfig )
96
- expect ( win . open . mock . calls . length ) . toEqual ( 1 )
96
+ expect ( authConfig . authActions . authPopup . mock . calls . length ) . toEqual ( 1 )
97
97
98
- const actualUrl = new URLSearchParams ( win . open . mock . calls [ 0 ] [ 0 ] )
98
+ const actualUrl = new URLSearchParams ( authConfig . authActions . authPopup . mock . calls [ 0 ] [ 0 ] )
99
99
expect ( actualUrl . get ( "code_challenge" ) ) . toBe ( expectedCodeChallenge )
100
100
expect ( actualUrl . get ( "code_challenge_method" ) ) . toBe ( "S256" )
101
101
@@ -107,14 +107,13 @@ describe("oauth2", () => {
107
107
expect ( authConfig . auth . codeVerifier ) . toBe ( expectedCodeVerifier )
108
108
109
109
// Restore spies
110
- windowOpenSpy . mockReset ( )
110
+ authConfig . authActions . authPopup . mockReset ( )
111
111
generateCodeVerifierSpy . mockReset ( )
112
112
createCodeChallengeSpy . mockReset ( )
113
113
} )
114
114
115
115
116
116
it ( "should send code_challenge when using accessCode flow with usePkceWithAuthorizationCodeGrant enabled" , ( ) => {
117
- const windowOpenSpy = jest . spyOn ( win , "open" )
118
117
mockSchema . flow = "accessCode"
119
118
120
119
const expectedCodeVerifier = "mock_code_verifier"
@@ -127,9 +126,10 @@ describe("oauth2", () => {
127
126
authConfig . authConfigs . usePkceWithAuthorizationCodeGrant = true
128
127
129
128
oauth2Authorize ( authConfig )
130
- expect ( win . open . mock . calls . length ) . toEqual ( 1 )
131
129
132
- const actualUrl = new URLSearchParams ( win . open . mock . calls [ 0 ] [ 0 ] )
130
+ expect ( authConfig . authActions . authPopup . mock . calls . length ) . toEqual ( 1 )
131
+
132
+ const actualUrl = new URLSearchParams ( authConfig . authActions . authPopup . mock . calls [ 0 ] [ 0 ] )
133
133
expect ( actualUrl . get ( "code_challenge" ) ) . toBe ( expectedCodeChallenge )
134
134
expect ( actualUrl . get ( "code_challenge_method" ) ) . toBe ( "S256" )
135
135
@@ -141,13 +141,12 @@ describe("oauth2", () => {
141
141
expect ( authConfig . auth . codeVerifier ) . toBe ( expectedCodeVerifier )
142
142
143
143
// Restore spies
144
- windowOpenSpy . mockReset ( )
144
+ authConfig . authActions . authPopup . mockReset ( )
145
145
generateCodeVerifierSpy . mockReset ( )
146
146
createCodeChallengeSpy . mockReset ( )
147
147
} )
148
148
149
149
it ( "should send code_challenge when using authorization_code flow with usePkceWithAuthorizationCodeGrant enabled" , ( ) => {
150
- const windowOpenSpy = jest . spyOn ( win , "open" )
151
150
mockSchema . flow = "authorization_code"
152
151
153
152
const expectedCodeVerifier = "mock_code_verifier"
@@ -159,9 +158,9 @@ describe("oauth2", () => {
159
158
authConfig . authConfigs . usePkceWithAuthorizationCodeGrant = true
160
159
161
160
oauth2Authorize ( authConfig )
162
- expect ( win . open . mock . calls . length ) . toEqual ( 1 )
161
+ expect ( authConfig . authActions . authPopup . mock . calls . length ) . toEqual ( 1 )
163
162
164
- const actualUrl = new URLSearchParams ( win . open . mock . calls [ 0 ] [ 0 ] )
163
+ const actualUrl = new URLSearchParams ( authConfig . authActions . authPopup . mock . calls [ 0 ] [ 0 ] )
165
164
expect ( actualUrl . get ( "code_challenge" ) ) . toBe ( expectedCodeChallenge )
166
165
expect ( actualUrl . get ( "code_challenge_method" ) ) . toBe ( "S256" )
167
166
@@ -173,32 +172,30 @@ describe("oauth2", () => {
173
172
expect ( authConfig . auth . codeVerifier ) . toBe ( expectedCodeVerifier )
174
173
175
174
// Restore spies
176
- windowOpenSpy . mockReset ( )
175
+ authConfig . authActions . authPopup . mockReset ( )
177
176
generateCodeVerifierSpy . mockReset ( )
178
177
createCodeChallengeSpy . mockReset ( )
179
178
} )
180
179
181
180
it ( "should add list of scopes to authorizeUrl" , ( ) => {
182
- const windowOpenSpy = jest . spyOn ( win , "open" )
183
181
mockSchema . authorizationUrl = "https://testAuthorizationUrl?param=1"
184
182
185
183
oauth2Authorize ( authConfig2 )
186
- expect ( windowOpenSpy . mock . calls . length ) . toEqual ( 1 )
187
- expect ( windowOpenSpy . mock . calls [ 0 ] [ 0 ] ) . toMatch ( "https://testAuthorizationUrl?param=1&response_type=code&redirect_uri=&scope=scope2%20scope3&state=" )
184
+ expect ( authConfig2 . authActions . authPopup . mock . calls . length ) . toEqual ( 1 )
185
+ expect ( authConfig2 . authActions . authPopup . mock . calls [ 0 ] [ 0 ] ) . toMatch ( "https://testAuthorizationUrl?param=1&response_type=code&redirect_uri=&scope=scope2%20scope3&state=" )
188
186
189
- windowOpenSpy . mockReset ( )
187
+ authConfig2 . authActions . authPopup . mockReset ( )
190
188
} )
191
189
192
190
it ( "should build authorize url for authorization_code flow" , ( ) => {
193
- const windowOpenSpy = jest . spyOn ( win , "open" )
194
191
mockSchema . authorizationUrl = "https://testAuthorizationUrl"
195
192
mockSchema . flow = "authorization_code"
196
193
197
194
oauth2Authorize ( authConfig3 )
198
- expect ( windowOpenSpy . mock . calls . length ) . toEqual ( 1 )
199
- expect ( windowOpenSpy . mock . calls [ 0 ] [ 0 ] ) . toMatch ( "https://testAuthorizationUrl?response_type=code&redirect_uri=&scope=scope4%20scope5&state=" )
195
+ expect ( authConfig3 . authActions . authPopup . mock . calls . length ) . toEqual ( 1 )
196
+ expect ( authConfig3 . authActions . authPopup . mock . calls [ 0 ] [ 0 ] ) . toMatch ( "https://testAuthorizationUrl?response_type=code&redirect_uri=&scope=scope4%20scope5&state=" )
200
197
201
- windowOpenSpy . mockReset ( )
198
+ authConfig3 . authActions . authPopup . mockReset ( )
202
199
} )
203
200
} )
204
201
} )
0 commit comments