Skip to content

Commit

Permalink
Refactor Get started tests
Browse files Browse the repository at this point in the history
The setup function run a a complete scenario as shown in docs.
Assertions are done in test function. This makes code examples easy to
copy and paste into docs.
  • Loading branch information
manumonti committed Jan 17, 2023
1 parent ea360dc commit b5f37b2
Showing 1 changed file with 90 additions and 88 deletions.
178 changes: 90 additions & 88 deletions test/docs/cbd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,20 @@ import {
} from '../utils';

describe('Get Started (CBD PoC)', () => {
beforeAll(() => {
jest
.spyOn(providers, 'Web3Provider')
.mockImplementation(() =>
mockWeb3Provider(SecretKey.random().toSecretBytes())
);
});

afterAll(() => {
jest.unmock('ethers');
});

it('can run the get started example', async () => {
const setup = async () => {
const detectEthereumProvider = mockDetectEthereumProvider();
const mockedUrsulas = mockUrsulas();
const getUrsulasSpy = mockGetUrsulas(mockedUrsulas);
const generateKFragsSpy = mockGenerateKFrags();
const publishToBlockchainSpy = mockPublishToBlockchain();
const makeTreasureMapSpy = mockMakeTreasureMap();
const encryptTreasureMapSpy = mockEncryptTreasureMap();
const detectEthereumProvider = mockDetectEthereumProvider();

jest
.spyOn(providers, 'Web3Provider')
.mockImplementation(() =>
mockWeb3Provider(SecretKey.random().toSecretBytes())
);

// Start of 2. Build a Cohort
const config = {
Expand All @@ -51,12 +45,6 @@ describe('Get Started (CBD PoC)', () => {
porterUri: 'https://porter-tapir.nucypher.community',
};
const newCohort = await Cohort.create(config);
// End of 2. Build a Cohort

const expectedAddresses = mockedUrsulas.map((u) => u.checksumAddress);
expect(newCohort.ursulaAddresses).toEqual(expectedAddresses);
expect(getUrsulasSpy).toHaveBeenCalled();
expect(newCohort.configuration).toEqual(config);

// Start of 3. Specify default Conditions
const NFTOwnership = new Conditions.ERC721Ownership({
Expand All @@ -69,83 +57,97 @@ describe('Get Started (CBD PoC)', () => {
NFTOwnership,
// Other conditions can be added here
]);
// End of 3. Specify default Conditions

const condObj = conditions.conditions[0].toObj();
expect(condObj).toEqual(NFTOwnership.toObj());
expect(condObj.parameters).toEqual([5954]);
expect(condObj.chain).toEqual(5);
expect(condObj.contractAddress).toEqual(
'0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D'
);
expect(conditions.validate()).toEqual(true);

// Start of 4. Build a Strategy
const newStrategy = Strategy.create(newCohort, conditions);

const MMprovider = await detectEthereumProvider();
const mumbai = providers.getNetwork(80001);

if (MMprovider) {
const web3Provider = new providers.Web3Provider(MMprovider, mumbai);
const newDeployed = await newStrategy.deploy('test', web3Provider);
// End of 4. Build a Strategy

expect(publishToBlockchainSpy).toHaveBeenCalled();
expect(newDeployed.label).toEqual('test');

// Start of 5. Encrypt the plaintext & update Conditions
const NFTBalanceConfig = {
contractAddress: '0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D',
standardContractType: 'ERC721',
chain: 5,
method: 'balanceOf',
parameters: [':userAddress'],
returnValueTest: {
comparator: '>=',
value: 3,
},
};
const NFTBalance = new Conditions.Condition(NFTBalanceConfig);

const encrypter = newDeployed.encrypter;

const plaintext = 'this is a secret';
const encryptedMessageKit = encrypter.encryptMessage(
plaintext,
new ConditionSet([NFTBalance])
);
// End of 5. Encrypt the plaintext & update Conditions

expect(getUrsulasSpy).toHaveBeenCalledTimes(2);
expect(generateKFragsSpy).toHaveBeenCalled();
expect(encryptTreasureMapSpy).toHaveBeenCalled();
expect(makeTreasureMapSpy).toHaveBeenCalled();

// Setup mocks for `retrieveAndDecrypt`
const ursulaAddresses = (
makeTreasureMapSpy.mock.calls[0][0] as readonly Ursula[]
).map((u) => u.checksumAddress);
const verifiedKFrags = makeTreasureMapSpy.mock
.calls[0][1] as readonly VerifiedKeyFrag[];
const retrieveCFragsSpy = mockRetrieveCFragsRequest(
ursulaAddresses,
verifiedKFrags,
encryptedMessageKit.capsule
);
const web3Provider = new providers.Web3Provider(MMprovider, mumbai);
const newDeployed = await newStrategy.deploy('test', web3Provider);

// Start of 5. Encrypt the plaintext & update Conditions
const NFTBalanceConfig = {
contractAddress: '0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D',
standardContractType: 'ERC721',
chain: 5,
method: 'balanceOf',
parameters: [':userAddress'],
returnValueTest: {
comparator: '>=',
value: 3,
},
};
const NFTBalance = new Conditions.Condition(NFTBalanceConfig);

// Start of 6. Request decryption rights
const decrypter = newDeployed.decrypter;
const encrypter = newDeployed.encrypter;

const conditionContext = conditions.buildContext(web3Provider);
const decryptedMessage = await decrypter.retrieveAndDecrypt(
[encryptedMessageKit],
conditionContext
);
// End of 6. Request decryption rights
const plaintext = 'this is a secret';
const encryptedMessageKit = encrypter.encryptMessage(
plaintext,
new ConditionSet([NFTBalance])
);
// End of 5. Encrypt the plaintext & update Conditions

// Setup mocks for `retrieveAndDecrypt`
const ursulaAddresses = (
makeTreasureMapSpy.mock.calls[0][0] as readonly Ursula[]
).map((u) => u.checksumAddress);
const verifiedKFrags = makeTreasureMapSpy.mock
.calls[0][1] as readonly VerifiedKeyFrag[];
const retrieveCFragsSpy = mockRetrieveCFragsRequest(
ursulaAddresses,
verifiedKFrags,
encryptedMessageKit.capsule
);

// Start of 6. Request decryption rights
const decrypter = newDeployed.decrypter;

const conditionContext = conditions.buildContext(web3Provider);
const decryptedMessage = await decrypter.retrieveAndDecrypt(
[encryptedMessageKit],
conditionContext
);
// End of 6. Request decryption rights

jest.unmock('ethers');

return {
newCohort,
conditions,
newDeployed,
decryptedMessage,
getUrsulasSpy,
generateKFragsSpy,
encryptTreasureMapSpy,
makeTreasureMapSpy,
publishToBlockchainSpy,
retrieveCFragsSpy,
};
};

it('can run the get started example', async () => {
const getStarted = await setup();

expect(retrieveCFragsSpy).toHaveBeenCalled();
expect(decryptedMessage[0]).toEqual(toBytes(plaintext));
}
const plaintext = 'this is a secret';
const expectedAddresses = mockUrsulas().map((u) => u.checksumAddress);
const condObj = getStarted.conditions.conditions[0].toObj();
expect(getStarted.newCohort.ursulaAddresses).toEqual(expectedAddresses);
expect(condObj.parameters).toEqual([5954]);
expect(condObj.chain).toEqual(5);
expect(condObj.contractAddress).toEqual(
'0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D'
);
expect(getStarted.conditions.validate()).toEqual(true);
expect(getStarted.publishToBlockchainSpy).toHaveBeenCalled();
expect(getStarted.newDeployed.label).toEqual('test');
expect(getStarted.getUrsulasSpy).toHaveBeenCalledTimes(2);
expect(getStarted.generateKFragsSpy).toHaveBeenCalled();
expect(getStarted.encryptTreasureMapSpy).toHaveBeenCalled();
expect(getStarted.makeTreasureMapSpy).toHaveBeenCalled();
expect(getStarted.retrieveCFragsSpy).toHaveBeenCalled();
expect(getStarted.decryptedMessage[0]).toEqual(toBytes(plaintext));
});
});

0 comments on commit b5f37b2

Please sign in to comment.