11// Copyright 2019 Stanford University see LICENSE for license
2- let mockKeycloak
3-
4- jest . mock ( "keycloak-js" , ( ) => {
5- mockKeycloak = {
6- init : jest . fn ( ( ) => Promise . resolve ( true ) ) ,
7- isTokenExpired : jest . fn ( ) ,
8- updateToken : jest . fn ( ) ,
9- login : jest . fn ( ( ) => Promise . resolve ( true ) ) ,
10- logout : jest . fn ( ( ) => Promise . resolve ( true ) ) ,
11- authenticated : false ,
12- }
13-
14- return jest . fn ( ) . mockImplementation ( ( config ) => {
15- return mockKeycloak
16- } )
17- } )
18-
192import { authenticate , signIn , signOut } from "actionCreators/authenticate"
203import configureMockStore from "redux-mock-store"
214import thunk from "redux-thunk"
225import * as sinopiaApi from "sinopiaApi"
236
7+ jest . mock ( "KeycloakContext" , ( ) => ( {
8+ useKeycloak : jest . fn ( ) . mockReturnValue ( { } ) ,
9+ } ) )
10+
2411const mockStore = configureMockStore ( [ thunk ] )
2512
2613const userData = {
@@ -30,28 +17,34 @@ const userData = {
3017describe ( "authenticate" , ( ) => {
3118 beforeEach ( ( ) => {
3219 jest . clearAllMocks ( )
33- mockKeycloak . authenticated = false
3420 } )
3521
3622 describe ( "user already in state" , ( ) => {
3723 it ( "does not authenticate" , async ( ) => {
24+ const mockKeycloak = { }
3825 const store = mockStore ( {
3926 authenticate : { user : { username : "havram" } } ,
4027 } )
41- await store . dispatch ( authenticate ( ) )
28+ await store . dispatch ( authenticate ( mockKeycloak ) )
4229 expect ( store . getActions ( ) ) . toEqual ( [ ] )
4330 } )
4431 } )
4532
4633 describe ( "successful" , ( ) => {
4734 sinopiaApi . fetchUser = jest . fn ( ) . mockResolvedValue ( userData )
4835 it ( "dispatches actions to add user" , async ( ) => {
49- mockKeycloak . authenticated = true
50- mockKeycloak . tokenParsed = {
51- preferred_username : "havram" ,
36+ const mockKeycloak = {
37+ authenticated : true ,
38+ isTokenExpired : jest . fn ( ) ,
39+ updateToken : jest . fn ( ) ,
40+ login : jest . fn ( ( ) => Promise . resolve ( true ) ) ,
41+ tokenParsed : {
42+ preferred_username : "havram" ,
43+ } ,
5244 }
45+
5346 const store = mockStore ( { authenticate : { user : undefined } } )
54- await store . dispatch ( authenticate ( ) )
47+ await store . dispatch ( authenticate ( mockKeycloak ) )
5548
5649 expect ( store . getActions ( ) ) . toHaveAction ( "SET_USER" , {
5750 username : "havram" ,
@@ -62,8 +55,9 @@ describe("authenticate", () => {
6255 } )
6356 describe ( "failure" , ( ) => {
6457 it ( "dispatches actions to remove user" , async ( ) => {
58+ const mockKeycloak = { authenticated : false }
6559 const store = mockStore ( { authenticate : { user : undefined } } )
66- await store . dispatch ( authenticate ( ) )
60+ await store . dispatch ( authenticate ( mockKeycloak ) )
6761 expect ( store . getActions ( ) ) . toHaveAction ( "REMOVE_USER" )
6862 } )
6963 } )
@@ -72,21 +66,25 @@ describe("authenticate", () => {
7266describe ( "signIn" , ( ) => {
7367 beforeEach ( ( ) => {
7468 jest . clearAllMocks ( )
75- mockKeycloak . authenticated = false
7669 } )
7770
7871 describe ( "successful" , ( ) => {
7972 sinopiaApi . fetchUser = jest . fn ( ) . mockResolvedValue ( userData )
8073 it ( "dispatches actions to add user" , async ( ) => {
8174 let store = mockStore ( )
82- await store . dispatch ( signIn ( "havram" , "m&rc" , "testerrorkey" ) )
75+ const mockKeycloak = {
76+ login : jest . fn ( ( ) => Promise . resolve ( true ) ) ,
77+ isTokenExpired : jest . fn ( ) ,
78+ updateToken : jest . fn ( ) ,
79+ }
80+ await store . dispatch ( signIn ( mockKeycloak , "testerrorkey" ) )
8381 // After successful signIn, redirected to Sinopia home-page
8482 mockKeycloak . authenticated = true
8583 mockKeycloak . tokenParsed = {
8684 preferred_username : "havram" ,
8785 }
8886 store = mockStore ( { authenticate : { user : undefined } } )
89- await store . dispatch ( authenticate ( ) )
87+ await store . dispatch ( authenticate ( mockKeycloak ) )
9088
9189 expect ( store . getActions ( ) ) . toHaveAction ( "SET_USER" , {
9290 username : "havram" ,
@@ -98,13 +96,16 @@ describe("signIn", () => {
9896 describe ( "failure" , ( ) => {
9997 it ( "dispatches actions to remove user" , async ( ) => {
10098 let store = mockStore ( )
101- await store . dispatch ( signIn ( "mdewey" , "amh&rst" , "testerrorkey" ) )
99+ const mockKeycloak = {
100+ login : jest . fn ( ( ) => Promise . resolve ( false ) ) ,
101+ }
102+ await store . dispatch ( signIn ( mockKeycloak , "testerrorkey" ) )
102103 expect ( store . getActions ( ) ) . toHaveAction ( "CLEAR_ERRORS" , "testerrorkey" )
103104
104105 // SignIn failures happen in Keycloak so can't test failures
105106 // directly, simulates user refreshing Sinopia
106107 store = mockStore ( { authenticate : { user : undefined } } )
107- await store . dispatch ( authenticate ( ) )
108+ await store . dispatch ( authenticate ( mockKeycloak ) )
108109
109110 expect ( store . getActions ( ) ) . toHaveAction ( "REMOVE_USER" )
110111 } )
@@ -115,7 +116,10 @@ describe("signOut", () => {
115116 describe ( "successful" , ( ) => {
116117 it ( "dispatches actions to remove user" , async ( ) => {
117118 const store = mockStore ( )
118- await store . dispatch ( signOut ( ) )
119+ const mockKeycloak = {
120+ logout : jest . fn ( ( ) => Promise . resolve ( true ) ) ,
121+ }
122+ await store . dispatch ( signOut ( mockKeycloak ) )
119123
120124 expect ( store . getActions ( ) ) . toHaveAction ( "REMOVE_USER" )
121125 } )
0 commit comments