diff --git a/e2e/assets.service.e2e.ts b/e2e/assets.service.e2e.ts index 51fd7280..8b005d8a 100644 --- a/e2e/assets.service.e2e.ts +++ b/e2e/assets.service.e2e.ts @@ -1,7 +1,12 @@ import { Wallet } from 'ethers'; import { ethAddrPattern } from '@ew-did-registry/did'; import { replenish, rpcUrl, setupENS } from './utils/setup-contracts'; -import { AssetsService, CacheClient, fromPrivateKey } from '../src'; +import { + AssetsService, + CacheClient, + fromPrivateKey, + AssetNotExist, +} from '../src'; const mockGetAssetById = jest.fn(); const mockGetOwnedAssets = jest.fn(); @@ -42,14 +47,28 @@ describe('Assets tests', () => { expect(assetAddress).toEqual(expect.stringMatching(ethAddrPattern)); }); - // As for now assets service just forwards calls to cache service, tests will make sense when asssets service will be able to communicate with IdentityManager + test('should throw AssetNotExist when getting owner of not existing asset', async () => { + mockGetAssetById.mockResolvedValueOnce(undefined); + const id = ''; + expect(assetsService.getAssetOwner(id)).rejects.toEqual( + new AssetNotExist(id) + ); + }); + + /** + * @todo when ssi hub is included in test environment + */ + test.todo('should be able to get asset owner'); + test.todo('owner should be able to offer asset'); + test.todo('owner should not be able to offer asset to himself'); + test.todo('owner should not be able to offer asset to asset'); - test.todo('asset should be able to cancel offer'); + test.todo('owner should be able to cancel offer'); - test.todo('asset should be able to accept offer'); + test.todo('receiver should be able to accept offer'); - test.todo('asset should be able to reject offer'); + test.todo('receiver should be able to reject offer'); - test.todo('update did document for asset'); + test.todo('owner should be able to update asset document'); }); diff --git a/src/errors/asset-not-exist.ts b/src/errors/asset-not-exist.ts new file mode 100644 index 00000000..68c2d92e --- /dev/null +++ b/src/errors/asset-not-exist.ts @@ -0,0 +1,5 @@ +export class AssetNotExist extends Error { + constructor(assetId: string) { + super(`Asset ${assetId} doesn not exist`); + } +} diff --git a/src/errors/index.ts b/src/errors/index.ts index 85a2aed6..d317bea6 100644 --- a/src/errors/index.ts +++ b/src/errors/index.ts @@ -6,3 +6,4 @@ export { ENSOwnerNotValidAddressError } from './ens-owner-not-valid-address.erro export { ERROR_MESSAGES } from './error-messages'; export { MalformedDIDError } from './malformed-did.error'; export { NotAuthorizedIssuer } from './not-authorized-issuer'; +export { AssetNotExist } from './asset-not-exist'; diff --git a/src/modules/assets/assets.service.ts b/src/modules/assets/assets.service.ts index 5f0f5a7a..a9330cdd 100644 --- a/src/modules/assets/assets.service.ts +++ b/src/modules/assets/assets.service.ts @@ -8,6 +8,7 @@ import { CacheClient } from '../cache-client/cache-client.service'; import { Order } from '../cache-client/cache-client.types'; import { SignerService } from '../signer/signer.service'; import { AssetHistoryEventType } from './assets.types'; +import { AssetNotExist } from '../../errors/asset-not-exist'; export class AssetsService { private _owner: string; @@ -41,7 +42,7 @@ export class AssetsService { /** * @description Registers a new Asset to the User - * @returns Asset DID + * @returns Asset address */ async registerAsset(): Promise { const data = this._assetManagerInterface.encodeFunctionData( @@ -148,13 +149,25 @@ export class AssetsService { /** * @description Get Asset by Id - * @param id Asset Id + * @param id Asset decentralized identifier * @returns Asset */ async getAssetById({ id }: { id: string }) { return this._cacheClient.getAssetById(id); } + /** + * Returns owner of the given asset + * @param id asset decentralized identifier + */ + async getAssetOwner(id: string) { + const asset = await this.getAssetById({ id }); + if (!asset) { + throw new AssetNotExist(id); + } + return asset.owner; + } + /** * @description Get previously owned asset for a given DID * @param params.owner User DID