diff --git a/dist/redux-oidc.js b/dist/redux-oidc.js index 0c507df..c8e8b81 100644 --- a/dist/redux-oidc.js +++ b/dist/redux-oidc.js @@ -1 +1 @@ -!function(e,r){if("object"==typeof exports&&"object"==typeof module)module.exports=r(require("prop-types"),require("react"),function(){try{return require("immutable")}catch(e){}}(),function(){try{return require("oidc-client")}catch(e){}}());else if("function"==typeof define&&define.amd)define(["prop-types","react","immutable","oidc-client"],r);else{var t="object"==typeof exports?r(require("prop-types"),require("react"),function(){try{return require("immutable")}catch(e){}}(),function(){try{return require("oidc-client")}catch(e){}}()):r(e["prop-types"],e.react,e.immutable,e["oidc-client"]);for(var n in t)("object"==typeof exports?exports:e)[n]=t[n]}}(this,function(e,r,t,n){return function(e){function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}var t={};return r.m=e,r.c=t,r.i=function(e){return e},r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:n})},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},r.p="",r(r.s=15)}([function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.USER_EXPIRED="redux-oidc/USER_EXPIRED",r.REDIRECT_SUCCESS="redux-oidc/REDIRECT_SUCCESS",r.USER_LOADED="redux-oidc/USER_LOADED",r.SILENT_RENEW_ERROR="redux-oidc/SILENT_RENEW_ERROR",r.SESSION_TERMINATED="redux-oidc/SESSION_TERMINATED",r.USER_EXPIRING="redux-oidc/USER_EXPIRING",r.USER_FOUND="redux-oidc/USER_FOUND",r.LOADING_USER="redux-oidc/LOADING_USER",r.USER_SIGNED_OUT="redux-oidc/USER_SIGNED_OUT",r.LOAD_USER_ERROR="redux-oidc/LOAD_USER_ERROR"},function(e,r,t){"use strict";function n(){return{type:E.USER_EXPIRED}}function o(e){return{type:E.REDIRECT_SUCCESS,payload:e}}function i(e){return{type:E.USER_FOUND,payload:e}}function u(e){return{type:E.SILENT_RENEW_ERROR,payload:e}}function s(){return{type:E.SESSION_TERMINATED}}function c(){return{type:E.USER_EXPIRING}}function a(){return{type:E.LOADING_USER}}function d(){return{type:E.USER_SIGNED_OUT}}function l(){return{type:E.LOAD_USER_ERROR}}Object.defineProperty(r,"__esModule",{value:!0}),r.userExpired=n,r.redirectSuccess=o,r.userFound=i,r.silentRenewError=u,r.sessionTerminated=s,r.userExpiring=c,r.loadingUser=a,r.userSignedOut=d,r.loadUserError=l;var E=t(0)},function(e,r,t){"use strict";function n(e){return new o(e)}Object.defineProperty(r,"__esModule",{value:!0}),r.default=n;var o=void 0;try{o=t(14).UserManager}catch(e){try{o=t(!function(){var e=new Error('Cannot find module "oidc-client-fetch"');throw e.code="MODULE_NOT_FOUND",e}()).UserManager}catch(e){throw new Error("redux-oidc: Couldn't find UserManager. Please install either 'oidc-client' or 'oidc-client-fetch'.")}}},function(e,r){e.exports=require("prop-types")},function(e,r){e.exports=require("react")},function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.userSignedOut=r.loadingUser=r.userExpiring=r.sessionTerminated=r.silentRenewError=r.userFound=r.redirectSuccess=r.userExpired=r.LOAD_USER_ERROR=r.USER_SIGNED_OUT=r.LOADING_USER=r.USER_EXPIRING=r.SESSION_TERMINATED=r.SILENT_RENEW_ERROR=r.USER_NOT_FOUND=r.USER_FOUND=r.REDIRECT_SUCCESS=r.USER_EXPIRED=r.OidcProvider=r.reducer=r.immutableReducer=r.CallbackComponent=r.loadUser=r.processSilentRenew=r.createUserManager=void 0;var n=t(10),o=function(e){return e&&e.__esModule?e:{default:e}}(n);r.createUserManager=t(2).default,r.processSilentRenew=t(9).default,r.loadUser=t(8).default,r.CallbackComponent=t(6).default,r.immutableReducer=t(11).default,r.reducer=t(12).default,r.OidcProvider=t(7).default,r.USER_EXPIRED=t(0).USER_EXPIRED,r.REDIRECT_SUCCESS=t(0).REDIRECT_SUCCESS,r.USER_FOUND=t(0).USER_FOUND,r.USER_NOT_FOUND=t(0).USER_NOT_FOUND,r.SILENT_RENEW_ERROR=t(0).SILENT_RENEW_ERROR,r.SESSION_TERMINATED=t(0).SESSION_TERMINATED,r.USER_EXPIRING=t(0).USER_EXPIRING,r.LOADING_USER=t(0).LOADING_USER,r.USER_SIGNED_OUT=t(0).USER_SIGNED_OUT,r.LOAD_USER_ERROR=t(0).LOAD_USER_ERROR,r.userExpired=t(1).userExpired,r.redirectSuccess=t(1).redirectSuccess,r.userFound=t(1).userFound,r.silentRenewError=t(1).silentRenewError,r.sessionTerminated=t(1).sessionTerminated,r.userExpiring=t(1).userExpiring,r.loadingUser=t(1).loadingUser,r.userSignedOut=t(1).userSignedOut,r.default=o.default},function(e,r,t){"use strict";function n(e){return e&&e.__esModule?e:{default:e}}function o(e,r){if(!(e instanceof r))throw new TypeError("Cannot call a class as a function")}function i(e,r){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!r||"object"!=typeof r&&"function"!=typeof r?e:r}function u(e,r){if("function"!=typeof r&&null!==r)throw new TypeError("Super expression must either be null or a function, not "+typeof r);e.prototype=Object.create(r&&r.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),r&&(Object.setPrototypeOf?Object.setPrototypeOf(e,r):e.__proto__=r)}Object.defineProperty(r,"__esModule",{value:!0});var s=function(){function e(e,r){for(var t=0;t0&&void 0!==arguments[0]?arguments[0]:u,r=arguments[1];switch(r.type){case n.USER_EXPIRED:case n.SILENT_RENEW_ERROR:return i({user:null,isLoadingUser:!1});case n.SESSION_TERMINATED:case n.USER_SIGNED_OUT:return i({user:null,isLoadingUser:!1});case n.REDIRECT_SUCCESS:case n.USER_FOUND:return i({user:r.payload,isLoadingUser:!1});case n.LOADING_USER:return e.set("isLoadingUser",!0);default:return e}}}catch(e){o=function(){console.error("You must install immutable-js for the immutable reducer to work!")}}r.default=o},function(e,r,t){"use strict";function n(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:u,r=arguments[1];switch(r.type){case i.USER_EXPIRED:case i.SILENT_RENEW_ERROR:return Object.assign({},o({},e),{user:null,isLoadingUser:!1});case i.SESSION_TERMINATED:case i.USER_SIGNED_OUT:return Object.assign({},o({},e),{user:null,isLoadingUser:!1});case i.REDIRECT_SUCCESS:case i.USER_FOUND:return Object.assign({},o({},e),{user:r.payload,isLoadingUser:!1});case i.LOADING_USER:return Object.assign({},o({},e),{isLoadingUser:!0});default:return e}}Object.defineProperty(r,"__esModule",{value:!0});var o=Object.assign||function(e){for(var r=1;r0&&void 0!==arguments[0]?arguments[0]:d,r=arguments[1];switch(r.type){case o.USER_EXPIRED:case o.SILENT_RENEW_ERROR:return s({user:null,isLoadingUser:!1});case o.SESSION_TERMINATED:case o.USER_SIGNED_OUT:return s({user:null,isLoadingUser:!1});case o.REDIRECT_SUCCESS:case o.USER_FOUND:return a({user:r.payload,isLoadingUser:!1});case o.LOADING_USER:return e.set("isLoadingUser",!0);default:return e}}}catch(e){i=function(){console.error("You must install immutable-js for the immutable reducer to work!")}}r.default=i},function(e,r,t){"use strict";function n(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:u,r=arguments[1];switch(r.type){case i.USER_EXPIRED:case i.SILENT_RENEW_ERROR:return Object.assign({},o({},e),{user:null,isLoadingUser:!1});case i.SESSION_TERMINATED:case i.USER_SIGNED_OUT:return Object.assign({},o({},e),{user:null,isLoadingUser:!1});case i.REDIRECT_SUCCESS:case i.USER_FOUND:return Object.assign({},o({},e),{user:r.payload,isLoadingUser:!1});case i.LOADING_USER:return Object.assign({},o({},e),{isLoadingUser:!0});default:return e}}Object.defineProperty(r,"__esModule",{value:!0});var o=Object.assign||function(e){for(var r=1;r { - switch (action.type) { - case USER_EXPIRED: - return fromJS({ - user: null, - isLoadingUser: false - }); - case SILENT_RENEW_ERROR: - return fromJS({ - user: null, - isLoadingUser: false - }); - case SESSION_TERMINATED: - case USER_SIGNED_OUT: - return fromJS({ - user: null, - isLoadingUser: false - }); - case REDIRECT_SUCCESS: - case USER_FOUND: - return fromJS({ - user: action.payload, - isLoadingUser: false - }); - case LOADING_USER: - return state.set('isLoadingUser', true); - default: - return state; + const fromJSGreedy = (js) => { + return typeof js !== 'object' || js === null ? js : + Array.isArray(js) ? + Seq(js).map(fromJSGreedy).toList() : + Seq(js).map(fromJSGreedy).toMap(); } - }; + + const initialState = fromJS({ + user: null, + isLoadingUser: false + }); + + reducer = (state = initialState, action) => { + switch (action.type) { + case USER_EXPIRED: + return fromJS({ + user: null, + isLoadingUser: false + }); + case SILENT_RENEW_ERROR: + return fromJS({ + user: null, + isLoadingUser: false + }); + case SESSION_TERMINATED: + case USER_SIGNED_OUT: + return fromJS({ + user: null, + isLoadingUser: false + }); + case REDIRECT_SUCCESS: + case USER_FOUND: + return fromJSGreedy({ + user: action.payload, + isLoadingUser: false + }); + case LOADING_USER: + return state.set('isLoadingUser', true); + default: + return state; + } + }; } catch (error) { reducer = () => { console.error("You must install immutable-js for the immutable reducer to work!"); diff --git a/tests/helpers/loadUser.test.js b/tests/helpers/loadUser.test.js index b9c5606..79f3a67 100644 --- a/tests/helpers/loadUser.test.js +++ b/tests/helpers/loadUser.test.js @@ -2,7 +2,7 @@ import '../setup'; import sinon from 'sinon'; import expect from 'expect'; import loadUserHandler, { getUserCallback, errorCallback, setReduxStore, getReduxStore } from '../../src/helpers/loadUser'; -import { userExpired, userFound, loadUserError } from '../../src/actions'; +import { userExpired, userFound, loadUserError, loadingUser } from '../../src/actions'; describe('helper - loadUser()', () => { let userManagerMock; @@ -26,9 +26,7 @@ describe('helper - loadUser()', () => { dispatch: dispatchStub }; - getUserStub.returns({ - then: thenStub - }); + getUserStub.returns(new Promise(() => {})); thenStub.returns({ catch: catchStub @@ -82,4 +80,21 @@ describe('helper - loadUser()', () => { const result2 = getUserCallback(expiredUser); expect(result2).toEqual(expiredUser); }); + + it ('loadUserCallback should dispatch USER_EXPIRED when the user returned is null', () => { + getUserCallback(null); + expect(dispatchStub.calledWith(userExpired())).toEqual(true); + }); + + it('loadUser should dispatch LOADING_USER', () => { + loadUserHandler(storeMock, userManagerMock); + + expect(dispatchStub.calledWith(loadingUser())).toEqual(true); + }); + + it('loadUser should return a promise', () => { + const promise = loadUserHandler(storeMock, userManagerMock); + + expect(promise).toBeA(Promise); + }); }); diff --git a/tests/reducer/reducer-immutable.test.js b/tests/reducer/reducer-immutable.test.js index 9d34f3e..2e77f8d 100644 --- a/tests/reducer/reducer-immutable.test.js +++ b/tests/reducer/reducer-immutable.test.js @@ -98,4 +98,18 @@ describe('immutable reducer', () => { expect(reducer(expectedResult, { type: 'UNKNOWN' })).toEqual(expectedResult); }); + + it('should handle USER_FOUND correctly when payload has non-object prototype', () => { + function NonObject() { + this.type = 'not-an-object' + } + const user = { some: 'user' }; + const nonObjectUser = Object.setPrototypeOf({ some: 'user' }, new NonObject()); + const expectedResult = fromJS({ + user, + isLoadingUser: false + }); + + expect(reducer(fromJS(initialState), userFound(nonObjectUser))).toEqual(expectedResult); + }); });