11import { OAuthIdentity } from "../oauth-identity" ;
2- import { LoginResponseDto , HttpMethods } from "../../contracts" ;
2+ import { OAuthResponseDto , HttpMethods } from "../../contracts" ;
33import fetchMock from "fetch-mock" ;
44jest . useFakeTimers ( ) ;
55
66const TEST_HOST = "https://example.com" ;
77const LOGIN_PATH = "/api/login" ;
88const LOGOUT_PATH = "/api/logout" ;
99
10- const LOGIN_RESPONSE : LoginResponseDto = {
10+ const LOGIN_RESPONSE : OAuthResponseDto = {
1111 scope : "offline_access" ,
1212 token_type : "Bearer" ,
1313 access_token : "ACCESS_TOKEN" ,
1414 refresh_token : "REFRESH_TOKEN" ,
1515 // Seconds
1616 expires_in : 28800
1717} ;
18+ const LOGIN_RESPONSE_NO_EXPIRES_IN : OAuthResponseDto = {
19+ scope : "offline_access" ,
20+ token_type : "Bearer" ,
21+ access_token : "ACCESS_TOKEN" ,
22+ refresh_token : "REFRESH_TOKEN"
23+ } ;
24+ const LOGIN_RESPONSE_NO_REFRESH_TOKEN : OAuthResponseDto = {
25+ scope : "offline_access" ,
26+ token_type : "Bearer" ,
27+ access_token : "ACCESS_TOKEN" ,
28+ // Seconds
29+ expires_in : 28800
30+ } ;
1831
1932// #region Mocked fetch results.
2033function mockLoginSuccess ( ) : void {
@@ -26,6 +39,25 @@ function mockLoginSuccess(): void {
2639 } )
2740 ) ;
2841}
42+ function mockLoginSuccessNoExpiresIn ( ) : void {
43+ fetchMock . post (
44+ `${ TEST_HOST } ${ LOGIN_PATH } ` ,
45+ Promise . resolve ( {
46+ status : 200 ,
47+ body : JSON . stringify ( LOGIN_RESPONSE_NO_EXPIRES_IN )
48+ } )
49+ ) ;
50+ }
51+
52+ function mockLoginSuccessNoRefreshToken ( ) : void {
53+ fetchMock . post (
54+ `${ TEST_HOST } ${ LOGIN_PATH } ` ,
55+ Promise . resolve ( {
56+ status : 200 ,
57+ body : JSON . stringify ( LOGIN_RESPONSE_NO_REFRESH_TOKEN )
58+ } )
59+ ) ;
60+ }
2961
3062function mockRenewFailed ( ) : void {
3163 fetchMock . post (
@@ -86,6 +118,38 @@ it("logins successfully", async done => {
86118 done ( ) ;
87119} ) ;
88120
121+ it ( "logins successfully with no expires_in property" , async done => {
122+ const identity = new OAuthIdentity ( {
123+ host : TEST_HOST ,
124+ loginPath : LOGIN_PATH ,
125+ logoutPath : LOGOUT_PATH
126+ } ) ;
127+
128+ mockLoginSuccessNoExpiresIn ( ) ;
129+ try {
130+ await identity . login ( "" , "" ) ;
131+ done . fail ( ) ;
132+ } catch {
133+ done ( ) ;
134+ }
135+ } ) ;
136+
137+ it ( "logins successfully with no refresh token" , async done => {
138+ const fn = jest . fn ( ) ;
139+ const identity = new OAuthIdentity ( {
140+ host : TEST_HOST ,
141+ loginPath : LOGIN_PATH ,
142+ logoutPath : LOGOUT_PATH
143+ } ) ;
144+
145+ mockLoginSuccessNoRefreshToken ( ) ;
146+ identity . on ( "login" , fn ) ;
147+ await identity . login ( "" , "" ) ;
148+
149+ expect ( fn ) . toBeCalled ( ) ;
150+ done ( ) ;
151+ } ) ;
152+
89153it ( "logins successfully with disabled renewal token" , async done => {
90154 const fn = jest . fn ( ) ;
91155 const identity = new OAuthIdentity ( {
@@ -143,7 +207,7 @@ it("logins successfully with time renewal time less than expiration time", async
143207 host : TEST_HOST ,
144208 loginPath : LOGIN_PATH ,
145209 logoutPath : LOGOUT_PATH ,
146- renewTokenTime : LOGIN_RESPONSE . expires_in + 100
210+ renewTokenTime : 28900
147211 } ) ;
148212
149213 mockLoginSuccess ( ) ;
@@ -167,7 +231,7 @@ it("logins successfully and new token", async done => {
167231 await identity . login ( "" , "" ) ;
168232 expect ( fn ) . toBeCalled ( ) ;
169233 jest . runAllTimers ( ) ;
170- expect ( setTimeout ) . toHaveBeenLastCalledWith ( expect . any ( Function ) , LOGIN_RESPONSE . expires_in - 120 ) ;
234+ expect ( setTimeout ) . toHaveBeenLastCalledWith ( expect . any ( Function ) , 28680 ) ;
171235 done ( ) ;
172236} ) ;
173237
0 commit comments