Files
threshold-ui/addons/auth/tests/unit/authenticators/base-test.js
T
Randall MoreyandSusmitha Girumala a2e27b09c3 refactor: 💡 add standalone server mocks to auth tests (#1283)
* refactor: 💡 replace ember notify with flash in notify decorator

* refactor: 💡 remove embery-notify and replace last instance

* chore: 🤖 upgrade ember dependencies in anticipation of ember 4

* refactor: 💡 add standalone server mocks to auth tests

Co-authored-by: Susmitha Girumala <susmitha@hashicorp.com>
2022-09-01 15:40:00 -04:00

87 lines
2.8 KiB
JavaScript

import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import Pretender from 'pretender';
module('Unit | Authenticator | base', function (hooks) {
setupTest(hooks);
let server;
hooks.beforeEach(() => {
server = new Pretender();
});
hooks.afterEach(() => {
server.shutdown();
});
test('it deauthenticates on invalidation', async function (assert) {
assert.expect(1);
const authenticator = this.owner.lookup('authenticator:base');
const endpoint = authenticator.buildDeauthEndpointURL({ id: 'token123' });
server.delete(endpoint, () => {
assert.ok(true, 'deauthentication occurred');
return [200];
});
await authenticator.invalidate({ token: 'token123' });
});
test('invalidation succeeds even if the deauthentication request fails', async function (assert) {
assert.expect(2);
const authenticator = this.owner.lookup('authenticator:base');
const endpoint = authenticator.buildDeauthEndpointURL({ id: 'token123' });
server.delete(endpoint, () => {
assert.ok(true, 'deauthentication was requested');
return [500];
});
await authenticator
.invalidate({ token: 'token123' })
.then(() => assert.ok(true))
.catch(() => assert.notOk(true, 'uh oh, this should not happen'));
});
test('it passes token validation on restoration if receiving 200', async function (assert) {
assert.expect(1);
const id = 'token_123';
const mockData = { attributes: { id } };
const authenticator = this.owner.lookup('authenticator:base');
server.get(authenticator.buildTokenValidationEndpointURL(id), () => {
assert.ok(true, 'token validation was requested');
return [200];
});
await authenticator.restore(mockData);
});
test('it fails token validation on restoration if receiving 401', async function (assert) {
assert.expect(2);
const id = 'token_123';
const mockData = { attributes: { id } };
const authenticator = this.owner.lookup('authenticator:base');
server.get(authenticator.buildTokenValidationEndpointURL(id), () => {
assert.ok(true, 'token validation was requested');
return [401];
});
try {
await authenticator.restore(mockData);
} catch (e) {
assert.ok(true, 'restoration failed');
}
});
test('it fails token validation on restoration if receiving 404', async function (assert) {
assert.expect(2);
const id = 'token_123';
const mockData = { attributes: { id } };
const authenticator = this.owner.lookup('authenticator:base');
server.get(authenticator.buildTokenValidationEndpointURL(id), () => {
assert.ok(true, 'token validation was requested');
return [404];
});
try {
await authenticator.restore(mockData);
} catch (e) {
assert.ok(true, 'restoration failed');
}
});
});