1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

Improve Ocean.assets.isConsumable return interface and message (#869)

This commit is contained in:
Kris Liew 2021-06-28 15:26:28 +08:00 committed by GitHub
parent ce0aab1062
commit 5d4250f8e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 10 deletions

View File

@ -1,4 +1,5 @@
export interface Consumable { export interface Consumable {
status: number status: number
message: string message: string
result: boolean
} }

View File

@ -333,6 +333,7 @@ export class Assets extends Instantiable {
): Consumable { ): Consumable {
let status = 0 let status = 0
let message = 'All good' let message = 'All good'
let result = true
if (ddo.credentials) { if (ddo.credentials) {
if (ddo.credentials.allow && ddo.credentials.allow.length > 0) { if (ddo.credentials.allow && ddo.credentials.allow.length > 0) {
const allowList = ddo.credentials.allow.find( const allowList = ddo.credentials.allow.find(
@ -340,7 +341,8 @@ export class Assets extends Instantiable {
) )
if (allowList && !allowList.value.includes(value)) { if (allowList && !allowList.value.includes(value)) {
status = 2 status = 2
message = 'Credential missing from allow list' message = 'Access is denied, your wallet address is not found on allow list'
result = false
} }
} }
if (ddo.credentials.deny && ddo.credentials.deny.length > 0) { if (ddo.credentials.deny && ddo.credentials.deny.length > 0) {
@ -349,11 +351,12 @@ export class Assets extends Instantiable {
) )
if (denyList && denyList.value.includes(value)) { if (denyList && denyList.value.includes(value)) {
status = 3 status = 3
message = 'Credential found on deny list' message = 'Access is denied, your wallet address is found on deny list'
result = false
} }
} }
} }
return { status, message } return { status, message, result }
} }
/** /**
@ -568,7 +571,7 @@ export class Assets extends Instantiable {
let service: Service let service: Service
const { ddo } = await assetResolve(asset, this.ocean) const { ddo } = await assetResolve(asset, this.ocean)
const consumable = await this.isConsumable(ddo, consumerAddress) const consumable = await this.isConsumable(ddo, consumerAddress)
if (consumable.status > 0) { if (!consumable.result) {
throw new Error(`Order asset failed, ` + consumable.message) throw new Error(`Order asset failed, ` + consumable.message)
} }
@ -756,21 +759,27 @@ export class Assets extends Instantiable {
public async isConsumable(ddo: DDO, consumer?: string): Promise<Consumable> { public async isConsumable(ddo: DDO, consumer?: string): Promise<Consumable> {
let status = 0 let status = 0
let message = 'All good' let message = 'All good'
if (!ddo) return { status, message } let result = true
if (!ddo) return { status, message, result }
const metadata = ddo.findServiceByType('metadata') const metadata = ddo.findServiceByType('metadata')
if (metadata.attributes.status?.isOrderDisabled) if (metadata.attributes.status?.isOrderDisabled)
return { return {
status: 1, status: 1,
message: 'Ordering this asset has been temporarily disabled by the publisher.' message: 'Ordering this asset has been temporarily disabled by the publisher.',
result: false
} }
if (consumer) { if (consumer) {
;({ status, message } = this.checkCredential(ddo, CredentialType.address, consumer)) ;({ status, message, result } = this.checkCredential(
ddo,
CredentialType.address,
consumer
))
} }
/* /*
// return: 2, Credential missing from allow list // return: 2, Access is denied, your wallet address is not found on allow list
// return: 3, Credential found on deny list // return: 3, Access is denied, your wallet address is found on deny list
*/ */
return { status, message } return { status, message, result }
} }
} }

View File

@ -546,6 +546,7 @@ describe('Marketplace flow', () => {
const response = await ocean.assets.isConsumable(ddo) const response = await ocean.assets.isConsumable(ddo)
assert(response !== null) assert(response !== null)
assert(response.status === 0) assert(response.status === 0)
assert(response.result === true)
}) })
it('Alice should update her asset and set isOrderDisabled = true', async () => { it('Alice should update her asset and set isOrderDisabled = true', async () => {
@ -569,6 +570,7 @@ describe('Marketplace flow', () => {
const response = await ocean.assets.isConsumable(ddo) const response = await ocean.assets.isConsumable(ddo)
assert(response !== null) assert(response !== null)
assert(response.status === 1) assert(response.status === 1)
assert(response.result === false)
}) })
it('Alice should create a FRE pricing for her asset', async () => { it('Alice should create a FRE pricing for her asset', async () => {
@ -676,29 +678,37 @@ describe('Marketplace flow', () => {
const ddoWithAllowList = await ocean.assets.resolve(ddoWithCredentialsAllowList.id) const ddoWithAllowList = await ocean.assets.resolve(ddoWithCredentialsAllowList.id)
let consumable = await ocean.assets.isConsumable(ddoWithAllowList, bob.getId()) let consumable = await ocean.assets.isConsumable(ddoWithAllowList, bob.getId())
assert(consumable.status === 0) assert(consumable.status === 0)
assert(consumable.result === true)
consumable = await ocean.assets.isConsumable(ddoWithCredentials, bob.getId()) consumable = await ocean.assets.isConsumable(ddoWithCredentials, bob.getId())
assert(consumable.status === 0) assert(consumable.status === 0)
assert(consumable.result === true)
}) })
it('Bob should be able to consume an asset with deny list, because he is not on that list', async () => { it('Bob should be able to consume an asset with deny list, because he is not on that list', async () => {
const ddoWithDenyList = await ocean.assets.resolve(ddoWithCredentialsDenyList.id) const ddoWithDenyList = await ocean.assets.resolve(ddoWithCredentialsDenyList.id)
let consumable = await ocean.assets.isConsumable(ddoWithDenyList, bob.getId()) let consumable = await ocean.assets.isConsumable(ddoWithDenyList, bob.getId())
assert(consumable.status === 0) assert(consumable.status === 0)
assert(consumable.result === true)
consumable = await ocean.assets.isConsumable(ddoWithCredentials, bob.getId()) consumable = await ocean.assets.isConsumable(ddoWithCredentials, bob.getId())
assert(consumable.status === 0) assert(consumable.status === 0)
assert(consumable.result === true)
}) })
it('Charlie should not be able to consume an asset with allow list, because he is not on that list', async () => { it('Charlie should not be able to consume an asset with allow list, because he is not on that list', async () => {
const ddoWithAllowList = await ocean.assets.resolve(ddoWithCredentialsAllowList.id) const ddoWithAllowList = await ocean.assets.resolve(ddoWithCredentialsAllowList.id)
let consumable = await ocean.assets.isConsumable(ddoWithAllowList, charlie.getId()) let consumable = await ocean.assets.isConsumable(ddoWithAllowList, charlie.getId())
assert(consumable.status === 2) assert(consumable.status === 2)
assert(consumable.result === false)
consumable = await ocean.assets.isConsumable(ddoWithCredentials, charlie.getId()) consumable = await ocean.assets.isConsumable(ddoWithCredentials, charlie.getId())
assert(consumable.status === 3) assert(consumable.status === 3)
assert(consumable.result === false)
}) })
it('Charlie should not be able to consume an asset with deny list, because he is on that list', async () => { it('Charlie should not be able to consume an asset with deny list, because he is on that list', async () => {
const ddoWithDenyList = await ocean.assets.resolve(ddoWithCredentialsDenyList.id) const ddoWithDenyList = await ocean.assets.resolve(ddoWithCredentialsDenyList.id)
let consumable = await ocean.assets.isConsumable(ddoWithDenyList, charlie.getId()) let consumable = await ocean.assets.isConsumable(ddoWithDenyList, charlie.getId())
assert(consumable.status === 3) assert(consumable.status === 3)
assert(consumable.result === false)
consumable = await ocean.assets.isConsumable(ddoWithCredentials, charlie.getId()) consumable = await ocean.assets.isConsumable(ddoWithCredentials, charlie.getId())
assert(consumable.status === 3) assert(consumable.status === 3)
assert(consumable.result === false)
}) })
}) })