Merge branch '15530-wb2-logincluster' refs #15530
[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             loginCluster: "",
47             remoteHosts: {},
48             remoteHostsConfig: {}
49         });
50     });
51
52     it('should save api token', () => {
53         const initialState = undefined;
54
55         const state = reducer(initialState, authActions.SAVE_API_TOKEN("token"));
56         expect(state).toEqual({
57             apiToken: "token",
58             user: undefined,
59             sshKeys: [],
60             sessions: [],
61             homeCluster: "",
62             localCluster: "",
63             loginCluster: "",
64             remoteHosts: {},
65             remoteHostsConfig: {}
66         });
67     });
68
69     it('should set user details on success fetch', () => {
70         const initialState = undefined;
71
72         const user = {
73             email: "test@test.com",
74             firstName: "John",
75             lastName: "Doe",
76             uuid: "uuid",
77             ownerUuid: "ownerUuid",
78             username: "username",
79             prefs: {},
80             isAdmin: false,
81             isActive: true
82         };
83
84         const state = reducer(initialState, authActions.USER_DETAILS_SUCCESS(user));
85         expect(state).toEqual({
86             apiToken: undefined,
87             sshKeys: [],
88             sessions: [],
89             homeCluster: "",
90             localCluster: "",
91             loginCluster: "",
92             remoteHosts: {},
93             remoteHostsConfig: {},
94             user: {
95                 email: "test@test.com",
96                 firstName: "John",
97                 lastName: "Doe",
98                 uuid: "uuid",
99                 ownerUuid: "ownerUuid",
100                 username: "username",
101                 prefs: {},
102                 isAdmin: false,
103                 isActive: true
104             }
105         });
106     });
107 });