2023-03-01 09:39:41 -06:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2020-07-24 11:15:40 -04:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
|
import { setupTest } from 'ember-qunit';
|
2022-09-01 12:40:00 -07:00
|
|
|
import Pretender from 'pretender';
|
2020-07-24 11:15:40 -04:00
|
|
|
|
|
|
|
|
module('Unit | Authenticator | password', function (hooks) {
|
|
|
|
|
setupTest(hooks);
|
2022-09-01 12:40:00 -07:00
|
|
|
|
|
|
|
|
let server;
|
|
|
|
|
|
|
|
|
|
hooks.beforeEach(() => {
|
|
|
|
|
server = new Pretender();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
hooks.afterEach(() => {
|
|
|
|
|
server.shutdown();
|
|
|
|
|
});
|
2020-07-24 11:15:40 -04:00
|
|
|
|
|
|
|
|
test('it authenticates to the specified authEndpoint', async function (assert) {
|
|
|
|
|
assert.expect(2);
|
|
|
|
|
const authenticator = this.owner.lookup('authenticator:password');
|
2022-09-01 12:40:00 -07:00
|
|
|
server.post(authenticator.buildAuthEndpointURL(), (request) => {
|
|
|
|
|
const json = JSON.parse(request.requestBody);
|
|
|
|
|
assert.ok(json.type, 'Requested token cookies by default');
|
|
|
|
|
return [200, {}, '{}'];
|
|
|
|
|
});
|
2020-07-24 11:29:19 -04:00
|
|
|
await authenticator.authenticate({}).then(() => {
|
2020-07-24 11:15:40 -04:00
|
|
|
assert.ok(true, 'authentication succeeded');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('it can authenticate without requesting cookies', async function (assert) {
|
|
|
|
|
assert.expect(2);
|
|
|
|
|
const authenticator = this.owner.lookup('authenticator:password');
|
2022-09-01 12:40:00 -07:00
|
|
|
server.post(authenticator.buildAuthEndpointURL(), (request) => {
|
|
|
|
|
const json = JSON.parse(request.requestBody);
|
|
|
|
|
assert.notOk(json.type, 'Did not request tokens cookies');
|
|
|
|
|
return [200, {}, '{}'];
|
|
|
|
|
});
|
2020-07-24 11:29:19 -04:00
|
|
|
await authenticator.authenticate({}, false).then(() => {
|
2020-07-24 11:15:40 -04:00
|
|
|
assert.ok(true, 'authentication succeeded');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('it authenticates with the expected payload', async function (assert) {
|
|
|
|
|
assert.expect(1);
|
|
|
|
|
const authenticator = this.owner.lookup('authenticator:password');
|
2022-09-01 12:40:00 -07:00
|
|
|
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, {}, '{}'];
|
|
|
|
|
});
|
2020-07-24 11:15:40 -04:00
|
|
|
const creds = {
|
|
|
|
|
identification: 'foo',
|
|
|
|
|
password: 'bar',
|
|
|
|
|
};
|
2020-07-24 11:29:19 -04:00
|
|
|
await authenticator.authenticate(creds);
|
2020-07-24 11:15:40 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('it rejects if the endpoint sends an error status code', async function (assert) {
|
|
|
|
|
assert.expect(1);
|
|
|
|
|
const authenticator = this.owner.lookup('authenticator:password');
|
2022-09-01 12:40:00 -07:00
|
|
|
server.post(authenticator.buildAuthEndpointURL(), () => {
|
|
|
|
|
return [400];
|
2020-07-24 11:15:40 -04:00
|
|
|
});
|
|
|
|
|
await authenticator.authenticate({}).catch(() => {
|
|
|
|
|
assert.ok(true, 'authentication failed');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|