doipjs/test/verify.test.js

87 lines
3.3 KiB
JavaScript
Raw Normal View History

2020-10-23 14:35:53 -06:00
/*
2020-10-24 07:11:14 -06:00
Copyright 2020 Yarmo Mackenbach
2020-10-23 14:35:53 -06:00
2020-10-24 07:11:14 -06:00
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
2020-10-23 14:35:53 -06:00
2020-10-24 07:11:14 -06:00
http://www.apache.org/licenses/LICENSE-2.0
2020-10-23 14:35:53 -06:00
2020-10-24 07:11:14 -06:00
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.
2020-10-23 14:35:53 -06:00
*/
2020-10-24 12:33:48 -06:00
const chai = require('chai')
const expect = chai.expect
2020-10-24 15:36:25 -06:00
const chaiMatchPattern = require('chai-match-pattern')
chai.use(chaiMatchPattern)
2020-10-24 14:28:42 -06:00
chai.use(require("chai-as-promised"))
2020-10-24 12:33:48 -06:00
2020-10-24 15:36:25 -06:00
const _ = chaiMatchPattern.getLodashModule()
2020-10-23 14:35:53 -06:00
const doipjs = require('../src')
2020-10-24 15:36:25 -06:00
const pattern = {
serviceprovider: {
type: _.isString,
name: _.isString
},
profile: {
display: _.isString,
uri: _.isString
},
proof: {
uri: (x) => { return _.isString(x) || _.isNull(x) },
fetch: (x) => { return _.isString(x) || _.isNull(x) },
useProxy: _.isBoolean,
format: _.isString
},
claim: {
fingerprint: (x) => { return _.isString(x) || _.isNull(x) },
format: _.isString,
path: _.isArray,
relation: _.isString
},
qr: (x) => { return _.isString(x) || _.isNull(x) }
}
describe('verify', () => {
it('should be a function (3 arguments)', () => {
2020-10-23 14:35:53 -06:00
expect(doipjs.verify).to.be.a('function')
expect(doipjs.verify).to.have.length(3)
})
2020-10-24 03:56:30 -06:00
it('should throw an error for non-valid URIs', () => {
2020-10-24 12:33:48 -06:00
return expect(doipjs.verify('noURI')).to.eventually.be.rejectedWith('Not a valid URI')
return expect(doipjs.verify('domain.org')).to.eventually.be.rejectedWith('Not a valid URI')
2020-10-24 03:56:30 -06:00
})
2020-10-24 12:33:48 -06:00
it('should match "dns:domain.org" to the DNS service provider', async () => {
const matches = await doipjs.verify('dns:domain.org', null, {returnMatchesOnly: true})
expect(matches).to.be.a('array')
expect(matches).to.be.length(1)
expect(matches[0].serviceprovider.name).to.be.equal('domain')
2020-10-24 15:36:25 -06:00
expect(matches[0]).to.matchPattern(pattern)
})
2020-10-24 12:33:48 -06:00
it('should match "xmpp:alice@domain.org" to the XMPP service provider', async () => {
const matches = await doipjs.verify('xmpp:alice@domain.org', null, {returnMatchesOnly: true})
expect(matches).to.be.a('array')
expect(matches).to.be.length(1)
expect(matches[0].serviceprovider.name).to.be.equal('xmpp')
2020-10-24 15:36:25 -06:00
expect(matches[0]).to.matchPattern(pattern)
})
2020-10-24 12:33:48 -06:00
it('should match "https://twitter.com/alice/status/1234567890123456789" to the Twitter service provider', async () => {
const matches = await doipjs.verify('https://twitter.com/alice/status/1234567890123456789', null, {returnMatchesOnly: true})
2020-10-24 07:33:51 -06:00
expect(matches).to.be.a('array')
expect(matches).to.be.length(1)
expect(matches[0].serviceprovider.name).to.be.equal('twitter')
2020-10-24 15:36:25 -06:00
expect(matches[0]).to.matchPattern(pattern)
2020-10-24 07:33:51 -06:00
})
2020-10-24 12:33:48 -06:00
it('should match "https://news.ycombinator.com/user?id=Alice" to the Hackernews service provider', async () => {
const matches = await doipjs.verify('https://news.ycombinator.com/user?id=Alice', null, {returnMatchesOnly: true})
2020-10-24 07:50:14 -06:00
expect(matches).to.be.a('array')
expect(matches).to.be.length(1)
expect(matches[0].serviceprovider.name).to.be.equal('hackernews')
2020-10-24 15:36:25 -06:00
expect(matches[0]).to.matchPattern(pattern)
2020-10-24 07:50:14 -06:00
})
2020-10-23 14:35:53 -06:00
})