diff --git a/package.json b/package.json index 35b6eb4..06b33ac 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", @@ -44,7 +45,7 @@ "scripts": { "start": "node --experimental-fetch ./", "dev": "LOG_LEVEL=debug yarn run watch & yarn run build:static:dev", - "test": "yarn run standard:check && yarn run rome:check && mocha", + "test": "yarn run standard:check && yarn run rome:check && mocha --loader=esmock", "watch": "./node_modules/.bin/nodemon --config nodemon.json ./", "build": "yarn run build:server & yarn run build:static", "build:server": "ncc build ./src/index.js -o dist", diff --git a/src/server/index.js b/src/server/index.js index d35dbc3..eeef49c 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -32,12 +32,6 @@ import * as doipjs from 'doipjs' import { fetchWKD, fetchHKP, fetchSignature, fetchKeybase } from './keys.js' import libravatar from 'libravatar' -const scheme = process.env.PROXY_SCHEME - ? process.env.PROXY_SCHEME - : process.env.SCHEME - ? process.env.SCHEME - : 'https' - const generateWKDProfile = async (id) => { logger.debug('Generating a WKD profile', { component: 'wkd_profile_generator', action: 'start', profile_id: id }) @@ -52,7 +46,7 @@ const generateWKDProfile = async (id) => { keyData = processKeyData(keyData) const keyoxideData = {} - keyoxideData.url = `${scheme}://${process.env.DOMAIN}/wkd/${id}` + keyoxideData.url = `${getScheme()}://${process.env.DOMAIN}/wkd/${id}` logger.debug('Generating a WKD profile', { component: 'wkd_profile_generator', action: 'done', profile_id: id }) @@ -94,9 +88,9 @@ const generateHKPProfile = async (id, keyserverDomain) => { const keyoxideData = {} if (!keyserverDomain || keyserverDomain === 'keys.openpgp.org') { - keyoxideData.url = `${scheme}://${process.env.DOMAIN}/hkp/${id}` + keyoxideData.url = `${getScheme()}://${process.env.DOMAIN}/hkp/${id}` } else { - keyoxideData.url = `${scheme}://${process.env.DOMAIN}/hkp/${keyserverDomain}/${id}` + keyoxideData.url = `${getScheme()}://${process.env.DOMAIN}/hkp/${keyserverDomain}/${id}` } logger.debug('Generating a HKP profile', @@ -202,7 +196,7 @@ const generateKeybaseProfile = async (username, fingerprint) => { keyData = processKeyData(keyData) const keyoxideData = {} - keyoxideData.url = `${scheme}://${process.env.DOMAIN}/keybase/${username}/${fingerprint}` + keyoxideData.url = `${getScheme()}://${process.env.DOMAIN}/keybase/${username}/${fingerprint}` logger.debug('Generating a Keybase profile', { component: 'keybase_profile_generator', action: 'done', username, fingerprint }) @@ -271,6 +265,14 @@ const computeExtraData = async (key, keyData) => { } } +const getScheme = () => { + return process.env.PROXY_SCHEME + ? process.env.PROXY_SCHEME + : process.env.SCHEME + ? process.env.SCHEME + : 'https' +} + export { generateWKDProfile } export { generateHKPProfile } export { generateAutoProfile } diff --git a/test/server.test.js b/test/server.test.js index 42185f9..192d53f 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,107 @@ describe('server', function () { }) }) }) + + // NOTE: This is necessarily brittle. If these tests fail + // in the future, start looking here for what new behaviour + // in the implementation is or isn't getting mocked + // appropriately. + describe('index', function () { + + describe('generateHKPProfile()', function() { + + let index; + let fingerprint; + + this.beforeEach(async () => { + + // Common arrangement pieces that don't change per test + fingerprint = '79895B2E0F87503F1DDE80B649765D7F0DDD9BD5' + process.env.DOMAIN = "keyoxide.org" + + // mock the appropriate pieces of our dependencies so we + // can test just the `keyoxide.url` return value. + index = await esmock('../src/server/index.js', { + '../src/server/keys.js': { + fetchHKP: () => { + return Promise.resolve({ + publicKey: { + getPrimaryUser: () => { + return { + user: { + userID: { + email: "example@example.net" + } + } + } + } + }, + fetchURL: 'example.com' + }) + } + }, + 'doipjs': { + keys: { + process: () => { + return { + key: {}, + 'fingerprint': fingerprint, + users: [] + } + } + } + }, + 'libravatar': { + get_avatar_url: () => { + return "example.org/avatar.png" + } + } + }) + }) + + this.afterEach(() => { + process.env = _env + }) + + it('should handle implicit scheme for keyoxide URL', async function () { + + // Arrange + // no setting process.env.SCHEME + + // Act + const local = await index.generateHKPProfile(fingerprint) + + // Assert + local.keyoxide.url.should.equal(`https://keyoxide.org/hkp/${fingerprint}`) + + }) + + it('should handle explicit http scheme for keyoxide URL', async function () { + + // Arrange + process.env.SCHEME = "http" + + // Act + const local = await index.generateHKPProfile(fingerprint) + + // Assert + local.keyoxide.url.should.equal(`http://keyoxide.org/hkp/${fingerprint}`) + + }) + + it('should handle explicit https scheme for keyoxide URL', async function () { + + // Arrange + process.env.SCHEME = "https" + + // 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"