diff --git a/package.json b/package.json index 35b6eb4..088dee9 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "chai": "^4.3.6", "copy-webpack-plugin": "^10.2.4", "css-loader": "^6.6.0", + "esmock": "^2.3.1", "license-check-and-add": "^4.0.5", "mini-css-extract-plugin": "^2.5.3", "mocha": "^10.1.0", diff --git a/test/server.test.js b/test/server.test.js index 42185f9..c19ea92 100644 --- a/test/server.test.js +++ b/test/server.test.js @@ -1,6 +1,10 @@ import 'chai/register-should.js' +import esmock from 'esmock' + import * as utils from '../src/server/utils.js' +const _env = Object.assign({},process.env) + describe('server', function () { describe('utils', function () { describe('computeWKDLocalPart()', function () { @@ -26,4 +30,63 @@ describe('server', function () { }) }) }) + describe('index', function () { + + // Brittle mocking :( + describe('generateHKPProfile', function() { + + it('should handle implicit scheme with implicit keys.openpgp.org keyserver', async function () { + + // Arrange + const fingerprint = '79895B2E0F87503F1DDE80B649765D7F0DDD9BD5' + // the process.env needs to be here, before the esmock setup. + process.env.DOMAIN = "keyoxide.org" + //process.env.SCHEME = "http" + + const index = await esmock('../src/server/index.js', { + '../src/server/keys.js': { + fetchHKP: () => { + return Promise.resolve({ + publicKey: { + getPrimaryUser: () => { + return { + user: { + userID: { + email: "example@example.org" + } + } + } + } + }, + fetchURL: 'example.com' + }) + } + }, + 'doipjs': { + keys: { + process: () => { + return { + key: {}, + 'fingerprint': fingerprint, + users: [] + } + } + } + }, + 'libravatar': { + get_avatar_url: () => { + return "example.org/avatar.png" + } + } + }) + + // Act + const local = await index.generateHKPProfile(fingerprint) + + // Assert + local.keyoxide.url.should.equal(`https://keyoxide.org/hkp/${fingerprint}`) + + }) + }) + }) }) \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 19a39c0..3614114 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2204,6 +2204,11 @@ eslint@^8.13.0: strip-json-comments "^3.1.0" text-table "^0.2.0" +esmock@^2.3.1: + version "2.3.1" + resolved "https://registry.yarnpkg.com/esmock/-/esmock-2.3.1.tgz#27a3afea73d7fb352f27c7ac04f66cfbd2c34316" + integrity sha512-ZxuxfhwGSlStiJFbw6Z+a70fB6SutTcUr0X8dhehx6aqiC5kgBvEYV4xNW94cKaD8gaqD7P00RjBH/pfao2CQA== + espree@^9.4.0: version "9.4.1" resolved "https://registry.yarnpkg.com/espree/-/espree-9.4.1.tgz#51d6092615567a2c2cff7833445e37c28c0065bd"