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";
12 } from "../../services/auth-service/auth-service";
13 import { API_HOST } from "../../common/server-api";
15 import 'jest-localstorage-mock';
17 describe('auth-reducer', () => {
22 it('should return default state on initialisation', () => {
23 const initialState = undefined;
24 const state = authReducer(initialState, actions.INIT());
25 expect(state).toEqual({
31 it('should read user and api token from local storage on init if they are there', () => {
32 const initialState = undefined;
34 localStorage.setItem(API_TOKEN_KEY, "token");
35 localStorage.setItem(USER_EMAIL_KEY, "test@test.com");
36 localStorage.setItem(USER_FIRST_NAME_KEY, "John");
37 localStorage.setItem(USER_LAST_NAME_KEY, "Doe");
39 const state = authReducer(initialState, actions.INIT());
40 expect(state).toEqual({
43 email: "test@test.com",
50 it('should store token in local storage', () => {
51 const initialState = undefined;
53 const state = authReducer(initialState, actions.SAVE_API_TOKEN("token"));
54 expect(state).toEqual({
59 expect(localStorage.getItem(API_TOKEN_KEY)).toBe("token");
62 it('should set user details on success fetch', () => {
63 const initialState = undefined;
66 email: "test@test.com",
72 const state = authReducer(initialState, actions.USER_DETAILS_SUCCESS(userDetails));
73 expect(state).toEqual({
76 email: "test@test.com",
82 expect(localStorage.getItem(API_TOKEN_KEY)).toBe("token");
85 it('should fire external url to login', () => {
86 const initialState = undefined;
87 window.location.assign = jest.fn();
88 authReducer(initialState, actions.LOGIN());
89 expect(window.location.assign).toBeCalledWith(
90 `${API_HOST}/login?return_to=${window.location.protocol}//${window.location.host}/token`
94 it('should fire external url to logout', () => {
95 const initialState = undefined;
96 window.location.assign = jest.fn();
97 authReducer(initialState, actions.LOGOUT());
98 expect(window.location.assign).toBeCalledWith(
99 `${API_HOST}/logout?return_to=${location.protocol}//${location.host}`