Skip to content

Commit

Permalink
[unit-tests] add ssh and secret tests
Browse files Browse the repository at this point in the history
Signed-off-by: Francesco Torchia <[email protected]>
  • Loading branch information
torchiaf committed Feb 26, 2025
1 parent 897baaa commit 6708040
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/aks/components/__tests__/Taint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ describe('aks taint component', () => {
}],
])('on edit, should populate each input field with parsed taint value', (taint, expected) => {
const wrapper = shallowMount(Taint, {
...requiredSetup(),
propsData: {
mode: _EDIT,
taint
},
...requiredSetup()
});

const keyInput = wrapper.find('[data-testid="aks-taint-key-input"]');
Expand Down
79 changes: 79 additions & 0 deletions shell/edit/secret/__tests__/ssh.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { mount } from '@vue/test-utils';
import { _VIEW, _EDIT, _CREATE } from '@shell/config/query-params';
import Ssh from '@shell/edit/secret/ssh.vue';

const mockedStore = () => {
return { getters: { 'i18n/t': jest.fn() } };
};

const mockedRoute = { query: {} };

const requiredSetup = () => {
return {
global: {
mocks: {
$store: mockedStore(),
$route: mockedRoute,
$fetchState: {},
}
}
};
};

const decodedData = {
known_hosts: 'S05PV05fSE9TVFM=',
'ssh-privatekey': 'dGVzdDE=',
'ssh-publickey': 'dGVzdDE='
};

describe.each([
_CREATE,
_EDIT,
_VIEW
])('component: Ssh.vue', (mode) => {
it(`mode: ${ mode }, should show input fields`, () => {
const wrapper = mount(Ssh, {
...requiredSetup(),
props: {
value: {
name: 'foo',
decodedData,
supportsSshKnownHosts: true
},
mode,
},
});

const publicKey = wrapper.find('[data-testid="ssh-public-key"]').element as HTMLInputElement;
const privateKey = wrapper.find('[data-testid="ssh-private-key"]').element as HTMLInputElement;
const knownHosts = wrapper.find('[data-testid="ssh-known-hosts"]').element as HTMLInputElement;

expect(publicKey.value).toBe('dGVzdDE=');
expect(privateKey.value).toBe('dGVzdDE=');
expect(knownHosts.value).toBe('S05PV05fSE9TVFM=');
});

it.each([
['show', true],
['hide', false],
])(`mode: ${ mode }, should %p known_hosts`, (
_,
supportsSshKnownHosts,
) => {
const wrapper = mount(Ssh, {
...requiredSetup(),
props: {
value: {
name: 'foo',
decodedData,
supportsSshKnownHosts
},
mode: _EDIT,
},
});

const knownHosts = wrapper.find('[data-testid="ssh-known-hosts"]');

expect(knownHosts.exists()).toBe(supportsSshKnownHosts);
});
});
3 changes: 3 additions & 0 deletions shell/edit/secret/ssh.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export default {
<LabeledInput
v-model:value="username"
type="multiline"
data-testid="ssh-public-key"
:label="t('secret.ssh.public')"
:mode="mode"
required
Expand All @@ -75,6 +76,7 @@ export default {
<LabeledInput
v-model:value="password"
type="multiline"
data-testid="ssh-private-key"
:label="t('secret.ssh.private')"
:mode="mode"
required
Expand All @@ -93,6 +95,7 @@ export default {
v-if="showKnownHosts"
v-model:value="knownHosts"
type="multiline"
data-testid="ssh-known-hosts"
:label="t('secret.ssh.knownHosts')"
:mode="mode"
:placeholder="t('secret.ssh.knownHostsPlaceholder')"
Expand Down
41 changes: 41 additions & 0 deletions shell/models/__tests__/secret.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,45 @@
import Secret from '@shell/models/secret';
import { SECRET_TYPES as TYPES } from '@shell/config/secret';

describe('model: Secret.js', () => {
it.each([
[
false,
'type is not SSH',
'generic',
{ known_hosts: 'S05PV05fSE9TVFM=' },
],
[
false,
'missing known_hosts',
TYPES.SSH,
{},
],
[
false,
'data is null',
TYPES.SSH,
null,
],
[
true,
'type is SSH key and known_hosts exists',
TYPES.SSH,
{ known_hosts: 'S05PV05fSE9TVFM=' },
],
])('fn: supportsSshKnownHosts, returns %p if %p', (
supported,
descr,
_type,
data
) => {
const secret = new Secret({ _type, data });

const result = secret.supportsSshKnownHosts;

expect(result).toBe(supported);
});
});

describe('class Secret', () => {
it('should contains the type attribute if cleanForDownload', async() => {
Expand Down

0 comments on commit 6708040

Please sign in to comment.