From 6708040c2532ef4f23358b17dbaa40896789679f Mon Sep 17 00:00:00 2001 From: Francesco Torchia Date: Wed, 26 Feb 2025 19:09:09 +0100 Subject: [PATCH] [unit-tests] add ssh and secret tests Signed-off-by: Francesco Torchia --- pkg/aks/components/__tests__/Taint.test.ts | 2 +- shell/edit/secret/__tests__/ssh.test.ts | 79 ++++++++++++++++++++++ shell/edit/secret/ssh.vue | 3 + shell/models/__tests__/secret.test.ts | 41 +++++++++++ 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 shell/edit/secret/__tests__/ssh.test.ts diff --git a/pkg/aks/components/__tests__/Taint.test.ts b/pkg/aks/components/__tests__/Taint.test.ts index 81a003afb4e..b385fcdd43a 100644 --- a/pkg/aks/components/__tests__/Taint.test.ts +++ b/pkg/aks/components/__tests__/Taint.test.ts @@ -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"]'); diff --git a/shell/edit/secret/__tests__/ssh.test.ts b/shell/edit/secret/__tests__/ssh.test.ts new file mode 100644 index 00000000000..8ff3680eb0b --- /dev/null +++ b/shell/edit/secret/__tests__/ssh.test.ts @@ -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); + }); +}); diff --git a/shell/edit/secret/ssh.vue b/shell/edit/secret/ssh.vue index 05c6e08153c..31cbdcb3895 100644 --- a/shell/edit/secret/ssh.vue +++ b/shell/edit/secret/ssh.vue @@ -60,6 +60,7 @@ export default { { + 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() => {