Use credential_type for multicred distinction

This commit is contained in:
Jake McDermott
2020-12-14 10:09:04 -05:00
parent 058cfc55a6
commit d88ba5873b
2 changed files with 187 additions and 53 deletions

View File

@@ -194,9 +194,10 @@ function MultiCredentialsLookup(props) {
const hasSameVaultID = val => const hasSameVaultID = val =>
val?.inputs?.vault_id !== undefined && val?.inputs?.vault_id !== undefined &&
val?.inputs?.vault_id === item?.inputs?.vault_id; val?.inputs?.vault_id === item?.inputs?.vault_id;
const hasSameKind = val => val.kind === item.kind; const hasSameCredentialType = val =>
val.credential_type === item.credential_type;
const selectedItems = state.selectedItems.filter(i => const selectedItems = state.selectedItems.filter(i =>
isVault ? !hasSameVaultID(i) : !hasSameKind(i) isVault ? !hasSameVaultID(i) : !hasSameCredentialType(i)
); );
selectedItems.push(item); selectedItems.push(item);
return dispatch({ return dispatch({

View File

@@ -13,11 +13,29 @@ describe('<MultiCredentialsLookup />', () => {
let wrapper; let wrapper;
const credentials = [ const credentials = [
{ id: 1, kind: 'cloud', name: 'Foo', url: 'www.google.com' }, {
{ id: 2, kind: 'ssh', name: 'Alex', url: 'www.google.com' }, id: 1,
{ name: 'Gatsby', id: 21, kind: 'vault', inputs: { vault_id: '1' } }, credential_type: 1,
{ name: 'Gatsby 2', id: 23, kind: 'vault' }, kind: 'gce',
{ name: 'Gatsby', id: 8, kind: 'Machine' }, name: 'Foo',
url: 'www.google.com',
},
{
id: 2,
credential_type: 2,
kind: 'ssh',
name: 'Alex',
url: 'www.google.com',
},
{
id: 21,
credential_type: 3,
kind: 'vault',
inputs: { vault_id: '1' },
name: 'Gatsby',
},
{ id: 23, credential_type: 3, kind: 'vault', name: 'Gatsby 2' },
{ id: 8, credential_type: 4, kind: 'Machine', name: 'Gatsby' },
]; ];
beforeEach(() => { beforeEach(() => {
@@ -34,11 +52,41 @@ describe('<MultiCredentialsLookup />', () => {
CredentialsAPI.read.mockResolvedValueOnce({ CredentialsAPI.read.mockResolvedValueOnce({
data: { data: {
results: [ results: [
{ id: 1, kind: 'cloud', name: 'Cred 1', url: 'www.google.com' }, {
{ id: 2, kind: 'ssh', name: 'Cred 2', url: 'www.google.com' }, id: 1,
{ id: 3, kind: 'Ansible', name: 'Cred 3', url: 'www.google.com' }, credential_type: 1,
{ id: 4, kind: 'Machine', name: 'Cred 4', url: 'www.google.com' }, kind: 'gc2',
{ id: 5, kind: 'Machine', name: 'Cred 5', url: 'www.google.com' }, name: 'Cred 1',
url: 'www.google.com',
},
{
id: 2,
credential_type: 2,
kind: 'ssh',
name: 'Cred 2',
url: 'www.google.com',
},
{
id: 3,
credential_type: 5,
kind: 'Ansible',
name: 'Cred 3',
url: 'www.google.com',
},
{
id: 4,
credential_type: 4,
kind: 'Machine',
name: 'Cred 4',
url: 'www.google.com',
},
{
id: 5,
credential_type: 4,
kind: 'Machine',
name: 'Cred 5',
url: 'www.google.com',
},
], ],
count: 3, count: 3,
}, },
@@ -95,10 +143,22 @@ describe('<MultiCredentialsLookup />', () => {
button.invoke('onClick')(); button.invoke('onClick')();
}); });
expect(onChange).toBeCalledWith([ expect(onChange).toBeCalledWith([
{ id: 1, kind: 'cloud', name: 'Foo', url: 'www.google.com' }, {
{ id: 21, inputs: { vault_id: '1' }, kind: 'vault', name: 'Gatsby' }, id: 1,
{ id: 23, kind: 'vault', name: 'Gatsby 2' }, credential_type: 1,
{ id: 8, kind: 'Machine', name: 'Gatsby' }, kind: 'gce',
name: 'Foo',
url: 'www.google.com',
},
{
id: 21,
credential_type: 3,
kind: 'vault',
inputs: { vault_id: '1' },
name: 'Gatsby',
},
{ id: 23, credential_type: 3, kind: 'vault', name: 'Gatsby 2' },
{ id: 8, credential_type: 4, kind: 'Machine', name: 'Gatsby' },
]); ]);
}); });
@@ -165,6 +225,7 @@ describe('<MultiCredentialsLookup />', () => {
act(() => { act(() => {
optionsList.invoke('selectItem')({ optionsList.invoke('selectItem')({
id: 5, id: 5,
credential_type: 4,
kind: 'Machine', kind: 'Machine',
name: 'Cred 5', name: 'Cred 5',
url: 'www.google.com', url: 'www.google.com',
@@ -175,11 +236,35 @@ describe('<MultiCredentialsLookup />', () => {
wrapper.find('Button[variant="primary"]').invoke('onClick')(); wrapper.find('Button[variant="primary"]').invoke('onClick')();
}); });
expect(onChange).toBeCalledWith([ expect(onChange).toBeCalledWith([
{ id: 1, kind: 'cloud', name: 'Foo', url: 'www.google.com' }, {
{ id: 2, kind: 'ssh', name: 'Alex', url: 'www.google.com' }, id: 1,
{ id: 21, inputs: { vault_id: '1' }, kind: 'vault', name: 'Gatsby' }, credential_type: 1,
{ id: 23, kind: 'vault', name: 'Gatsby 2' }, kind: 'gce',
{ id: 5, kind: 'Machine', name: 'Cred 5', url: 'www.google.com' }, name: 'Foo',
url: 'www.google.com',
},
{
id: 2,
credential_type: 2,
kind: 'ssh',
name: 'Alex',
url: 'www.google.com',
},
{
id: 21,
credential_type: 3,
kind: 'vault',
inputs: { vault_id: '1' },
name: 'Gatsby',
},
{ id: 23, credential_type: 3, kind: 'vault', name: 'Gatsby 2' },
{
id: 5,
credential_type: 4,
kind: 'Machine',
name: 'Cred 5',
url: 'www.google.com',
},
]); ]);
}); });
@@ -212,10 +297,10 @@ describe('<MultiCredentialsLookup />', () => {
expect(optionsList.prop('multiple')).toEqual(true); expect(optionsList.prop('multiple')).toEqual(true);
act(() => { act(() => {
optionsList.invoke('selectItem')({ optionsList.invoke('selectItem')({
id: 5, id: 11,
credential_type: 3,
kind: 'vault', kind: 'vault',
name: 'Cred 5', name: 'Vault',
url: 'www.google.com',
}); });
}); });
wrapper.update(); wrapper.update();
@@ -223,12 +308,30 @@ describe('<MultiCredentialsLookup />', () => {
wrapper.find('Button[variant="primary"]').invoke('onClick')(); wrapper.find('Button[variant="primary"]').invoke('onClick')();
}); });
expect(onChange).toBeCalledWith([ expect(onChange).toBeCalledWith([
{ id: 1, kind: 'cloud', name: 'Foo', url: 'www.google.com' }, {
{ id: 2, kind: 'ssh', name: 'Alex', url: 'www.google.com' }, id: 1,
{ id: 21, kind: 'vault', name: 'Gatsby', inputs: { vault_id: '1' } }, credential_type: 1,
{ id: 23, kind: 'vault', name: 'Gatsby 2' }, kind: 'gce',
{ id: 8, kind: 'Machine', name: 'Gatsby' }, name: 'Foo',
{ id: 5, kind: 'vault', name: 'Cred 5', url: 'www.google.com' }, url: 'www.google.com',
},
{
id: 2,
credential_type: 2,
kind: 'ssh',
name: 'Alex',
url: 'www.google.com',
},
{
id: 21,
credential_type: 3,
kind: 'vault',
inputs: { vault_id: '1' },
name: 'Gatsby',
},
{ id: 23, credential_type: 3, kind: 'vault', name: 'Gatsby 2' },
{ id: 8, credential_type: 4, kind: 'Machine', name: 'Gatsby' },
{ id: 11, credential_type: 3, kind: 'vault', name: 'Vault' },
]); ]);
}); });
@@ -261,11 +364,11 @@ describe('<MultiCredentialsLookup />', () => {
expect(optionsList.prop('multiple')).toEqual(true); expect(optionsList.prop('multiple')).toEqual(true);
act(() => { act(() => {
optionsList.invoke('selectItem')({ optionsList.invoke('selectItem')({
id: 5, id: 12,
credential_type: 3,
kind: 'vault', kind: 'vault',
name: 'Cred 5', name: 'Other Vault',
url: 'www.google.com', vault_id: '2',
inputs: { vault_id: '2' },
}); });
}); });
wrapper.update(); wrapper.update();
@@ -273,17 +376,35 @@ describe('<MultiCredentialsLookup />', () => {
wrapper.find('Button[variant="primary"]').invoke('onClick')(); wrapper.find('Button[variant="primary"]').invoke('onClick')();
}); });
expect(onChange).toBeCalledWith([ expect(onChange).toBeCalledWith([
{ id: 1, kind: 'cloud', name: 'Foo', url: 'www.google.com' },
{ id: 2, kind: 'ssh', name: 'Alex', url: 'www.google.com' },
{ id: 21, kind: 'vault', name: 'Gatsby', inputs: { vault_id: '1' } },
{ id: 23, kind: 'vault', name: 'Gatsby 2' },
{ id: 8, kind: 'Machine', name: 'Gatsby' },
{ {
id: 5, id: 1,
kind: 'vault', credential_type: 1,
name: 'Cred 5', kind: 'gce',
name: 'Foo',
url: 'www.google.com', url: 'www.google.com',
inputs: { vault_id: '2' }, },
{
id: 2,
credential_type: 2,
kind: 'ssh',
name: 'Alex',
url: 'www.google.com',
},
{
id: 21,
credential_type: 3,
kind: 'vault',
inputs: { vault_id: '1' },
name: 'Gatsby',
},
{ id: 23, credential_type: 3, kind: 'vault', name: 'Gatsby 2' },
{ id: 8, credential_type: 4, kind: 'Machine', name: 'Gatsby' },
{
id: 12,
credential_type: 3,
kind: 'vault',
name: 'Other Vault',
vault_id: '2',
}, },
]); ]);
}); });
@@ -317,10 +438,10 @@ describe('<MultiCredentialsLookup />', () => {
expect(optionsList.prop('multiple')).toEqual(true); expect(optionsList.prop('multiple')).toEqual(true);
act(() => { act(() => {
optionsList.invoke('selectItem')({ optionsList.invoke('selectItem')({
id: 24, id: 13,
credential_type: 3,
kind: 'vault', kind: 'vault',
name: 'Cred 5', name: 'Vault Cred with Same Vault Id',
url: 'www.google.com',
inputs: { vault_id: '1' }, inputs: { vault_id: '1' },
}); });
}); });
@@ -329,15 +450,27 @@ describe('<MultiCredentialsLookup />', () => {
wrapper.find('Button[variant="primary"]').invoke('onClick')(); wrapper.find('Button[variant="primary"]').invoke('onClick')();
}); });
expect(onChange).toBeCalledWith([ expect(onChange).toBeCalledWith([
{ id: 1, kind: 'cloud', name: 'Foo', url: 'www.google.com' },
{ id: 2, kind: 'ssh', name: 'Alex', url: 'www.google.com' },
{ id: 23, kind: 'vault', name: 'Gatsby 2' },
{ id: 8, kind: 'Machine', name: 'Gatsby' },
{ {
id: 24, id: 1,
kind: 'vault', credential_type: 1,
name: 'Cred 5', kind: 'gce',
name: 'Foo',
url: 'www.google.com', url: 'www.google.com',
},
{
id: 2,
credential_type: 2,
kind: 'ssh',
name: 'Alex',
url: 'www.google.com',
},
{ id: 23, credential_type: 3, kind: 'vault', name: 'Gatsby 2' },
{ id: 8, credential_type: 4, kind: 'Machine', name: 'Gatsby' },
{
id: 13,
credential_type: 3,
kind: 'vault',
name: 'Vault Cred with Same Vault Id',
inputs: { vault_id: '1' }, inputs: { vault_id: '1' },
}, },
]); ]);