@@ -37,8 +37,13 @@ module('Integration | Component | tools/wrap', function (hooks) {
3737 await this . renderComponent ( ) ;
3838
3939 assert . dom ( 'h1' ) . hasText ( 'Wrap Data' , 'Title renders' ) ;
40- assert . dom ( 'label' ) . hasText ( 'Data to wrap (json-formatted)' ) ;
41- assert . strictEqual ( codemirror ( ) . getValue ( ' ' ) , '{ }' , 'json editor initializes with empty object' ) ;
40+ assert . dom ( '[data-test-toggle-label="json"]' ) . hasText ( 'JSON' ) ;
41+ assert . dom ( '[data-test-component="json-editor-title"]' ) . hasText ( 'Data to wrap (json-formatted)' ) ;
42+ assert . strictEqual (
43+ codemirror ( ) . getValue ( ' ' ) ,
44+ `{ \"\": \"\" }` , // eslint-disable-line no-useless-escape
45+ 'json editor initializes with empty object that includes whitespace'
46+ ) ;
4247 assert . dom ( TTL . toggleByLabel ( 'Wrap TTL' ) ) . isNotChecked ( 'Wrap TTL defaults to unchecked' ) ;
4348 assert . dom ( TS . submit ) . isEnabled ( ) ;
4449 assert . dom ( TS . toolsInput ( 'wrapping-token' ) ) . doesNotExist ( ) ;
@@ -104,6 +109,67 @@ module('Integration | Component | tools/wrap', function (hooks) {
104109 await click ( TS . submit ) ;
105110 } ) ;
106111
112+ test ( 'it toggles between views and preserves input data' , async function ( assert ) {
113+ assert . expect ( 6 ) ;
114+ await this . renderComponent ( ) ;
115+ await codemirror ( ) . setValue ( this . wrapData ) ;
116+ assert . dom ( '[data-test-component="json-editor-title"]' ) . hasText ( 'Data to wrap (json-formatted)' ) ;
117+ await click ( '[data-test-toggle-input="json"]' ) ;
118+ assert . dom ( '[data-test-component="json-editor-title"]' ) . doesNotExist ( ) ;
119+ assert . dom ( '[data-test-kv-key="0"]' ) . hasValue ( 'foo' ) ;
120+ assert . dom ( '[data-test-kv-value="0"]' ) . hasValue ( 'bar' ) ;
121+ await click ( '[data-test-toggle-input="json"]' ) ;
122+ assert . dom ( '[data-test-component="json-editor-title"]' ) . exists ( ) ;
123+ assert . strictEqual (
124+ codemirror ( ) . getValue ( ' ' ) ,
125+ `{ \"foo": \"bar" }` , // eslint-disable-line no-useless-escape
126+ 'json editor has original data'
127+ ) ;
128+ } ) ;
129+
130+ test ( 'it submits from kv view' , async function ( assert ) {
131+ assert . expect ( 6 ) ;
132+
133+ const multilineData = `this is a multi-line secret
134+ that contains
135+ some seriously important config` ;
136+ const flashSpy = sinon . spy ( this . owner . lookup ( 'service:flash-messages' ) , 'success' ) ;
137+ const updatedWrapData = JSON . stringify ( {
138+ ...JSON . parse ( this . wrapData ) ,
139+ foo : 'bar' ,
140+ foo2 : multilineData ,
141+ } ) ;
142+
143+ this . server . post ( 'sys/wrapping/wrap' , ( schema , { requestBody, requestHeaders } ) => {
144+ const payload = JSON . parse ( requestBody ) ;
145+ assert . propEqual ( payload , JSON . parse ( updatedWrapData ) , `payload contains data: ${ requestBody } ` ) ;
146+ assert . strictEqual ( requestHeaders [ 'X-Vault-Wrap-TTL' ] , '30m' , 'request header has default wrap ttl' ) ;
147+ return {
148+ wrap_info : {
149+ token : this . token ,
150+ accessor : '5yjKx6Om9NmBx1mjiN1aIrnm' ,
151+ ttl : 1800 ,
152+ creation_time : '2024-06-07T12:02:22.096254-07:00' ,
153+ creation_path : 'sys/wrapping/wrap' ,
154+ } ,
155+ } ;
156+ } ) ;
157+
158+ await this . renderComponent ( ) ;
159+ await click ( '[data-test-toggle-input="json"]' ) ;
160+ await fillIn ( '[data-test-kv-key="0"]' , 'foo' ) ;
161+ await fillIn ( '[data-test-kv-value="0"]' , 'bar' ) ;
162+ await click ( '[data-test-kv-add-row="0"]' ) ;
163+ await fillIn ( '[data-test-kv-key="1"]' , 'foo2' ) ;
164+ await fillIn ( '[data-test-kv-value="1"]' , multilineData ) ;
165+ await click ( TS . submit ) ;
166+ await waitUntil ( ( ) => find ( TS . toolsInput ( 'wrapping-token' ) ) ) ;
167+ assert . true ( flashSpy . calledWith ( 'Wrap was successful.' ) , 'it renders success flash' ) ;
168+ assert . dom ( TS . toolsInput ( 'wrapping-token' ) ) . hasText ( this . token ) ;
169+ assert . dom ( 'label' ) . hasText ( 'Wrapped token' ) ;
170+ assert . dom ( '.CodeMirror' ) . doesNotExist ( ) ;
171+ } ) ;
172+
107173 test ( 'it resets on done' , async function ( assert ) {
108174 await this . renderComponent ( ) ;
109175 await codemirror ( ) . setValue ( this . wrapData ) ;
@@ -113,7 +179,11 @@ module('Integration | Component | tools/wrap', function (hooks) {
113179
114180 await waitUntil ( ( ) => find ( TS . button ( 'Done' ) ) ) ;
115181 await click ( TS . button ( 'Done' ) ) ;
116- assert . strictEqual ( codemirror ( ) . getValue ( ' ' ) , '{ }' , 'json editor resets to empty object' ) ;
182+ assert . strictEqual (
183+ codemirror ( ) . getValue ( ' ' ) ,
184+ `{ \"\": \"\" }` , // eslint-disable-line no-useless-escape
185+ 'json editor initializes with empty object that includes whitespace'
186+ ) ;
117187 assert . dom ( TTL . toggleByLabel ( 'Wrap TTL' ) ) . isNotChecked ( 'Wrap TTL resets to unchecked' ) ;
118188 await click ( TTL . toggleByLabel ( 'Wrap TTL' ) ) ;
119189 assert . dom ( TTL . valueInputByLabel ( 'Wrap TTL' ) ) . hasValue ( '30' , 'ttl resets to default when toggled' ) ;
@@ -126,16 +196,51 @@ module('Integration | Component | tools/wrap', function (hooks) {
126196
127197 await waitUntil ( ( ) => find ( TS . button ( 'Back' ) ) ) ;
128198 await click ( TS . button ( 'Back' ) ) ;
129- assert . strictEqual ( codemirror ( ) . getValue ( ' ' ) , `{"foo": "bar"}` , 'json editor has original data' ) ;
199+ assert . strictEqual (
200+ codemirror ( ) . getValue ( ' ' ) ,
201+ `{ \"foo": \"bar" }` , // eslint-disable-line no-useless-escape
202+ 'json editor has original data'
203+ ) ;
130204 assert . dom ( TTL . toggleByLabel ( 'Wrap TTL' ) ) . isNotChecked ( 'Wrap TTL defaults to unchecked' ) ;
131205 } ) ;
132206
133- test ( 'it disables/enables submit based on json linting' , async function ( assert ) {
207+ test ( 'it renders/hides warning based on json linting' , async function ( assert ) {
134208 await this . renderComponent ( ) ;
135209 await codemirror ( ) . setValue ( `{bad json}` ) ;
136- assert . dom ( TS . submit ) . isDisabled ( 'submit disables if json editor has linting errors' ) ;
137-
210+ assert
211+ . dom ( '[data-test-inline-alert]' )
212+ . hasText (
213+ 'JSON is unparsable. Fix linting errors to avoid data discrepancies.' ,
214+ 'Linting error message is shown for json view'
215+ ) ;
138216 await codemirror ( ) . setValue ( this . wrapData ) ;
139- assert . dom ( TS . submit ) . isEnabled ( 'submit reenables if json editor has no linting errors' ) ;
217+ assert . dom ( '[data-test-inline-alert]' ) . doesNotExist ( ) ;
218+ } ) ;
219+
220+ test ( 'it hides json warning on back and on done' , async function ( assert ) {
221+ await this . renderComponent ( ) ;
222+ await codemirror ( ) . setValue ( `{bad json}` ) ;
223+ assert
224+ . dom ( '[data-test-inline-alert]' )
225+ . hasText (
226+ 'JSON is unparsable. Fix linting errors to avoid data discrepancies.' ,
227+ 'Linting error message is shown for json view'
228+ ) ;
229+ await click ( TS . submit ) ;
230+ await waitUntil ( ( ) => find ( TS . button ( 'Done' ) ) ) ;
231+ await click ( TS . button ( 'Done' ) ) ;
232+ assert . dom ( '[data-test-inline-alert]' ) . doesNotExist ( ) ;
233+
234+ await codemirror ( ) . setValue ( `{bad json}` ) ;
235+ assert
236+ . dom ( '[data-test-inline-alert]' )
237+ . hasText (
238+ 'JSON is unparsable. Fix linting errors to avoid data discrepancies.' ,
239+ 'Linting error message is shown for json view'
240+ ) ;
241+ await click ( TS . submit ) ;
242+ await waitUntil ( ( ) => find ( TS . button ( 'Back' ) ) ) ;
243+ await click ( TS . button ( 'Back' ) ) ;
244+ assert . dom ( '[data-test-inline-alert]' ) . doesNotExist ( ) ;
140245 } ) ;
141246} ) ;
0 commit comments