forked from tektoncd/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87d667a
commit aebf376
Showing
9 changed files
with
524 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.