vault/ui/tests/integration/components/shamir/flow-test.js
hashicorp-copywrite[bot] 0b12cdcfd1
[COMPLIANCE] License changes (#22290)
* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Updating the license from MPL to Business Source License.

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl.

* add missing license headers

* Update copyright file headers to BUS-1.1

* Fix test that expected exact offset on hcl file

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Sarah Thompson <sthompson@hashicorp.com>
Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
2023-08-10 18:14:03 -07:00

135 lines
4.3 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import sinon from 'sinon';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'vault/tests/helpers';
import { click, fillIn, render, settled } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import Service from '@ember/service';
import { run } from '@ember/runloop';
import { reject, resolve } from 'rsvp';
import { SHAMIR_FORM } from 'vault/tests/helpers/components/shamir';
const licenseError = { httpStatus: 500, errors: ['failed because licensing is in an invalid state'] };
const response = {
progress: 1,
required: 3,
complete: false,
};
const adapter = {
foo() {
return resolve(response);
},
responseWithErrors() {
return reject({ httpStatus: 400, errors: ['something is wrong', 'seriously wrong'] });
},
responseWithLicense() {
return reject(licenseError);
},
};
const storeStub = Service.extend({
adapterFor() {
return adapter;
},
});
// Checks that the correct data were passed around happens in the integration test
// this one is checking that things happen at the right time
module('Integration | Component | shamir/flow', function (hooks) {
setupRenderingTest(hooks);
hooks.beforeEach(function () {
this.keyPart = 'some-key-partition';
run(() => {
this.owner.unregister('service:store');
this.owner.register('service:store', storeStub);
this.storeService = this.owner.lookup('service:store');
});
});
test('it sends data to the passed action and calls updateProgress', async function (assert) {
const updateSpy = sinon.spy();
const completeSpy = sinon.spy();
this.set('updateProgress', updateSpy);
this.set('checkComplete', () => false);
this.set('onSuccess', completeSpy);
this.set('progress', 0);
await render(hbs`
<Shamir::Flow
@action="foo"
@threshold={{3}}
@progress={{this.progress}}
@updateProgress={{this.updateProgress}}
@checkComplete={{this.checkComplete}}
@onShamirSuccess={{this.onSuccess}}
/>`);
await fillIn(SHAMIR_FORM.input, this.keyPart);
await click(SHAMIR_FORM.submitButton);
assert.ok(completeSpy.notCalled, 'onShamirSuccess was not called');
assert.ok(updateSpy.calledOnce, 'updateProgress was called');
// Default shamir flow expects the updated values to be passed
// in from parent model, so this approximates the update happening
// from a side effect of the updateProgress call
this.set('progress', 2);
// Pretend the next call will mean completion
this.set('checkComplete', () => true);
await settled();
await fillIn(SHAMIR_FORM.input, this.keyPart);
await click(SHAMIR_FORM.submitButton);
assert.ok(completeSpy.calledOnce, 'onShamirSuccess was called');
assert.ok(updateSpy.calledTwice, 'updateProgress was called again');
});
test('it shows the error when adapter fails with 400 httpStatus', async function (assert) {
assert.expect(3);
const updateSpy = sinon.spy();
const completeSpy = sinon.spy();
this.set('updateProgress', updateSpy);
this.set('checkComplete', completeSpy);
await render(hbs`
<Shamir::Flow
@action="response-with-errors"
@threshold={{3}}
@progress={{2}}
@updateProgress={{this.updateProgress}}
@checkComplete={{this.checkComplete}}
/>`);
await fillIn(SHAMIR_FORM.input, this.keyPart);
await click(SHAMIR_FORM.submitButton);
assert.dom(SHAMIR_FORM.error).exists({ count: 2 }, 'renders errors');
assert.ok(completeSpy.notCalled, 'checkComplete was not called');
assert.ok(updateSpy.notCalled, 'updateProgress was not called');
});
test.skip('it throws the error when adapter fails with license error', async function (assert) {
assert.expect(2);
try {
const licenseSpy = sinon.spy();
this.set('onLicenseError', licenseSpy);
await render(hbs`
<Shamir::Flow
@action="response-with-license"
@threshold={{3}}
@progress={{2}}
@onLicenseError={{this.onLicenseError}}
/>`);
await fillIn(SHAMIR_FORM.input, this.keyPart);
await click(SHAMIR_FORM.submitButton);
assert.ok(licenseSpy.calledOnce, 'license error triggered');
} catch (e) {
assert.deepEqual(e, licenseError, 'throws the error');
}
});
});