From bec1beb30f4ee26331c70b8432e6ee7c7fc7a183 Mon Sep 17 00:00:00 2001
From: Konstantin Tsabolov <konstantin.tsabolov@spherity.com>
Date: Fri, 22 Dec 2023 12:02:13 +0100
Subject: [PATCH] chore(unit): add unit tests

---
 .../src/__tests__/application.spec.ts         |  26 +++
 .../__tests__/connections.controller.spec.ts  | 132 ++++++++++++++
 .../__tests__/connections.module.spec.ts      |  37 ++++
 .../__tests__/connections.service.spec.ts     | 161 ++++++++++++++++++
 .../__tests__/invitations.controller.spec.ts  |  88 ++++++++++
 .../__tests__/invitations.module.spec.ts      |  37 ++++
 .../__tests__/invitations.service.spec.ts     | 108 ++++++++++++
 7 files changed, 589 insertions(+)
 create mode 100644 apps/connection-manager/src/__tests__/application.spec.ts
 create mode 100644 apps/connection-manager/src/connections/__tests__/connections.controller.spec.ts
 create mode 100644 apps/connection-manager/src/connections/__tests__/connections.module.spec.ts
 create mode 100644 apps/connection-manager/src/connections/__tests__/connections.service.spec.ts
 create mode 100644 apps/connection-manager/src/invitations/__tests__/invitations.controller.spec.ts
 create mode 100644 apps/connection-manager/src/invitations/__tests__/invitations.module.spec.ts
 create mode 100644 apps/connection-manager/src/invitations/__tests__/invitations.service.spec.ts

diff --git a/apps/connection-manager/src/__tests__/application.spec.ts b/apps/connection-manager/src/__tests__/application.spec.ts
new file mode 100644
index 0000000..3d41dce
--- /dev/null
+++ b/apps/connection-manager/src/__tests__/application.spec.ts
@@ -0,0 +1,26 @@
+import type { INestApplication } from '@nestjs/common';
+
+import { Test } from '@nestjs/testing';
+
+import { Application } from '../application.js';
+
+describe('Application', () => {
+  let app: INestApplication;
+
+  beforeEach(async () => {
+    const moduleRef = await Test.createTestingModule({
+      imports: [Application],
+    }).compile();
+
+    app = moduleRef.createNestApplication();
+    await app.init();
+  });
+
+  afterEach(async () => {
+    await app.close();
+  });
+
+  it('should be defined', () => {
+    expect(app).toBeDefined();
+  });
+});
diff --git a/apps/connection-manager/src/connections/__tests__/connections.controller.spec.ts b/apps/connection-manager/src/connections/__tests__/connections.controller.spec.ts
new file mode 100644
index 0000000..3c60415
--- /dev/null
+++ b/apps/connection-manager/src/connections/__tests__/connections.controller.spec.ts
@@ -0,0 +1,132 @@
+import type { TestingModule } from '@nestjs/testing';
+import type {
+  EventDidcommConnectionsCreateWithSelf,
+  EventDidcommConnectionsGetAll,
+  EventDidcommConnectionsGetById,
+} from '@ocm/shared';
+
+import { Test } from '@nestjs/testing';
+import { Subject, of, takeUntil } from 'rxjs';
+
+import { NATS_CLIENT } from '../../common/constants.js';
+import { ConnectionsController } from '../connections.controller.js';
+import { ConnectionsService } from '../connections.service.js';
+
+describe('ConnectionsController', () => {
+  const natsClientMock = {};
+
+  let controller: ConnectionsController;
+  let service: ConnectionsService;
+
+  beforeEach(async () => {
+    const module: TestingModule = await Test.createTestingModule({
+      controllers: [ConnectionsController],
+      providers: [
+        { provide: NATS_CLIENT, useValue: natsClientMock },
+        ConnectionsService,
+      ],
+    }).compile();
+
+    controller = module.get<ConnectionsController>(ConnectionsController);
+    service = module.get<ConnectionsService>(ConnectionsService);
+  });
+
+  describe('getAll', () => {
+    it('should return a list of connections', (done) => {
+      const unsubscribe$ = new Subject<void>();
+      const tenantId = 'exampleTenantId';
+      const expectedResult: EventDidcommConnectionsGetAll['data'] = [];
+
+      jest
+        .spyOn(service, 'getAllConnections')
+        .mockReturnValue(of(expectedResult));
+
+      controller
+        .getAll({ tenantId })
+        .pipe(takeUntil(unsubscribe$))
+        .subscribe((result) => {
+          expect(result).toStrictEqual(expectedResult);
+
+          unsubscribe$.next();
+          unsubscribe$.complete();
+
+          done();
+        });
+    });
+  });
+
+  describe('getById', () => {
+    it('should return a connection', (done) => {
+      const unsubscribe$ = new Subject<void>();
+      const tenantId = 'exampleTenantId';
+      const connectionId = 'exampleConnectionId';
+      const expectedResult = {} as EventDidcommConnectionsGetById['data'];
+
+      jest
+        .spyOn(service, 'getConnectionById')
+        .mockReturnValue(of(expectedResult));
+
+      controller
+        .getById({ connectionId }, { tenantId })
+        .pipe(takeUntil(unsubscribe$))
+        .subscribe((result) => {
+          expect(result).toStrictEqual(expectedResult);
+
+          unsubscribe$.next();
+          unsubscribe$.complete();
+
+          done();
+        });
+    });
+  });
+
+  describe('createWithSelf', () => {
+    it('should return a connection', (done) => {
+      const unsubscribe$ = new Subject<void>();
+      const tenantId = 'exampleTenantId';
+      const expectedResult =
+        {} as EventDidcommConnectionsCreateWithSelf['data'];
+
+      jest
+        .spyOn(service, 'createConnectionWithSelf')
+        .mockReturnValue(of(expectedResult));
+
+      controller
+        .createWithSelf({ tenantId })
+        .pipe(takeUntil(unsubscribe$))
+        .subscribe((result) => {
+          expect(result).toStrictEqual(expectedResult);
+
+          unsubscribe$.next();
+          unsubscribe$.complete();
+
+          done();
+        });
+    });
+  });
+
+  describe('block', () => {
+    it('should return a connection', (done) => {
+      const unsubscribe$ = new Subject<void>();
+      const tenantId = 'exampleTenantId';
+      const idOrDid = 'exampleConnectionId';
+      const expectedResult = {} as EventDidcommConnectionsGetById['data'];
+
+      jest
+        .spyOn(service, 'blockConnection')
+        .mockReturnValue(of(expectedResult));
+
+      controller
+        .block({ idOrDid }, { tenantId })
+        .pipe(takeUntil(unsubscribe$))
+        .subscribe((result) => {
+          expect(result).toStrictEqual(expectedResult);
+
+          unsubscribe$.next();
+          unsubscribe$.complete();
+
+          done();
+        });
+    });
+  });
+});
diff --git a/apps/connection-manager/src/connections/__tests__/connections.module.spec.ts b/apps/connection-manager/src/connections/__tests__/connections.module.spec.ts
new file mode 100644
index 0000000..c0eaeb1
--- /dev/null
+++ b/apps/connection-manager/src/connections/__tests__/connections.module.spec.ts
@@ -0,0 +1,37 @@
+import { ClientsModule } from '@nestjs/microservices';
+import { Test } from '@nestjs/testing';
+
+import { NATS_CLIENT } from '../../common/constants.js';
+import { ConnectionsController } from '../connections.controller.js';
+import { ConnectionsModule } from '../connections.module.js';
+import { ConnectionsService } from '../connections.service.js';
+
+describe('ConnectionsModule', () => {
+  let connectionsController: ConnectionsController;
+  let connectionsService: ConnectionsService;
+
+  beforeEach(async () => {
+    const moduleRef = await Test.createTestingModule({
+      imports: [
+        ClientsModule.registerAsync({
+          isGlobal: true,
+          clients: [{ name: NATS_CLIENT, useFactory: () => ({}) }],
+        }),
+        ConnectionsModule,
+      ],
+    }).compile();
+
+    connectionsController = moduleRef.get<ConnectionsController>(
+      ConnectionsController,
+    );
+    connectionsService = moduleRef.get<ConnectionsService>(ConnectionsService);
+  });
+
+  it('should be defined', () => {
+    expect(connectionsController).toBeDefined();
+    expect(connectionsController).toBeInstanceOf(ConnectionsController);
+
+    expect(connectionsService).toBeDefined();
+    expect(connectionsService).toBeInstanceOf(ConnectionsService);
+  });
+});
diff --git a/apps/connection-manager/src/connections/__tests__/connections.service.spec.ts b/apps/connection-manager/src/connections/__tests__/connections.service.spec.ts
new file mode 100644
index 0000000..b054cf9
--- /dev/null
+++ b/apps/connection-manager/src/connections/__tests__/connections.service.spec.ts
@@ -0,0 +1,161 @@
+import type { ClientProxy } from '@nestjs/microservices';
+import type { TestingModule } from '@nestjs/testing';
+
+import { Test } from '@nestjs/testing';
+import {
+  EventDidcommConnectionsBlock,
+  EventDidcommConnectionsCreateWithSelf,
+  EventDidcommConnectionsGetAll,
+  EventDidcommConnectionsGetById,
+} from '@ocm/shared';
+import { Subject, of, takeUntil } from 'rxjs';
+
+import { NATS_CLIENT } from '../../common/constants.js';
+import { ConnectionsService } from '../connections.service.js';
+
+describe('ConnectionsService', () => {
+  let service: ConnectionsService;
+  let natsClient: ClientProxy;
+
+  beforeEach(async () => {
+    const module: TestingModule = await Test.createTestingModule({
+      providers: [
+        ConnectionsService,
+        {
+          provide: NATS_CLIENT,
+          useValue: {
+            send: jest.fn(),
+          },
+        },
+      ],
+    }).compile();
+
+    service = module.get<ConnectionsService>(ConnectionsService);
+    natsClient = module.get<ClientProxy>(NATS_CLIENT);
+  });
+
+  describe('getAllConnections', () => {
+    it('should return all connections', (done) => {
+      const unsubscribe$ = new Subject<void>();
+      const tenantId = 'exampleTenantId';
+      const expectedResult: EventDidcommConnectionsGetAll['data'] = [];
+
+      jest
+        .spyOn(natsClient, 'send')
+        .mockReturnValue(
+          of(new EventDidcommConnectionsGetAll(expectedResult, tenantId)),
+        );
+
+      service
+        .getAllConnections(tenantId)
+        .pipe(takeUntil(unsubscribe$))
+        .subscribe((result) => {
+          expect(natsClient.send).toHaveBeenCalledWith(
+            EventDidcommConnectionsGetAll.token,
+            { tenantId },
+          );
+          expect(result).toStrictEqual(expectedResult);
+
+          unsubscribe$.next();
+          unsubscribe$.complete();
+
+          done();
+        });
+    });
+  });
+
+  describe('getConnectionById', () => {
+    it('should return a connection', (done) => {
+      const unsubscribe$ = new Subject<void>();
+      const tenantId = 'exampleTenantId';
+      const connectionId = 'exampleConnectionId';
+      const expectedResult = {} as EventDidcommConnectionsGetById['data'];
+
+      jest
+        .spyOn(natsClient, 'send')
+        .mockReturnValue(
+          of(new EventDidcommConnectionsGetById(expectedResult, tenantId)),
+        );
+
+      service
+        .getConnectionById(tenantId, connectionId)
+        .pipe(takeUntil(unsubscribe$))
+        .subscribe((result) => {
+          expect(natsClient.send).toHaveBeenCalledWith(
+            EventDidcommConnectionsGetById.token,
+            { tenantId, id: connectionId },
+          );
+          expect(result).toStrictEqual(expectedResult);
+
+          unsubscribe$.next();
+          unsubscribe$.complete();
+
+          done();
+        });
+    });
+  });
+
+  describe('createConnectionWithSelf', () => {
+    it('should create a connection with self', (done) => {
+      const unsubscribe$ = new Subject<void>();
+      const tenantId = 'exampleTenantId';
+      const expectedResult =
+        {} as EventDidcommConnectionsCreateWithSelf['data'];
+
+      jest
+        .spyOn(natsClient, 'send')
+        .mockReturnValue(
+          of(
+            new EventDidcommConnectionsCreateWithSelf(expectedResult, tenantId),
+          ),
+        );
+
+      service
+        .createConnectionWithSelf(tenantId)
+        .pipe(takeUntil(unsubscribe$))
+        .subscribe((result) => {
+          expect(natsClient.send).toHaveBeenCalledWith(
+            EventDidcommConnectionsCreateWithSelf.token,
+            { tenantId },
+          );
+          expect(result).toStrictEqual(expectedResult);
+
+          unsubscribe$.next();
+          unsubscribe$.complete();
+
+          done();
+        });
+    });
+  });
+
+  describe('blockConnection', () => {
+    it('should block a connection', (done) => {
+      const unsubscribe$ = new Subject<void>();
+      const tenantId = 'exampleTenantId';
+      const idOrDid = 'exampleConnectionId';
+      const expectedResult = {} as EventDidcommConnectionsBlock['data'];
+
+      jest
+        .spyOn(natsClient, 'send')
+        .mockReturnValue(
+          of(new EventDidcommConnectionsBlock(expectedResult, tenantId)),
+        );
+
+      service
+        .blockConnection(tenantId, idOrDid)
+        .pipe(takeUntil(unsubscribe$))
+        .subscribe((result) => {
+          expect(natsClient.send).toHaveBeenCalledWith(
+            EventDidcommConnectionsBlock.token,
+            { tenantId, idOrDid },
+          );
+          expect(result).toStrictEqual(expectedResult);
+
+          unsubscribe$.next();
+          unsubscribe$.complete();
+
+          done();
+        });
+    });
+  });
+});
diff --git a/apps/connection-manager/src/invitations/__tests__/invitations.controller.spec.ts b/apps/connection-manager/src/invitations/__tests__/invitations.controller.spec.ts
new file mode 100644
index 0000000..c6acb10
--- /dev/null
+++ b/apps/connection-manager/src/invitations/__tests__/invitations.controller.spec.ts
@@ -0,0 +1,88 @@
+import type { TestingModule } from '@nestjs/testing';
+import type {
+  EventDidcommConnectionsCreateInvitation,
+  EventDidcommConnectionsReceiveInvitationFromUrl,
+} from '@ocm/shared';
+
+import { Test } from '@nestjs/testing';
+import { Subject, of, takeUntil } from 'rxjs';
+
+import { NATS_CLIENT } from '../../common/constants.js';
+import { InvitationsController } from '../invitations.controller.js';
+import { InvitationsService } from '../invitations.service.js';
+
+describe('InvitationsController', () => {
+  const natsClientMock = {};
+
+  let controller: InvitationsController;
+  let service: InvitationsService;
+
+  beforeEach(async () => {
+    const module: TestingModule = await Test.createTestingModule({
+      controllers: [InvitationsController],
+      providers: [
+        { provide: NATS_CLIENT, useValue: natsClientMock },
+        InvitationsService,
+      ],
+    }).compile();
+
+    controller = module.get<InvitationsController>(InvitationsController);
+    service = module.get<InvitationsService>(InvitationsService);
+  });
+
+  it('should be defined', () => {
+    expect(controller).toBeDefined();
+  });
+
+  describe('create', () => {
+    it('should return an invitation', (done) => {
+      const unsubscribe$ = new Subject<void>();
+      const tenantId = 'exampleTenantId';
+      const expectedResult: EventDidcommConnectionsCreateInvitation['data'] = {
+        invitationUrl: 'https://example.com/invitation',
+      };
+
+      jest
+        .spyOn(service, 'createInvitation')
+        .mockReturnValue(of(expectedResult));
+
+      controller
+        .createInvitation({ tenantId })
+        .pipe(takeUntil(unsubscribe$))
+        .subscribe((result) => {
+          expect(result).toStrictEqual(expectedResult);
+
+          unsubscribe$.next();
+          unsubscribe$.complete();
+
+          done();
+        });
+    });
+  });
+
+  describe('receive', () => {
+    it('should return a connection', (done) => {
+      const unsubscribe$ = new Subject<void>();
+      const tenantId = 'exampleTenantId';
+      const invitationUrl = 'https://example.com/invitation';
+      const expectedResult =
+        {} as EventDidcommConnectionsReceiveInvitationFromUrl['data'];
+
+      jest
+        .spyOn(service, 'receiveInvitationFromURL')
+        .mockReturnValue(of(expectedResult));
+
+      controller
+        .receiveInvitation({ tenantId }, { invitationUrl })
+        .pipe(takeUntil(unsubscribe$))
+        .subscribe((result) => {
+          expect(result).toStrictEqual(expectedResult);
+
+          unsubscribe$.next();
+          unsubscribe$.complete();
+
+          done();
+        });
+    });
+  });
+});
diff --git a/apps/connection-manager/src/invitations/__tests__/invitations.module.spec.ts b/apps/connection-manager/src/invitations/__tests__/invitations.module.spec.ts
new file mode 100644
index 0000000..46c9ad7
--- /dev/null
+++ b/apps/connection-manager/src/invitations/__tests__/invitations.module.spec.ts
@@ -0,0 +1,37 @@
+import { ClientsModule } from '@nestjs/microservices';
+import { Test } from '@nestjs/testing';
+
+import { NATS_CLIENT } from '../../common/constants.js';
+import { InvitationsController } from '../invitations.controller.js';
+import { InvitationsModule } from '../invitations.module.js';
+import { InvitationsService } from '../invitations.service.js';
+
+describe('InvitationsModule', () => {
+  let invitationsService: InvitationsService;
+  let invitationsController: InvitationsController;
+
+  beforeEach(async () => {
+    const moduleRef = await Test.createTestingModule({
+      imports: [
+        ClientsModule.registerAsync({
+          isGlobal: true,
+          clients: [{ name: NATS_CLIENT, useFactory: () => ({}) }],
+        }),
+        InvitationsModule,
+      ],
+    }).compile();
+
+    invitationsService = moduleRef.get<InvitationsService>(InvitationsService);
+    invitationsController = moduleRef.get<InvitationsController>(
+      InvitationsController,
+    );
+  });
+
+  it('should be defined', () => {
+    expect(invitationsService).toBeDefined();
+    expect(invitationsService).toBeInstanceOf(InvitationsService);
+
+    expect(invitationsController).toBeDefined();
+    expect(invitationsController).toBeInstanceOf(InvitationsController);
+  });
+});
diff --git a/apps/connection-manager/src/invitations/__tests__/invitations.service.spec.ts b/apps/connection-manager/src/invitations/__tests__/invitations.service.spec.ts
new file mode 100644
index 0000000..d5adb78
--- /dev/null
+++ b/apps/connection-manager/src/invitations/__tests__/invitations.service.spec.ts
@@ -0,0 +1,108 @@
+import type { ClientProxy } from '@nestjs/microservices';
+import type { TestingModule } from '@nestjs/testing';
+
+import { Test } from '@nestjs/testing';
+import {
+  EventDidcommConnectionsCreateInvitation,
+  EventDidcommConnectionsReceiveInvitationFromUrl,
+} from '@ocm/shared';
+import { Subject, of, takeUntil } from 'rxjs';
+
+import { NATS_CLIENT } from '../../common/constants.js';
+import { InvitationsService } from '../invitations.service.js';
+
+describe('InvitationsService', () => {
+  let service: InvitationsService;
+  let natsClient: ClientProxy;
+
+  beforeEach(async () => {
+    const module: TestingModule = await Test.createTestingModule({
+      providers: [
+        InvitationsService,
+        {
+          provide: NATS_CLIENT,
+          useValue: {
+            send: jest.fn(),
+          },
+        },
+      ],
+    }).compile();
+
+    service = module.get<InvitationsService>(InvitationsService);
+    natsClient = module.get<ClientProxy>(NATS_CLIENT);
+  });
+
+  describe('createInvitation', () => {
+    it('should return an invitation', (done) => {
+      const unsubscribe$ = new Subject<void>();
+      const tenantId = 'exampleTenantId';
+      const expectedResult: EventDidcommConnectionsCreateInvitation['data'] = {
+        invitationUrl: 'https://example.com/invitation',
+      };
+
+      jest
+        .spyOn(natsClient, 'send')
+        .mockReturnValue(
+          of(
+            new EventDidcommConnectionsCreateInvitation(
+              expectedResult,
+              tenantId,
+            ),
+          ),
+        );
+
+      service
+        .createInvitation(tenantId)
+        .pipe(takeUntil(unsubscribe$))
+        .subscribe((result) => {
+          expect(natsClient.send).toHaveBeenCalledWith(
+            EventDidcommConnectionsCreateInvitation.token,
+            { tenantId },
+          );
+          expect(result).toStrictEqual(expectedResult);
+
+          unsubscribe$.next();
+          unsubscribe$.complete();
+
+          done();
+        });
+    });
+  });
+
+  describe('receiveInvitationFromURL', () => {
+    it('should return a connection', (done) => {
+      const unsubscribe$ = new Subject<void>();
+      const tenantId = 'exampleTenantId';
+      const invitationUrl = 'https://example.com/invitation';
+      const expectedResult =
+        {} as EventDidcommConnectionsReceiveInvitationFromUrl['data'];
+
+      jest
+        .spyOn(natsClient, 'send')
+        .mockReturnValue(
+          of(
+            new EventDidcommConnectionsReceiveInvitationFromUrl(
+              expectedResult,
+              tenantId,
+            ),
+          ),
+        );
+
+      service
+        .receiveInvitationFromURL(tenantId, invitationUrl)
+        .pipe(takeUntil(unsubscribe$))
+        .subscribe((result) => {
+          expect(natsClient.send).toHaveBeenCalledWith(
+            EventDidcommConnectionsReceiveInvitationFromUrl.token,
+            { tenantId, invitationUrl },
+          );
+          expect(result).toStrictEqual(expectedResult);
+
+          unsubscribe$.next();
+          unsubscribe$.complete();
+
+          done();
+        });
+    });
+  });
+});
-- 
GitLab