2019-08-19 11:00:38 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
import Vuex from 'vuex';
|
2019-09-09 11:33:39 +01:00
|
|
|
|
2019-08-19 11:00:38 +01:00
|
|
|
import { ApiKeysApiGql } from '@/api/apiKeys';
|
2019-09-09 11:33:39 +01:00
|
|
|
import { ProjectsApiGql } from '@/api/projects';
|
2020-06-02 17:38:44 +01:00
|
|
|
import { API_KEYS_ACTIONS, API_KEYS_MUTATIONS, makeApiKeysModule } from '@/store/modules/apiKeys';
|
2019-08-19 11:00:38 +01:00
|
|
|
import { makeProjectsModule } from '@/store/modules/projects';
|
2019-09-12 15:10:50 +01:00
|
|
|
import { ApiKey, ApiKeyOrderBy, ApiKeysPage } from '@/types/apiKeys';
|
|
|
|
import { SortDirection } from '@/types/common';
|
2019-08-19 11:00:38 +01:00
|
|
|
import { Project } from '@/types/projects';
|
2019-09-09 11:33:39 +01:00
|
|
|
import { createLocalVue } from '@vue/test-utils';
|
2019-08-19 11:00:38 +01:00
|
|
|
|
|
|
|
const Vue = createLocalVue();
|
|
|
|
const apiKeysApi = new ApiKeysApiGql();
|
|
|
|
const apiKeysModule = makeApiKeysModule(apiKeysApi);
|
2020-06-02 17:38:44 +01:00
|
|
|
const {
|
|
|
|
FETCH,
|
|
|
|
CREATE,
|
|
|
|
CLEAR_SELECTION,
|
|
|
|
DELETE,
|
|
|
|
CLEAR,
|
|
|
|
} = API_KEYS_ACTIONS;
|
2019-08-19 11:00:38 +01:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
const projectsApi = new ProjectsApiGql();
|
|
|
|
const projectsModule = makeProjectsModule(projectsApi);
|
|
|
|
const selectedProject = new Project('', '', '', '');
|
2019-08-19 11:00:38 +01:00
|
|
|
selectedProject.id = '1';
|
|
|
|
projectsModule.state.selectedProject = selectedProject;
|
2020-09-28 14:44:01 +01:00
|
|
|
const date = new Date(0);
|
|
|
|
const apiKey = new ApiKey('testId', 'testName', date, 'testSecret');
|
|
|
|
const apiKey2 = new ApiKey('testId2', 'testName2', date, 'testSecret2');
|
2019-09-12 15:10:50 +01:00
|
|
|
|
|
|
|
const FIRST_PAGE = 1;
|
|
|
|
const TEST_ERROR = 'testError';
|
|
|
|
const UNREACHABLE_ERROR = 'should be unreachable';
|
2019-08-19 11:00:38 +01:00
|
|
|
|
|
|
|
Vue.use(Vuex);
|
|
|
|
|
2019-09-12 15:10:50 +01:00
|
|
|
const store = new Vuex.Store({modules: {projectsModule, apiKeysModule}});
|
2019-08-19 11:00:38 +01:00
|
|
|
|
|
|
|
const state = (store.state as any).apiKeysModule;
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
describe('mutations', (): void => {
|
|
|
|
it('fetch api keys', (): void => {
|
2019-09-12 15:10:50 +01:00
|
|
|
const testApiKeysPage = new ApiKeysPage();
|
|
|
|
testApiKeysPage.apiKeys = [apiKey];
|
|
|
|
testApiKeysPage.totalCount = 1;
|
|
|
|
testApiKeysPage.pageCount = 1;
|
|
|
|
|
|
|
|
store.commit(API_KEYS_MUTATIONS.SET_PAGE, testApiKeysPage);
|
|
|
|
|
|
|
|
expect(state.page.apiKeys.length).toBe(1);
|
|
|
|
expect(state.page.search).toBe('');
|
|
|
|
expect(state.page.order).toBe(ApiKeyOrderBy.NAME);
|
|
|
|
expect(state.page.orderDirection).toBe(SortDirection.ASCENDING);
|
|
|
|
expect(state.page.limit).toBe(6);
|
|
|
|
expect(state.page.pageCount).toBe(1);
|
|
|
|
expect(state.page.currentPage).toBe(1);
|
|
|
|
expect(state.page.totalCount).toBe(1);
|
2019-08-19 11:00:38 +01:00
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('set api keys page', (): void => {
|
2019-09-12 15:10:50 +01:00
|
|
|
store.commit(API_KEYS_MUTATIONS.SET_PAGE_NUMBER, 2);
|
2019-08-19 11:00:38 +01:00
|
|
|
|
2019-09-12 15:10:50 +01:00
|
|
|
expect(state.cursor.page).toBe(2);
|
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('set search query', (): void => {
|
2019-09-12 15:10:50 +01:00
|
|
|
store.commit(API_KEYS_MUTATIONS.SET_SEARCH_QUERY, 'testSearchQuery');
|
|
|
|
|
|
|
|
expect(state.cursor.search).toBe('testSearchQuery');
|
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('set sort order', (): void => {
|
2019-10-04 14:06:22 +01:00
|
|
|
store.commit(API_KEYS_MUTATIONS.CHANGE_SORT_ORDER, ApiKeyOrderBy.CREATED_AT);
|
2019-09-12 15:10:50 +01:00
|
|
|
|
2019-10-04 14:06:22 +01:00
|
|
|
expect(state.cursor.order).toBe(ApiKeyOrderBy.CREATED_AT);
|
2019-09-12 15:10:50 +01:00
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('set sort direction', (): void => {
|
2019-09-12 15:10:50 +01:00
|
|
|
store.commit(API_KEYS_MUTATIONS.CHANGE_SORT_ORDER_DIRECTION, SortDirection.DESCENDING);
|
|
|
|
|
|
|
|
expect(state.cursor.orderDirection).toBe(SortDirection.DESCENDING);
|
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('toggle selection', (): void => {
|
2019-10-15 13:07:18 +01:00
|
|
|
store.commit(API_KEYS_MUTATIONS.TOGGLE_SELECTION, apiKey);
|
2019-09-12 15:10:50 +01:00
|
|
|
|
|
|
|
expect(state.page.apiKeys[0].isSelected).toBe(true);
|
2019-10-15 13:07:18 +01:00
|
|
|
expect(state.selectedApiKeysIds.length).toBe(1);
|
|
|
|
|
|
|
|
store.commit(API_KEYS_MUTATIONS.TOGGLE_SELECTION, apiKey);
|
|
|
|
|
|
|
|
expect(state.page.apiKeys[0].isSelected).toBe(false);
|
|
|
|
expect(state.selectedApiKeysIds.length).toBe(0);
|
2019-09-12 15:10:50 +01:00
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('clear selection', (): void => {
|
2019-09-12 15:10:50 +01:00
|
|
|
store.commit(API_KEYS_MUTATIONS.CLEAR_SELECTION);
|
|
|
|
|
|
|
|
state.page.apiKeys.forEach((key: ApiKey) => {
|
|
|
|
expect(key.isSelected).toBe(false);
|
|
|
|
});
|
2019-10-15 13:07:18 +01:00
|
|
|
|
|
|
|
expect(state.selectedApiKeysIds.length).toBe(0);
|
2019-09-12 15:10:50 +01:00
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('clear store', (): void => {
|
2019-09-12 15:10:50 +01:00
|
|
|
store.commit(API_KEYS_MUTATIONS.CLEAR);
|
|
|
|
|
|
|
|
expect(state.cursor.page).toBe(1);
|
|
|
|
expect(state.cursor.search).toBe('');
|
|
|
|
expect(state.cursor.order).toBe(ApiKeyOrderBy.NAME);
|
|
|
|
expect(state.cursor.orderDirection).toBe(SortDirection.ASCENDING);
|
|
|
|
expect(state.page.apiKeys.length).toBe(0);
|
2019-10-15 13:07:18 +01:00
|
|
|
expect(state.selectedApiKeysIds.length).toBe(0);
|
2019-08-19 11:00:38 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
describe('actions', (): void => {
|
|
|
|
beforeEach((): void => {
|
2019-08-19 11:00:38 +01:00
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('success fetch apiKeys', async (): Promise<void> => {
|
2019-09-12 15:10:50 +01:00
|
|
|
const testApiKeysPage = new ApiKeysPage();
|
|
|
|
testApiKeysPage.apiKeys = [apiKey];
|
|
|
|
testApiKeysPage.totalCount = 1;
|
|
|
|
testApiKeysPage.pageCount = 1;
|
|
|
|
|
2019-08-19 11:00:38 +01:00
|
|
|
jest.spyOn(apiKeysApi, 'get').mockReturnValue(
|
2019-09-13 15:58:18 +01:00
|
|
|
Promise.resolve(testApiKeysPage),
|
2019-08-19 11:00:38 +01:00
|
|
|
);
|
|
|
|
|
2019-09-12 15:10:50 +01:00
|
|
|
await store.dispatch(FETCH, FIRST_PAGE);
|
2019-08-19 11:00:38 +01:00
|
|
|
|
2019-09-12 15:10:50 +01:00
|
|
|
expect(state.page.apiKeys[0].id).toBe(apiKey.id);
|
|
|
|
expect(state.page.apiKeys[0].name).toBe(apiKey.name);
|
|
|
|
expect(state.page.apiKeys[0].createdAt).toBe(apiKey.createdAt);
|
|
|
|
expect(state.page.apiKeys[0].secret).toBe(apiKey.secret);
|
2019-08-19 11:00:38 +01:00
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('fetch throws an error when api call fails', async (): Promise<void> => {
|
2019-09-12 15:10:50 +01:00
|
|
|
jest.spyOn(apiKeysApi, 'get').mockImplementation(() => {
|
|
|
|
throw new Error(TEST_ERROR);
|
|
|
|
});
|
2019-08-19 11:00:38 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch(FETCH);
|
|
|
|
} catch (error) {
|
2019-09-12 15:10:50 +01:00
|
|
|
store.commit(API_KEYS_MUTATIONS.CHANGE_SORT_ORDER_DIRECTION, SortDirection.DESCENDING);
|
|
|
|
expect(error.message).toBe(TEST_ERROR);
|
|
|
|
|
|
|
|
return;
|
2019-08-19 11:00:38 +01:00
|
|
|
}
|
2019-09-12 15:10:50 +01:00
|
|
|
|
|
|
|
fail(UNREACHABLE_ERROR);
|
2019-08-19 11:00:38 +01:00
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('success create apiKeys', async (): Promise<void> => {
|
2019-09-12 15:10:50 +01:00
|
|
|
jest.spyOn(apiKeysApi, 'create').mockReturnValue(Promise.resolve(apiKey));
|
2019-08-19 11:00:38 +01:00
|
|
|
|
2019-09-12 15:10:50 +01:00
|
|
|
try {
|
|
|
|
await store.dispatch(CREATE, 'testName');
|
|
|
|
throw new Error(TEST_ERROR);
|
|
|
|
} catch (error) {
|
|
|
|
expect(error.message).toBe(TEST_ERROR);
|
|
|
|
}
|
2019-08-19 11:00:38 +01:00
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('create throws an error when api call fails', async (): Promise<void> => {
|
2019-09-12 15:10:50 +01:00
|
|
|
jest.spyOn(apiKeysApi, 'create').mockImplementation(() => {
|
|
|
|
throw new Error(TEST_ERROR);
|
|
|
|
});
|
2019-08-19 11:00:38 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch(CREATE, 'testName');
|
|
|
|
} catch (error) {
|
2019-09-12 15:10:50 +01:00
|
|
|
expect(error.message).toBe(TEST_ERROR);
|
|
|
|
|
|
|
|
return;
|
2019-08-19 11:00:38 +01:00
|
|
|
}
|
2019-09-12 15:10:50 +01:00
|
|
|
|
|
|
|
fail(UNREACHABLE_ERROR);
|
2019-08-19 11:00:38 +01:00
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('success delete apiKeys', async (): Promise<void> => {
|
2019-08-19 11:00:38 +01:00
|
|
|
jest.spyOn(apiKeysApi, 'delete').mockReturnValue(
|
2019-09-13 15:58:18 +01:00
|
|
|
Promise.resolve(),
|
2019-08-19 11:00:38 +01:00
|
|
|
);
|
|
|
|
|
2019-09-12 15:10:50 +01:00
|
|
|
try {
|
|
|
|
await store.dispatch(DELETE, ['testId', 'testId']);
|
|
|
|
throw new Error(TEST_ERROR);
|
|
|
|
} catch (error) {
|
|
|
|
expect(error.message).toBe(TEST_ERROR);
|
|
|
|
}
|
2019-08-19 11:00:38 +01:00
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('delete throws an error when api call fails', async (): Promise<void> => {
|
2019-09-12 15:10:50 +01:00
|
|
|
jest.spyOn(apiKeysApi, 'delete').mockImplementation(() => {
|
|
|
|
throw new Error(TEST_ERROR);
|
|
|
|
});
|
2019-08-19 11:00:38 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch(DELETE, 'testId');
|
|
|
|
} catch (error) {
|
2019-09-12 15:10:50 +01:00
|
|
|
expect(error.message).toBe(TEST_ERROR);
|
|
|
|
|
|
|
|
return;
|
2019-08-19 11:00:38 +01:00
|
|
|
}
|
2019-09-12 15:10:50 +01:00
|
|
|
|
|
|
|
fail(UNREACHABLE_ERROR);
|
2019-08-19 11:00:38 +01:00
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('set api keys search query', async (): Promise<void> => {
|
2019-09-12 15:10:50 +01:00
|
|
|
await store.dispatch(API_KEYS_ACTIONS.SET_SEARCH_QUERY, 'search');
|
2019-08-19 11:00:38 +01:00
|
|
|
|
2019-09-12 15:10:50 +01:00
|
|
|
expect(state.cursor.search).toBe('search');
|
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('set api keys sort by', async (): Promise<void> => {
|
2019-09-12 15:10:50 +01:00
|
|
|
await store.dispatch(API_KEYS_ACTIONS.SET_SORT_BY, ApiKeyOrderBy.CREATED_AT);
|
|
|
|
|
|
|
|
expect(state.cursor.order).toBe(ApiKeyOrderBy.CREATED_AT);
|
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('set sort direction', async (): Promise<void> => {
|
2019-09-12 15:10:50 +01:00
|
|
|
await store.dispatch(API_KEYS_ACTIONS.SET_SORT_DIRECTION, SortDirection.DESCENDING);
|
2019-08-19 11:00:38 +01:00
|
|
|
|
2019-09-12 15:10:50 +01:00
|
|
|
expect(state.cursor.orderDirection).toBe(SortDirection.DESCENDING);
|
2019-08-19 11:00:38 +01:00
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('success toggleAPIKeySelection apiKeys', async (): Promise<void> => {
|
2019-09-12 15:10:50 +01:00
|
|
|
jest.spyOn(apiKeysApi, 'get').mockReturnValue(
|
|
|
|
Promise.resolve(new ApiKeysPage([apiKey, apiKey2],
|
|
|
|
'',
|
|
|
|
ApiKeyOrderBy.NAME,
|
|
|
|
SortDirection.ASCENDING,
|
|
|
|
6,
|
|
|
|
2,
|
|
|
|
1,
|
2019-09-13 15:58:18 +01:00
|
|
|
2,
|
|
|
|
)),
|
2019-09-12 15:10:50 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
await store.dispatch(API_KEYS_ACTIONS.FETCH, FIRST_PAGE);
|
|
|
|
|
2019-10-15 13:07:18 +01:00
|
|
|
await store.dispatch(API_KEYS_ACTIONS.TOGGLE_SELECTION, apiKey);
|
2019-08-19 11:00:38 +01:00
|
|
|
|
2019-09-12 15:10:50 +01:00
|
|
|
expect(state.page.apiKeys[0].isSelected).toBe(true);
|
2019-10-15 13:07:18 +01:00
|
|
|
expect(state.selectedApiKeysIds.length).toBe(1);
|
|
|
|
|
|
|
|
await store.dispatch(API_KEYS_ACTIONS.TOGGLE_SELECTION, apiKey2);
|
|
|
|
|
|
|
|
expect(state.page.apiKeys[1].isSelected).toBe(true);
|
|
|
|
expect(state.selectedApiKeysIds.length).toBe(2);
|
|
|
|
|
|
|
|
await store.dispatch(API_KEYS_ACTIONS.FETCH, FIRST_PAGE);
|
|
|
|
|
|
|
|
expect(state.page.apiKeys[1].isSelected).toBe(true);
|
|
|
|
expect(state.selectedApiKeysIds.length).toBe(2);
|
|
|
|
|
|
|
|
await store.dispatch(API_KEYS_ACTIONS.TOGGLE_SELECTION, apiKey2);
|
|
|
|
|
|
|
|
expect(state.page.apiKeys[1].isSelected).toBe(false);
|
|
|
|
expect(state.selectedApiKeysIds.length).toBe(1);
|
2019-08-19 11:00:38 +01:00
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('success clearSelection apiKeys', async (): Promise<void> => {
|
2019-09-12 15:10:50 +01:00
|
|
|
await store.dispatch(CLEAR_SELECTION);
|
2019-08-19 11:00:38 +01:00
|
|
|
|
2019-09-12 15:10:50 +01:00
|
|
|
state.page.apiKeys.forEach((key: ApiKey) => {
|
|
|
|
expect(key.isSelected).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('success clearAPIKeys', async (): Promise<void> => {
|
2019-09-12 15:10:50 +01:00
|
|
|
await store.dispatch(CLEAR);
|
|
|
|
|
|
|
|
expect(state.cursor.search).toBe('');
|
|
|
|
expect(state.cursor.limit).toBe(6);
|
|
|
|
expect(state.cursor.page).toBe(1);
|
|
|
|
expect(state.cursor.order).toBe(ApiKeyOrderBy.NAME);
|
|
|
|
expect(state.cursor.orderDirection).toBe(SortDirection.ASCENDING);
|
|
|
|
|
|
|
|
expect(state.page.apiKeys.length).toBe(0);
|
|
|
|
expect(state.page.search).toBe('');
|
|
|
|
expect(state.page.order).toBe(ApiKeyOrderBy.NAME);
|
|
|
|
expect(state.page.orderDirection).toBe(SortDirection.ASCENDING);
|
|
|
|
expect(state.page.limit).toBe(6);
|
|
|
|
expect(state.page.pageCount).toBe(0);
|
|
|
|
expect(state.page.currentPage).toBe(1);
|
|
|
|
expect(state.page.totalCount).toBe(0);
|
2019-10-17 11:34:45 +01:00
|
|
|
|
|
|
|
state.page.apiKeys.forEach((key: ApiKey) => {
|
|
|
|
expect(key.isSelected).toBe(false);
|
|
|
|
});
|
2019-08-19 11:00:38 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
describe('getters', (): void => {
|
2020-09-28 14:44:01 +01:00
|
|
|
const selectedApiKey = new ApiKey('testtestId', 'testtestName', date, 'testtestSecret');
|
2019-08-19 11:00:38 +01:00
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('selected apiKeys', (): void => {
|
2019-09-12 15:10:50 +01:00
|
|
|
const testApiKeysPage = new ApiKeysPage();
|
|
|
|
testApiKeysPage.apiKeys = [selectedApiKey];
|
|
|
|
testApiKeysPage.totalCount = 1;
|
|
|
|
testApiKeysPage.pageCount = 1;
|
|
|
|
|
|
|
|
store.commit(API_KEYS_MUTATIONS.SET_PAGE, testApiKeysPage);
|
2019-10-15 13:07:18 +01:00
|
|
|
store.commit(API_KEYS_MUTATIONS.TOGGLE_SELECTION, selectedApiKey);
|
2019-08-19 11:00:38 +01:00
|
|
|
|
2019-09-12 15:10:50 +01:00
|
|
|
const retrievedApiKeys = store.getters.selectedApiKeys;
|
2019-08-19 11:00:38 +01:00
|
|
|
|
|
|
|
expect(retrievedApiKeys[0].id).toBe('testtestId');
|
|
|
|
});
|
|
|
|
|
2020-06-02 17:38:44 +01:00
|
|
|
it('apiKeys array', (): void => {
|
2019-09-12 15:10:50 +01:00
|
|
|
const retrievedApiKeys = store.getters.selectedApiKeys;
|
2019-08-19 11:00:38 +01:00
|
|
|
|
|
|
|
expect(retrievedApiKeys).toEqual([selectedApiKey]);
|
|
|
|
});
|
|
|
|
});
|