1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { authReducer } from "./auth-reducer";
6 import { authActions } from "./auth-action";
14 } from "../../services/auth-service/auth-service";
16 import 'jest-localstorage-mock';
18 describe('auth-reducer', () => {
23 it('should return default state on initialisation', () => {
24 const initialState = undefined;
25 const state = authReducer(initialState, authActions.INIT());
26 expect(state).toEqual({
32 it('should read user and api token from local storage on init if they are there', () => {
33 const initialState = undefined;
35 localStorage.setItem(API_TOKEN_KEY, "token");
36 localStorage.setItem(USER_EMAIL_KEY, "test@test.com");
37 localStorage.setItem(USER_FIRST_NAME_KEY, "John");
38 localStorage.setItem(USER_LAST_NAME_KEY, "Doe");
39 localStorage.setItem(USER_UUID_KEY, "uuid");
40 localStorage.setItem(USER_OWNER_UUID_KEY, "ownerUuid");
42 const state = authReducer(initialState, authActions.INIT());
43 expect(state).toEqual({
46 email: "test@test.com",
50 ownerUuid: "ownerUuid"
55 it('should store token in local storage', () => {
56 const initialState = undefined;
58 const state = authReducer(initialState, authActions.SAVE_API_TOKEN("token"));
59 expect(state).toEqual({
64 expect(localStorage.getItem(API_TOKEN_KEY)).toBe("token");
67 it('should set user details on success fetch', () => {
68 const initialState = undefined;
71 email: "test@test.com",
75 ownerUuid: "ownerUuid"
78 const state = authReducer(initialState, authActions.USER_DETAILS_SUCCESS(user));
79 expect(state).toEqual({
82 email: "test@test.com",
86 ownerUuid: "ownerUuid",
90 expect(localStorage.getItem(API_TOKEN_KEY)).toBe("token");
93 it('should fire external url to login', () => {
94 const initialState = undefined;
95 window.location.assign = jest.fn();
96 authReducer(initialState, authActions.LOGIN());
97 expect(window.location.assign).toBeCalledWith(
98 `/login?return_to=${window.location.protocol}//${window.location.host}/token`
102 it('should fire external url to logout', () => {
103 const initialState = undefined;
104 window.location.assign = jest.fn();
105 authReducer(initialState, authActions.LOGOUT());
106 expect(window.location.assign).toBeCalledWith(
107 `/logout?return_to=${location.protocol}//${location.host}`