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