left-side-panel-small-refactor
[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 from "./auth-reducer";
6 import actions 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 import { API_HOST } from "../../common/api/server-api";
16
17 import 'jest-localstorage-mock';
18
19 describe('auth-reducer', () => {
20     beforeAll(() => {
21         localStorage.clear();
22     });
23
24     it('should return default state on initialisation', () => {
25         const initialState = undefined;
26         const state = authReducer(initialState, actions.INIT());
27         expect(state).toEqual({
28             apiToken: undefined,
29             user: undefined
30         });
31     });
32
33     it('should read user and api token from local storage on init if they are there', () => {
34         const initialState = undefined;
35
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");
42
43         const state = authReducer(initialState, actions.INIT());
44         expect(state).toEqual({
45             apiToken: "token",
46             user: {
47                 email: "test@test.com",
48                 firstName: "John",
49                 lastName: "Doe",
50                 uuid: "uuid",
51                 ownerUuid: "ownerUuid"
52             }
53         });
54     });
55
56     it('should store token in local storage', () => {
57         const initialState = undefined;
58
59         const state = authReducer(initialState, actions.SAVE_API_TOKEN("token"));
60         expect(state).toEqual({
61             apiToken: "token",
62             user: undefined
63         });
64
65         expect(localStorage.getItem(API_TOKEN_KEY)).toBe("token");
66     });
67
68     it('should set user details on success fetch', () => {
69         const initialState = undefined;
70
71         const userDetails = {
72             email: "test@test.com",
73             first_name: "John",
74             last_name: "Doe",
75             uuid: "uuid",
76             owner_uuid: "ownerUuid",
77             is_admin: true
78         };
79
80         const state = authReducer(initialState, actions.USER_DETAILS_SUCCESS(userDetails));
81         expect(state).toEqual({
82             apiToken: undefined,
83             user: {
84                 email: "test@test.com",
85                 firstName: "John",
86                 lastName: "Doe",
87                 uuid: "uuid",
88                 ownerUuid: "ownerUuid",
89             }
90         });
91
92         expect(localStorage.getItem(API_TOKEN_KEY)).toBe("token");
93     });
94
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`
101         );
102     });
103
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}`
110         );
111     });
112 });