Remove setBaseUrl
[arvados-workbench2.git] / src / store / auth / auth-reducer.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { authReducer, AuthState } from "./auth-reducer";
6 import { AuthAction, authActions } from "./auth-action";
7 import {
8     API_TOKEN_KEY,
9     USER_EMAIL_KEY,
10     USER_FIRST_NAME_KEY,
11     USER_LAST_NAME_KEY,
12     USER_OWNER_UUID_KEY,
13     USER_UUID_KEY
14 } from "../../services/auth-service/auth-service";
15
16 import 'jest-localstorage-mock';
17 import { createServices } from "../../services/services";
18
19 describe('auth-reducer', () => {
20     let reducer: (state: AuthState | undefined, action: AuthAction) => any;
21
22     beforeAll(() => {
23         localStorage.clear();
24         reducer = authReducer(createServices("/arvados/v1"));
25     });
26
27     it('should return default state on initialisation', () => {
28         const initialState = undefined;
29         const state = reducer(initialState, authActions.INIT());
30         expect(state).toEqual({
31             apiToken: undefined,
32             user: undefined
33         });
34     });
35
36     it('should read user and api token from local storage on init if they are there', () => {
37         const initialState = undefined;
38
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");
45
46         const state = reducer(initialState, authActions.INIT());
47         expect(state).toEqual({
48             apiToken: "token",
49             user: {
50                 email: "test@test.com",
51                 firstName: "John",
52                 lastName: "Doe",
53                 uuid: "uuid",
54                 ownerUuid: "ownerUuid"
55             }
56         });
57     });
58
59     it('should store token in local storage', () => {
60         const initialState = undefined;
61
62         const state = reducer(initialState, authActions.SAVE_API_TOKEN("token"));
63         expect(state).toEqual({
64             apiToken: "token",
65             user: undefined
66         });
67
68         expect(localStorage.getItem(API_TOKEN_KEY)).toBe("token");
69     });
70
71     it('should set user details on success fetch', () => {
72         const initialState = undefined;
73
74         const user = {
75             email: "test@test.com",
76             firstName: "John",
77             lastName: "Doe",
78             uuid: "uuid",
79             ownerUuid: "ownerUuid"
80         };
81
82         const state = reducer(initialState, authActions.USER_DETAILS_SUCCESS(user));
83         expect(state).toEqual({
84             apiToken: undefined,
85             user: {
86                 email: "test@test.com",
87                 firstName: "John",
88                 lastName: "Doe",
89                 uuid: "uuid",
90                 ownerUuid: "ownerUuid",
91             }
92         });
93
94         expect(localStorage.getItem(API_TOKEN_KEY)).toBe("token");
95     });
96
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`
103         );
104     });
105
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}`
112         );
113     });
114 });