1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import authReducer from "./auth-reducer";
6 import actions from "./auth-action";
14 } from "../../services/auth-service/auth-service";
15 import { API_HOST } from "../../common/api/server-api";
17 import 'jest-localstorage-mock';
19 describe('auth-reducer', () => {
24 it('should return default state on initialisation', () => {
25 const initialState = undefined;
26 const state = authReducer(initialState, actions.INIT());
27 expect(state).toEqual({
33 it('should read user and api token from local storage on init if they are there', () => {
34 const initialState = undefined;
36 localStorage.setItem(API_TOKEN_KEY, "token");
37 localStorage.setItem(USER_EMAIL_KEY, "test@test.com");
38 localStorage.setItem(USER_FIRST_NAME_KEY, "John");
39 localStorage.setItem(USER_LAST_NAME_KEY, "Doe");
40 localStorage.setItem(USER_UUID_KEY, "uuid");
41 localStorage.setItem(USER_OWNER_UUID_KEY, "ownerUuid");
43 const state = authReducer(initialState, actions.INIT());
44 expect(state).toEqual({
47 email: "test@test.com",
51 ownerUuid: "ownerUuid"
56 it('should store token in local storage', () => {
57 const initialState = undefined;
59 const state = authReducer(initialState, actions.SAVE_API_TOKEN("token"));
60 expect(state).toEqual({
65 expect(localStorage.getItem(API_TOKEN_KEY)).toBe("token");
68 it('should set user details on success fetch', () => {
69 const initialState = undefined;
72 email: "test@test.com",
76 owner_uuid: "ownerUuid",
80 const state = authReducer(initialState, actions.USER_DETAILS_SUCCESS(userDetails));
81 expect(state).toEqual({
84 email: "test@test.com",
88 ownerUuid: "ownerUuid",
92 expect(localStorage.getItem(API_TOKEN_KEY)).toBe("token");
95 it('should fire external url to login', () => {
96 const initialState = undefined;
97 window.location.assign = jest.fn();
98 authReducer(initialState, actions.LOGIN());
99 expect(window.location.assign).toBeCalledWith(
100 `${API_HOST}/login?return_to=${window.location.protocol}//${window.location.host}/token`
104 it('should fire external url to logout', () => {
105 const initialState = undefined;
106 window.location.assign = jest.fn();
107 authReducer(initialState, actions.LOGOUT());
108 expect(window.location.assign).toBeCalledWith(
109 `${API_HOST}/logout?return_to=${location.protocol}//${location.host}`