Skip to content

Commit

Permalink
remove ts-sinon dependency, upgrade sinon (#804)
Browse files Browse the repository at this point in the history
`ts-sinon` is no longer maintained, and doesn't seem to be very useful
for us.

Upgrade `sinon` and `@types/sinon` to latest.
  • Loading branch information
LiranCohen authored Sep 11, 2024
1 parent 080359c commit 6d82b58
Show file tree
Hide file tree
Showing 10 changed files with 289 additions and 305 deletions.
400 changes: 187 additions & 213 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tbd54566975/dwn-sdk-js",
"version": "0.4.6",
"version": "0.4.7",
"description": "A reference implementation of https://identity.foundation/decentralized-web-node/spec/",
"repository": {
"type": "git",
Expand Down Expand Up @@ -101,7 +101,7 @@
"@types/randombytes": "2.0.0",
"@types/readable-stream": "4.0.10",
"@types/secp256k1": "4.0.3",
"@types/sinon": "10.0.11",
"@types/sinon": "^17.0.3",
"@types/uuid": "^9.0.1",
"@types/varint": "6.0.0",
"@typescript-eslint/eslint-plugin": "^7.9.0",
Expand Down Expand Up @@ -133,8 +133,7 @@
"playwright": "^1.44.0",
"rimraf": "^3.0.2",
"search-index": "3.4.0",
"sinon": "13.0.1",
"ts-sinon": "^2.0.2",
"sinon": "18.0.1",
"typescript": "^5.1.6",
"util": "0.12.4"
},
Expand Down
14 changes: 9 additions & 5 deletions tests/core/protocol-authorization.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import type { MessageStore } from '../../src/index.js';

import { DwnErrorCode } from '../../src/core/dwn-error.js';
import { expect } from 'chai';
import { MessageStoreLevel } from '../../src/index.js';
import { ProtocolAuthorization } from '../../src/core/protocol-authorization.js';
import { stubInterface } from 'ts-sinon';
import { TestDataGenerator } from '../utils/test-data-generator.js';

import sinon from 'sinon';

describe('ProtocolAuthorization', () => {
beforeEach(() => {
sinon.restore();
});

describe('authorizeWrite()', () => {
it('should throw if message references non-existent parent', async () => {
const alice = await TestDataGenerator.generateDidKeyPersona();
Expand All @@ -17,7 +21,7 @@ describe('ProtocolAuthorization', () => {
});

// stub the message store
const messageStoreStub = stubInterface<MessageStore>();
const messageStoreStub = sinon.createStubInstance(MessageStoreLevel);
messageStoreStub.query.resolves({ messages: [] }); // simulate parent not in message store

await expect(ProtocolAuthorization.authorizeWrite(alice.did, recordsWrite, messageStoreStub)).to.be.rejectedWith(
Expand All @@ -38,7 +42,7 @@ describe('ProtocolAuthorization', () => {
}
} as any;

const messageStoreStub = stubInterface<MessageStore>();
const messageStoreStub = sinon.createStubInstance(MessageStoreLevel);
expect(ProtocolAuthorization['getActionsSeekingARuleMatch'](alice.did, deliberatelyCraftedInvalidMessage, messageStoreStub)).to.be.empty;
});
});
Expand Down
24 changes: 12 additions & 12 deletions tests/dwn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import type { DataStore, EventLog, MessageStore, ResumableTaskStore } from '../s
import chaiAsPromised from 'chai-as-promised';
import sinon from 'sinon';
import chai, { expect } from 'chai';
import { DataStoreLevel, EventEmitterStream, EventLogLevel, MessageStoreLevel, ResumableTaskStoreLevel } from '../src/index.js';

import { Dwn } from '../src/dwn.js';
import { Message } from '../src/core/message.js';
import { stubInterface } from 'ts-sinon';
import { TestDataGenerator } from './utils/test-data-generator.js';
import { TestEventStream } from './test-event-stream.js';
import { TestStores } from './test-stores.js';
Expand Down Expand Up @@ -50,6 +50,7 @@ export function testDwnClass(): void {
});

after(async () => {
sinon.restore();
await dwn.close();
});

Expand Down Expand Up @@ -133,11 +134,11 @@ export function testDwnClass(): void {
}
};

const messageStoreStub = stubInterface<MessageStore>();
const dataStoreStub = stubInterface<DataStore>();
const resumableTaskStoreStub = stubInterface<ResumableTaskStore>();
const eventLogStub = stubInterface<EventLog>();
const eventStreamStub = stubInterface<EventStream>();
const messageStoreStub = sinon.createStubInstance(MessageStoreLevel);
const dataStoreStub = sinon.createStubInstance(DataStoreLevel);
const resumableTaskStoreStub = sinon.createStubInstance(ResumableTaskStoreLevel);
const eventLogStub = sinon.createStubInstance(EventLogLevel);
const eventStreamStub = sinon.createStubInstance(EventEmitterStream);

const dwnWithConfig = await Dwn.create({
tenantGate : blockAllTenantGate,
Expand Down Expand Up @@ -166,12 +167,11 @@ export function testDwnClass(): void {
return { isActiveTenant: false, detail: customMessage };
}
};

const messageStoreStub = stubInterface<MessageStore>();
const dataStoreStub = stubInterface<DataStore>();
const resumableTaskStoreStub = stubInterface<ResumableTaskStore>();
const eventLogStub = stubInterface<EventLog>();
const eventStreamStub = stubInterface<EventStream>();
const messageStoreStub = sinon.createStubInstance(MessageStoreLevel);
const dataStoreStub = sinon.createStubInstance(DataStoreLevel);
const resumableTaskStoreStub = sinon.createStubInstance(ResumableTaskStoreLevel);
const eventLogStub = sinon.createStubInstance(EventLogLevel);
const eventStreamStub = sinon.createStubInstance(EventEmitterStream);

const dwnWithConfig = await Dwn.create({
tenantGate : blockAllTenantGate,
Expand Down
29 changes: 18 additions & 11 deletions tests/handlers/records-delete.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DidResolver } from '@web5/dids';
import type { EventStream } from '../../src/types/subscriptions.js';
import type { ResumableTaskManager } from '../../src/core/resumable-task-manager.js';
import { ResumableTaskManager } from '../../src/core/resumable-task-manager.js';
import type {
DataStore,
EventLog,
Expand All @@ -25,13 +25,12 @@ import { DwnMethodName } from '../../src/enums/dwn-interface-method.js';
import { Message } from '../../src/core/message.js';
import { normalizeSchemaUrl } from '../../src/utils/url.js';
import { RecordsDeleteHandler } from '../../src/handlers/records-delete.js';
import { stubInterface } from 'ts-sinon';
import { TestDataGenerator } from '../utils/test-data-generator.js';
import { TestEventStream } from '../test-event-stream.js';
import { TestStores } from '../test-stores.js';
import { TestStubGenerator } from '../utils/test-stub-generator.js';
import { Time } from '../../src/utils/time.js';
import { DataStream, Dwn, Encoder, Jws, RecordsDelete, RecordsRead, RecordsWrite } from '../../src/index.js';
import { DataStream, Dwn, Encoder, Jws, MessageStoreLevel, RecordsDelete, RecordsRead, RecordsWrite } from '../../src/index.js';
import { DidKey, UniversalResolver } from '@web5/dids';

chai.use(chaiAsPromised);
Expand All @@ -46,6 +45,14 @@ export function testRecordsDeleteHandler(): void {
let eventStream: EventStream;
let dwn: Dwn;

beforeEach(() => {
sinon.restore();
});

after(() => {
sinon.restore();
});

describe('functional tests', () => {

// important to follow the `before` and `after` pattern to initialize and clean the stores in tests
Expand All @@ -64,8 +71,6 @@ export function testRecordsDeleteHandler(): void {
});

beforeEach(async () => {
sinon.restore(); // wipe all previous stubs/spies/mocks/fakes

// clean up before each test rather than after so that a test does not depend on other tests to do the clean up
await messageStore.clear();
await dataStore.clear();
Expand Down Expand Up @@ -764,10 +769,12 @@ export function testRecordsDeleteHandler(): void {
// intentionally not supplying the public key so a different public key is generated to simulate invalid signature
const mismatchingPersona = await TestDataGenerator.generatePersona({ did: author.did, keyId: author.keyId });
const didResolver = TestStubGenerator.createDidResolverStub(mismatchingPersona);
const messageStore = stubInterface<MessageStore>();
const resumableTaskManager = stubInterface<ResumableTaskManager>();

const recordsDeleteHandler = new RecordsDeleteHandler(didResolver, messageStore, resumableTaskManager);
// setting up a stub method resolver & message store
const messageStoreStub = sinon.createStubInstance(MessageStoreLevel);
const resumableTaskManagerStub = sinon.createStubInstance(ResumableTaskManager);

const recordsDeleteHandler = new RecordsDeleteHandler(didResolver, messageStoreStub, resumableTaskManagerStub);
const reply = await recordsDeleteHandler.handle({ tenant, message });
expect(reply.status.code).to.equal(401);
});
Expand All @@ -777,10 +784,10 @@ export function testRecordsDeleteHandler(): void {
const tenant = author.did;

// setting up a stub method resolver & message store
const messageStore = stubInterface<MessageStore>();
const resumableTaskManager = stubInterface<ResumableTaskManager>();
const messageStoreStub = sinon.createStubInstance(MessageStoreLevel);
const resumableTaskManagerStub = sinon.createStubInstance(ResumableTaskManager);

const recordsDeleteHandler = new RecordsDeleteHandler(didResolver, messageStore, resumableTaskManager);
const recordsDeleteHandler = new RecordsDeleteHandler(didResolver, messageStoreStub, resumableTaskManagerStub);

// stub the `parse()` function to throw an error
sinon.stub(RecordsDelete, 'parse').throws('anyError');
Expand Down
22 changes: 12 additions & 10 deletions tests/handlers/records-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@ import { Message } from '../../src/core/message.js';
import { RecordsQuery } from '../../src/interfaces/records-query.js';
import { RecordsQueryHandler } from '../../src/handlers/records-query.js';
import { RecordsWriteHandler } from '../../src/handlers/records-write.js';
import { stubInterface } from 'ts-sinon';
import { TestDataGenerator } from '../utils/test-data-generator.js';
import { TestEventStream } from '../test-event-stream.js';
import { TestStores } from '../test-stores.js';
import { TestStubGenerator } from '../utils/test-stub-generator.js';
import { DataStoreLevel, Dwn, MessageStoreLevel, RecordsWrite, Time } from '../../src/index.js';
import { DidKey, UniversalResolver } from '@web5/dids';
import { Dwn, RecordsWrite, Time } from '../../src/index.js';

chai.use(chaiAsPromised);

export function testRecordsQueryHandler(): void {
describe('RecordsQueryHandler.handle()', () => {

beforeEach(() => {
sinon.restore(); // wipe all previous stubs/spies/mocks/fakes
});

describe('functional tests', () => {
let didResolver: DidResolver;
let messageStore: MessageStore;
Expand All @@ -60,8 +64,6 @@ export function testRecordsQueryHandler(): void {
});

beforeEach(async () => {
sinon.restore(); // wipe all previous stubs/spies/mocks/fakes

// clean up before each test rather than after so that a test does not depend on other tests to do the clean up
await messageStore.clear();
await dataStore.clear();
Expand Down Expand Up @@ -3005,10 +3007,10 @@ export function testRecordsQueryHandler(): void {
// intentionally not supplying the public key so a different public key is generated to simulate invalid signature
const mismatchingPersona = await TestDataGenerator.generatePersona({ did: author!.did, keyId: author!.keyId });
const didResolver = TestStubGenerator.createDidResolverStub(mismatchingPersona);
const messageStore = stubInterface<MessageStore>();
const dataStore = stubInterface<DataStore>();
const messageStoreStub = sinon.createStubInstance(MessageStoreLevel);
const dataStoreStub = sinon.createStubInstance(DataStoreLevel);

const recordsQueryHandler = new RecordsQueryHandler(didResolver, messageStore, dataStore);
const recordsQueryHandler = new RecordsQueryHandler(didResolver, messageStoreStub, dataStoreStub);
const reply = await recordsQueryHandler.handle({ tenant, message });

expect(reply.status.code).to.equal(401);
Expand All @@ -3020,9 +3022,9 @@ export function testRecordsQueryHandler(): void {

// setting up a stub method resolver & message store
const didResolver = TestStubGenerator.createDidResolverStub(author!);
const messageStore = stubInterface<MessageStore>();
const dataStore = stubInterface<DataStore>();
const recordsQueryHandler = new RecordsQueryHandler(didResolver, messageStore, dataStore);
const messageStoreStub = sinon.createStubInstance(MessageStoreLevel);
const dataStoreStub = sinon.createStubInstance(DataStoreLevel);
const recordsQueryHandler = new RecordsQueryHandler(didResolver, messageStoreStub, dataStoreStub);

// stub the `parse()` function to throw an error
sinon.stub(RecordsQuery, 'parse').throws('anyError');
Expand Down
21 changes: 11 additions & 10 deletions tests/handlers/records-read.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { EncryptionInput } from '../../src/interfaces/records-write.js';
import type { EventStream } from '../../src/types/subscriptions.js';
import type { DataStore, EventLog, MessageStore, ProtocolDefinition, ProtocolsConfigureMessage, ResumableTaskStore } from '../../src/index.js';

import { DwnConstant, PermissionsProtocol, Time } from '../../src/index.js';
import { DataStoreLevel, DwnConstant, MessageStoreLevel, PermissionsProtocol, Time } from '../../src/index.js';
import { DwnInterfaceName, DwnMethodName } from '../../src/index.js';

import chaiAsPromised from 'chai-as-promised';
Expand All @@ -26,7 +26,6 @@ import { Encryption } from '../../src/utils/encryption.js';
import { HdKey } from '../../src/utils/hd-key.js';
import { KeyDerivationScheme } from '../../src/utils/hd-key.js';
import { RecordsReadHandler } from '../../src/handlers/records-read.js';
import { stubInterface } from 'ts-sinon';
import { TestDataGenerator } from '../utils/test-data-generator.js';
import { TestEventStream } from '../test-event-stream.js';
import { TestStores } from '../test-stores.js';
Expand All @@ -48,6 +47,10 @@ export function testRecordsReadHandler(): void {
let eventStream: EventStream;
let dwn: Dwn;

beforeEach(() => {
sinon.restore(); // wipe all previous stubs/spies/mocks/fakes
});

describe('functional tests', () => {

// important to follow the `before` and `after` pattern to initialize and clean the stores in tests
Expand All @@ -66,8 +69,6 @@ export function testRecordsReadHandler(): void {
});

beforeEach(async () => {
sinon.restore(); // wipe all previous stubs/spies/mocks/fakes

// clean up before each test rather than after so that a test does not depend on other tests to do the clean up
await messageStore.clear();
await dataStore.clear();
Expand Down Expand Up @@ -1893,10 +1894,10 @@ export function testRecordsReadHandler(): void {
// intentionally not supplying the public key so a different public key is generated to simulate invalid signature
const mismatchingPersona = await TestDataGenerator.generatePersona({ did: alice.did, keyId: alice.keyId });
const didResolver = TestStubGenerator.createDidResolverStub(mismatchingPersona);
const messageStore = stubInterface<MessageStore>();
const dataStore = stubInterface<DataStore>();
const messageStoreStub = sinon.createStubInstance(MessageStoreLevel);
const dataStoreStub = sinon.createStubInstance(DataStoreLevel);

const recordsReadHandler = new RecordsReadHandler(didResolver, messageStore, dataStore);
const recordsReadHandler = new RecordsReadHandler(didResolver, messageStoreStub, dataStoreStub);
const reply = await recordsReadHandler.handle({ tenant: alice.did, message: recordsRead.message });
expect(reply.status.code).to.equal(401);
});
Expand All @@ -1911,10 +1912,10 @@ export function testRecordsReadHandler(): void {
});

// setting up a stub method resolver & message store
const messageStore = stubInterface<MessageStore>();
const dataStore = stubInterface<DataStore>();
const messageStoreStub = sinon.createStubInstance(MessageStoreLevel);
const dataStoreStub = sinon.createStubInstance(DataStoreLevel);

const recordsReadHandler = new RecordsReadHandler(didResolver, messageStore, dataStore);
const recordsReadHandler = new RecordsReadHandler(didResolver, messageStoreStub, dataStoreStub);

// stub the `parse()` function to throw an error
sinon.stub(RecordsRead, 'parse').throws('anyError');
Expand Down
15 changes: 7 additions & 8 deletions tests/handlers/records-subscribe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ import { Message } from '../../src/core/message.js';
import { Poller } from '../utils/poller.js';
import { RecordsSubscribe } from '../../src/interfaces/records-subscribe.js';
import { RecordsSubscribeHandler } from '../../src/handlers/records-subscribe.js';
import { stubInterface } from 'ts-sinon';
import { TestDataGenerator } from '../utils/test-data-generator.js';
import { TestEventStream } from '../test-event-stream.js';
import { TestStores } from '../test-stores.js';
import { TestStubGenerator } from '../utils/test-stub-generator.js';
import { DidKey, UniversalResolver } from '@web5/dids';
import { Dwn, DwnErrorCode, DwnMethodName, Time } from '../../src/index.js';
import { Dwn, DwnErrorCode, DwnMethodName, EventEmitterStream, MessageStoreLevel, Time } from '../../src/index.js';

chai.use(chaiAsPromised);

Expand Down Expand Up @@ -232,10 +231,10 @@ export function testRecordsSubscribeHandler(): void {
// intentionally not supplying the public key so a different public key is generated to simulate invalid signature
const mismatchingPersona = await TestDataGenerator.generatePersona({ did: author!.did, keyId: author!.keyId });
const didResolver = TestStubGenerator.createDidResolverStub(mismatchingPersona);
const messageStore = stubInterface<MessageStore>();
const eventStream = stubInterface<EventStream>();
const messageStoreStub = sinon.createStubInstance(MessageStoreLevel);
const eventStreamStub = sinon.createStubInstance(EventEmitterStream);

const recordsSubscribeHandler = new RecordsSubscribeHandler(didResolver, messageStore, eventStream);
const recordsSubscribeHandler = new RecordsSubscribeHandler(didResolver, messageStoreStub, eventStreamStub);
const reply = await recordsSubscribeHandler.handle({ tenant, message, subscriptionHandler: () => {} });

expect(reply.status.code).to.equal(401);
Expand All @@ -247,9 +246,9 @@ export function testRecordsSubscribeHandler(): void {

// setting up a stub method resolver & message store
const didResolver = TestStubGenerator.createDidResolverStub(author!);
const messageStore = stubInterface<MessageStore>();
const eventStream = stubInterface<EventStream>();
const recordsSubscribeHandler = new RecordsSubscribeHandler(didResolver, messageStore, eventStream);
const messageStoreStub = sinon.createStubInstance(MessageStoreLevel);
const eventStreamStub = sinon.createStubInstance(EventEmitterStream);
const recordsSubscribeHandler = new RecordsSubscribeHandler(didResolver, messageStoreStub, eventStreamStub);

// stub the `parse()` function to throw an error
sinon.stub(RecordsSubscribe, 'parse').throws('anyError');
Expand Down
Loading

0 comments on commit 6d82b58

Please sign in to comment.