Skip to content

Commit

Permalink
chore: change create iam in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiter committed Jul 12, 2021
1 parent 06b990b commit 64b4b31
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 145 deletions.
39 changes: 16 additions & 23 deletions test/application.testSuite.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ENSNamespaceTypes, IAM } from "../src/iam";
import { iam, root, rootOwner } from "./iam.test";
import { ENSNamespaceTypes } from "../src/iam";
import { createIam, rootOwnerIam, root, rootOwner } from "./iam.test";
import { org1 } from "./organization.testSuite";
import { ensResolver, replenish } from "./setup_contracts";
import { utils } from "ethers";
Expand All @@ -13,61 +13,54 @@ export const appsTests = () => {

test("application can be created", async () => {
const appDefinition = { appName: "Application 1" };
await iam.createApplication({
await rootOwnerIam.createApplication({
appName: app,
data: appDefinition,
namespace: `${ENSNamespaceTypes.Application}.${org1}.${root}`
});

expect(await iam.checkExistenceOfDomain({ domain: appNode })).toBe(true);
expect(await rootOwnerIam.checkExistenceOfDomain({ domain: appNode })).toBe(true);
expect(
await iam.checkExistenceOfDomain({ domain: `${ENSNamespaceTypes.Roles}.${appNode}` })
await rootOwnerIam.checkExistenceOfDomain({ domain: `${ENSNamespaceTypes.Roles}.${appNode}` })
).toBe(true);
expect(await ensResolver.name(namehash(appNode))).toBe(appNode);
expect(
await iam.getSubdomains({ domain: `${ENSNamespaceTypes.Application}.${org1}.${root}` })
await rootOwnerIam.getSubdomains({ domain: `${ENSNamespaceTypes.Application}.${org1}.${root}` })
).toContain(`${app}.${ENSNamespaceTypes.Application}.${org1}.${root}`);

const readAppDefinition = await iam.getDefinition({ type: ENSNamespaceTypes.Application, namespace: appNode});
const readAppDefinition = await rootOwnerIam.getDefinition({ type: ENSNamespaceTypes.Application, namespace: appNode });
expect(readAppDefinition).toMatchObject(appDefinition);
});

test("application owner can be changed", async () => {
const newOwner = new Keys();
await replenish(newOwner.getAddress());
const newOwnerIam = new IAM({
rpcUrl: "http://localhost:8544/",
privateKey: newOwner.privateKey
});
await newOwnerIam.initializeConnection({
reinitializeMetamask: false,
walletProvider: undefined
});
const newOwnerIam = await createIam(newOwner.privateKey);

expect(await iam.isOwner({ domain: appNode, user: rootOwner.getAddress() }));
expect(await rootOwnerIam.isOwner({ domain: appNode, user: rootOwner.address }));

await iam.changeAppOwnership({
await rootOwnerIam.changeAppOwnership({
namespace: appNode,
newOwner: newOwner.getAddress()
});

expect(await iam.isOwner({ domain: appNode, user: newOwner.getAddress() }));
expect(await rootOwnerIam.isOwner({ domain: appNode, user: newOwner.getAddress() }));

await newOwnerIam.changeAppOwnership({
namespace: appNode,
newOwner: rootOwner.getAddress()
newOwner: rootOwner.address
});

expect(await iam.isOwner({ domain: appNode, user: rootOwner.getAddress() }));
expect(await rootOwnerIam.isOwner({ domain: appNode, user: rootOwner.address }));
});

test("application can be deleted", async () => {
expect(await iam.checkExistenceOfDomain({ domain: appNode })).toBe(true);
expect(await rootOwnerIam.checkExistenceOfDomain({ domain: appNode })).toBe(true);

await iam.deleteApplication({
await rootOwnerIam.deleteApplication({
namespace: appNode
});

expect(await iam.checkExistenceOfDomain({ domain: appNode })).toBe(false);
expect(await rootOwnerIam.checkExistenceOfDomain({ domain: appNode })).toBe(false);
});
};
58 changes: 32 additions & 26 deletions test/assets.testsuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@ import { Keys } from "@ew-did-registry/keys";
import { OfferableIdentityFactory } from "../ethers/OfferableIdentityFactory";
import { IAM } from "../src/iam";
import { emptyAddress } from "../src/utils/constants";
import { iam, provider, rootOwner, rpcUrl } from "./iam.test";
import { replenish } from "./setup_contracts";
import { PubKeyType } from '@ew-did-registry/did-resolver-interface/src/models/operator';
import { Algorithms, DIDAttribute, Encoding } from '@ew-did-registry/did-resolver-interface';
import { Methods } from '@ew-did-registry/did';
import { createIam, rootOwner } from "./iam.test";
import { replenish, provider, rpcUrl } from "./setup_contracts";
import { PubKeyType } from "@ew-did-registry/did-resolver-interface/src/models/operator";
import { Algorithms, DIDAttribute, Encoding } from "@ew-did-registry/did-resolver-interface";
import { Methods } from "@ew-did-registry/did";

export const assetsTests = () => {
let rootOwnerIam: IAM;

beforeAll(async () => {
rootOwnerIam = await createIam(rootOwner.privateKey, { initDID: true });
});

test("asset should be created", async () => {
const assetAddress = await iam.registerAsset();
const assetAddress = await rootOwnerIam.registerAsset();
const assetContract = OfferableIdentityFactory.connect(assetAddress, provider);
const owner = await assetContract.owner();
expect(owner).toBe(rootOwner.getAddress());
expect(owner).toBe(rootOwner.address);
});
test("asset should be offered", async () => {
const assetAddress = await iam.registerAsset();
const assetAddress = await rootOwnerIam.registerAsset();
const newOwner = new Keys();
await iam.offerAsset({
await rootOwnerIam.offerAsset({
assetDID: `did:ethr:${assetAddress}`,
offerTo: `did:ethr:${newOwner.getAddress()}`
});
Expand All @@ -27,31 +33,31 @@ export const assetsTests = () => {
expect(offered).toBe(newOwner.getAddress());
});
test("asset should be able to cancel offer", async () => {
const assetAddress = await iam.registerAsset();
const assetAddress = await rootOwnerIam.registerAsset();
const newOwner = new Keys();
const assetDID = `did:ethr:${assetAddress}`;
await iam.offerAsset({
await rootOwnerIam.offerAsset({
assetDID,
offerTo: `did:ethr:${newOwner.getAddress()}`
});
const assetContract = OfferableIdentityFactory.connect(assetAddress, provider);
const offered = await assetContract.offeredTo();
expect(offered).toBe(newOwner.getAddress());
await iam.cancelAssetOffer({ assetDID });
await rootOwnerIam.cancelAssetOffer({ assetDID });
const notOffered = await assetContract.offeredTo();
expect(notOffered).toBe(emptyAddress);
});
test("asset should be able to accept offer", async () => {
const assetAddress = await iam.registerAsset();
const assetAddress = await rootOwnerIam.registerAsset();
const newOwner = new Keys();
await replenish(newOwner.getAddress());
const assetDID = `did:ethr:${assetAddress}`;
await iam.offerAsset({ assetDID, offerTo: `did:ethr:${newOwner.getAddress()}` });
await rootOwnerIam.offerAsset({ assetDID, offerTo: `did:ethr:${newOwner.getAddress()}` });
const newOwnerIAM = new IAM({
privateKey: newOwner.privateKey,
rpcUrl
rpcUrl,
});
await newOwnerIAM.initializeConnection();
await newOwnerIAM.initializeConnection({ initCacheServer: false, initDID: false });
await newOwnerIAM.acceptAssetOffer({
assetDID
});
Expand All @@ -63,49 +69,49 @@ export const assetsTests = () => {
});

test("asset should be able to reject offer", async () => {
const assetAddress = await iam.registerAsset();
const assetAddress = await rootOwnerIam.registerAsset();
const newOwner = new Keys();
await replenish(newOwner.getAddress());
const assetDID = `did:ethr:${assetAddress}`;
await iam.offerAsset({ assetDID, offerTo: `did:ethr:${newOwner.getAddress()}` });
await rootOwnerIam.offerAsset({ assetDID, offerTo: `did:ethr:${newOwner.getAddress()}` });
const newOwnerIAM = new IAM({
privateKey: newOwner.privateKey,
rpcUrl
});
await newOwnerIAM.initializeConnection();
await newOwnerIAM.initializeConnection({ initCacheServer: false, initDID: false });
await newOwnerIAM.rejectAssetOffer({
assetDID
});
const assetContract = OfferableIdentityFactory.connect(assetAddress, provider);
const owner = await assetContract.owner();
const offeredTo = await assetContract.offeredTo();
expect(owner).toBe(rootOwner.getAddress());
expect(owner).toBe(rootOwner.address);
expect(offeredTo).toBe(emptyAddress);
});

test("update did document for asset", async () => {
const assetAddress = await iam.registerAsset();
const assetAddress = await rootOwnerIam.registerAsset();

const asset1 = await iam.getDidDocument({ did: `did:${Methods.Erc1056}:${assetAddress}` });
const asset1 = await rootOwnerIam.getDidDocument({ did: `did:${Methods.Erc1056}:${assetAddress}` });
expect(asset1.publicKey.length).toBe(0);

const update = await iam.updateDidDocument({
const update = await rootOwnerIam.updateDidDocument({
didAttribute: DIDAttribute.PublicKey,
did: `did:ethr:${assetAddress}`,
data: {
algo: Algorithms.Secp256k1,
encoding: Encoding.HEX,
type: PubKeyType.SignatureAuthentication2018,
value: { tag: 'key-1', publicKey: `0x${new Keys().publicKey}` }
value: { tag: "key-1", publicKey: `0x${new Keys().publicKey}` }
}
});
expect(update).toBeTruthy();

const asset = await iam.getDidDocument({ did: `did:${Methods.Erc1056}:${assetAddress}` });
const asset = await rootOwnerIam.getDidDocument({ did: `did:${Methods.Erc1056}:${assetAddress}` });
expect(asset.publicKey.length).toBe(1);

const did = `did:${Methods.Erc1056}:${assetAddress}#key-1`;
const type = 'Secp256k1sigAuth';
const type = "Secp256k1sigAuth";
expect(asset.publicKey.find((asset) => asset.id === did && asset.type === type)).toBeTruthy();
});
};
15 changes: 9 additions & 6 deletions test/claimsTests/enrollmentClaimsTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { JSONCodec } from "nats.ws";
import { IAM, RegistrationTypes } from "../../src/iam";
import { IRoleDefinition, NATS_EXCHANGE_TOPIC } from "../../src/iam-client-lib";
import { createIam, root, rootOwner } from "../iam.test";
import { claimManager } from "../setup_contracts";
import { claimManager, replenish } from "../setup_contracts";

const { namehash } = utils;

Expand All @@ -15,7 +15,7 @@ export function enrollmentClaimsTests() {
const staticIssuerDID = `did:${Methods.Erc1056}:${staticIissuer.address}`;
const dynamicIssuer = Wallet.createRandom();
const dynamicIssuerDID = `did:${Methods.Erc1056}:${dynamicIssuer.address}`;
const roleCreator = new Wallet(rootOwner.privateKey); // owns root
const roleCreator = rootOwner; // owns root
const roleCreatorDID = `did:${Methods.Erc1056}:${roleCreator.address}`;
const subject = roleCreator;
const subjectDID = `did:${Methods.Erc1056}:${subject.address}`;
Expand Down Expand Up @@ -50,10 +50,13 @@ export function enrollmentClaimsTests() {
let _jsonCodec;

beforeAll(async () => {
roleCreatorIam = await createIam(roleCreator.privateKey);
subjectIam = await createIam(subject.privateKey);
staticIssuerIam = await createIam(staticIissuer.privateKey);
dynamicIssuerIam = await createIam(dynamicIssuer.privateKey);
await replenish(roleCreator.address);
roleCreatorIam = await createIam(roleCreator.privateKey, { initDID: true });
subjectIam = await createIam(subject.privateKey, { initDID: true });
await replenish(staticIissuer.address);
staticIssuerIam = await createIam(staticIissuer.privateKey, { initDID: true });
await replenish(dynamicIssuer.address);
dynamicIssuerIam = await createIam(dynamicIssuer.privateKey, { initDID: true });

await roleCreatorIam.createRole({
roleName: roleName1,
Expand Down
52 changes: 34 additions & 18 deletions test/claimsTests/selfsignedClaimsTests.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
import { iam } from "../iam.test";
import { IAM } from "../../src/iam";
import { rootOwner } from "../iam.test";
import { rpcUrl } from "../setup_contracts";

export const selfsignedClaimsTests = function () {
let rootOwnerIam: IAM;

const namespace = "daniel.iam.ewc";

beforeAll(async () => {
rootOwnerIam = new IAM({
rpcUrl,
privateKey: rootOwner.privateKey
});
await rootOwnerIam.initializeConnection({
reinitializeMetamask: false,
initCacheServer: false,
initDID: true
});
});

test("ens claim can be created", async () => {
await iam.createSelfSignedClaim({
await rootOwnerIam.createSelfSignedClaim({
data: { claimType: namespace, claimTypeVersion: 1 }
});
const { service = [] } = await iam.getDidDocument();
const { service = [] } = await rootOwnerIam.getDidDocument();
expect(service.find(({ claimType }) => claimType === namespace)).toBeTruthy();
});
test("ens claim should by updated", async () => {
const { service = [] } = await iam.getDidDocument();
const { service = [] } = await rootOwnerIam.getDidDocument();
const { claimType, claimTypeVersion } =
service.find(({ claimType }) => claimType === namespace) || {};
await iam.createSelfSignedClaim({ data: { claimType, claimTypeVersion } });
const { service: newServices = [] } = await iam.getDidDocument();
await rootOwnerIam.createSelfSignedClaim({ data: { claimType, claimTypeVersion } });
const { service: newServices = [] } = await rootOwnerIam.getDidDocument();
expect(
newServices.find(
claim => claim.claimType === claimType && claim.claimTypeVersion === claimTypeVersion
Expand All @@ -24,12 +40,12 @@ export const selfsignedClaimsTests = function () {
expect(newServices.length).toBe(1);
});
test("ens claim should be created when claimType do not match", async () => {
const { service = [] } = await iam.getDidDocument();
const { service = [] } = await rootOwnerIam.getDidDocument();
const { claimTypeVersion } = service.find(({ claimType }) => claimType === namespace) || {};
await iam.createSelfSignedClaim({
await rootOwnerIam.createSelfSignedClaim({
data: { claimType: "edvin.iam.ewc", claimTypeVersion }
});
const { service: newServices = [] } = await iam.getDidDocument();
const { service: newServices = [] } = await rootOwnerIam.getDidDocument();
expect(
newServices.find(
claim => claim.claimType === "edvin.iam.ewc" && claim.claimTypeVersion === claimTypeVersion
Expand All @@ -38,35 +54,35 @@ export const selfsignedClaimsTests = function () {
expect(newServices.length).toBe(2);
});
test("ens claim should be created when claimTypeVersion do not match", async () => {
await iam.createSelfSignedClaim({
await rootOwnerIam.createSelfSignedClaim({
data: { claimType: namespace, claimTypeVersion: 2 }
});
const { service: newServices = [] } = await iam.getDidDocument();
const { service: newServices = [] } = await rootOwnerIam.getDidDocument();
expect(
newServices.find(claim => claim.claimType === namespace && claim.claimTypeVersion === 2)
).toBeTruthy();
expect(newServices.length).toBe(3);
});
test("ens claim should not be created", async () => {
await iam.createSelfSignedClaim({
await rootOwnerIam.createSelfSignedClaim({
data: { claimType: namespace, claimTypeVersion: 2 }
});
const { service: newServices = [] } = await iam.getDidDocument();
const { service: newServices = [] } = await rootOwnerIam.getDidDocument();
expect(newServices.length).toBe(3);
});

test("profile claim should be created", async () => {
await iam.createSelfSignedClaim({
await rootOwnerIam.createSelfSignedClaim({
data: { profile: { name: "Edwin" } }
});
const { service = [] } = await iam.getDidDocument();
const { service = [] } = await rootOwnerIam.getDidDocument();
expect(service.find(claim => claim.profile && claim.profile.name === "Edwin")).toBeTruthy();
});
test("profile claim should be updated", async () => {
const { service = [] } = await iam.getDidDocument();
const { service = [] } = await rootOwnerIam.getDidDocument();
const { id } = service.find(({ profile }) => Boolean(profile)) || {};
await iam.createSelfSignedClaim({ data: { profile: { name: "Dan" } } });
const { service: updatedService = [] } = await iam.getDidDocument();
await rootOwnerIam.createSelfSignedClaim({ data: { profile: { name: "Dan" } } });
const { service: updatedService = [] } = await rootOwnerIam.getDidDocument();
const { profile } = updatedService.find(({ id: claimId }) => id === claimId) || {};
expect(profile?.name).toBe("Dan");
expect(updatedService.length).toBe(4);
Expand Down
Loading

0 comments on commit 64b4b31

Please sign in to comment.