2018-10-29 17:28:40 +01:00
|
|
|
import {assert} from "chai"
|
|
|
|
import ConfigProvider from "../../src/ConfigProvider"
|
2019-02-04 11:46:24 +01:00
|
|
|
import { Condition } from "../../src/ddo/Condition"
|
|
|
|
import { DDO } from "../../src/ddo/DDO"
|
|
|
|
import { Service } from "../../src/ddo/Service"
|
2018-10-29 17:28:40 +01:00
|
|
|
import Account from "../../src/ocean/Account"
|
2018-12-17 15:54:58 +01:00
|
|
|
import DID from "../../src/ocean/DID"
|
2018-10-29 17:28:40 +01:00
|
|
|
import IdGenerator from "../../src/ocean/IdGenerator"
|
|
|
|
import Ocean from "../../src/ocean/Ocean"
|
2018-11-05 14:56:14 +01:00
|
|
|
import ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement"
|
|
|
|
import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreements/ServiceAgreementTemplate"
|
2018-11-05 17:50:56 +01:00
|
|
|
import Access from "../../src/ocean/ServiceAgreements/Templates/Access"
|
2018-11-19 12:16:11 +01:00
|
|
|
import WebServiceConnectorProvider from "../../src/utils/WebServiceConnectorProvider"
|
2018-10-30 08:30:05 +01:00
|
|
|
import config from "../config"
|
2018-11-07 14:33:56 +01:00
|
|
|
import TestContractHandler from "../keeper/TestContractHandler"
|
2018-11-19 12:16:11 +01:00
|
|
|
import WebServiceConnectorMock from "../mocks/WebServiceConnector.mock"
|
2019-02-04 11:46:24 +01:00
|
|
|
import { metadataMock } from "../testdata/MetaData"
|
|
|
|
|
2018-10-29 17:28:40 +01:00
|
|
|
let ocean: Ocean
|
|
|
|
let accounts: Account[]
|
2018-11-01 08:31:21 +01:00
|
|
|
let publisherAccount: Account
|
|
|
|
let consumerAccount: Account
|
|
|
|
|
2019-02-15 23:40:55 +01:00
|
|
|
let accessService: Service<"Access">
|
2019-02-07 12:10:25 +01:00
|
|
|
let metaDataService: Service<"Metadata">
|
2018-10-29 17:28:40 +01:00
|
|
|
|
2018-12-17 15:54:58 +01:00
|
|
|
const did: DID = DID.generate()
|
2018-11-20 10:43:11 +01:00
|
|
|
|
2018-10-29 17:28:40 +01:00
|
|
|
describe("ServiceAgreement", () => {
|
|
|
|
|
2019-02-04 11:46:24 +01:00
|
|
|
const metadata = metadataMock
|
|
|
|
|
2018-10-29 17:28:40 +01:00
|
|
|
before(async () => {
|
|
|
|
ConfigProvider.setConfig(config)
|
2018-11-07 14:33:56 +01:00
|
|
|
await TestContractHandler.prepareContracts()
|
2018-10-29 17:28:40 +01:00
|
|
|
ocean = await Ocean.getInstance(config)
|
|
|
|
accounts = await ocean.getAccounts()
|
|
|
|
|
2018-11-01 08:31:21 +01:00
|
|
|
publisherAccount = accounts[1]
|
|
|
|
consumerAccount = accounts[2]
|
|
|
|
|
2018-11-06 16:13:12 +01:00
|
|
|
const serviceAgreementTemplate: ServiceAgreementTemplate =
|
|
|
|
new ServiceAgreementTemplate(new Access())
|
2018-11-05 10:01:58 +01:00
|
|
|
|
2018-12-17 15:54:58 +01:00
|
|
|
const conditions: Condition[] = await serviceAgreementTemplate.getConditions(metadata, did.getId())
|
2018-11-20 09:13:03 +01:00
|
|
|
|
|
|
|
accessService = {
|
2018-11-14 15:44:25 +01:00
|
|
|
type: "Access",
|
2018-12-14 13:40:08 +01:00
|
|
|
serviceDefinitionId: "0",
|
2018-11-14 15:44:25 +01:00
|
|
|
templateId: serviceAgreementTemplate.getId(),
|
2018-11-23 14:14:35 +01:00
|
|
|
conditions,
|
2019-02-07 12:10:25 +01:00
|
|
|
}
|
2018-11-20 09:13:03 +01:00
|
|
|
|
|
|
|
metaDataService = {
|
2019-02-07 12:10:25 +01:00
|
|
|
type: "Metadata",
|
2018-11-20 10:43:11 +01:00
|
|
|
metadata,
|
2019-02-07 12:10:25 +01:00
|
|
|
}
|
2018-10-29 17:28:40 +01:00
|
|
|
})
|
|
|
|
|
2018-11-07 08:43:04 +01:00
|
|
|
describe("#signServiceAgreement()", () => {
|
2018-11-12 08:33:19 +01:00
|
|
|
it("should sign an service agreement", async () => {
|
|
|
|
|
2018-12-17 15:54:58 +01:00
|
|
|
const ddo = new DDO({id: did.getDid(), service: [accessService]})
|
2018-12-17 14:29:09 +01:00
|
|
|
const serviceAgreementId: string = IdGenerator.generateId()
|
2018-11-12 08:33:19 +01:00
|
|
|
|
|
|
|
// @ts-ignore
|
2018-11-19 12:16:11 +01:00
|
|
|
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
|
|
|
|
|
2018-11-12 08:33:19 +01:00
|
|
|
const serviceAgreementSignature: string =
|
2018-12-17 15:54:58 +01:00
|
|
|
await ServiceAgreement.signServiceAgreement(ddo, accessService.serviceDefinitionId,
|
2018-11-14 15:44:25 +01:00
|
|
|
serviceAgreementId, consumerAccount)
|
2018-11-12 08:33:19 +01:00
|
|
|
|
|
|
|
assert(serviceAgreementSignature)
|
|
|
|
assert(serviceAgreementSignature.startsWith("0x"))
|
2018-11-26 11:04:05 +01:00
|
|
|
assert(serviceAgreementSignature.length === 132)
|
2018-11-12 08:33:19 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("#executeServiceAgreement()", () => {
|
2019-01-30 15:35:31 +01:00
|
|
|
it("should execute a service agreement", async () => {
|
2018-10-29 17:28:40 +01:00
|
|
|
|
2018-12-17 15:54:58 +01:00
|
|
|
const ddo = new DDO({id: did.getDid(), service: [accessService]})
|
2018-12-17 14:29:09 +01:00
|
|
|
const serviceAgreementId: string = IdGenerator.generateId()
|
2018-11-01 08:31:21 +01:00
|
|
|
|
2018-11-05 10:01:58 +01:00
|
|
|
// @ts-ignore
|
2018-11-19 12:16:11 +01:00
|
|
|
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
|
2018-11-12 08:33:19 +01:00
|
|
|
const serviceAgreementSignature: string =
|
2018-12-17 15:54:58 +01:00
|
|
|
await ServiceAgreement.signServiceAgreement(ddo, accessService.serviceDefinitionId,
|
2018-11-14 15:44:25 +01:00
|
|
|
serviceAgreementId, consumerAccount)
|
2018-11-12 08:33:19 +01:00
|
|
|
|
2018-10-29 17:28:40 +01:00
|
|
|
const serviceAgreement: ServiceAgreement =
|
2018-12-17 15:54:58 +01:00
|
|
|
await ServiceAgreement.executeServiceAgreement(did, ddo, accessService.serviceDefinitionId,
|
2018-11-14 15:44:25 +01:00
|
|
|
serviceAgreementId, serviceAgreementSignature, consumerAccount, publisherAccount)
|
2018-10-29 17:28:40 +01:00
|
|
|
assert(serviceAgreement)
|
2018-11-05 10:01:58 +01:00
|
|
|
|
|
|
|
const serviceDefinitionId = serviceAgreement.getId()
|
|
|
|
assert(serviceDefinitionId)
|
2018-12-17 15:54:58 +01:00
|
|
|
assert(serviceDefinitionId !== did.getId())
|
2018-10-29 17:28:40 +01:00
|
|
|
})
|
2018-12-19 12:34:07 +01:00
|
|
|
|
|
|
|
it("should throw on invalid sig", (done) => {
|
|
|
|
|
|
|
|
const ddo = new DDO({id: did.getDid(), service: [accessService]})
|
|
|
|
const serviceAgreementId: string = IdGenerator.generateId()
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
|
|
|
|
|
|
|
|
ServiceAgreement.executeServiceAgreement(did, ddo, accessService.serviceDefinitionId,
|
|
|
|
serviceAgreementId, "0x00", consumerAccount, publisherAccount)
|
|
|
|
.catch((err) => {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2018-10-29 17:28:40 +01:00
|
|
|
})
|
|
|
|
|
2018-11-23 16:09:31 +01:00
|
|
|
describe("#payAsset()", () => {
|
2018-11-22 09:22:27 +01:00
|
|
|
it("should lock the payment in that service agreement", async () => {
|
2018-11-20 09:13:03 +01:00
|
|
|
|
2018-12-17 15:54:58 +01:00
|
|
|
const ddo = new DDO({id: did.getDid(), service: [accessService, metaDataService]})
|
2018-12-17 14:29:09 +01:00
|
|
|
const serviceAgreementId: string = IdGenerator.generateId()
|
2018-11-20 09:13:03 +01:00
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
|
2018-11-22 13:14:38 +01:00
|
|
|
|
2018-11-20 09:13:03 +01:00
|
|
|
const serviceAgreementSignature: string =
|
2018-12-17 15:54:58 +01:00
|
|
|
await ServiceAgreement.signServiceAgreement(ddo, accessService.serviceDefinitionId,
|
2018-11-20 09:13:03 +01:00
|
|
|
serviceAgreementId, consumerAccount)
|
|
|
|
assert(serviceAgreementSignature)
|
|
|
|
|
|
|
|
const serviceAgreement: ServiceAgreement =
|
2018-12-17 15:54:58 +01:00
|
|
|
await ServiceAgreement.executeServiceAgreement(did, ddo, accessService.serviceDefinitionId,
|
2018-11-20 09:13:03 +01:00
|
|
|
serviceAgreementId, serviceAgreementSignature, consumerAccount, publisherAccount)
|
|
|
|
assert(serviceAgreement)
|
|
|
|
|
2018-11-22 09:22:27 +01:00
|
|
|
// get funds
|
|
|
|
await consumerAccount.requestTokens(metaDataService.metadata.base.price)
|
|
|
|
|
2018-12-17 15:54:58 +01:00
|
|
|
const paid: boolean = await serviceAgreement.payAsset(did.getId(), metaDataService.metadata.base.price,
|
2018-11-20 09:13:03 +01:00
|
|
|
consumerAccount)
|
|
|
|
assert(paid)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-11-01 08:31:21 +01:00
|
|
|
describe("#grantAccess()", () => {
|
2018-11-22 10:56:06 +01:00
|
|
|
it("should grant access in that service agreement", async () => {
|
2018-11-01 08:31:21 +01:00
|
|
|
|
2018-12-17 15:54:58 +01:00
|
|
|
const ddo = new DDO({id: did.getDid(), service: [accessService]})
|
2018-12-17 14:29:09 +01:00
|
|
|
const serviceAgreementId: string = IdGenerator.generateId()
|
2018-11-01 08:31:21 +01:00
|
|
|
|
2018-11-05 10:01:58 +01:00
|
|
|
// @ts-ignore
|
2018-11-19 12:16:11 +01:00
|
|
|
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
|
2018-11-12 08:33:19 +01:00
|
|
|
const serviceAgreementSignature: string =
|
2018-12-17 15:54:58 +01:00
|
|
|
await ServiceAgreement.signServiceAgreement(ddo, accessService.serviceDefinitionId,
|
2018-11-14 15:44:25 +01:00
|
|
|
serviceAgreementId, consumerAccount)
|
2018-11-12 08:33:19 +01:00
|
|
|
assert(serviceAgreementSignature)
|
|
|
|
|
2018-11-01 08:31:21 +01:00
|
|
|
const serviceAgreement: ServiceAgreement =
|
2018-12-17 15:54:58 +01:00
|
|
|
await ServiceAgreement.executeServiceAgreement(did, ddo, accessService.serviceDefinitionId,
|
2018-11-14 15:44:25 +01:00
|
|
|
serviceAgreementId, serviceAgreementSignature, consumerAccount, publisherAccount)
|
2018-11-01 08:31:21 +01:00
|
|
|
assert(serviceAgreement)
|
|
|
|
|
2018-11-22 09:22:27 +01:00
|
|
|
// get funds
|
2019-02-14 11:26:12 +01:00
|
|
|
// TODO: remove small delay to prevent failtures
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 100))
|
2018-11-22 09:22:27 +01:00
|
|
|
await consumerAccount.requestTokens(metaDataService.metadata.base.price)
|
|
|
|
|
2018-12-17 15:54:58 +01:00
|
|
|
const paid: boolean = await serviceAgreement.payAsset(did.getId(), metaDataService.metadata.base.price,
|
2018-11-22 09:22:27 +01:00
|
|
|
consumerAccount)
|
2018-11-20 09:13:03 +01:00
|
|
|
assert(paid)
|
|
|
|
|
2018-11-22 13:14:38 +01:00
|
|
|
// todo: use document id
|
2019-01-30 15:35:31 +01:00
|
|
|
const accessGranted: boolean = await serviceAgreement.grantAccess(did.getId(), publisherAccount)
|
2018-11-20 09:13:03 +01:00
|
|
|
assert(accessGranted)
|
2018-10-29 17:28:40 +01:00
|
|
|
})
|
2018-11-22 10:56:06 +01:00
|
|
|
|
2018-11-22 13:14:38 +01:00
|
|
|
it("should fail to grant grant access if there is no payment", async () => {
|
2018-11-22 10:56:06 +01:00
|
|
|
|
2018-12-17 15:54:58 +01:00
|
|
|
const ddo = new DDO({id: did.getDid(), service: [accessService]})
|
2018-12-17 14:29:09 +01:00
|
|
|
const serviceAgreementId: string = IdGenerator.generateId()
|
2018-11-22 10:56:06 +01:00
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
|
|
|
|
const serviceAgreementSignature: string =
|
2018-12-17 15:54:58 +01:00
|
|
|
await ServiceAgreement.signServiceAgreement(ddo, accessService.serviceDefinitionId,
|
2018-11-22 10:56:06 +01:00
|
|
|
serviceAgreementId, consumerAccount)
|
|
|
|
assert(serviceAgreementSignature)
|
|
|
|
|
|
|
|
const serviceAgreement: ServiceAgreement =
|
2018-12-17 15:54:58 +01:00
|
|
|
await ServiceAgreement.executeServiceAgreement(did, ddo, accessService.serviceDefinitionId,
|
2018-11-22 10:56:06 +01:00
|
|
|
serviceAgreementId, serviceAgreementSignature, consumerAccount, publisherAccount)
|
|
|
|
assert(serviceAgreement)
|
|
|
|
|
2018-11-22 13:14:38 +01:00
|
|
|
// todo: use document id
|
2019-01-30 15:35:31 +01:00
|
|
|
const accessGranted: boolean = await serviceAgreement.grantAccess(did.getId(), publisherAccount)
|
2018-11-22 10:56:06 +01:00
|
|
|
assert(!accessGranted)
|
|
|
|
})
|
2018-10-29 17:28:40 +01:00
|
|
|
})
|
|
|
|
})
|