2018-10-29 17:28:40 +01:00
|
|
|
import {assert} from "chai"
|
|
|
|
import ConfigProvider from "../../src/ConfigProvider"
|
2018-11-05 14:56:14 +01:00
|
|
|
import DDOCondition from "../../src/ddo/Condition"
|
2018-11-05 10:01:58 +01:00
|
|
|
import DDO from "../../src/ddo/DDO"
|
2018-11-20 09:13:03 +01:00
|
|
|
import Event from "../../src/ddo/Event"
|
|
|
|
import EventHandlers from "../../src/ddo/EventHandlers"
|
|
|
|
import MetaData from "../../src/ddo/MetaData"
|
2018-11-05 14:56:14 +01:00
|
|
|
import Parameter from "../../src/ddo/Parameter"
|
2018-11-05 10:08:06 +01:00
|
|
|
import Service from "../../src/ddo/Service"
|
2018-11-20 09:13:03 +01:00
|
|
|
import ValuePair from "../../src/models/ValuePair"
|
2018-10-29 17:28:40 +01:00
|
|
|
import Account from "../../src/ocean/Account"
|
|
|
|
import IdGenerator from "../../src/ocean/IdGenerator"
|
|
|
|
import Ocean from "../../src/ocean/Ocean"
|
2018-11-05 14:56:14 +01:00
|
|
|
import Condition from "../../src/ocean/ServiceAgreements/Condition"
|
|
|
|
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"
|
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
|
|
|
|
|
2018-11-20 09:13:03 +01:00
|
|
|
let accessService: Service
|
|
|
|
let metaDataService: Service
|
2018-10-29 17:28:40 +01:00
|
|
|
|
|
|
|
describe("ServiceAgreement", () => {
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// get condition keys from template
|
2018-11-06 16:13:12 +01:00
|
|
|
const conditions: Condition[] = await serviceAgreementTemplate.getConditions()
|
2018-11-05 10:01:58 +01:00
|
|
|
|
|
|
|
// create ddo conditions out of the keys
|
2018-11-20 09:13:03 +01:00
|
|
|
const ddoConditions: DDOCondition[] = conditions.map((condition, index): DDOCondition => {
|
|
|
|
|
|
|
|
const events: Event[] = [
|
|
|
|
{
|
|
|
|
name: "PaymentReleased",
|
|
|
|
actorType: [
|
|
|
|
"consumer",
|
|
|
|
],
|
|
|
|
handlers: {
|
|
|
|
moduleName: "serviceAgreement",
|
|
|
|
functionName: "fulfillAgreement",
|
|
|
|
version: "0.1",
|
|
|
|
} as EventHandlers,
|
|
|
|
} as Event,
|
|
|
|
]
|
|
|
|
|
|
|
|
const parameters: Parameter[] = condition.methodReflection.inputs.map((input: ValuePair) => {
|
|
|
|
return {
|
|
|
|
...input,
|
|
|
|
value: "xxx",
|
|
|
|
} as Parameter
|
|
|
|
})
|
|
|
|
|
2018-11-05 10:01:58 +01:00
|
|
|
return {
|
2018-11-16 15:22:08 +01:00
|
|
|
contractName: condition.methodReflection.contractName,
|
|
|
|
methodName: condition.methodReflection.methodName,
|
2018-11-07 08:43:04 +01:00
|
|
|
timeout: condition.timeout,
|
2018-11-20 09:13:03 +01:00
|
|
|
index,
|
2018-11-05 14:56:14 +01:00
|
|
|
conditionKey: condition.condtionKey,
|
2018-11-20 09:13:03 +01:00
|
|
|
parameters,
|
|
|
|
events,
|
|
|
|
dependencies: condition.dependencies,
|
|
|
|
dependencyTimeoutFlags: condition.dependencyTimeoutFlags,
|
|
|
|
isTerminalCondition: condition.isTerminalCondition,
|
2018-11-05 14:56:14 +01:00
|
|
|
} as DDOCondition
|
2018-11-05 10:01:58 +01:00
|
|
|
})
|
|
|
|
|
2018-11-20 09:13:03 +01:00
|
|
|
accessService = {
|
2018-11-14 15:44:25 +01:00
|
|
|
type: "Access",
|
|
|
|
serviceDefinitionId: IdGenerator.generateId(),
|
|
|
|
templateId: serviceAgreementTemplate.getId(),
|
|
|
|
conditions: ddoConditions,
|
|
|
|
} as Service
|
2018-11-20 09:13:03 +01:00
|
|
|
|
|
|
|
metaDataService = {
|
|
|
|
type: "MetaData",
|
|
|
|
metadata: new MetaData(),
|
|
|
|
} as Service
|
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 () => {
|
|
|
|
|
|
|
|
const id: string = IdGenerator.generateId()
|
|
|
|
const did: string = `did:op:${id}`
|
2018-11-20 09:13:03 +01:00
|
|
|
const ddo = new DDO({id: did, service: [accessService]})
|
2018-11-12 08:33:19 +01:00
|
|
|
const assetId: string = IdGenerator.generateId()
|
|
|
|
const serviceAgreementId: string = IdGenerator.generateId()
|
|
|
|
|
|
|
|
// @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-11-20 09:13:03 +01:00
|
|
|
await ServiceAgreement.signServiceAgreement(assetId, 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"))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("#executeServiceAgreement()", () => {
|
2018-11-01 08:31:21 +01:00
|
|
|
it("should execute an service agreement", async () => {
|
2018-10-29 17:28:40 +01:00
|
|
|
|
2018-11-05 10:01:58 +01:00
|
|
|
const id: string = IdGenerator.generateId()
|
|
|
|
const did: string = `did:op:${id}`
|
2018-11-20 09:13:03 +01:00
|
|
|
const ddo = new DDO({id: did, service: [accessService]})
|
2018-11-01 08:31:21 +01:00
|
|
|
const assetId: string = IdGenerator.generateId()
|
2018-11-05 10:01:58 +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-11-20 09:13:03 +01:00
|
|
|
await ServiceAgreement.signServiceAgreement(assetId, 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-11-20 09:13:03 +01:00
|
|
|
await ServiceAgreement.executeServiceAgreement(assetId, 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)
|
|
|
|
assert(serviceDefinitionId !== did)
|
2018-10-29 17:28:40 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("#getStatus()", () => {
|
2018-11-01 08:31:21 +01:00
|
|
|
it("should get the status of a newly created service agreement", async () => {
|
2018-10-29 17:28:40 +01:00
|
|
|
|
2018-11-05 10:01:58 +01:00
|
|
|
const id: string = IdGenerator.generateId()
|
|
|
|
const did: string = `did:op:${id}`
|
2018-11-20 09:13:03 +01:00
|
|
|
const ddo = new DDO({id: did, service: [accessService]})
|
2018-11-01 08:31:21 +01:00
|
|
|
const assetId: string = IdGenerator.generateId()
|
2018-11-05 10:01:58 +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-11-20 09:13:03 +01:00
|
|
|
await ServiceAgreement.signServiceAgreement(assetId, ddo, accessService.serviceDefinitionId,
|
2018-11-14 15:44:25 +01:00
|
|
|
serviceAgreementId, consumerAccount)
|
2018-11-12 08:33:19 +01:00
|
|
|
assert(serviceAgreementSignature)
|
|
|
|
|
2018-10-29 17:28:40 +01:00
|
|
|
const serviceAgreement: ServiceAgreement =
|
2018-11-20 09:13:03 +01:00
|
|
|
await ServiceAgreement.executeServiceAgreement(assetId, 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-05 10:01:58 +01:00
|
|
|
|
2018-10-29 17:28:40 +01:00
|
|
|
const status = await serviceAgreement.getStatus()
|
2018-11-01 08:31:21 +01:00
|
|
|
assert(status === false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-11-20 09:13:03 +01:00
|
|
|
describe("#lockPayment()", () => {
|
|
|
|
xit("should lock the payment in that service agreement", async () => {
|
|
|
|
|
|
|
|
const id: string = IdGenerator.generateId()
|
|
|
|
const did: string = `did:op:${id}`
|
|
|
|
const ddo = new DDO({id: did, service: [accessService, metaDataService]})
|
|
|
|
const assetId: string = IdGenerator.generateId()
|
|
|
|
const serviceAgreementId: string = IdGenerator.generateId()
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
|
|
|
|
const serviceAgreementSignature: string =
|
|
|
|
await ServiceAgreement.signServiceAgreement(assetId, ddo, accessService.serviceDefinitionId,
|
|
|
|
serviceAgreementId, consumerAccount)
|
|
|
|
assert(serviceAgreementSignature)
|
|
|
|
|
|
|
|
const serviceAgreement: ServiceAgreement =
|
|
|
|
await ServiceAgreement.executeServiceAgreement(assetId, ddo, accessService.serviceDefinitionId,
|
|
|
|
serviceAgreementId, serviceAgreementSignature, consumerAccount, publisherAccount)
|
|
|
|
assert(serviceAgreement)
|
|
|
|
|
|
|
|
const paid: boolean = await serviceAgreement.lockPayment(assetId, metaDataService.metadata.base.price,
|
|
|
|
consumerAccount)
|
|
|
|
assert(paid)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-11-01 08:31:21 +01:00
|
|
|
describe("#grantAccess()", () => {
|
2018-11-20 09:13:03 +01:00
|
|
|
xit("should grant access in that service agreement", async () => {
|
2018-11-01 08:31:21 +01:00
|
|
|
|
2018-11-05 10:01:58 +01:00
|
|
|
const id: string = IdGenerator.generateId()
|
|
|
|
const did: string = `did:op:${id}`
|
2018-11-20 09:13:03 +01:00
|
|
|
const ddo = new DDO({id: did, service: [accessService]})
|
2018-11-01 08:31:21 +01:00
|
|
|
const assetId: string = IdGenerator.generateId()
|
2018-11-05 10:01:58 +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-11-20 09:13:03 +01:00
|
|
|
await ServiceAgreement.signServiceAgreement(assetId, 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-11-20 09:13:03 +01:00
|
|
|
await ServiceAgreement.executeServiceAgreement(assetId, 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-20 09:13:03 +01:00
|
|
|
const paid: boolean = await serviceAgreement.lockPayment(assetId, 10, consumerAccount)
|
|
|
|
assert(paid)
|
|
|
|
|
|
|
|
const accessGranted: boolean = await serviceAgreement.grantAccess(assetId, IdGenerator.generateId())
|
|
|
|
assert(accessGranted)
|
2018-10-29 17:28:40 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|