keyoxide-web/test/server.test.js

120 lines
4 KiB
JavaScript
Raw Normal View History

2022-03-03 08:25:49 -07:00
import 'chai/register-should.js'
import esmock from 'esmock'
import * as doipjs from 'doipjs'
2022-11-17 14:17:42 -07:00
import * as utils from '../src/server/utils.js'
2022-03-03 08:25:49 -07:00
const _env = Object.assign({},process.env)
2022-03-03 08:25:49 -07:00
describe('server', function () {
describe('utils', function () {
describe('computeWKDLocalPart()', function () {
it('should handle "test"', async function () {
const local = await utils.computeWKDLocalPart('test')
local.should.equal('iffe93qcsgp4c8ncbb378rxjo6cn9q6u')
})
it('should handle "zaphod"', async function () {
const local = await utils.computeWKDLocalPart('zaphod')
local.should.equal('xrea5za9y9auaxq463c9opxt338bnaxu')
})
})
describe('encodeZBase32()', function () {
it('should handle a Uint8Array', async function () {
const data = new Uint8Array([
169, 74, 143, 229, 204, 177,
155, 166, 28, 76, 8, 115,
211, 145, 233, 135, 152, 47,
187, 211
])
const local = utils.encodeZBase32(data)
local.should.equal('iffe93qcsgp4c8ncbb378rxjo6cn9q6u')
})
})
})
2023-07-04 09:43:41 -06:00
// 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 () {
2023-07-04 09:43:41 -06:00
describe('generateHKPProfile()', function() {
2023-07-04 09:43:41 -06:00
let index;
let fingerprint;
/** @type {import('doipjs').Profile */
let profile;
2023-07-04 09:43:41 -06:00
this.beforeEach(async () => {
// Common arrangement pieces that don't change per test
fingerprint = '79895B2E0F87503F1DDE80B649765D7F0DDD9BD5'
process.env.DOMAIN = "keyoxide.org"
const persona = new doipjs.Persona("test", [new doipjs.Claim('dns:domain.tld?type=TXT')])
profile = new doipjs.Profile(doipjs.enums.ProfileType.OPENPGP, fingerprint, [persona])
2023-07-04 09:43:41 -06:00
// 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/openpgpProfiles.js': {
fetchHKP: () => {
return Promise.resolve(profile)
}
},
'libravatar': {
get_avatar_url: () => {
return "example.org/avatar.png"
}
}
})
2023-07-04 09:43:41 -06:00
})
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.verifiers[0].url.should.equal(`https://keyoxide.org/hkp/${fingerprint}`)
})
2023-07-04 09:43:41 -06:00
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.verifiers[0].url.should.equal(`http://keyoxide.org/hkp/${fingerprint}`)
2023-07-04 09:43:41 -06:00
})
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.verifiers[0].url.should.equal(`https://keyoxide.org/hkp/${fingerprint}`)
2023-07-04 09:43:41 -06:00
})
})
})
})