Use esmock for mocking dependencies

This commit is contained in:
Preston Maness 2023-07-04 10:43:41 -05:00
parent f5ef4b6623
commit f74260f79b
3 changed files with 66 additions and 20 deletions

View file

@ -45,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",

View file

@ -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 }

View file

@ -30,20 +30,27 @@ 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 () {
// Brittle mocking :(
describe('generateHKPProfile', function() {
describe('generateHKPProfile()', function() {
it('should handle implicit scheme with implicit keys.openpgp.org keyserver', async function () {
let index;
let fingerprint;
// Arrange
const fingerprint = '79895B2E0F87503F1DDE80B649765D7F0DDD9BD5'
// the process.env needs to be here, before the esmock setup.
this.beforeEach(async () => {
// Common arrangement pieces that don't change per test
fingerprint = '79895B2E0F87503F1DDE80B649765D7F0DDD9BD5'
process.env.DOMAIN = "keyoxide.org"
//process.env.SCHEME = "http"
const index = await esmock('../src/server/index.js', {
// 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({
@ -52,7 +59,7 @@ describe('server', function () {
return {
user: {
userID: {
email: "example@example.org"
email: "example@example.net"
}
}
}
@ -79,6 +86,16 @@ describe('server', function () {
}
}
})
})
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)
@ -87,6 +104,33 @@ describe('server', function () {
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}`)
})
})
})
})