@@ -4,6 +4,14 @@ import { expect } from 'chai';
44import { DestroyableServer } from "destroyable-server" ;
55import { AUTH0_PORT , auth0Server , startServer } from "./test-util" ;
66
7+ const TOKEN_RESPONSE = {
8+ "access_token" : "at" ,
9+ "refresh_token" : "rt" ,
10+ "scope" : "openid email offline_access" ,
11+ "expires_in" : 86400 ,
12+ "token_type" : "Bearer"
13+ } ;
14+
715describe ( "API auth endpoints" , ( ) => {
816
917 let apiServer : DestroyableServer ;
@@ -73,13 +81,7 @@ describe("API auth endpoints", () => {
7381 describe ( "/auth/login" , ( ) => {
7482
7583 it ( "returns a 400 if you don't provide a body" , async ( ) => {
76- const tokenEndpoint = await auth0Server . forPost ( '/oauth/token' ) . thenJson ( 200 , {
77- "access_token" : "at" ,
78- "refresh_token" : "rt" ,
79- "scope" : "openid email offline_access" ,
80- "expires_in" : 86400 ,
81- "token_type" : "Bearer"
82- } ) ;
84+ const tokenEndpoint = await auth0Server . forPost ( '/oauth/token' ) . thenJson ( 200 , TOKEN_RESPONSE ) ;
8385
8486 const response = await fetch ( `${ apiAddress } /api/auth/login` , {
8587 method : 'POST'
@@ -114,13 +116,7 @@ describe("API auth endpoints", () => {
114116 scope : 'openid email offline_access app_metadata' ,
115117 grant_type : 'http://auth0.com/oauth/grant-type/passwordless/otp'
116118 } )
117- . thenJson ( 200 , {
118- "access_token" : "at" ,
119- "refresh_token" : "rt" ,
120- "scope" : "openid email offline_access" ,
121- "expires_in" : 86400 ,
122- "token_type" : "Bearer"
123- } ) ;
119+ . thenJson ( 200 , TOKEN_RESPONSE ) ;
124120
125121 const response = await fetch ( `${ apiAddress } /api/auth/login` , {
126122 method : 'POST' ,
@@ -140,4 +136,55 @@ describe("API auth endpoints", () => {
140136
141137 } ) ;
142138
139+ describe ( "/auth/refresh-token" , ( ) => {
140+
141+ it ( "returns a 400 if you don't provide a body" , async ( ) => {
142+ const tokenEndpoint = await auth0Server . forPost ( '/oauth/token' ) . thenJson ( 200 , TOKEN_RESPONSE ) ;
143+
144+ const response = await fetch ( `${ apiAddress } /api/auth/refresh-token` , {
145+ method : 'POST'
146+ } ) ;
147+
148+ expect ( response . status ) . to . equal ( 400 ) ;
149+ expect ( await tokenEndpoint . getSeenRequests ( ) ) . to . have . length ( 0 ) ;
150+ } ) ;
151+
152+ it ( "returns a 400 if you don't provide a refreshToken" , async ( ) => {
153+ const tokenEndpoint = await auth0Server . forPost ( '/oauth/token' ) . thenReply ( 200 ) ;
154+
155+ const response = await fetch ( `${ apiAddress } /api/auth/refresh-token` , {
156+ method : 'POST' ,
157+ headers : { 'content-type' : 'application/json' } ,
158+ body : JSON . stringify ( { } )
159+ } ) ;
160+
161+ expect ( response . status ) . to . equal ( 400 ) ;
162+ expect ( await tokenEndpoint . getSeenRequests ( ) ) . to . have . length ( 0 ) ;
163+ } ) ;
164+
165+ it ( "sends a request to Auth0 to refresh the token" , async ( ) => {
166+ const refreshToken = 'rt' ;
167+ const tokenEndpoint = await auth0Server . forPost ( '/oauth/token' )
168+ . withForm ( {
169+ refresh_token : refreshToken ,
170+ grant_type : 'refresh_token'
171+ } )
172+ . thenJson ( 200 , TOKEN_RESPONSE ) ;
173+
174+ const response = await fetch ( `${ apiAddress } /api/auth/refresh-token` , {
175+ method : 'POST' ,
176+ headers : { 'content-type' : 'application/json' } ,
177+ body : JSON . stringify ( { refreshToken } )
178+ } ) ;
179+
180+ expect ( response . status ) . to . equal ( 200 ) ;
181+ expect ( await tokenEndpoint . getSeenRequests ( ) ) . to . have . length ( 1 ) ;
182+
183+ const result = await response . json ( ) ;
184+ expect ( result . accessToken ) . to . equal ( 'at' ) ;
185+ expect ( result . expiresAt ) . to . be . greaterThan ( Date . now ( ) ) ;
186+ expect ( result . expiresAt ) . to . be . lessThan ( Date . now ( ) + 100_000_000 ) ;
187+ } ) ;
188+ } ) ;
189+
143190} ) ;
0 commit comments