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";
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, authActions.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, authActions.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, authActions.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 ownerUuid: "ownerUuid"
79 const state = authReducer(initialState, authActions.USER_DETAILS_SUCCESS(user));
80 expect(state).toEqual({
83 email: "test@test.com",
87 ownerUuid: "ownerUuid",
91 expect(localStorage.getItem(API_TOKEN_KEY)).toBe("token");
94 it('should fire external url to login', () => {
95 const initialState = undefined;
96 window.location.assign = jest.fn();
97 authReducer(initialState, authActions.LOGIN());
98 expect(window.location.assign).toBeCalledWith(
99 `${API_HOST}/login?return_to=${window.location.protocol}//${window.location.host}/token`
103 it('should fire external url to logout', () => {
104 const initialState = undefined;
105 window.location.assign = jest.fn();
106 authReducer(initialState, authActions.LOGOUT());
107 expect(window.location.assign).toBeCalledWith(
108 `${API_HOST}/logout?return_to=${location.protocol}//${location.host}`