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>
This commit is contained in:
Randall MoreyandSusmitha Girumala authored and GitHub committed 2022-09-01 15:40:00 -04:00
1 parent 3ff89279e6
commit a2e27b09c3
4 files changed
+74 -61

No files matched your search

+1 -1
View File
@@ -35,7 +35,6 @@
"@glimmer/component": "^1.1.2",
"@glimmer/tracking": "^1.1.2",
"broccoli-asset-rev": "^3.0.0",
"core": "*",
"doctoc": "^2.2.0",
"ember-cli": "^4.4.0",
"ember-cli-dependency-checker": "^3.2.0",
@@ -59,6 +58,7 @@
"eslint-plugin-qunit": "^7.3.0",
"loader.js": "^4.7.0",
"npm-run-all": "^4.1.5",
"pretender": "^3.4.3",
"prettier": "^2.4.0",
"qunit": "^2.17.1",
"qunit-dom": "^2.0.0",
@@ -1,19 +1,27 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { Response } from 'miragejs';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import Pretender from 'pretender';
module('Unit | Authenticator | base', function (hooks) {
setupTest(hooks);
setupMirage(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' });
this.server.delete(endpoint, () => {
server.delete(endpoint, () => {
assert.ok(true, 'deauthentication occurred');
return new Response(200);
return [200];
});
await authenticator.invalidate({ token: 'token123' });
});
@@ -22,9 +30,9 @@ module('Unit | Authenticator | base', function (hooks) {
assert.expect(2);
const authenticator = this.owner.lookup('authenticator:base');
const endpoint = authenticator.buildDeauthEndpointURL({ id: 'token123' });
this.server.delete(endpoint, () => {
server.delete(endpoint, () => {
assert.ok(true, 'deauthentication was requested');
return new Response(500);
return [500];
});
await authenticator
.invalidate({ token: 'token123' })
@@ -37,9 +45,9 @@ module('Unit | Authenticator | base', function (hooks) {
const id = 'token_123';
const mockData = { attributes: { id } };
const authenticator = this.owner.lookup('authenticator:base');
this.server.get(authenticator.buildTokenValidationEndpointURL(id), () => {
server.get(authenticator.buildTokenValidationEndpointURL(id), () => {
assert.ok(true, 'token validation was requested');
return new Response(200);
return [200];
});
await authenticator.restore(mockData);
});
@@ -49,9 +57,9 @@ module('Unit | Authenticator | base', function (hooks) {
const id = 'token_123';
const mockData = { attributes: { id } };
const authenticator = this.owner.lookup('authenticator:base');
this.server.get(authenticator.buildTokenValidationEndpointURL(id), () => {
server.get(authenticator.buildTokenValidationEndpointURL(id), () => {
assert.ok(true, 'token validation was requested');
return new Response(401);
return [401];
});
try {
await authenticator.restore(mockData);
@@ -65,9 +73,9 @@ module('Unit | Authenticator | base', function (hooks) {
const id = 'token_123';
const mockData = { attributes: { id } };
const authenticator = this.owner.lookup('authenticator:base');
this.server.get(authenticator.buildTokenValidationEndpointURL(id), () => {
server.get(authenticator.buildTokenValidationEndpointURL(id), () => {
assert.ok(true, 'token validation was requested');
return new Response(404);
return [404];
});
try {
await authenticator.restore(mockData);
@@ -1,19 +1,28 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { Response } from 'miragejs';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import Pretender from 'pretender';
module('Unit | Authenticator | OIDC', function (hooks) {
setupTest(hooks);
setupMirage(hooks);
let server;
hooks.beforeEach(() => {
server = new Pretender();
});
hooks.afterEach(() => {
server.shutdown();
});
test('`startAuthentication` initiates the authentication flow and returns the response', async function (assert) {
assert.expect(2);
const authenticator = this.owner.lookup('authenticator:oidc');
this.server.post(
authenticator.buildAuthEndpointURL(),
() => new Response(200, {}, { foo: 'bar' })
);
server.post(authenticator.buildAuthEndpointURL(), () => [
200,
{},
JSON.stringify({ foo: 'bar' }),
]);
const response = await authenticator.startAuthentication();
assert.ok(true, 'authentication succeeded');
assert.deepEqual(response, { foo: 'bar' });
@@ -22,10 +31,7 @@ module('Unit | Authenticator | OIDC', function (hooks) {
test('`startAuthentication` rejects/errors if API returns an error response', async function (assert) {
assert.expect(1);
const authenticator = this.owner.lookup('authenticator:oidc');
this.server.post(
authenticator.buildAuthEndpointURL(),
() => new Response(400)
);
server.post(authenticator.buildAuthEndpointURL(), () => [400]);
// Because assert.throws doesn't work with async errors/promise rejections
try {
await authenticator.startAuthentication();
@@ -1,23 +1,28 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { Response } from 'miragejs';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import Pretender from 'pretender';
module('Unit | Authenticator | password', function (hooks) {
setupTest(hooks);
setupMirage(hooks);
let server;
hooks.beforeEach(() => {
server = new Pretender();
});
hooks.afterEach(() => {
server.shutdown();
});
test('it authenticates to the specified authEndpoint', async function (assert) {
assert.expect(2);
const authenticator = this.owner.lookup('authenticator:password');
this.server.post(
authenticator.buildAuthEndpointURL(),
(schema, request) => {
const json = JSON.parse(request.requestBody);
assert.ok(json.type, 'Requested token cookies by default');
return new Response(200);
}
);
server.post(authenticator.buildAuthEndpointURL(), (request) => {
const json = JSON.parse(request.requestBody);
assert.ok(json.type, 'Requested token cookies by default');
return [200, {}, '{}'];
});
await authenticator.authenticate({}).then(() => {
assert.ok(true, 'authentication succeeded');
});
@@ -26,14 +31,11 @@ module('Unit | Authenticator | password', function (hooks) {
test('it can authenticate without requesting cookies', async function (assert) {
assert.expect(2);
const authenticator = this.owner.lookup('authenticator:password');
this.server.post(
authenticator.buildAuthEndpointURL(),
(schema, request) => {
const json = JSON.parse(request.requestBody);
assert.notOk(json.type, 'Did not request tokens cookies');
return new Response(200);
}
);
server.post(authenticator.buildAuthEndpointURL(), (request) => {
const json = JSON.parse(request.requestBody);
assert.notOk(json.type, 'Did not request tokens cookies');
return [200, {}, '{}'];
});
await authenticator.authenticate({}, false).then(() => {
assert.ok(true, 'authentication succeeded');
});
@@ -42,21 +44,18 @@ module('Unit | Authenticator | password', function (hooks) {
test('it authenticates with the expected payload', async function (assert) {
assert.expect(1);
const authenticator = this.owner.lookup('authenticator:password');
this.server.post(
authenticator.buildAuthEndpointURL(),
(schema, request) => {
const json = JSON.parse(request.requestBody);
assert.deepEqual(json, {
command: 'login',
type: 'cookie',
attributes: {
login_name: 'foo',
password: 'bar',
},
});
return new Response(200);
}
);
server.post(authenticator.buildAuthEndpointURL(), (request) => {
const json = JSON.parse(request.requestBody);
assert.deepEqual(json, {
command: 'login',
type: 'cookie',
attributes: {
login_name: 'foo',
password: 'bar',
},
});
return [200, {}, '{}'];
});
const creds = {
identification: 'foo',
password: 'bar',
@@ -67,8 +66,8 @@ module('Unit | Authenticator | password', function (hooks) {
test('it rejects if the endpoint sends an error status code', async function (assert) {
assert.expect(1);
const authenticator = this.owner.lookup('authenticator:password');
this.server.post(authenticator.buildAuthEndpointURL(), () => {
return new Response(400);
server.post(authenticator.buildAuthEndpointURL(), () => {
return [400];
});
await authenticator.authenticate({}).catch(() => {
assert.ok(true, 'authentication failed');