38cf1581d3796dc5b89a4d7cfc897adc425271ce
[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
8 import 'jest-localstorage-mock';
9 import { createServices } from "~/services/services";
10 import { mockConfig } from '~/common/config';
11 import { ApiActions } from "~/services/api/api-actions";
12
13 describe('auth-reducer', () => {
14     let reducer: (state: AuthState | undefined, action: AuthAction) => any;
15     const actions: ApiActions = {
16         progressFn: (id: string, working: boolean) => { },
17         errorFn: (id: string, message: string) => { }
18     };
19
20     beforeAll(() => {
21         localStorage.clear();
22         reducer = authReducer(createServices(mockConfig({}), actions));
23     });
24
25     it('should correctly initialise state', () => {
26         const initialState = undefined;
27         const user = {
28             email: "test@test.com",
29             firstName: "John",
30             lastName: "Doe",
31             uuid: "zzzzz-tpzed-xurymjxw79nv3jz",
32             ownerUuid: "ownerUuid",
33             username: "username",
34             prefs: {},
35             isAdmin: false,
36             isActive: true
37         };
38         const state = reducer(initialState, authActions.INIT({ user, token: "token" }));
39         expect(state).toEqual({
40             apiToken: "token",
41             user,
42             sshKeys: [],
43             sessions: [],
44             homeCluster: "zzzzz",
45             localCluster: "",
46             remoteHosts: {}
47         });
48     });
49
50     it('should save api token', () => {
51         const initialState = undefined;
52
53         const state = reducer(initialState, authActions.SAVE_API_TOKEN("token"));
54         expect(state).toEqual({
55             apiToken: "token",
56             user: undefined,
57             sshKeys: [],
58             sessions: [],
59             homeCluster: "",
60             localCluster: "",
61             remoteHosts: {},
62         });
63     });
64
65     it('should set user details on success fetch', () => {
66         const initialState = undefined;
67
68         const user = {
69             email: "test@test.com",
70             firstName: "John",
71             lastName: "Doe",
72             uuid: "uuid",
73             ownerUuid: "ownerUuid",
74             username: "username",
75             prefs: {},
76             isAdmin: false,
77             isActive: true
78         };
79
80         const state = reducer(initialState, authActions.USER_DETAILS_SUCCESS(user));
81         expect(state).toEqual({
82             apiToken: undefined,
83             sshKeys: [],
84             sessions: [],
85             homeCluster: "",
86             localCluster: "",
87             remoteHosts: {},
88             user: {
89                 email: "test@test.com",
90                 firstName: "John",
91                 lastName: "Doe",
92                 uuid: "uuid",
93                 ownerUuid: "ownerUuid",
94                 username: "username",
95                 prefs: {},
96                 isAdmin: false,
97                 isActive: true
98             }
99         });
100     });
101 });