1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { authReducer, AuthState } from "./auth-reducer";
6 import { AuthAction, authActions } from "./auth-action";
14 } from "../../services/auth-service/auth-service";
16 import 'jest-localstorage-mock';
17 import { createServices } from "../../services/services";
19 describe('auth-reducer', () => {
20 let reducer: (state: AuthState | undefined, action: AuthAction) => any;
24 reducer = authReducer(createServices("/arvados/v1"));
27 it('should return default state on initialisation', () => {
28 const initialState = undefined;
29 const state = reducer(initialState, authActions.INIT());
30 expect(state).toEqual({
36 it('should read user and api token from local storage on init if they are there', () => {
37 const initialState = undefined;
39 localStorage.setItem(API_TOKEN_KEY, "token");
40 localStorage.setItem(USER_EMAIL_KEY, "test@test.com");
41 localStorage.setItem(USER_FIRST_NAME_KEY, "John");
42 localStorage.setItem(USER_LAST_NAME_KEY, "Doe");
43 localStorage.setItem(USER_UUID_KEY, "uuid");
44 localStorage.setItem(USER_OWNER_UUID_KEY, "ownerUuid");
46 const state = reducer(initialState, authActions.INIT());
47 expect(state).toEqual({
50 email: "test@test.com",
54 ownerUuid: "ownerUuid"
59 it('should store token in local storage', () => {
60 const initialState = undefined;
62 const state = reducer(initialState, authActions.SAVE_API_TOKEN("token"));
63 expect(state).toEqual({
68 expect(localStorage.getItem(API_TOKEN_KEY)).toBe("token");
71 it('should set user details on success fetch', () => {
72 const initialState = undefined;
75 email: "test@test.com",
79 ownerUuid: "ownerUuid"
82 const state = reducer(initialState, authActions.USER_DETAILS_SUCCESS(user));
83 expect(state).toEqual({
86 email: "test@test.com",
90 ownerUuid: "ownerUuid",
94 expect(localStorage.getItem(API_TOKEN_KEY)).toBe("token");
97 it('should fire external url to login', () => {
98 const initialState = undefined;
99 window.location.assign = jest.fn();
100 reducer(initialState, authActions.LOGIN());
101 expect(window.location.assign).toBeCalledWith(
102 `/login?return_to=${window.location.protocol}//${window.location.host}/token`
106 it('should fire external url to logout', () => {
107 const initialState = undefined;
108 window.location.assign = jest.fn();
109 reducer(initialState, authActions.LOGOUT());
110 expect(window.location.assign).toBeCalledWith(
111 `/logout?return_to=${location.protocol}//${location.host}`