From: Peter Amstutz Date: Fri, 15 Nov 2019 20:39:14 +0000 (-0500) Subject: 15803: Update test X-Git-Tag: 2.0.0~26^2~5 X-Git-Url: https://git.arvados.org/arvados-workbench2.git/commitdiff_plain/707b848245596bd4f25d497b45c1c3e501785ee4 15803: Update test Uses axios mocking and typescript module mocking. --- diff --git a/package.json b/package.json index a4ea3b19..6a56d826 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,8 @@ "redux-thunk": "2.3.0", "reselect": "4.0.0", "shell-quote": "1.6.1", + "sinon": "7.3", + "ts-mock-imports": "1.2.6", "tslint-etc": "1.6.0", "unionize": "2.1.2", "uuid": "3.3.2", diff --git a/src/services/auth-service/auth-service.ts b/src/services/auth-service/auth-service.ts index 2562dc6a..63cdd08c 100644 --- a/src/services/auth-service/auth-service.ts +++ b/src/services/auth-service/auth-service.ts @@ -43,7 +43,10 @@ export class AuthService { public saveApiToken(token: string) { localStorage.setItem(API_TOKEN_KEY, token); - localStorage.setItem(HOME_CLUSTER, token.split('/')[1].substr(0, 5)); + const sp = token.split('/'); + if (sp.length == 3) { + localStorage.setItem(HOME_CLUSTER, sp[1].substr(0, 5)); + } } public removeApiToken() { diff --git a/src/store/auth/auth-action.test.ts b/src/store/auth/auth-action.test.ts index b0449c0a..48f06d95 100644 --- a/src/store/auth/auth-action.test.ts +++ b/src/store/auth/auth-action.test.ts @@ -4,18 +4,7 @@ import { authReducer, AuthState } from "./auth-reducer"; import { AuthAction, initAuth } from "./auth-action"; -import { - API_TOKEN_KEY, - USER_EMAIL_KEY, - USER_FIRST_NAME_KEY, - USER_LAST_NAME_KEY, - USER_OWNER_UUID_KEY, - USER_UUID_KEY, - USER_IS_ADMIN, - USER_IS_ACTIVE, - USER_USERNAME, - USER_PREFS -} from "~/services/auth-service/auth-service"; +import { API_TOKEN_KEY } from "~/services/auth-service/auth-service"; import 'jest-localstorage-mock'; import { ServiceRepository, createServices } from "~/services/services"; @@ -26,11 +15,12 @@ import { ApiActions } from "~/services/api/api-actions"; import { ACCOUNT_LINK_STATUS_KEY } from '~/services/link-account-service/link-account-service'; import axios from "axios"; import MockAdapter from "axios-mock-adapter"; +import { ImportMock } from 'ts-mock-imports'; +import * as servicesModule from "~/services/services"; describe('auth-actions', () => { const axiosMock = new MockAdapter(axios); - let reducer: (state: AuthState | undefined, action: AuthAction) => any; let store: RootStore; let services: ServiceRepository; const actions: ApiActions = { @@ -43,10 +33,9 @@ describe('auth-actions', () => { services = createServices(mockConfig({}), actions, axios); store = configureStore(createBrowserHistory(), services); localStorage.clear(); - reducer = authReducer(services); }); - it('should initialise state with user and api token from local storage', () => { + it('should initialise state with user and api token from local storage', (done) => { axiosMock .onGet("/users/current") @@ -55,13 +44,15 @@ describe('auth-actions', () => { first_name: "John", last_name: "Doe", uuid: "zzzzz-tpzed-abcefg", - ownerUuid: "ownerUuid", + owner_uuid: "ownerUuid", is_admin: false, is_active: true, username: "jdoe", prefs: {} }); + ImportMock.mockFunction(servicesModule, 'createServices', services); + // Only test the case when a link account operation is not being cancelled sessionStorage.setItem(ACCOUNT_LINK_STATUS_KEY, "0"); localStorage.setItem(API_TOKEN_KEY, "token"); @@ -74,62 +65,77 @@ describe('auth-actions', () => { store.dispatch(initAuth(config)); - expect(store.getState().auth).toEqual({ - apiToken: "token", - sshKeys: [], - homeCluster: "zzzzz", - localCluster: "zzzzz", - loginCluster: undefined, - remoteHostsConfig: { - "zzzzz": { - "remoteHosts": { - "xc59z": "xc59z.arvadosapi.com", - }, - "rootUrl": "https://zzzzz.arvadosapi.com", - "uuidPrefix": "zzzzz", - }, - }, - remoteHosts: { - zzzzz: "zzzzz.arvadosapi.com", - xc59z: "xc59z.arvadosapi.com" - }, - sessions: [{ - "active": true, - "baseUrl": undefined, - "clusterId": "zzzzz", - "email": "test@test.com", - "loggedIn": true, - "remoteHost": "https://zzzzz.arvadosapi.com", - "status": 2, - "token": "token", - "name": "John Doe" - "uuid": "zzzzz-tpzed-abcefg", - }, { - "active": false, - "baseUrl": "", - "clusterId": "xc59z", - "email": "", - "loggedIn": false, - "remoteHost": "xc59z.arvadosapi.com", - "status": 1, - "token": "", - "name": "", - "uuid": "", - }], - user: { - email: "test@test.com", - firstName: "John", - lastName: "Doe", - uuid: "zzzzz-tpzed-abcefg", - ownerUuid: "ownerUuid", - username: "jdoe", - prefs: {}, - isAdmin: false, - isActive: true + store.subscribe(() => { + const auth = store.getState().auth; + if (auth.apiToken === "token" && + auth.sessions.length === 2 && + auth.sessions[0].status === 2 && + auth.sessions[1].status === 2 + ) { + try { + expect(auth).toEqual({ + apiToken: "token", + sshKeys: [], + homeCluster: "zzzzz", + localCluster: "zzzzz", + loginCluster: undefined, + remoteHostsConfig: { + "zzzzz": { + "remoteHosts": { + "xc59z": "xc59z.arvadosapi.com", + }, + "rootUrl": "https://zzzzz.arvadosapi.com", + "uuidPrefix": "zzzzz", + }, + }, + remoteHosts: { + zzzzz: "zzzzz.arvadosapi.com", + xc59z: "xc59z.arvadosapi.com" + }, + sessions: [{ + "active": true, + "baseUrl": undefined, + "clusterId": "zzzzz", + "email": "test@test.com", + "loggedIn": true, + "remoteHost": "https://zzzzz.arvadosapi.com", + "status": 2, + "token": "token", + "name": "John Doe" + "uuid": "zzzzz-tpzed-abcefg", + }, { + "active": false, + "baseUrl": "", + "clusterId": "xc59z", + "email": "", + "loggedIn": false, + "remoteHost": "xc59z.arvadosapi.com", + "status": 2, + "token": "", + "name": "", + "uuid": "", + }], + user: { + email: "test@test.com", + firstName: "John", + lastName: "Doe", + uuid: "zzzzz-tpzed-abcefg", + ownerUuid: "ownerUuid", + username: "jdoe", + prefs: { profile: {} }, + isAdmin: false, + isActive: true + } + }); + done(); + } catch (e) { + console.log(e); + } } }); }); + // TODO: Add remaining action tests /* it('should fire external url to login', () => { diff --git a/yarn.lock b/yarn.lock index 1e718dab..948b7fa6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -73,6 +73,35 @@ dependencies: esquery "^1.0.1" +"@sinonjs/commons@^1", "@sinonjs/commons@^1.3.0", "@sinonjs/commons@^1.4.0": + version "1.6.0" + resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.6.0.tgz#ec7670432ae9c8eb710400d112c201a362d83393" + integrity sha512-w4/WHG7C4WWFyE5geCieFJF6MZkbW4VAriol5KlmQXpAQdxvV0p26sqNZOW6Qyw6Y0l9K4g+cHvvczR2sEEpqg== + dependencies: + type-detect "4.0.8" + +"@sinonjs/formatio@^3.2.1": + version "3.2.2" + resolved "https://registry.yarnpkg.com/@sinonjs/formatio/-/formatio-3.2.2.tgz#771c60dfa75ea7f2d68e3b94c7e888a78781372c" + integrity sha512-B8SEsgd8gArBLMD6zpRw3juQ2FVSsmdd7qlevyDqzS9WTCtvF55/gAL+h6gue8ZvPYcdiPdvueM/qm//9XzyTQ== + dependencies: + "@sinonjs/commons" "^1" + "@sinonjs/samsam" "^3.1.0" + +"@sinonjs/samsam@^3.1.0", "@sinonjs/samsam@^3.3.1": + version "3.3.3" + resolved "https://registry.yarnpkg.com/@sinonjs/samsam/-/samsam-3.3.3.tgz#46682efd9967b259b81136b9f120fd54585feb4a" + integrity sha512-bKCMKZvWIjYD0BLGnNrxVuw4dkWCYsLqFOUWw8VgKF/+5Y+mE7LfHWPIYoDXowH+3a9LsWDMo0uAP8YDosPvHQ== + dependencies: + "@sinonjs/commons" "^1.3.0" + array-from "^2.1.1" + lodash "^4.17.15" + +"@sinonjs/text-encoding@^0.7.1": + version "0.7.1" + resolved "https://registry.yarnpkg.com/@sinonjs/text-encoding/-/text-encoding-0.7.1.tgz#8da5c6530915653f3a1f38fd5f101d8c3f8079c5" + integrity sha512-+iTbntw2IZPb/anVDbypzfQa+ay64MW0Zo8aJ8gZPWMMK6/OubMVb6lUPMagqjOPnmtauXnFCACVl3O7ogjeqQ== + "@types/cheerio@*": version "0.22.9" resolved "https://registry.yarnpkg.com/@types/cheerio/-/cheerio-0.22.9.tgz#b5990152604c2ada749b7f88cab3476f21f39d7b" @@ -474,6 +503,11 @@ array-flatten@^2.1.0: version "2.1.1" resolved "https://registry.yarnpkg.com/array-flatten/-/array-flatten-2.1.1.tgz#426bb9da84090c1838d812c8150af20a8331e296" +array-from@^2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/array-from/-/array-from-2.1.1.tgz#cfe9d8c26628b9dc5aecc62a9f5d8f1f352c1195" + integrity sha1-z+nYwmYoudxa7MYqn12PHzUsEZU= + array-includes@^3.0.3: version "3.0.3" resolved "https://registry.yarnpkg.com/array-includes/-/array-includes-3.0.3.tgz#184b48f62d92d7452bb31b323165c7f8bd02266d" @@ -2416,7 +2450,7 @@ detect-port-alt@1.1.6: address "^1.0.1" debug "^2.6.0" -diff@^3.2.0: +diff@^3.2.0, diff@^3.5.0: version "3.5.0" resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12" @@ -4924,6 +4958,11 @@ jszip@3.1.5: pako "~1.0.2" readable-stream "~2.0.6" +just-extend@^4.0.2: + version "4.0.2" + resolved "https://registry.yarnpkg.com/just-extend/-/just-extend-4.0.2.tgz#f3f47f7dfca0f989c55410a7ebc8854b07108afc" + integrity sha512-FrLwOgm+iXrPV+5zDU6Jqu4gCRXbWEQg2O3SKONsWE4w7AXFRkryS53bpWdaL9cNol+AmR3AEYz6kn+o0fCPnw== + keycode@^2.1.9: version "2.2.0" resolved "https://registry.yarnpkg.com/keycode/-/keycode-2.2.0.tgz#3d0af56dc7b8b8e5cba8d0a97f107204eec22b04" @@ -5154,6 +5193,11 @@ lodash@4.17.13: version "4.17.11" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d" +lodash@^4.17.15: + version "4.17.15" + resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548" + integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A== + log-symbols@^2.1.0: version "2.2.0" resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-2.2.0.tgz#5740e1c5d6f0dfda4ad9323b5332107ef6b4c40a" @@ -5173,6 +5217,11 @@ loglevelnext@^1.0.1: es6-symbol "^3.1.1" object.assign "^4.1.0" +lolex@^4.0.1, lolex@^4.1.0: + version "4.2.0" + resolved "https://registry.yarnpkg.com/lolex/-/lolex-4.2.0.tgz#ddbd7f6213ca1ea5826901ab1222b65d714b3cd7" + integrity sha512-gKO5uExCXvSm6zbF562EvM+rd1kQDnB9AZBbiQVzf1ZmdDpxUSvpnAaVOP83N/31mRK8Ml8/VE8DMvsAZQ+7wg== + longest@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097" @@ -5546,6 +5595,17 @@ next-tick@1: version "1.0.0" resolved "https://registry.yarnpkg.com/next-tick/-/next-tick-1.0.0.tgz#ca86d1fe8828169b0120208e3dc8424b9db8342c" +nise@^1.4.10: + version "1.5.2" + resolved "https://registry.yarnpkg.com/nise/-/nise-1.5.2.tgz#b6d29af10e48b321b307e10e065199338eeb2652" + integrity sha512-/6RhOUlicRCbE9s+94qCUsyE+pKlVJ5AhIv+jEE7ESKwnbXqulKZ1FYU+XAtHHWE9TinYvAxDUJAb912PwPoWA== + dependencies: + "@sinonjs/formatio" "^3.2.1" + "@sinonjs/text-encoding" "^0.7.1" + just-extend "^4.0.2" + lolex "^4.1.0" + path-to-regexp "^1.7.0" + no-case@^2.2.0: version "2.3.2" resolved "https://registry.yarnpkg.com/no-case/-/no-case-2.3.2.tgz#60b813396be39b3f1288a4c1ed5d1e7d28b464ac" @@ -7600,6 +7660,19 @@ signal-exit@^3.0.0, signal-exit@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d" +sinon@7.3: + version "7.3.2" + resolved "https://registry.yarnpkg.com/sinon/-/sinon-7.3.2.tgz#82dba3a6d85f6d2181e1eca2c10d8657c2161f28" + integrity sha512-thErC1z64BeyGiPvF8aoSg0LEnptSaWE7YhdWWbWXgelOyThent7uKOnnEh9zBxDbKixtr5dEko+ws1sZMuFMA== + dependencies: + "@sinonjs/commons" "^1.4.0" + "@sinonjs/formatio" "^3.2.1" + "@sinonjs/samsam" "^3.3.1" + diff "^3.5.0" + lolex "^4.0.1" + nise "^1.4.10" + supports-color "^5.5.0" + slash@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/slash/-/slash-1.0.0.tgz#c41f2f6c39fc16d1cd17ad4b5d896114ae470d55" @@ -7959,7 +8032,7 @@ supports-color@^4.2.1: dependencies: has-flag "^2.0.0" -supports-color@^5.1.0, supports-color@^5.3.0, supports-color@^5.4.0: +supports-color@^5.1.0, supports-color@^5.3.0, supports-color@^5.4.0, supports-color@^5.5.0: version "5.5.0" resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f" dependencies: @@ -8203,6 +8276,11 @@ ts-loader@^2.3.7: loader-utils "^1.0.2" semver "^5.0.1" +ts-mock-imports@1.2.6: + version "1.2.6" + resolved "https://registry.yarnpkg.com/ts-mock-imports/-/ts-mock-imports-1.2.6.tgz#5a98a398c3eadb7f75b6904984bb0ba5f3fbb912" + integrity sha512-rZjsIEBWx9a3RGUo4Rhj/hzEGB4GPWJx46fls9EJf4UBsf5SxS2qiozf6dQp0Ym/9LC5MArlXZbZ+93wJzAmjA== + tsconfig-paths-webpack-plugin@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/tsconfig-paths-webpack-plugin/-/tsconfig-paths-webpack-plugin-2.0.0.tgz#7652dc684bb3206c8e7e446831ca01cbf4d11772" @@ -8299,6 +8377,11 @@ type-check@~0.3.2: dependencies: prelude-ls "~1.1.2" +type-detect@4.0.8: + version "4.0.8" + resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c" + integrity sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g== + type-is@~1.6.16: version "1.6.16" resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.16.tgz#f89ce341541c672b25ee7ae3c73dee3b2be50194"