Skip to content

Commit

Permalink
actions & reducers
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-logro authored and tekton-robot committed Jun 19, 2019
1 parent 87d667a commit aebf376
Show file tree
Hide file tree
Showing 9 changed files with 524 additions and 4 deletions.
80 changes: 80 additions & 0 deletions src/actions/secrets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { createCredential, deleteCredential, getCredentials } from '../api';
import { getSelectedNamespace } from '../reducers';

export function fetchSecretsSuccess(data) {
return {
type: 'SECRETS_FETCH_SUCCESS',
data
};
}

export function fetchSecrets({ namespace } = {}) {
return async (dispatch, getState) => {
dispatch({ type: 'SECRETS_FETCH_REQUEST' });
let secrets;
try {
const selectedNamespace = namespace || getSelectedNamespace(getState());
secrets = await getCredentials(selectedNamespace);
const secretsFormatted = [];
secrets.forEach(secret => {
const object = {
name: secret.name,
namespace: secret.namespace,
annotations: secret.url
};
secretsFormatted.push(object);
});
dispatch(fetchSecretsSuccess(secretsFormatted));
} catch (e) {
const error = new Error('Could not fetch secrets');
dispatch({ type: 'SECRETS_FETCH_FAILURE', error });
}
return secrets;
};
}

export function deleteSecret(name, namespace) {
return async dispatch => {
dispatch({ type: 'SECRET_DELETE_REQUEST' });
try {
await deleteCredential(name, namespace);
dispatch({ type: 'SECRET_DELETE_SUCCESS', name, namespace });
} catch (e) {
const error = new Error(`Could not delete secret ${name}`);
dispatch({ type: 'SECRET_DELETE_FAILURE', error });
}
};
}

/* istanbul ignore next */
export function createSecret(postData, namespace) {
return async dispatch => {
dispatch({ type: 'SECRET_CREATE_REQUEST' });
try {
await createCredential(postData, namespace);
dispatch(fetchSecrets());
} catch (e) {
const error = new Error(
`Could not create secret ${postData.name} in namespace ${namespace}`
);
dispatch({ type: 'SECRET_CREATE_FAILURE', error });
}
};
}

export function clearNotification() {
return { type: 'CLEAR_SECRET_ERROR_NOTIFICATION' };
}
203 changes: 203 additions & 0 deletions src/actions/secrets.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';

import * as API from '../api';
import * as selectors from '../reducers';
import {
clearNotification,
createSecret,
deleteSecret,
fetchSecrets,
fetchSecretsSuccess
} from './secrets';

const data = [
{
name: 'secret-name',
password: '********',
resourceVersion: '####',
serviceAccount: 'default',
type: 'userpass',
url: undefined,
username: 'someUser@email.com',
namespace: 'default'
}
];
const dataFormatted = [
{
name: 'secret-name',
annotations: undefined,
namespace: 'default'
}
];
const postData = {
name: 'secret-name',
namespace: 'default',
password: '********',
serviceAccount: 'default',
type: 'userpass',
url: { 'tekton.dev/git-0': 'https://github.ibm.com' },
username: 'someUser@email.com'
};
const response = { ok: true, status: 204 };
const namespace = 'default';

it('fetchSecretsSuccess', () => {
expect(fetchSecretsSuccess(data, namespace)).toEqual({
type: 'SECRETS_FETCH_SUCCESS',
data
});
});

it('fetchSecrets', async () => {
const secrets = data;
const secretsFromatted = dataFormatted;
const middleware = [thunk];
const mockStore = configureStore(middleware);
const store = mockStore();

jest
.spyOn(selectors, 'getSelectedNamespace')
.mockImplementation(() => namespace);
jest.spyOn(API, 'getCredentials').mockImplementation(() => secrets);

const expectedActions = [
{ type: 'SECRETS_FETCH_REQUEST' },
fetchSecretsSuccess(secretsFromatted)
];

await store.dispatch(fetchSecrets({ namespace }));
expect(store.getActions()).toEqual(expectedActions);
});

it('fetchSecrets error', async () => {
const middleware = [thunk];
const mockStore = configureStore(middleware);
const store = mockStore();

const error = new Error('Could not fetch secrets');

jest.spyOn(API, 'getCredentials').mockImplementation(() => {
throw error;
});

const expectedActions = [
{ type: 'SECRETS_FETCH_REQUEST' },
{ type: 'SECRETS_FETCH_FAILURE', error }
];

await store.dispatch(fetchSecrets());
expect(store.getActions()).toEqual(expectedActions);
});

it('deleteSecret', async () => {
const name = 'secret-name';
const middleware = [thunk];
const mockStore = configureStore(middleware);
const store = mockStore();

jest
.spyOn(selectors, 'getSelectedNamespace')
.mockImplementation(() => namespace);

jest.spyOn(API, 'deleteCredential').mockImplementation(() => {});

const expectedActions = [
{ type: 'SECRET_DELETE_REQUEST' },
{ type: 'SECRET_DELETE_SUCCESS', name, namespace }
];

await store.dispatch(deleteSecret(name, namespace));
expect(store.getActions()).toEqual(expectedActions);
});

it('deleteSecret error', async () => {
const secret = 'secret';
const middleware = [thunk];
const mockStore = configureStore(middleware);
const store = mockStore();

const error = new Error('Could not delete secret secret');

jest.spyOn(API, 'deleteCredential').mockImplementation(() => {
throw error;
});

const expectedActions = [
{ type: 'SECRET_DELETE_REQUEST' },
{ type: 'SECRET_DELETE_FAILURE', error }
];

await store.dispatch(deleteSecret(secret, namespace));
expect(store.getActions()).toEqual(expectedActions);
});

it('createSecret', async () => {
const secrets = data;
const secretsFormatted = dataFormatted;
const middleware = [thunk];
const mockStore = configureStore(middleware);
const store = mockStore();

jest
.spyOn(selectors, 'getSelectedNamespace')
.mockImplementation(() => namespace);
jest.spyOn(API, 'getCredentials').mockImplementation(() => secrets);

jest.spyOn(API, 'createCredential').mockImplementation(() => response);

const expectedActions = [
{ type: 'SECRET_CREATE_REQUEST' },
{ type: 'SECRETS_FETCH_REQUEST' },
fetchSecretsSuccess(secretsFormatted)
];

await store.dispatch(createSecret(postData, namespace));
expect(store.getActions()).toEqual(expectedActions);
});

it('createSecret error', async () => {
const middleware = [thunk];
const mockStore = configureStore(middleware);
const store = mockStore();

const error = new Error(
'Could not create secret secret-name in namespace default'
);

jest.spyOn(API, 'createCredential').mockImplementation(() => {
throw error;
});

const expectedActions = [
{ type: 'SECRET_CREATE_REQUEST' },
{ type: 'SECRET_CREATE_FAILURE', error }
];

await store.dispatch(createSecret(postData, namespace));
expect(store.getActions()).toEqual(expectedActions);
});

it('clearNotification', async () => {
const middleware = [thunk];
const mockStore = configureStore(middleware);
const store = mockStore();

const expectedActions = [{ type: 'CLEAR_SECRET_ERROR_NOTIFICATION' }];

await store.dispatch(clearNotification());
expect(store.getActions()).toEqual(expectedActions);
});
2 changes: 1 addition & 1 deletion src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export function getTaskRunLog(name, namespace) {

export function getCredentials(namespace) {
const uri = getAPI('credentials', { namespace });
return get(uri).then(checkData);
return get(uri);
}

export function getCredential(id, namespace) {
Expand Down
2 changes: 1 addition & 1 deletion src/api/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ it('getCredentials', () => {
};
fetchMock.get(/credentials/, data);
return getCredentials().then(credentials => {
expect(credentials).toEqual(data.items);
expect(credentials).toEqual(data);
fetchMock.restore();
});
});
Expand Down
3 changes: 2 additions & 1 deletion src/containers/Extension/actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { fetchNamespaces } from '../../actions/namespaces';
export { fetchPipeline, fetchPipelines } from '../../actions/pipelines';
export { fetchTask, fetchTasks } from '../../actions/tasks';
export { fetchSecrets } from '../../actions/secrets';
export { fetchServiceAccounts } from '../../actions/serviceAccounts';
export { fetchTask, fetchTasks } from '../../actions/tasks';
19 changes: 18 additions & 1 deletion src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import { combineReducers } from 'redux';

import extensions, * as extensionSelectors from './extensions';
import namespaces, * as namespaceSelectors from './namespaces';
import serviceAccounts, * as serviceAccountSelectors from './serviceAccounts';
import pipelines, * as pipelineSelectors from './pipelines';
import pipelineResources, * as pipelineResourcesSelectors from './pipelineResources';
import pipelineRuns, * as pipelineRunsSelectors from './pipelineRuns';
import secrets, * as secretSelectors from './secrets';
import serviceAccounts, * as serviceAccountSelectors from './serviceAccounts';
import tasks, * as taskSelectors from './tasks';
import taskRuns, * as taskRunsSelectors from './taskRuns';

Expand All @@ -28,6 +29,7 @@ export default combineReducers({
pipelines: pipelines(),
pipelineResources: pipelineResources(),
pipelineRuns: pipelineRuns(),
secrets,
serviceAccounts: serviceAccounts(),
tasks: tasks(),
taskRuns: taskRuns()
Expand Down Expand Up @@ -217,3 +219,18 @@ export function getTasksErrorMessage(state) {
export function isFetchingTasks(state) {
return taskSelectors.isFetchingTasks(state.tasks);
}

export function getSecrets(
state,
{ namespace = getSelectedNamespace(state) } = {}
) {
return secretSelectors.getSecrets(state.secrets, namespace);
}

export function getSecretsErrorMessage(state) {
return secretSelectors.getSecretsErrorMessage(state.secrets);
}

export function isFetchingSecrets(state) {
return secretSelectors.isFetchingSecrets(state.secrets);
}
Loading

0 comments on commit aebf376

Please sign in to comment.