Files
threshold-ui/addons/auth/tests/unit/authenticators/oidc-test.js
T

47 lines
1.3 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
2021-03-19 17:03:03 -04:00
import { module, test } from 'qunit';
2021-01-15 11:10:33 -05:00
import { setupTest } from 'ember-qunit';
import Pretender from 'pretender';
2021-01-15 11:10:33 -05:00
module('Unit | Authenticator | OIDC', function (hooks) {
setupTest(hooks);
let server;
hooks.beforeEach(() => {
server = new Pretender();
});
hooks.afterEach(() => {
server.shutdown();
});
2021-03-19 17:03:03 -04:00
test('`startAuthentication` initiates the authentication flow and returns the response', async function (assert) {
assert.expect(2);
const authenticator = this.owner.lookup('authenticator:oidc');
server.post(authenticator.buildAuthEndpointURL(), () => [
200,
{},
JSON.stringify({ foo: 'bar' }),
]);
2021-03-19 17:03:03 -04:00
const response = await authenticator.startAuthentication();
assert.ok(true, 'authentication succeeded');
assert.deepEqual(response, { foo: 'bar' });
});
test('`startAuthentication` rejects/errors if API returns an error response', async function (assert) {
assert.expect(1);
const authenticator = this.owner.lookup('authenticator:oidc');
server.post(authenticator.buildAuthEndpointURL(), () => [400]);
2021-03-19 17:03:03 -04:00
// Because assert.throws doesn't work with async errors/promise rejections
try {
await authenticator.startAuthentication();
} catch (e) {
assert.ok(true);
}
});
2021-01-15 11:10:33 -05:00
});