Skip to content

Commit

Permalink
fix(test): fix pre tests after a bad rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Oct 6, 2023
1 parent d5c70a5 commit acb0fa1
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 45 deletions.
16 changes: 11 additions & 5 deletions packages/pre/test/acceptance/delay-enact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import {
fakePorterUri,
fakeProvider,
fakeSigner,
fakeUrsulas,
mockGetUrsulas,
} from '@nucypher/test-utils';
import { expect, test } from 'vitest';
import { beforeAll, describe, expect, it } from 'vitest';

import { initialize } from '../../src';
import {
fakeAlice,
fakeRemoteBob,
Expand All @@ -15,17 +17,21 @@ import {
mockPublishToBlockchain,
} from '../utils';

test('story: alice creates a policy but someone else enacts it', () => {
describe('story: alice creates a policy but someone else enacts it', () => {
const threshold = 2;
const shares = 3;
const startDate = new Date();
const endDate = new Date(Date.now() + 60 * 1000); // 60s later
const label = 'fake-data-label';

test('verifies capsule frags', async () => {
test('alice generates a new policy', async () => {
describe('verifies capsule frags', async () => {
beforeAll(async () => {
await initialize();
});

it('alice generates a new policy', async () => {
const provider = fakeProvider();
const getUrsulasSpy = mockGetUrsulas();
const getUrsulasSpy = mockGetUrsulas(fakeUrsulas(shares));
const generateKFragsSpy = mockGenerateKFrags();
const publishToBlockchainSpy = mockPublishToBlockchain();
const encryptTreasureMapSpy = mockEncryptTreasureMap();
Expand Down
14 changes: 9 additions & 5 deletions packages/pre/test/enrico.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
// Disabling because we want to access Alice.keyring which is a private property
/* eslint-disable @typescript-eslint/no-explicit-any */
import { bytesEqual, fromBytes } from '@nucypher/test-utils';
import { expect, test } from 'vitest';
import { beforeAll, describe, expect, it } from 'vitest';

import { Enrico, toBytes } from '../src';
import { Enrico, initialize, toBytes } from '../src';
import { PolicyMessageKit, RetrievalResult } from '../src/kits';

import { fakeAlice, fakeBob, reencryptKFrags } from './utils';

test('enrico', () => {
test('alice decrypts message encrypted by enrico', async () => {
describe('enrico', () => {
beforeAll(async () => {
await initialize();
});

it('alice decrypts message encrypted by enrico', async () => {
const label = 'fake-label';
const message = 'fake-message';
const alice = fakeAlice();
Expand All @@ -24,7 +28,7 @@ test('enrico', () => {
expect(alicePlaintext).toEqual(alicePlaintext);
});

test('bob decrypts reencrypted message', async () => {
it('bob decrypts reencrypted message', async () => {
const label = 'fake-label';
const alice = fakeAlice();
const bob = fakeBob();
Expand Down
12 changes: 8 additions & 4 deletions packages/pre/test/message-kit.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { expect, test } from 'vitest';
import { beforeAll, describe, expect, it } from 'vitest';

import { MessageKit, toBytes } from '../src';
import { initialize, MessageKit, toBytes } from '../src';

import { fakeBob } from './utils';

test('message kit', () => {
test('bob decrypts', () => {
describe('message kit', () => {
beforeAll(async () => {
await initialize();
});

it('bob decrypts', () => {
const bob = fakeBob();
const plaintext = toBytes('fake-message');
const messageKit = new MessageKit(bob.decryptingKey, plaintext, null);
Expand Down
25 changes: 13 additions & 12 deletions packages/pre/test/pre.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import { CapsuleFrag, reencrypt } from '@nucypher/nucypher-core';
import { CapsuleFrag, initialize, reencrypt } from '@nucypher/nucypher-core';
import { zip } from '@nucypher/shared';
import { fakeUrsulas } from '@nucypher/test-utils';
import { beforeAll, expect, test } from 'vitest';
import { beforeAll, describe, expect, it } from 'vitest';

import { Alice, Bob, Enrico, MessageKit, toBytes } from '../src';
import { PolicyMessageKit, RetrievalResult } from '../src/kits';

import { fakeAlice, fakeBob, reencryptKFrags } from './utils';

test('proxy reencryption', () => {
describe('proxy reencryption', () => {
let alice: Alice;
let bob: Bob;
const plaintext = toBytes('plaintext-message');
const threshold = 2;
const shares = 3;
const label = 'fake-data-label';

test('verifies capsule frags', async () => {
beforeAll(async () => {
bob = fakeBob();
alice = fakeAlice();
});
beforeAll(async () => {
await initialize();
bob = fakeBob();
alice = fakeAlice();
});

it('verifies capsule frags', async () => {
const { capsule } = new MessageKit(bob.decryptingKey, plaintext, null);
const { delegatingKey, verifiedKFrags } = alice.generateKFrags(
bob,
Expand All @@ -45,7 +46,7 @@ test('proxy reencryption', () => {
expect(areVerified).toBeTruthy();
});

test('encrypts and decrypts reencrypted message', async () => {
it('encrypts and decrypts reencrypted message', async () => {
const { verifiedKFrags } = alice.generateKFrags(
bob,
label,
Expand All @@ -57,7 +58,7 @@ test('proxy reencryption', () => {
const enrico = new Enrico(policyEncryptingKey);
const encryptedMessage = enrico.encryptMessage(plaintext);

const ursulaAddresses = fakeUrsulas().map(
const ursulaAddresses = fakeUrsulas(3).map(
(ursula) => ursula.checksumAddress,
);
const reencrypted = verifiedKFrags.map((kFrag) =>
Expand All @@ -77,7 +78,7 @@ test('proxy reencryption', () => {
expect(bobPlaintext).toEqual(plaintext);
});

test('encrypts and decrypts reencrypted message with conditions', async () => {
it('encrypts and decrypts reencrypted message with conditions', async () => {
const { verifiedKFrags } = alice.generateKFrags(
bob,
label,
Expand All @@ -90,7 +91,7 @@ test('proxy reencryption', () => {
const enrico = new Enrico(policyEncryptingKey);
const encryptedMessage = enrico.encryptMessage(plaintext);

const ursulaAddresses = fakeUrsulas().map(
const ursulaAddresses = fakeUrsulas(shares).map(
(ursula) => ursula.checksumAddress,
);
const reencrypted = verifiedKFrags.map((kFrag) =>
Expand Down
12 changes: 8 additions & 4 deletions packages/taco/test/ritual.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { DkgPublicKey } from '@nucypher/nucypher-core';
import { fromHexString } from '@nucypher/shared';
import { expect, test } from 'vitest';
import { fromHexString, initialize } from '@nucypher/shared';
import { beforeAll, describe, expect, it } from 'vitest';

test('Ritual', () => {
test('deserializes pre-made dkg ritual', async () => {
describe('Ritual', () => {
beforeAll(async () => {
await initialize();
});

it('deserializes pre-made dkg ritual', async () => {
const pkWord1 = fromHexString(
'9045795411ed251bf2eecc9415552c41863502a207104ef7ab482bc2364729d9',
);
Expand Down
25 changes: 18 additions & 7 deletions packages/taco/test/taco.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { FerveoVariant, SessionStaticSecret } from '@nucypher/nucypher-core';
import {
FerveoVariant,
initialize,
SessionStaticSecret,
} from '@nucypher/nucypher-core';
import {
aliceSecretKeyBytes,
fakeDkgFlow,
Expand All @@ -9,16 +13,17 @@ import {
mockCbdDecrypt,
mockGetRitualIdFromPublicKey,
} from '@nucypher/test-utils';
import { expect, test } from 'vitest';
import { beforeAll, describe, expect, it } from 'vitest';

import * as taco from '../src';
import { conditions, toBytes } from '../src';

import {
fakeDkgRitual,
mockDkgParticipants,
mockGetFinalizedRitualSpy,
mockGetFinalizedRitual,
mockGetParticipants,
mockIsEncryptionAuthorized,
mockMakeSessionKey,
} from './test-utils';

Expand All @@ -30,13 +35,18 @@ const ownsNFT = new conditions.ERC721Ownership({
chain: 5,
});

test('taco', () => {
test('encrypts and decrypts', async () => {
describe('taco', () => {
beforeAll(async () => {
await initialize();
});

it('encrypts and decrypts', async () => {
const mockedDkg = fakeDkgFlow(FerveoVariant.precomputed, 0, 4, 4);
const mockedDkgRitual = fakeDkgRitual(mockedDkg);
const provider = fakeProvider(aliceSecretKeyBytes);
const signer = fakeSigner(aliceSecretKeyBytes);
const getFinalizedRitualSpy = mockGetFinalizedRitualSpy(mockedDkgRitual);
const getFinalizedRitualSpy = mockGetFinalizedRitual(mockedDkgRitual);
const isEncryptionAuthorizedSpy = mockIsEncryptionAuthorized();

const messageKit = await taco.encrypt(
provider,
Expand All @@ -46,6 +56,7 @@ test('taco', () => {
signer,
);
expect(getFinalizedRitualSpy).toHaveBeenCalled();
expect(isEncryptionAuthorizedSpy).toHaveBeenCalled();

const { decryptionShares } = fakeTDecFlow({
...mockedDkg,
Expand All @@ -68,7 +79,7 @@ test('taco', () => {
const getRitualIdFromPublicKey = mockGetRitualIdFromPublicKey(
mockedDkg.ritualId,
);
const getRitualSpy = mockGetFinalizedRitualSpy(mockedDkgRitual);
const getRitualSpy = mockGetFinalizedRitual(mockedDkgRitual);

const decryptedMessage = await taco.decrypt(
provider,
Expand Down
14 changes: 11 additions & 3 deletions packages/taco/test/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,22 @@ export const mockGetRitual = (): SpyInstance => {
);
};

export const mockGetFinalizedRitualSpy = (
dkgRitual: DkgRitual,
): SpyInstance => {
export const mockGetFinalizedRitual = (dkgRitual: DkgRitual): SpyInstance => {
return vi.spyOn(DkgClient, 'getFinalizedRitual').mockImplementation(() => {
return Promise.resolve(dkgRitual);
});
};

export const mockIsEncryptionAuthorized = (
isAuthorized = true,
): SpyInstance => {
return vi
.spyOn(DkgCoordinatorAgent, 'isEncryptionAuthorized')
.mockImplementation(async () => {
return Promise.resolve(isAuthorized);
});
};

export const mockMakeSessionKey = (secret: SessionStaticSecret) => {
return vi
.spyOn(SessionStaticSecret, 'random')
Expand Down
2 changes: 2 additions & 0 deletions packages/test-utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ export const fakeProvider = (

const genChecksumAddress = (i: number) =>
'0x' + '0'.repeat(40 - i.toString(16).length) + i.toString(16);

const genEthAddr = (i: number) =>
EthereumAddress.fromString(genChecksumAddress(i));

export const fakeUrsulas = (n = 4): Ursula[] =>
// 0...n-1
Array.from(Array(n).keys()).map((i: number) => ({
Expand Down
5 changes: 0 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit acb0fa1

Please sign in to comment.