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

164 lines
6.9 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 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-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-10-30 08:30:05 +01:00
import config from "../config"
import TestContractHandler from "../keeper/TestContractHandler"
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 consumerAccount: Account
2018-11-14 15:44:25 +01:00
let service: Service
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]
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-05 14:56:14 +01:00
const ddoConditions: DDOCondition[] = conditions.map((condition): DDOCondition => {
2018-11-05 10:01:58 +01:00
return {
2018-11-05 14:56:14 +01:00
name: condition.methodReflection.methodName,
2018-11-07 08:43:04 +01:00
timeout: condition.timeout,
2018-11-05 14:56:14 +01:00
conditionKey: condition.condtionKey,
parameters: condition.methodReflection.inputs.map((input) => {
return {
...input,
value: "xx",
}as Parameter
}),
} as DDOCondition
2018-11-05 10:01:58 +01:00
})
2018-11-14 15:44:25 +01:00
service = {
type: "Access",
serviceDefinitionId: IdGenerator.generateId(),
templateId: serviceAgreementTemplate.getId(),
conditions: ddoConditions,
} 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-14 15:44:25 +01:00
const ddo = new DDO({id: did, service: [service]})
2018-11-12 08:33:19 +01:00
const assetId: string = IdGenerator.generateId()
const serviceAgreementId: string = IdGenerator.generateId()
// @ts-ignore
AquariusConnectorProvider.setConnector(new AquariusConnectorMock(ddo))
const serviceAgreementSignature: string =
2018-11-14 15:44:25 +01:00
await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId,
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-05 10:01:58 +01:00
const id: string = IdGenerator.generateId()
const did: string = `did:op:${id}`
2018-11-14 15:44:25 +01:00
const ddo = new DDO({id: did, service: [service]})
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-11-12 08:33:19 +01:00
const serviceAgreementSignature: string =
2018-11-14 15:44:25 +01:00
await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId,
serviceAgreementId, consumerAccount)
2018-11-12 08:33:19 +01:00
2018-10-29 17:28:40 +01:00
const serviceAgreement: ServiceAgreement =
2018-11-14 15:44:25 +01:00
await ServiceAgreement.executeServiceAgreement(assetId, ddo, service.serviceDefinitionId,
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-05 10:01:58 +01:00
const id: string = IdGenerator.generateId()
const did: string = `did:op:${id}`
2018-11-14 15:44:25 +01:00
const ddo = new DDO({id: did, service: [service]})
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-11-12 08:33:19 +01:00
const serviceAgreementSignature: string =
2018-11-14 15:44:25 +01:00
await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId,
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-14 15:44:25 +01:00
await ServiceAgreement.executeServiceAgreement(assetId, ddo, service.serviceDefinitionId,
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)
})
})
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}`
2018-11-14 15:44:25 +01:00
const ddo = new DDO({id: did, service: [service]})
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-11-12 08:33:19 +01:00
const serviceAgreementSignature: string =
2018-11-14 15:44:25 +01:00
await ServiceAgreement.signServiceAgreement(assetId, ddo, service.serviceDefinitionId,
serviceAgreementId, consumerAccount)
2018-11-12 08:33:19 +01:00
assert(serviceAgreementSignature)
const serviceAgreement: ServiceAgreement =
2018-11-14 15:44:25 +01:00
await ServiceAgreement.executeServiceAgreement(assetId, ddo, service.serviceDefinitionId,
serviceAgreementId, serviceAgreementSignature, 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
})
})
})