@@ -4,6 +4,9 @@ import { _EDIT } from '@shell/config/query-params';
44import { PROV_CLUSTER } from '@shell/edit/provisioning.cattle.io.cluster/__tests__/utils/cluster' ;
55import RegistryConfigs from '@shell/edit/provisioning.cattle.io.cluster/tabs/registries/RegistryConfigs.vue' ;
66
7+ const VALID_BASE64_CERT = 'LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0t' ;
8+ const VALID_PEM_TEXT = '-----BEGIN CERTIFICATE-----\nMIIBkTCB+wIJA' ;
9+
710describe ( 'component: RegistryConfigs' , ( ) => {
811 let wrapper : Wrapper < InstanceType < typeof RegistryConfigs > & { [ key : string ] : any } > ;
912
@@ -18,14 +21,14 @@ describe('component: RegistryConfigs', () => {
1821 SelectOrCreateAuthSecret : true ,
1922 SecretSelector : true ,
2023 } ,
21- mocks : { $store : { getters : { 'i18n/t' : jest . fn ( ) } } }
24+ mocks : { $store : { getters : { 'i18n/t' : jest . fn ( ( key : string ) => key ) } } }
2225 }
2326 } ;
2427
2528 describe ( 'key CA Cert Bundle' , ( ) => {
2629 it . each ( [
27- [ 'source is plain text ' , 'Zm9vYmFy' , 'foobar ' ] ,
28- [ 'source is base64 ' , 'foobar' , 'foobar' ] ,
30+ [ 'source is base64 ' , VALID_BASE64_CERT , '-----BEGIN CERTIFICATE----- ' ] ,
31+ [ 'source is plain text ' , 'foobar' , 'foobar' ] ,
2932 ] ) ( 'should display key, %p' , ( _ , sourceCaBundle , displayedCaBundle ) => {
3033 const value = clone ( PROV_CLUSTER ) ;
3134
@@ -43,10 +46,10 @@ describe('component: RegistryConfigs', () => {
4346 expect ( registry . props ( ) . value ) . toBe ( displayedCaBundle ) ;
4447 } ) ;
4548
46- it ( 'should update key in base64 format ' , async ( ) => {
49+ it ( 'should base64 encode plain PEM text on save ' , async ( ) => {
4750 const value = clone ( PROV_CLUSTER ) ;
4851
49- value . spec . rkeConfig . registries . configs = { foo : { caBundle : 'Zm9vYmFy' } } ;
52+ value . spec . rkeConfig . registries . configs = { foo : { caBundle : VALID_BASE64_CERT } } ;
5053
5154 mountOptions . propsData . value = value ;
5255
@@ -57,10 +60,132 @@ describe('component: RegistryConfigs', () => {
5760
5861 const registry = wrapper . findComponent ( '[data-testid^="registry-caBundle"]' ) ;
5962
60- registry . vm . $emit ( 'update:value' , 'ssh key' ) ;
63+ registry . vm . $emit ( 'update:value' , VALID_PEM_TEXT ) ;
6164 wrapper . vm . update ( ) ;
6265
63- expect ( wrapper . emitted ( 'updateConfigs' ) [ 0 ] [ 0 ] [ 'foo' ] [ 'caBundle' ] ) . toBe ( 'c3NoIGtleQ==' ) ;
66+ expect ( wrapper . emitted ( 'updateConfigs' ) [ 0 ] [ 0 ] [ 'foo' ] [ 'caBundle' ] ) . toBe ( 'LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJrVENCK3dJSkE=' ) ;
67+ } ) ;
68+
69+ it ( 'should keep base64 value as-is on save' , async ( ) => {
70+ const value = clone ( PROV_CLUSTER ) ;
71+
72+ value . spec . rkeConfig . registries . configs = { foo : { caBundle : VALID_BASE64_CERT } } ;
73+
74+ mountOptions . propsData . value = value ;
75+
76+ wrapper = mount (
77+ RegistryConfigs ,
78+ mountOptions
79+ ) ;
80+
81+ const registry = wrapper . findComponent ( '[data-testid^="registry-caBundle"]' ) ;
82+
83+ registry . vm . $emit ( 'update:value' , VALID_BASE64_CERT ) ;
84+ wrapper . vm . update ( ) ;
85+
86+ expect ( wrapper . emitted ( 'updateConfigs' ) [ 0 ] [ 0 ] [ 'foo' ] [ 'caBundle' ] ) . toBe ( VALID_BASE64_CERT ) ;
87+ } ) ;
88+ } ) ;
89+
90+ describe ( 'cA Cert Bundle validation' , ( ) => {
91+ it ( 'should pass validation for valid base64 value' , ( ) => {
92+ const value = clone ( PROV_CLUSTER ) ;
93+
94+ value . spec . rkeConfig . registries . configs = { } ;
95+ mountOptions . propsData . value = value ;
96+
97+ wrapper = mount ( RegistryConfigs , mountOptions ) ;
98+
99+ const rule = wrapper . vm . caBundleRules [ 0 ] ;
100+
101+ expect ( rule ( VALID_BASE64_CERT ) ) . toBeUndefined ( ) ;
102+ } ) ;
103+
104+ it ( 'should pass validation for PEM text' , ( ) => {
105+ const value = clone ( PROV_CLUSTER ) ;
106+
107+ value . spec . rkeConfig . registries . configs = { } ;
108+ mountOptions . propsData . value = value ;
109+
110+ wrapper = mount ( RegistryConfigs , mountOptions ) ;
111+
112+ const rule = wrapper . vm . caBundleRules [ 0 ] ;
113+
114+ expect ( rule ( VALID_PEM_TEXT ) ) . toBeUndefined ( ) ;
115+ } ) ;
116+
117+ it ( 'should pass validation for empty value' , ( ) => {
118+ const value = clone ( PROV_CLUSTER ) ;
119+
120+ value . spec . rkeConfig . registries . configs = { } ;
121+ mountOptions . propsData . value = value ;
122+
123+ wrapper = mount ( RegistryConfigs , mountOptions ) ;
124+
125+ const rule = wrapper . vm . caBundleRules [ 0 ] ;
126+
127+ expect ( rule ( '' ) ) . toBeUndefined ( ) ;
128+ expect ( rule ( null ) ) . toBeUndefined ( ) ;
129+ expect ( rule ( undefined ) ) . toBeUndefined ( ) ;
130+ } ) ;
131+
132+ it ( 'should fail validation for invalid value' , ( ) => {
133+ const value = clone ( PROV_CLUSTER ) ;
134+
135+ value . spec . rkeConfig . registries . configs = { } ;
136+ mountOptions . propsData . value = value ;
137+
138+ wrapper = mount ( RegistryConfigs , mountOptions ) ;
139+
140+ const rule = wrapper . vm . caBundleRules [ 0 ] ;
141+
142+ expect ( rule ( 'not-valid-base64!' ) ) . toContain ( 'registryConfig.caBundle.validationError' ) ;
143+ } ) ;
144+
145+ it ( 'should report allCaBundlesValid as true when all entries have valid caBundles' , ( ) => {
146+ const value = clone ( PROV_CLUSTER ) ;
147+
148+ value . spec . rkeConfig . registries . configs = {
149+ 'reg1.example.com' : { caBundle : VALID_BASE64_CERT } ,
150+ 'reg2.example.com' : { caBundle : VALID_BASE64_CERT } ,
151+ } ;
152+ mountOptions . propsData . value = value ;
153+
154+ wrapper = mount ( RegistryConfigs , mountOptions ) ;
155+
156+ expect ( wrapper . vm . allCaBundlesValid ) . toBe ( true ) ;
157+ } ) ;
158+
159+ it ( 'should report allCaBundlesValid as false when any entry has an invalid caBundle' , ( ) => {
160+ const value = clone ( PROV_CLUSTER ) ;
161+
162+ value . spec . rkeConfig . registries . configs = {
163+ 'reg1.example.com' : { caBundle : VALID_BASE64_CERT } ,
164+ 'reg2.example.com' : { caBundle : 'not-valid!' } ,
165+ } ;
166+ mountOptions . propsData . value = value ;
167+
168+ wrapper = mount ( RegistryConfigs , mountOptions ) ;
169+
170+ expect ( wrapper . vm . allCaBundlesValid ) . toBe ( false ) ;
171+ } ) ;
172+
173+ it ( 'should update validation when an invalid entry is removed' , ( ) => {
174+ const value = clone ( PROV_CLUSTER ) ;
175+
176+ value . spec . rkeConfig . registries . configs = {
177+ 'reg1.example.com' : { caBundle : VALID_BASE64_CERT } ,
178+ 'reg2.example.com' : { caBundle : 'not-valid!' } ,
179+ } ;
180+ mountOptions . propsData . value = value ;
181+
182+ wrapper = mount ( RegistryConfigs , mountOptions ) ;
183+
184+ expect ( wrapper . vm . allCaBundlesValid ) . toBe ( false ) ;
185+
186+ wrapper . vm . entries . splice ( 1 , 1 ) ;
187+
188+ expect ( wrapper . vm . allCaBundlesValid ) . toBe ( true ) ;
64189 } ) ;
65190 } ) ;
66191} ) ;
0 commit comments