Update tests

This commit is contained in:
Yarmo Mackenbach 2021-04-19 13:38:09 +02:00
parent 5f5f663e4b
commit 841b90d580
No known key found for this signature in database
GPG key ID: 37367F4AF4087AD1
6 changed files with 138 additions and 181 deletions

94
test/claimDefinitions.js Normal file
View file

@ -0,0 +1,94 @@
/*
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.
*/
const chai = require('chai')
const expect = chai.expect
const chaiMatchPattern = require('chai-match-pattern')
chai.use(chaiMatchPattern)
chai.use(require('chai-as-promised'))
const _ = chaiMatchPattern.getLodashModule()
const doipjs = require('../src')
const pattern = {
serviceprovider: {
type: _.isString,
name: _.isString,
},
match: {
regularExpression: _.isRegExp,
isAmbiguous: _.isBoolean,
},
profile: {
display: _.isString,
uri: _.isString,
qr: (x) => {
return _.isString(x) || _.isNull(x)
},
},
proof: {
uri: (x) => {
return _.isString(x) || _.isNull(x)
},
request: {
fetcher: _.isString,
access: _.isInteger,
format: _.isString,
data: _.isObject,
},
},
claim: {
format: _.isInteger,
relation: _.isInteger,
path: _.isArray,
},
}
doipjs.claimDefinitions.list.forEach((claimDefName, i) => {
const claimDef = doipjs.claimDefinitions.data[claimDefName]
describe(`claimDefinitions.${claimDefName}`, () => {
it('should be an object', () => {
expect(claimDef).to.be.a('object')
})
it('should have a RegExp instance named "reURI"', () => {
expect(claimDef.reURI).to.be.instanceof(RegExp)
})
it('should have a function named "processURI" (1 argument)', () => {
expect(claimDef.processURI).to.be.a('function')
expect(claimDef.processURI).to.have.length(1)
})
it('should have an array named "tests"', () => {
expect(claimDef.tests).to.be.instanceof(Array)
})
claimDef.tests.forEach((test, j) => {
if (test.shouldMatch) {
it(`should match "${test.uri}"`, () => {
expect(claimDef.reURI.test(test.uri)).to.be.true
})
it(`should return a valid object for "${test.uri}"`, async () => {
const obj = claimDef.processURI(claimDef.tests[0].uri)
expect(obj).to.be.a('object')
expect(obj).to.matchPattern(pattern)
})
} else {
it(`should not match "${test.uri}"`, () => {
expect(claimDef.reURI.test(test.uri)).to.be.false
})
}
})
})
})

View file

@ -1,86 +0,0 @@
/*
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.
*/
const chai = require('chai')
const expect = chai.expect
const chaiMatchPattern = require('chai-match-pattern')
chai.use(chaiMatchPattern)
chai.use(require('chai-as-promised'))
const _ = chaiMatchPattern.getLodashModule()
const doipjs = require('../src')
const pattern = {
serviceprovider: {
type: _.isString,
name: _.isString,
},
profile: {
display: _.isString,
uri: _.isString,
qr: (x) => {
return _.isString(x) || _.isNull(x)
},
},
proof: {
uri: (x) => {
return _.isString(x) || _.isNull(x)
},
request: {
fetcher: _.isString,
access: _.isInteger,
format: _.isInteger,
data: _.isObject,
},
},
claim: {
fingerprint: (x) => {
return _.isString(x) || _.isNull(x)
},
format: _.isInteger,
relation: _.isInteger,
path: _.isArray,
},
}
describe('claims.verify', () => {
it('should be a function (3 arguments)', () => {
expect(doipjs.claims.verify).to.be.a('function')
expect(doipjs.claims.verify).to.have.length(3)
})
it('should return undefined for non-valid URIs', () => {
return expect(doipjs.claims.verify('noURI')).to.eventually.be.rejectedWith(
undefined
)
})
doipjs.serviceproviders.list.forEach((spName, i) => {
const sp = doipjs.serviceproviders.data[spName]
if (sp.tests.length == 0) {
return
}
it(`should return a valid object for the "${spName}" service provider`, async () => {
const matches = await doipjs.claims.verify(sp.tests[0].uri, null, {
returnMatchesOnly: true,
})
expect(matches).to.be.a('array')
expect(matches).to.be.length.above(0)
expect(matches[0].serviceprovider.name).to.be.equal(spName)
expect(matches[0]).to.matchPattern(pattern)
})
})
})

View file

@ -117,32 +117,11 @@ describe('keys.process', () => {
it('should return an object with specific keys', async () => {
const pubKey = await doipjs.keys.fetch.plaintext(pubKeyPlaintext)
const obj = await doipjs.keys.process(pubKey)
expect(obj).to.have.keys(['users', 'fingerprint', 'primaryUserIndex'])
})
})
describe('keys.getFingerprint', () => {
it('should be a function (1 argument)', () => {
expect(doipjs.keys.getFingerprint).to.be.a('function')
expect(doipjs.keys.getFingerprint).to.have.length(1)
})
it('should return a string', async () => {
const pubKey = await doipjs.keys.fetch.plaintext(pubKeyPlaintext)
const fp = await doipjs.keys.getFingerprint(pubKey)
expect(fp).to.be.equal(pubKeyFingerprint)
})
})
describe('keys.getUserData', () => {
it('should be a function (1 argument)', () => {
expect(doipjs.keys.getUserData).to.be.a('function')
expect(doipjs.keys.getUserData).to.have.length(1)
})
it('should return an array of userData objects', async () => {
const pubKey = await doipjs.keys.fetch.plaintext(pubKeyPlaintext)
const userData = await doipjs.keys.getUserData(pubKey)
expect(userData).to.be.lengthOf(1)
expect(userData[0].userData.email).to.be.equal(pubKeyEmail)
expect(userData[0].notations[0]).to.be.equal('dns:doip.rocks')
expect(obj).to.have.keys([
'users',
'fingerprint',
'primaryUserIndex',
'key',
])
})
})

View file

@ -1,51 +0,0 @@
/*
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.
*/
const chai = require('chai')
const expect = chai.expect
const doipjs = require('../src')
doipjs.serviceproviders.list.forEach((sp, i) => {
describe(`serviceproviders.${sp}`, () => {
it('should be an object', () => {
expect(doipjs.serviceproviders.data[sp]).to.be.a('object')
})
it('should have a RegExp instance named "reURI"', () => {
expect(doipjs.serviceproviders.data[sp].reURI).to.be.instanceof(RegExp)
})
it('should have a function named "processURI" (2 arguments)', () => {
expect(doipjs.serviceproviders.data[sp].processURI).to.be.a('function')
expect(doipjs.serviceproviders.data[sp].processURI).to.have.length(2)
})
it('should have an array named "tests"', () => {
expect(doipjs.serviceproviders.data[sp].tests).to.be.instanceof(Array)
})
doipjs.serviceproviders.data[sp].tests.forEach((test, j) => {
if (test.shouldMatch) {
it(`should match "${test.uri}"`, () => {
expect(doipjs.serviceproviders.data[sp].reURI.test(test.uri)).to.be
.true
})
} else {
it(`should not match "${test.uri}"`, () => {
expect(doipjs.serviceproviders.data[sp].reURI.test(test.uri)).to.be
.false
})
}
})
})
})

View file

@ -41,18 +41,16 @@ cXbjvHSGniZ7M3S9S8knAfIquPvTp7+L7wWgSSB5VObPp1r+96n87hyFZUp7PCvl
=ADl+
-----END PGP SIGNATURE-----`
describe('signatures.verify', () => {
describe('signatures.process', () => {
it('should be a function (2 arguments)', () => {
expect(doipjs.signatures.verify).to.be.a('function')
expect(doipjs.signatures.verify).to.have.length(2)
expect(doipjs.signatures.process).to.be.a('function')
expect(doipjs.signatures.process).to.have.length(1)
})
it('should verify the demonstration signature', async () => {
const verification = await doipjs.signatures.verify(sigProfile)
expect(verification.errors).to.be.length(0)
expect(verification.publicKey.fingerprint).to.be.equal(
const verification = await doipjs.signatures.process(sigProfile)
expect(verification.fingerprint).to.be.equal(
'3637202523e7c1309ab79e99ef2dc5827b445f4b'
)
expect(verification.claims).to.be.length(1)
expect(verification.claims[0].isVerified).to.be.true
expect(verification.users[0].claims).to.be.length(1)
})
})

View file

@ -24,18 +24,41 @@ describe('utils.generateClaim', () => {
expect(doipjs.utils.generateClaim).to.have.length(2)
})
it('should generate a correct "uri" claim', () => {
expect(doipjs.utils.generateClaim('123456789', 'uri')).to.equal(
'openpgp4fpr:123456789'
)
expect(
doipjs.utils.generateClaim('123456789', doipjs.enums.ClaimFormat.URI)
).to.equal('openpgp4fpr:123456789')
})
it('should generate a correct "message" claim', () => {
expect(doipjs.utils.generateClaim('123456789', 'message')).to.equal(
'[Verifying my OpenPGP key: openpgp4fpr:123456789]'
)
expect(
doipjs.utils.generateClaim('123456789', doipjs.enums.ClaimFormat.MESSAGE)
).to.equal('[Verifying my OpenPGP key: openpgp4fpr:123456789]')
})
it('should generate a correct "fingerprint" claim', () => {
expect(doipjs.utils.generateClaim('123456789', 'fingerprint')).to.equal(
'123456789'
expect(
doipjs.utils.generateClaim(
'123456789',
doipjs.enums.ClaimFormat.FINGERPRINT
)
).to.equal('123456789')
})
})
describe('utils.generateProxyURL', () => {
it('should be a function (3 arguments)', () => {
expect(doipjs.utils.generateProxyURL).to.be.a('function')
expect(doipjs.utils.generateProxyURL).to.have.length(3)
})
it('should generate correct proxy URLs', () => {
const opts = {
proxy: {
hostname: 'localhost',
},
}
expect(
doipjs.utils.generateProxyURL('http', { domain: 'domain.org' }, opts)
).to.equal('http://localhost/api/2/get/http?domain=domain.org')
expect(
doipjs.utils.generateProxyURL('dns', { domain: 'domain.org' }, opts)
).to.equal('http://localhost/api/2/get/dns?domain=domain.org')
})
})