mirror of
https://codeberg.org/keyoxide/doipjs.git
synced 2024-12-22 06:29:28 -07:00
198 lines
8.3 KiB
JavaScript
198 lines
8.3 KiB
JavaScript
/*
|
|
Copyright 2021 Yarmo Mackenbach
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
import { expect, use } from 'chai'
|
|
import chaiAsPromised from 'chai-as-promised'
|
|
use(chaiAsPromised)
|
|
|
|
import { openpgp, Profile } from '../src/index.js'
|
|
|
|
const pubKeyFingerprint = "3637202523e7c1309ab79e99ef2dc5827b445f4b"
|
|
const pubKeyEmail = "test@doip.rocks"
|
|
|
|
const pubKeyPlaintext = `-----BEGIN PGP PUBLIC KEY BLOCK-----
|
|
|
|
mQGNBF+036UBDACoxWRdp7rBAFB2l/+dxX0XA50NJC92EEacB5L0TnC0lP/MsNHv
|
|
fAv/A9vgTwrPudvcHdE/urAjQswfIU3LpFxbBOWNYWOv6ssrzBH4vVGMyxfu2GGu
|
|
b2mxjWj0eWXnWXnzkO5fscX2y0HqNjBZjDSkYohHZJTbz91NnxK3a8+Erpk+sgEH
|
|
hQH1h75SfaW6GZucuhenxgjwEiGz84UEVS0AEWD9yNgfWCsK/6HuIRnv5Jv5V9z9
|
|
bx9Ik7QNGBks3tpNmdbeaaadkHYZpF3Fm8mCoIt2+Xx9OvyuLssZnVkuQdj8C2/z
|
|
E45If4+pHRnRcCWXpDrHUWoJaeyGuTq5triePI6h/4lgr/m/du0O/lhOrr6MUhAe
|
|
7xc0B+X+bTF/balZmmlbk5bnDoZMzdH8caui5XrkuRif/I0nYPRnc9zrqWJDDO/p
|
|
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|
-----END PGP PUBLIC KEY BLOCK-----`
|
|
|
|
describe('openpgp.fetch', () => {
|
|
it('should be a function (1 argument)', () => {
|
|
expect(openpgp.fetch).to.be.a('function')
|
|
expect(openpgp.fetch).to.have.length(1)
|
|
})
|
|
it('should return a Key object when provided a valid fingerprint', async () => {
|
|
expect(
|
|
await openpgp.fetch(pubKeyFingerprint)
|
|
).to.be.instanceOf(Profile)
|
|
}).timeout('12s')
|
|
it('should return a Key object when provided a valid email address', async () => {
|
|
expect(
|
|
await openpgp.fetch(pubKeyEmail)
|
|
).to.be.instanceOf(Profile)
|
|
}).timeout('12s')
|
|
it('should reject when provided an invalid email address', () => {
|
|
return expect(
|
|
openpgp.fetch('invalid@doip.rocks')
|
|
).to.eventually.be.rejectedWith('Key does not exist or could not be fetched')
|
|
}).timeout('12s')
|
|
})
|
|
|
|
describe('openpgp.fetchURI', () => {
|
|
it('should be a function (1 argument)', () => {
|
|
expect(openpgp.fetchURI).to.be.a('function')
|
|
expect(openpgp.fetchURI).to.have.length(1)
|
|
})
|
|
it('should return a Key object when provided a hkp: uri', async () => {
|
|
expect(
|
|
await openpgp.fetchURI(`hkp:${pubKeyFingerprint}`)
|
|
).to.be.instanceOf(Profile)
|
|
}).timeout('12s')
|
|
it('should reject when provided an invalid uri', () => {
|
|
return expect(
|
|
openpgp.fetchURI(`inv:${pubKeyFingerprint}`)
|
|
).to.eventually.be.rejectedWith('Invalid URI protocol')
|
|
}).timeout('12s')
|
|
})
|
|
|
|
describe('openpgp.fetchHKP', () => {
|
|
it('should be a function (1 required argument, 1 optional argument)', () => {
|
|
expect(openpgp.fetchHKP).to.be.a('function')
|
|
expect(openpgp.fetchHKP).to.have.length(1)
|
|
})
|
|
it('should return a Key object when provided a valid fingerprint', async () => {
|
|
expect(await openpgp.fetchHKP(pubKeyFingerprint)).to.be.instanceOf(
|
|
Profile
|
|
)
|
|
}).timeout('12s')
|
|
it('should return a Key object when provided a valid email address', async () => {
|
|
expect(await openpgp.fetchHKP(pubKeyEmail)).to.be.instanceOf(
|
|
Profile
|
|
)
|
|
}).timeout('12s')
|
|
it('should reject when provided an invalid fingerprint', async () => {
|
|
return expect(
|
|
openpgp.fetchHKP('4637202523e7c1309ab79e99ef2dc5827b445f4b')
|
|
).to.eventually.be.rejectedWith(
|
|
'Key does not exist or could not be fetched'
|
|
)
|
|
}).timeout('12s')
|
|
it('should reject when provided an invalid email address', async () => {
|
|
return expect(
|
|
openpgp.fetchHKP('invalid@doip.rocks')
|
|
).to.eventually.be.rejectedWith(
|
|
'Key does not exist or could not be fetched'
|
|
)
|
|
}).timeout('12s')
|
|
})
|
|
|
|
describe('openpgp.fetchPlaintext', () => {
|
|
it('should be a function (1 argument)', () => {
|
|
expect(openpgp.fetchPlaintext).to.be.a('function')
|
|
expect(openpgp.fetchPlaintext).to.have.length(1)
|
|
})
|
|
it('should return a Key object', async () => {
|
|
expect(await openpgp.fetchPlaintext(pubKeyPlaintext)).to.be.instanceOf(
|
|
Profile
|
|
)
|
|
}).timeout('12s')
|
|
})
|
|
|
|
describe('openpgp.parsePublicKey', () => {
|
|
it('should be a function (1 argument)', () => {
|
|
expect(openpgp.parsePublicKey).to.be.a('function')
|
|
expect(openpgp.parsePublicKey).to.have.length(1)
|
|
})
|
|
it('should return an object with specific openpgp', async () => {
|
|
const pubKey = await openpgp.fetchPlaintext(pubKeyPlaintext)
|
|
const profile = await openpgp.parsePublicKey(pubKey.publicKey.key)
|
|
expect(profile).to.be.instanceOf(Profile)
|
|
})
|
|
it('should ignore non-proof notations', async () => {
|
|
const pubKey = await openpgp.fetchPlaintext(pubKeyWithOtherNotations)
|
|
const profile = await openpgp.parsePublicKey(pubKey.publicKey.key)
|
|
expect(profile.personas).to.be.lengthOf(1)
|
|
expect(profile.personas[0].claims).to.be.lengthOf(1)
|
|
expect(profile.personas[0].claims[0].uri).to.be.equal('dns:yarmo.eu?type=TXT')
|
|
})
|
|
it('should properly handle revoked UIDs', async () => {
|
|
const pubKey = await openpgp.fetchPlaintext(pubKeyWithRevokedUID)
|
|
const profile = await openpgp.parsePublicKey(pubKey.publicKey.key)
|
|
expect(profile.personas).to.be.lengthOf(2)
|
|
expect(profile.personas[0].isRevoked).to.be.true
|
|
expect(profile.personas[1].isRevoked).to.be.false
|
|
})
|
|
})
|