Repository: hashicorp/vault
File: ui/tests/integration/components/tools/wrap-test.js (JavaScript)

Code before:
      foo3: 'foo\nbar\nbaz',
    });

    this.server.post('sys/wrapping/wrap', (schema, { requestBody, requestHeaders }) => {
      const payload = JSON.parse(requestBody);
      assert.propEqual(payload, JSON.parse(updatedWrapData), `payload contains data: ${requestBody}`);
      assert.strictEqual(requestHeaders['X-Vault-Wrap-TTL'], '30m', 'request header has default wrap ttl');
      return {
        wrap_info: {
          token: this.token,
          accessor: '5yjKx6Om9NmBx1mjiN1aIrnm',
          ttl: 1800,
          creation_time: '2024-06-07T12:02:22.096254-07:00',
          creation_path: 'sys/wrapping/wrap',
        },
      };
    });

    await this.renderComponent();
    await codemirror().setValue(this.wrapData);
    await click('[data-test-toggle-input="json"]');
    await fillIn('[data-test-kv-key="1"]', 'foo2');
    await fillIn('[data-test-kv-value="1"]', 'bar2');
    await click('[data-test-kv-add-row="1"]');
    await fillIn('[data-test-kv-key="2"]', 'foo3');
    await fillIn('[data-test-kv-value="2"]', 'foo\nbar\nbaz');
    await click(TS.submit);
    await waitUntil(() => find(TS.toolsInput('wrapping-token')));
    assert.true(flashSpy.calledWith('Wrap was successful.'), 'it renders success flash');
    assert.dom(TS.toolsInput('wrapping-token')).hasText(this.token);
    assert.dom('label').hasText('Wrapped token');
    assert.dom('.CodeMirror').doesNotExist();
  });

  test('it resets on done', async function (assert) {
    await this.renderComponent();
    await codemirror().setValue(this.wrapData);
    await click(TTL.toggleByLabel('Wrap TTL'));
    await fillIn(TTL.valueInputByLabel('Wrap TTL'), '20');
    await click(TS.submit);

    await waitUntil(() => find(TS.button('Done')));
    await click(TS.button('Done'));
    assert.strictEqual(
      codemirror().getValue(' '),
      `{   \"\": \"\" }`, // eslint-disable-line no-useless-escape
      'json editor initializes with empty object that includes whitespace'
    );
    assert.dom(TTL.toggleByLabel('Wrap TTL')).isNotChecked('Wrap TTL resets to unchecked');
    await click(TTL.toggleByLabel('Wrap TTL'));
    assert.dom(TTL.valueInputByLabel('Wrap TTL')).hasValue('30', 'ttl resets to default when toggled');

Review comment (security):
this is technically still a single line string, we could save a variable in this test using backticks, and pass that in here instead, i.e:
```js
const topSecretData = `this is my multi-line secret
that I want to wrap
blah blah, something
some seriously important config`

Code after:
      some seriously important config`;
    const flashSpy = sinon.spy(this.owner.lookup('service:flash-messages'), 'success');
    const updatedWrapData = JSON.stringify({
      ...JSON.parse(this.wrapData),
      foo: 'bar',
      foo2: multilineData,
    });

    this.server.post('sys/wrapping/wrap', (schema, { requestBody, requestHeaders }) => {
      const payload = JSON.parse(requestBody);
      assert.propEqual(payload, JSON.parse(updatedWrapData), `payload contains data: ${requestBody}`);
      assert.strictEqual(requestHeaders['X-Vault-Wrap-TTL'], '30m', 'request header has default wrap ttl');
      return {
        wrap_info: {
          token: this.token,
          accessor: '5yjKx6Om9NmBx1mjiN1aIrnm',
          ttl: 1800,
          creation_time: '2024-06-07T12:02:22.096254-07:00',
          creation_path: 'sys/wrapping/wrap',
        },
      };
    });

    await this.renderComponent();
    await click('[data-test-toggle-input="json"]');
    await fillIn('[data-test-kv-key="0"]', 'foo');
    await fillIn('[data-test-kv-value="0"]', 'bar');
    await click('[data-test-kv-add-row="0"]');
    await fillIn('[data-test-kv-key="1"]', 'foo2');
    await fillIn('[data-test-kv-value="1"]', multilineData);
    await click(TS.submit);
    await waitUntil(() => find(TS.toolsInput('wrapping-token')));
    assert.true(flashSpy.calledWith('Wrap was successful.'), 'it renders success flash');
    assert.dom(TS.toolsInput('wrapping-token')).hasText(this.token);
    assert.dom('label').hasText('Wrapped token');
    assert.dom('.CodeMirror').doesNotExist();
  });

  test('it resets on done', async function (assert) {
    await this.renderComponent();
    await codemirror().setValue(this.wrapData);
    await click(TTL.toggleByLabel('Wrap TTL'));
    await fillIn(TTL.valueInputByLabel('Wrap TTL'), '20');
    await click(TS.submit);

    await waitUntil(() => find(TS.button('Done')));
    await click(TS.button('Done'));
    assert.strictEqual(
      codemirror().getValue(' '),
      `{   \"\": \"\" }`, // eslint-disable-line no-useless-escape
      'json editor initializes with empty object that includes whitespace'