8a17fe9f42da87b0360845580d2ce4e8fcdabb6f
[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             apiRevision: 12345678,
71         };
72
73         store.dispatch(initAuth(config));
74
75         store.subscribe(() => {
76             const auth = store.getState().auth;
77             if (auth.apiToken === "token" &&
78                 auth.sessions.length === 2 &&
79                 auth.sessions[0].status === 2 &&
80                 auth.sessions[1].status === 2
81             ) {
82                 try {
83                     expect(auth).toEqual({
84                         apiToken: "token",
85                         config: {
86                             apiRevision: 12345678,
87                             remoteHosts: {
88                                 "xc59z": "xc59z.arvadosapi.com",
89                             },
90                             rootUrl: "https://zzzzz.arvadosapi.com",
91                             uuidPrefix: "zzzzz",
92                         },
93                         sshKeys: [],
94                         homeCluster: "zzzzz",
95                         localCluster: "zzzzz",
96                         loginCluster: undefined,
97                         remoteHostsConfig: {
98                             "zzzzz": {
99                                 "apiRevision": 12345678,
100                                 "remoteHosts": {
101                                     "xc59z": "xc59z.arvadosapi.com",
102                                 },
103                                 "rootUrl": "https://zzzzz.arvadosapi.com",
104                                 "uuidPrefix": "zzzzz",
105                             },
106                         },
107                         remoteHosts: {
108                             zzzzz: "zzzzz.arvadosapi.com",
109                             xc59z: "xc59z.arvadosapi.com"
110                         },
111                         sessions: [{
112                             "active": true,
113                             "baseUrl": undefined,
114                             "clusterId": "zzzzz",
115                             "email": "test@test.com",
116                             "loggedIn": true,
117                             "remoteHost": "https://zzzzz.arvadosapi.com",
118                             "status": 2,
119                             "token": "token",
120                             "name": "John Doe",
121                             "apiRevision": 12345678,
122                             "uuid": "zzzzz-tpzed-abcefg",
123                         }, {
124                             "active": false,
125                             "baseUrl": "",
126                             "clusterId": "xc59z",
127                             "email": "",
128                             "loggedIn": false,
129                             "remoteHost": "xc59z.arvadosapi.com",
130                             "status": 2,
131                             "token": "",
132                             "name": "",
133                             "uuid": "",
134                             "apiRevision": 0,
135                         }],
136                         user: {
137                             email: "test@test.com",
138                             firstName: "John",
139                             lastName: "Doe",
140                             uuid: "zzzzz-tpzed-abcefg",
141                             ownerUuid: "ownerUuid",
142                             username: "jdoe",
143                             prefs: { profile: {} },
144                             isAdmin: false,
145                             isActive: true
146                         }
147                     });
148                     done();
149                 } catch (e) {
150                     console.log(e);
151                 }
152             }
153         });
154     });
155
156
157     // TODO: Add remaining action tests
158     /*
159     it('should fire external url to login', () => {
160         const initialState = undefined;
161         window.location.assign = jest.fn();
162         reducer(initialState, authActions.LOGIN());
163         expect(window.location.assign).toBeCalledWith(
164             `/login?return_to=${window.location.protocol}//${window.location.host}/token`
165         );
166     });
167
168     it('should fire external url to logout', () => {
169         const initialState = undefined;
170         window.location.assign = jest.fn();
171         reducer(initialState, authActions.LOGOUT());
172         expect(window.location.assign).toBeCalledWith(
173             `/logout?return_to=${location.protocol}//${location.host}`
174         );
175     });
176     */
177 });