Files

77 lines
2.3 KiB
JavaScript

import { module, test } from 'qunit';
import {
normalizeDiscourseUrl,
selectDecryptedIntegration,
discourseProvider,
} from 'dashtab/auth/providers/discourse';
module('Unit | Auth | providers | discourse', function () {
test('normalizeDiscourseUrl adds a scheme and rejects empty input', function (assert) {
assert.strictEqual(
normalizeDiscourseUrl('forum.example.com'),
'https://forum.example.com'
);
assert.strictEqual(
normalizeDiscourseUrl('http://forum.example.com'),
'http://forum.example.com'
);
assert.strictEqual(normalizeDiscourseUrl(' '), null);
});
test('selects the candidate whose payload nonce matches', function (assert) {
const first = { key: 'discourse-a', data: { nonce: 'nonce-a' } };
const second = { key: 'discourse-b', data: { nonce: 'nonce-b' } };
const decrypt = (candidate) => {
if (candidate === first) {
throw new Error('wrong key');
}
return { key: 'KEY', nonce: 'nonce-b' };
};
const match = selectDecryptedIntegration([first, second], decrypt);
assert.strictEqual(match.candidate, second);
assert.strictEqual(match.apiData.key, 'KEY');
});
test('skips a candidate whose nonce does not match', function (assert) {
const candidate = { key: 'discourse-a', data: { nonce: 'nonce-a' } };
const match = selectDecryptedIntegration([candidate], () => ({
key: 'KEY',
nonce: 'other',
}));
assert.strictEqual(match, null);
});
test('returns null when nothing decrypts', function (assert) {
const candidate = { key: 'discourse-a', data: { nonce: 'nonce-a' } };
const match = selectDecryptedIntegration([candidate], () => {
throw new Error('cannot decrypt');
});
assert.strictEqual(match, null);
});
test('accepts a payload without a nonce', function (assert) {
const candidate = { key: 'discourse-a', data: { nonce: 'nonce-a' } };
const match = selectDecryptedIntegration([candidate], () => ({
key: 'KEY',
}));
assert.strictEqual(match.candidate, candidate);
});
test('exposes its id and web callback path', function (assert) {
assert.strictEqual(discourseProvider.id, 'discourse');
assert.strictEqual(
discourseProvider.webCallbackPath,
'/auth/discourse/callback'
);
});
});