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

143 lines
5.4 KiB
TypeScript
Raw Normal View History

2018-10-29 17:28:40 +01:00
import {assert} from "chai"
2018-11-05 10:01:58 +01:00
import AquariusConnectorProvider from "../../src/aquarius/AquariusConnectorProvider"
2018-10-29 17:28:40 +01:00
import ConfigProvider from "../../src/ConfigProvider"
2018-11-05 10:08:06 +01:00
import Condition from "../../src/ddo/Condition"
2018-11-05 10:01:58 +01:00
import DDO from "../../src/ddo/DDO"
2018-11-05 10:08:06 +01:00
import Service from "../../src/ddo/Service"
2018-10-29 17:28:40 +01:00
import ContractHandler from "../../src/keeper/ContractHandler"
import Account from "../../src/ocean/Account"
import IdGenerator from "../../src/ocean/IdGenerator"
import Ocean from "../../src/ocean/Ocean"
import ServiceAgreement from "../../src/ocean/ServiceAgreement"
import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreementTemplate"
2018-10-30 08:30:05 +01:00
import config from "../config"
2018-11-05 10:01:58 +01:00
import AquariusConnectorMock from "../mocks/AquariusConnector.mock"
2018-10-29 17:28:40 +01:00
let ocean: Ocean
let accounts: Account[]
let publisherAccount: Account
let templateOwnerAccount: Account
let consumerAccount: Account
2018-10-29 17:28:40 +01:00
let testServiceAgreementTemplate: ServiceAgreementTemplate
2018-11-05 10:01:58 +01:00
let serviceDefintion
2018-10-29 17:28:40 +01:00
describe("ServiceAgreement", () => {
before(async () => {
ConfigProvider.setConfig(config)
await ContractHandler.deployContracts()
ocean = await Ocean.getInstance(config)
accounts = await ocean.getAccounts()
templateOwnerAccount = accounts[0]
publisherAccount = accounts[1]
consumerAccount = accounts[2]
2018-10-29 17:28:40 +01:00
const resourceName = "superb car data"
const methods: string[] = [
"PaymentConditions.lockPayment",
"AccessConditions.grantAccess",
"PaymentConditions.releasePayment",
"PaymentConditions.refundPayment",
]
2018-11-05 10:01:58 +01:00
// tslint:disable
const dependencyMatrix = [0, 1, 4, 1 | 2 ** 4 | 2 ** 5] // dependency bit | timeout bit
2018-10-29 17:28:40 +01:00
testServiceAgreementTemplate =
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, methods,
dependencyMatrix, templateOwnerAccount)
2018-11-05 10:01:58 +01:00
// get condition keys from template
const conditionKeys: string[] = testServiceAgreementTemplate.getConditionKeys()
// create ddo conditions out of the keys
const conditions: Condition[] = conditionKeys.map((conditionKey, i): Condition => {
return {
name: methods[i].split(".")[1],
timeout: 100,
conditionKey: conditionKey,
parameters: {
// todo wtf?
assetId: "bytes32",
price: "integer"
},
} as Condition
})
serviceDefintion = [
{
serviceDefinitionId: IdGenerator.generateId(),
templateId: testServiceAgreementTemplate.getId(),
conditions,
} as Service
]
2018-10-29 17:28:40 +01:00
})
describe("#createServiceAgreement()", () => {
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}`
const ddo = new DDO({id: did, service: serviceDefintion})
const assetId: string = IdGenerator.generateId()
2018-11-05 10:01:58 +01:00
const serviceAgreementId: string = IdGenerator.generateId()
2018-11-05 10:01:58 +01:00
// @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
2018-10-29 17:28:40 +01:00
const serviceAgreement: ServiceAgreement =
2018-11-05 10:01:58 +01:00
await ServiceAgreement.createServiceAgreement(assetId, ddo, serviceAgreementId, 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-05 10:01:58 +01:00
const id: string = IdGenerator.generateId()
const did: string = `did:op:${id}`
const ddo = new DDO({id: did, service: serviceDefintion})
const assetId: string = IdGenerator.generateId()
2018-11-05 10:01:58 +01:00
const serviceAgreementId: string = IdGenerator.generateId()
2018-11-05 10:01:58 +01:00
// @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
2018-10-29 17:28:40 +01:00
const serviceAgreement: ServiceAgreement =
2018-11-05 10:01:58 +01:00
await ServiceAgreement.createServiceAgreement(assetId, ddo, serviceAgreementId, 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)
})
})
describe("#grantAccess()", () => {
it("should grant access in that service agreement", async () => {
2018-11-05 10:01:58 +01:00
const id: string = IdGenerator.generateId()
const did: string = `did:op:${id}`
const ddo = new DDO({id: did, service: serviceDefintion})
const assetId: string = IdGenerator.generateId()
2018-11-05 10:01:58 +01:00
const serviceAgreementId: string = IdGenerator.generateId()
2018-11-05 10:01:58 +01:00
// @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
const serviceAgreement: ServiceAgreement =
2018-11-05 10:01:58 +01:00
await ServiceAgreement.createServiceAgreement(assetId, ddo, serviceAgreementId, consumerAccount,
publisherAccount)
assert(serviceAgreement)
2018-11-05 10:01:58 +01:00
const fulfilled: boolean = await serviceAgreement.grantAccess(assetId, IdGenerator.generateId())
assert(fulfilled)
2018-10-29 17:28:40 +01:00
})
})
})