Merge branch '21128-toolbar-context-menu'
[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({ user, token: "token" }));
39         expect(state).toEqual({
40             apiToken: "token",
41             config: mockConfig({}),
42             user,
43             sshKeys: [],
44             sessions: [],
45             homeCluster: "zzzzz",
46             localCluster: "",
47             loginCluster: "",
48             remoteHosts: {},
49             remoteHostsConfig: {}
50         });
51     });
52
53     it('should set user details on success fetch', () => {
54         const initialState = undefined;
55
56         const user = {
57             email: "test@test.com",
58             firstName: "John",
59             lastName: "Doe",
60             uuid: "uuid",
61             ownerUuid: "ownerUuid",
62             username: "username",
63             prefs: {},
64             isAdmin: false,
65             isActive: true
66         };
67
68         const state = reducer(initialState, authActions.USER_DETAILS_SUCCESS(user));
69         expect(state).toEqual({
70             apiToken: undefined,
71             config: mockConfig({}),
72             sshKeys: [],
73             sessions: [],
74             homeCluster: "uuid",
75             localCluster: "",
76             loginCluster: "",
77             remoteHosts: {},
78             remoteHostsConfig: {},
79             user: {
80                 email: "test@test.com",
81                 firstName: "John",
82                 lastName: "Doe",
83                 uuid: "uuid",
84                 ownerUuid: "ownerUuid",
85                 username: "username",
86                 prefs: {},
87                 isAdmin: false,
88                 isActive: true
89             }
90         });
91     });
92 });