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

270 lines
11 KiB
TypeScript
Raw Normal View History

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"
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-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"
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"
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[]
let publisherAccount: Account
let consumerAccount: Account
let accessService: Service
let metaDataService: Service
2018-10-29 17:28:40 +01:00
2018-11-20 10:43:11 +01:00
const assetId: string = IdGenerator.generateId()
2018-10-29 17:28:40 +01:00
describe("ServiceAgreement", () => {
before(async () => {
ConfigProvider.setConfig(config)
await TestContractHandler.prepareContracts()
2018-10-29 17:28:40 +01:00
ocean = await Ocean.getInstance(config)
accounts = await ocean.getAccounts()
publisherAccount = accounts[1]
consumerAccount = accounts[2]
2018-11-20 10:43:11 +01:00
const metadata = new MetaData()
const serviceAgreementTemplate: ServiceAgreementTemplate =
new ServiceAgreementTemplate(new Access())
2018-11-05 10:01:58 +01:00
// get condition keys from template
const conditions: Condition[] = await serviceAgreementTemplate.getConditions()
2018-11-05 10:01:58 +01:00
// create ddo conditions out of the keys
2018-11-22 09:22:27 +01:00
const ddoConditions: DDOCondition[] = conditions
.map((condition: Condition, index): DDOCondition => {
const events: Event[] = [
{
name: "PaymentReleased",
actorType: [
"consumer",
],
handlers: {
moduleName: "serviceAgreement",
functionName: "fulfillAgreement",
version: "0.1",
} as EventHandlers,
} as Event,
]
const mapParameterValueToName = (name) => {
switch (name) {
case "price":
return metadata.base.price
case "assetId":
return "0x" + assetId
case "documentKeyId":
2018-11-22 13:14:38 +01:00
return "0x" + assetId
2018-11-22 09:22:27 +01:00
}
return null
2018-11-20 10:43:11 +01:00
}
2018-11-22 09:22:27 +01:00
const parameters: Parameter[] = condition.parameters
.map((parameter: Parameter) => {
return {
name: parameter.name,
type: parameter.type,
value: mapParameterValueToName(parameter.name),
} as Parameter
})
2018-11-20 10:43:11 +01:00
return {
2018-11-22 09:22:27 +01:00
contractName: condition.methodReflection.contractName,
methodName: condition.methodReflection.methodName,
timeout: condition.timeout,
index,
conditionKey: condition.condtionKey,
parameters,
events,
dependencies: condition.dependencies,
dependencyTimeoutFlags: condition.dependencyTimeoutFlags,
isTerminalCondition: condition.isTerminalCondition,
} as DDOCondition
})
accessService = {
2018-11-14 15:44:25 +01:00
type: "Access",
serviceDefinitionId: IdGenerator.generateId(),
templateId: serviceAgreementTemplate.getId(),
conditions: ddoConditions,
} as Service
metaDataService = {
type: "MetaData",
2018-11-20 10:43:11 +01:00
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 () => {
2018-11-22 13:14:38 +01:00
const did: string = `did:op:${assetId}`
const ddo = new DDO({id: did, service: [accessService]})
2018-11-12 08:33:19 +01:00
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 =
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()", () => {
it("should execute an service agreement", async () => {
2018-10-29 17:28:40 +01:00
2018-11-22 13:14:38 +01:00
const did: string = `did:op:${assetId}`
const ddo = new DDO({id: did, service: [accessService]})
2018-11-05 10:01:58 +01:00
const serviceAgreementId: string = IdGenerator.generateId()
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 =
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 =
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()", () => {
it("should get the status of a newly created service agreement", async () => {
2018-10-29 17:28:40 +01:00
2018-11-22 13:14:38 +01:00
const did: string = `did:op:${assetId}`
const ddo = new DDO({id: did, service: [accessService]})
2018-11-05 10:01:58 +01:00
const serviceAgreementId: string = IdGenerator.generateId()
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 =
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 =
await ServiceAgreement.executeServiceAgreement(assetId, ddo, accessService.serviceDefinitionId,
2018-11-14 15:44:25 +01:00
serviceAgreementId, serviceAgreementSignature, consumerAccount, publisherAccount)
assert(serviceAgreement)
2018-11-05 10:01:58 +01:00
2018-10-29 17:28:40 +01:00
const status = await serviceAgreement.getStatus()
assert(status === false)
})
})
2018-11-22 13:14:38 +01:00
describe("#buyAsset()", () => {
2018-11-22 09:22:27 +01:00
it("should lock the payment in that service agreement", async () => {
2018-11-22 13:14:38 +01:00
const did: string = `did:op:${assetId}`
const ddo = new DDO({id: did, service: [accessService, metaDataService]})
const serviceAgreementId: string = IdGenerator.generateId()
// @ts-ignore
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
2018-11-22 13:14:38 +01:00
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)
2018-11-22 09:22:27 +01:00
// get funds
await consumerAccount.requestTokens(metaDataService.metadata.base.price)
2018-11-22 13:14:38 +01:00
const paid: boolean = await serviceAgreement.buyAsset(assetId, metaDataService.metadata.base.price,
consumerAccount)
assert(paid)
})
})
describe("#grantAccess()", () => {
2018-11-22 10:56:06 +01:00
it("should grant access in that service agreement", async () => {
2018-11-22 13:14:38 +01:00
const did: string = `did:op:${assetId}`
const ddo = new DDO({id: did, service: [accessService]})
2018-11-05 10:01:58 +01:00
const serviceAgreementId: string = IdGenerator.generateId()
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 =
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)
const serviceAgreement: ServiceAgreement =
await ServiceAgreement.executeServiceAgreement(assetId, ddo, accessService.serviceDefinitionId,
2018-11-14 15:44:25 +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-11-22 13:14:38 +01:00
const paid: boolean = await serviceAgreement.buyAsset(assetId, metaDataService.metadata.base.price,
2018-11-22 09:22:27 +01:00
consumerAccount)
assert(paid)
2018-11-22 13:14:38 +01:00
// todo: use document id
const accessGranted: boolean = await serviceAgreement.grantAccess(assetId, assetId)
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-11-22 13:14:38 +01:00
const did: string = `did:op:${assetId}`
2018-11-22 10:56:06 +01:00
const ddo = new DDO({id: did, service: [accessService]})
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)
2018-11-22 13:14:38 +01:00
// todo: use document id
const accessGranted: boolean = await serviceAgreement.grantAccess(assetId, assetId)
2018-11-22 10:56:06 +01:00
assert(!accessGranted)
})
2018-10-29 17:28:40 +01:00
})
})