Merge branch '13819-fix-dynamic-url-configuration'
[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 { 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
18 describe('auth-reducer', () => {
19     beforeAll(() => {
20         localStorage.clear();
21     });
22
23     it('should return default state on initialisation', () => {
24         const initialState = undefined;
25         const state = authReducer(initialState, authActions.INIT());
26         expect(state).toEqual({
27             apiToken: undefined,
28             user: undefined
29         });
30     });
31
32     it('should read user and api token from local storage on init if they are there', () => {
33         const initialState = undefined;
34
35         localStorage.setItem(API_TOKEN_KEY, "token");
36         localStorage.setItem(USER_EMAIL_KEY, "test@test.com");
37         localStorage.setItem(USER_FIRST_NAME_KEY, "John");
38         localStorage.setItem(USER_LAST_NAME_KEY, "Doe");
39         localStorage.setItem(USER_UUID_KEY, "uuid");
40         localStorage.setItem(USER_OWNER_UUID_KEY, "ownerUuid");
41
42         const state = authReducer(initialState, authActions.INIT());
43         expect(state).toEqual({
44             apiToken: "token",
45             user: {
46                 email: "test@test.com",
47                 firstName: "John",
48                 lastName: "Doe",
49                 uuid: "uuid",
50                 ownerUuid: "ownerUuid"
51             }
52         });
53     });
54
55     it('should store token in local storage', () => {
56         const initialState = undefined;
57
58         const state = authReducer(initialState, authActions.SAVE_API_TOKEN("token"));
59         expect(state).toEqual({
60             apiToken: "token",
61             user: undefined
62         });
63
64         expect(localStorage.getItem(API_TOKEN_KEY)).toBe("token");
65     });
66
67     it('should set user details on success fetch', () => {
68         const initialState = undefined;
69
70         const user = {
71             email: "test@test.com",
72             firstName: "John",
73             lastName: "Doe",
74             uuid: "uuid",
75             ownerUuid: "ownerUuid"
76         };
77
78         const state = authReducer(initialState, authActions.USER_DETAILS_SUCCESS(user));
79         expect(state).toEqual({
80             apiToken: undefined,
81             user: {
82                 email: "test@test.com",
83                 firstName: "John",
84                 lastName: "Doe",
85                 uuid: "uuid",
86                 ownerUuid: "ownerUuid",
87             }
88         });
89
90         expect(localStorage.getItem(API_TOKEN_KEY)).toBe("token");
91     });
92
93     it('should fire external url to login', () => {
94         const initialState = undefined;
95         window.location.assign = jest.fn();
96         authReducer(initialState, authActions.LOGIN());
97         expect(window.location.assign).toBeCalledWith(
98             `/login?return_to=${window.location.protocol}//${window.location.host}/token`
99         );
100     });
101
102     it('should fire external url to logout', () => {
103         const initialState = undefined;
104         window.location.assign = jest.fn();
105         authReducer(initialState, authActions.LOGOUT());
106         expect(window.location.assign).toBeCalledWith(
107             `/logout?return_to=${location.protocol}//${location.host}`
108         );
109     });
110 });