1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00
squid-js/test/libDDO/DDO.test.ts

179 lines
5.3 KiB
TypeScript
Raw Normal View History

2018-11-20 04:54:50 +01:00
import {assert} from "chai"
import DDO from "../../src/libDDO/DDO"
2018-11-23 01:53:39 +01:00
import IdGenerator from "../../src/ocean/IdGenerator"
2018-11-20 04:54:50 +01:00
2018-11-21 01:35:41 +01:00
import * as jsonDDO from "../testdata/ddoSample1.json"
2018-11-20 04:54:50 +01:00
2018-11-20 06:01:52 +01:00
describe("libDDO", () => {
2018-11-20 04:54:50 +01:00
describe("#constructor()", () => {
it("should create an empty ddo", async () => {
const ddo = new DDO()
assert(ddo)
assert(ddo.did == null)
})
})
2018-11-21 01:35:41 +01:00
describe("JSON serialization unserialization", () => {
2018-11-20 04:54:50 +01:00
it("should create ddo with the sample JSON", async () => {
assert(jsonDDO)
2018-11-21 01:35:41 +01:00
const ddo = new DDO(jsonDDO)
2018-11-20 04:54:50 +01:00
assert(ddo)
assert(ddo.validate())
2018-11-21 01:35:41 +01:00
const jsonText = ddo.toJSON()
2018-11-20 04:54:50 +01:00
assert(jsonText)
})
})
2018-11-21 01:35:41 +01:00
describe("validation", () => {
2018-11-20 04:54:50 +01:00
it("should test ddo core validation", async () => {
// core ddo values
assert(jsonDDO)
2018-11-21 01:35:41 +01:00
const ddo = new DDO(jsonDDO)
2018-11-20 04:54:50 +01:00
assert(ddo)
assert(ddo.validate())
2018-11-21 01:35:41 +01:00
ddo.did = ""
2018-11-20 04:54:50 +01:00
assert(!ddo.validate())
})
it("should test ddo public key validation", async () => {
// public key
2018-11-21 01:35:41 +01:00
const ddo = new DDO(jsonDDO)
2018-11-20 04:54:50 +01:00
assert(ddo)
assert(ddo.validate())
2018-11-21 01:35:41 +01:00
ddo.publicKeys[0].id = ""
2018-11-20 04:54:50 +01:00
assert(!ddo.validate())
})
it("should test ddo authentication validation", async () => {
// authentication
2018-11-21 01:35:41 +01:00
const ddo = new DDO(jsonDDO)
2018-11-20 04:54:50 +01:00
assert(ddo)
assert(ddo.validate())
2018-11-21 01:35:41 +01:00
ddo.authentications[0].type = ""
2018-11-20 04:54:50 +01:00
assert(!ddo.validate())
})
it("should test ddo service validation", async () => {
// service
2018-11-21 01:35:41 +01:00
const ddo = new DDO(jsonDDO)
2018-11-20 04:54:50 +01:00
assert(ddo)
assert(ddo.validate())
2018-11-21 01:35:41 +01:00
ddo.services[0].endpoint = ""
2018-11-20 04:54:50 +01:00
assert(!ddo.validate())
})
it("should test ddo proof validation", async () => {
// proof
2018-11-21 01:35:41 +01:00
const ddo = new DDO(jsonDDO)
2018-11-20 04:54:50 +01:00
assert(ddo)
assert(ddo.validate())
2018-11-21 01:35:41 +01:00
ddo.proof.signatureValue = ""
2018-11-20 04:54:50 +01:00
assert(!ddo.validate())
})
})
2018-11-21 01:35:41 +01:00
describe("DDO access data", () => {
2018-11-23 01:53:39 +01:00
it("should find the correct public key", async () => {
const did = "did:op:" + IdGenerator.generateId()
const ddo = new DDO(did)
assert(ddo)
for( let i = 0; i < 5; i ++ ) {
const privateKey = ddo.addSignature()
assert(privateKey)
}
const publicKey = ddo.getPublicKey(4)
assert(publicKey)
2018-11-23 02:30:44 +01:00
2018-11-23 01:53:39 +01:00
const publicKeyId = ddo.getPublicKey(did + "#keys=5")
assert(publicKeyId)
2018-11-23 02:30:44 +01:00
assert(publicKeyId.id === publicKey.id)
2018-11-23 01:53:39 +01:00
})
2018-11-23 02:30:44 +01:00
2018-11-20 04:54:50 +01:00
it("should find a service in the ddo", async () => {
2018-11-21 01:35:41 +01:00
const ddo = new DDO(jsonDDO)
2018-11-20 04:54:50 +01:00
assert(ddo)
assert(ddo.validate())
2018-11-21 01:55:02 +01:00
const serviceFound = ddo.getService("Metadata")
assert(serviceFound)
2018-11-20 04:54:50 +01:00
2018-11-21 01:55:02 +01:00
const serviceNotFound = ddo.getService("MetadataCannotFind")
assert(serviceNotFound == null)
2018-11-21 01:35:41 +01:00
// var item = ddo.findServiceKeyValue("serviceDefinitionId", "test")
2018-11-20 04:54:50 +01:00
})
})
2018-11-21 01:35:41 +01:00
describe("DDO hashing", () => {
2018-11-20 04:54:50 +01:00
it("should hash a valid ddo", async () => {
2018-11-21 01:35:41 +01:00
const ddo = new DDO(jsonDDO)
2018-11-20 04:54:50 +01:00
assert(ddo)
assert(ddo.validate())
2018-11-21 01:35:41 +01:00
const hash = ddo.calculateHash()
2018-11-20 04:54:50 +01:00
assert(hash)
// console.log(hash)
2018-11-20 04:54:50 +01:00
})
})
2018-11-21 01:35:41 +01:00
describe("DDO validate proof from JSON", () => {
2018-11-20 07:40:37 +01:00
it("should have a valid ddo proof", async () => {
2018-11-21 01:35:41 +01:00
const ddo = new DDO(jsonDDO)
2018-11-20 07:40:37 +01:00
assert(ddo)
2018-11-21 01:35:41 +01:00
assert(ddo.validate())
ddo.validateProof()
2018-11-20 07:40:37 +01:00
})
})
2018-11-21 01:35:41 +01:00
describe("DDO creation", () => {
it("should add a signature", async () => {
2018-11-21 01:35:41 +01:00
const ddo = new DDO()
assert(ddo)
const privateKey = ddo.addSignature()
2018-11-21 01:35:41 +01:00
assert(privateKey.match("-----BEGIN RSA PRIVATE KEY-----"))
})
2018-11-21 01:35:41 +01:00
it("should add a service", async () => {
2018-11-23 01:53:39 +01:00
const did = "did:op:" + IdGenerator.generateId()
2018-11-21 01:35:41 +01:00
const ddo = new DDO(did)
assert(ddo)
2018-11-21 01:35:41 +01:00
const service = ddo.addService({type: "metatrippy", serviceEndpoint: "http://localhost:5000"})
assert(service)
assert(service.id === did)
})
2018-11-20 11:24:38 +01:00
it("should add a static proof and validate", async () => {
2018-11-23 01:53:39 +01:00
const did = "did:op:" + IdGenerator.generateId()
2018-11-21 01:35:41 +01:00
const ddo = new DDO(did)
2018-11-20 11:24:38 +01:00
assert(ddo)
const privateKey = ddo.addSignature()
2018-11-21 01:35:41 +01:00
assert(privateKey.match("-----BEGIN RSA PRIVATE KEY-----"))
2018-11-22 16:30:07 +01:00
ddo.addProof(privateKey)
2018-11-21 04:24:53 +01:00
// console.log(ddo.toJSON())
assert(ddo.validateProof())
2018-11-20 11:24:38 +01:00
})
2018-11-21 04:24:53 +01:00
it("should add a static embedded proof and validate", async () => {
2018-11-23 01:53:39 +01:00
const did = "did:op:" + IdGenerator.generateId()
2018-11-21 04:24:53 +01:00
const ddo = new DDO(did)
assert(ddo)
const privateKey = ddo.addSignature("pem", true)
assert(privateKey.match("-----BEGIN RSA PRIVATE KEY-----"))
2018-11-22 16:30:07 +01:00
ddo.addProof(privateKey)
2018-11-21 04:24:53 +01:00
// console.log(ddo.toJSON())
assert(ddo.validateProof())
})
})
2018-11-20 04:54:50 +01:00
})