Restric order and filters arguments of favorite list
[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 user = {
72             email: "test@test.com",
73             firstName: "John",
74             lastName: "Doe",
75             uuid: "uuid",
76             ownerUuid: "ownerUuid"
77         };
78
79         const state = authReducer(initialState, actions.USER_DETAILS_SUCCESS(user));
80         expect(state).toEqual({
81             apiToken: undefined,
82             user: {
83                 email: "test@test.com",
84                 firstName: "John",
85                 lastName: "Doe",
86                 uuid: "uuid",
87                 ownerUuid: "ownerUuid",
88             }
89         });
90
91         expect(localStorage.getItem(API_TOKEN_KEY)).toBe("token");
92     });
93
94     it('should fire external url to login', () => {
95         const initialState = undefined;
96         window.location.assign = jest.fn();
97         authReducer(initialState, actions.LOGIN());
98         expect(window.location.assign).toBeCalledWith(
99             `${API_HOST}/login?return_to=${window.location.protocol}//${window.location.host}/token`
100         );
101     });
102
103     it('should fire external url to logout', () => {
104         const initialState = undefined;
105         window.location.assign = jest.fn();
106         authReducer(initialState, actions.LOGOUT());
107         expect(window.location.assign).toBeCalledWith(
108             `${API_HOST}/logout?return_to=${location.protocol}//${location.host}`
109         );
110     });
111 });