Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: 💍 claimManagement #42

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions envs/6.3.0.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CHAIN_IMAGE=polymeshassociation/polymesh:6.3.0-staging-debian
SUBQUERY_INDEXER_IMAGE=polymeshassociation/polymesh-subquery:v15.1.0-alpha.2
SUBQUERY_INDEXER_IMAGE=polymeshassociation/polymesh-subquery:v17.0.1

SUBQUERY_QUERY_IMAGE=onfinality/subql-query:v2.11.0
REST_IMAGE=polymeshassociation/polymesh-rest-api:v5.5.0-alpha.1
REST_IMAGE=polymeshassociation/polymesh-rest-api:v6.1.0-alpha.1
2 changes: 1 addition & 1 deletion envs/7.0.0.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CHAIN_IMAGE=polymeshassociation/polymesh:latest-develop-debian
SUBQUERY_INDEXER_IMAGE=polymeshassociation/polymesh-subquery:v18.0.2

SUBQUERY_QUERY_IMAGE=onfinality/subql-query:v2.11.0
SUBQUERY_QUERY_IMAGE=onfinality/subql-query:v2.19.0
REST_IMAGE=polymeshassociation/polymesh-rest-api:v7.0.0
131 changes: 131 additions & 0 deletions src/__tests__/rest/claims/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { ClaimType } from '@polymeshassociation/polymesh-sdk/types';

import { expectBasicTxInfo } from '~/__tests__/rest/utils';
import { TestFactory } from '~/helpers';
import { RestClient } from '~/rest';
import { createClaimParams } from '~/rest/claims/params';
import { Identity } from '~/rest/identities/interfaces';

const handles = ['issuer', 'target'];
let factory: TestFactory;

// TODO: fix this test
// eslint-disable-next-line
describe.skip('CustomClaim', () => {
let restClient: RestClient;
let signer: string;
let issuer: Identity;
let target: Identity;
let targetDid: string;
let issuerDid: string;
let claimParams: ReturnType<typeof createClaimParams>;

beforeAll(async () => {
factory = await TestFactory.create({ handles });
({ restClient } = factory);
issuer = factory.getSignerIdentity(handles[0]);
target = factory.getSignerIdentity(handles[1]);

signer = issuer.signer;
targetDid = target.did;
issuerDid = issuer.did;

claimParams = createClaimParams({
signer,
dryRun: false,
claims: [
{
target: targetDid,
claim: {
type: ClaimType.Exempted,
scope: {
type: 'Identity',
value: issuerDid,
},
},
},
],
});
});

afterAll(async () => {
await factory.close();
});

it('should list CDD claims for an identity', async () => {
const result = await restClient.identities.getCddClaims(issuerDid);

expect(result.results[0].claim).toMatchObject({
id: expect.any(String),
type: ClaimType.CustomerDueDiligence,
});
});

it('should add an `Exempted` claim', async () => {
const result = await restClient.claims.addClaim(claimParams);

expect(result).toMatchObject({
transactions: expect.arrayContaining([
{
transactionTag: 'identity.addClaim',
type: 'single',
...expectBasicTxInfo,
},
]),
});
});

it('should list Claims issued by an identity', async () => {
const result = await restClient.identities.getIssuedClaims(issuerDid);

expect(result).toMatchObject({
results: expect.arrayContaining([
expect.objectContaining({
issuer: issuerDid,
target: targetDid,
claim: expect.objectContaining({ claim: ClaimType.Custom }),
}),
]),
});
});

it('should list Claims associated with an identity', async () => {
const result = await restClient.identities.getAssociatedClaims(targetDid);

expect(result).toMatchObject({
results: expect.arrayContaining([
expect.objectContaining({
issuer: issuerDid,
target: targetDid,
claim: expect.objectContaining({ claim: ClaimType.Custom }),
}),
]),
});
});

it('should find claim scopes by did', async () => {
const result = await restClient.identities.findClaimScopesByDid(targetDid);

expect(result).toMatchObject({
results: expect.arrayContaining([
expect.objectContaining({
scope: expect.objectContaining({ type: 'Identity', value: issuerDid }),
}),
]),
});
});

it('should remove an `Exempted` claim', async () => {
const result = await restClient.claims.removeClaim(claimParams);

expect(result).toMatchObject({
transactions: expect.arrayContaining([
{
transactionTag: 'identity.revokeClaim',
type: 'single',
...expectBasicTxInfo,
},
]),
});
});
});
14 changes: 13 additions & 1 deletion src/__tests__/rest/claims/customClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,19 @@ describe('CustomClaim', () => {
});
});

it('should add a custom claim', async () => {
it('should find claim scopes by did', async () => {
const result = await restClient.identities.findClaimScopesByDid(targetDid);

expect(result).toMatchObject({
results: expect.arrayContaining([
expect.objectContaining({
scope: expect.objectContaining({ type: 'Identity', value: issuerDid }),
}),
]),
});
});

it('should remove a custom claim', async () => {
const result = await restClient.claims.removeClaim(claimParams);

expect(result).toMatchObject({
Expand Down
4 changes: 4 additions & 0 deletions src/rest/claims/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ export class Claims {
public async removeClaim(params: CreateClaimParams): Promise<PostResult> {
return this.client.post('/claims/remove', params);
}

public async editClaim(params: CreateClaimParams): Promise<PostResult> {
return this.client.post('/claims/remove', params);
}
}
16 changes: 16 additions & 0 deletions src/rest/identities/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,20 @@ export class Identities {
accounts,
});
}

public async getIssuedClaims(did: string): Promise<ResultSet<unknown>> {
return this.client.get(`/identities/${did}/issued-claims`);
}

public async getAssociatedClaims(did: string): Promise<ResultSet<unknown>> {
return this.client.get(`/identities/${did}/associated-claims`);
}

public async getCddClaims(did: string): Promise<ResultSet<Record<string, unknown>>> {
return this.client.get(`/identities/${did}/cdd-claims`);
}

public async findClaimScopesByDid(did: string): Promise<ResultSet<Record<string, unknown>>> {
return this.client.get(`/identities/${did}/claim-scopes`);
}
}
25 changes: 24 additions & 1 deletion src/sdk/identities/claims.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Polymesh } from '@polymeshassociation/polymesh-sdk';
import { BigNumber, Polymesh } from '@polymeshassociation/polymesh-sdk';
import { ClaimType, ScopeType } from '@polymeshassociation/polymesh-sdk/types';
import assert from 'node:assert';

Expand Down Expand Up @@ -87,4 +87,27 @@ export const manageClaims = async (
// `target` here refers to the issuer of the claim
const claimsIssuedByTarget = await sdk.claims.getIssuedClaims({ target: targetDid });
assert(Array.isArray(claimsIssuedByTarget.data), 'Data should be an Array for `getIssuedClaims`');

// get identities with claims using filters
const identitiesWithCddClaims = await sdk.claims.getIdentitiesWithClaims({
claimTypes: [ClaimType.CustomerDueDiligence],
size: new BigNumber(1),
start: new BigNumber(0),
});
assert(Array.isArray(identitiesWithCddClaims.data));
expect(identitiesWithCddClaims.data.length).toBe(1);
assert(identitiesWithCddClaims.data.some((i) => i.identity.did === targetDid));

// get the revoked accredited claim by filtering
const identitiesWithAccreditedClaim = await sdk.claims.getIdentitiesWithClaims({
targets: [targetDid],
claimTypes: [ClaimType.Accredited],
size: new BigNumber(1),
start: new BigNumber(0),
includeExpired: true,
trustedClaimIssuers: [identity.did],
});
assert(Array.isArray(identitiesWithAccreditedClaim.data));
expect(identitiesWithAccreditedClaim.data.length).toBe(1);
assert(identitiesWithAccreditedClaim.data[0].identity.did === targetDid);
};
Loading