d126d9caeb52f519e2b643be84384311765cfa5e
[arvados-workbench2.git] / src / store / auth / auth-action.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { initAuth } from "./auth-action";
6 import { API_TOKEN_KEY } from "~/services/auth-service/auth-service";
7
8 import 'jest-localstorage-mock';
9 import { ServiceRepository, createServices } from "~/services/services";
10 import { configureStore, RootStore } from "../store";
11 import { createBrowserHistory } from "history";
12 import { mockConfig } from '~/common/config';
13 import { ApiActions } from "~/services/api/api-actions";
14 import { ACCOUNT_LINK_STATUS_KEY } from '~/services/link-account-service/link-account-service';
15 import Axios from "axios";
16 import MockAdapter from "axios-mock-adapter";
17 import { ImportMock } from 'ts-mock-imports';
18 import * as servicesModule from "~/services/services";
19
20 describe('auth-actions', () => {
21     const axiosInst = Axios.create({ headers: {} });
22     const axiosMock = new MockAdapter(axiosInst);
23
24     let store: RootStore;
25     let services: ServiceRepository;
26     const actions: ApiActions = {
27         progressFn: (id: string, working: boolean) => { },
28         errorFn: (id: string, message: string) => { }
29     };
30     let importMocks: any[];
31
32     beforeEach(() => {
33         axiosMock.reset();
34         services = createServices(mockConfig({}), actions, axiosInst);
35         store = configureStore(createBrowserHistory(), services);
36         localStorage.clear();
37         importMocks = [];
38     });
39
40     afterEach(() => {
41         importMocks.map(m => m.restore());
42     });
43
44     it('should initialise state with user and api token from local storage', (done) => {
45
46         axiosMock
47             .onGet("/users/current")
48             .reply(200, {
49                 email: "test@test.com",
50                 first_name: "John",
51                 last_name: "Doe",
52                 uuid: "zzzzz-tpzed-abcefg",
53                 owner_uuid: "ownerUuid",
54                 is_admin: false,
55                 is_active: true,
56                 username: "jdoe",
57                 prefs: {}
58             });
59
60         importMocks.push(ImportMock.mockFunction(servicesModule, 'createServices', services));
61
62         // Only test the case when a link account operation is not being cancelled
63         sessionStorage.setItem(ACCOUNT_LINK_STATUS_KEY, "0");
64         localStorage.setItem(API_TOKEN_KEY, "token");
65
66         const config: any = {
67             rootUrl: "https://zzzzz.arvadosapi.com",
68             uuidPrefix: "zzzzz",
69             remoteHosts: { xc59z: "xc59z.arvadosapi.com" },
70         };
71
72         store.dispatch(initAuth(config));
73
74         store.subscribe(() => {
75             const auth = store.getState().auth;
76             if (auth.apiToken === "token" &&
77                 auth.sessions.length === 2 &&
78                 auth.sessions[0].status === 2 &&
79                 auth.sessions[1].status === 2
80             ) {
81                 try {
82                     expect(auth).toEqual({
83                         apiToken: "token",
84                         config: {
85                             remoteHosts: {
86                                 "xc59z": "xc59z.arvadosapi.com",
87                             },
88                             rootUrl: "https://zzzzz.arvadosapi.com",
89                             uuidPrefix: "zzzzz",
90                         },
91                         sshKeys: [],
92                         homeCluster: "zzzzz",
93                         localCluster: "zzzzz",
94                         loginCluster: undefined,
95                         remoteHostsConfig: {
96                             "zzzzz": {
97                                 "remoteHosts": {
98                                     "xc59z": "xc59z.arvadosapi.com",
99                                 },
100                                 "rootUrl": "https://zzzzz.arvadosapi.com",
101                                 "uuidPrefix": "zzzzz",
102                             },
103                         },
104                         remoteHosts: {
105                             zzzzz: "zzzzz.arvadosapi.com",
106                             xc59z: "xc59z.arvadosapi.com"
107                         },
108                         sessions: [{
109                             "active": true,
110                             "baseUrl": undefined,
111                             "clusterId": "zzzzz",
112                             "email": "test@test.com",
113                             "loggedIn": true,
114                             "remoteHost": "https://zzzzz.arvadosapi.com",
115                             "status": 2,
116                             "token": "token",
117                             "name": "John Doe"
118                     "uuid": "zzzzz-tpzed-abcefg",
119                         }, {
120                             "active": false,
121                             "baseUrl": "",
122                             "clusterId": "xc59z",
123                             "email": "",
124                             "loggedIn": false,
125                             "remoteHost": "xc59z.arvadosapi.com",
126                             "status": 2,
127                             "token": "",
128                             "name": "",
129                             "uuid": "",
130                         }],
131                         user: {
132                             email: "test@test.com",
133                             firstName: "John",
134                             lastName: "Doe",
135                             uuid: "zzzzz-tpzed-abcefg",
136                             ownerUuid: "ownerUuid",
137                             username: "jdoe",
138                             prefs: { profile: {} },
139                             isAdmin: false,
140                             isActive: true
141                         }
142                     });
143                     done();
144                 } catch (e) {
145                     console.log(e);
146                 }
147             }
148         });
149     });
150
151
152     // TODO: Add remaining action tests
153     /*
154     it('should fire external url to login', () => {
155         const initialState = undefined;
156         window.location.assign = jest.fn();
157         reducer(initialState, authActions.LOGIN());
158         expect(window.location.assign).toBeCalledWith(
159             `/login?return_to=${window.location.protocol}//${window.location.host}/token`
160         );
161     });
162
163     it('should fire external url to logout', () => {
164         const initialState = undefined;
165         window.location.assign = jest.fn();
166         reducer(initialState, authActions.LOGOUT());
167         expect(window.location.assign).toBeCalledWith(
168             `/logout?return_to=${location.protocol}//${location.host}`
169         );
170     });
171     */
172 });