From cf5bae906507c98ad09c8a5b3c152f160a80698a Mon Sep 17 00:00:00 2001 From: Kyle Breeding Date: Mon, 8 Apr 2024 15:09:53 -0700 Subject: [PATCH 1/4] chore/unit-tests-for-connect-module - added unit tests for connect module controller - removed express response object where possible to avoid mocking (commited accidently in [rekey support PR](https://github.com/algorandfoundation/liquid-auth/pull/2)) --- .../src/connect/connect.controller.spec.ts | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 services/liquid-auth-api-js/src/connect/connect.controller.spec.ts diff --git a/services/liquid-auth-api-js/src/connect/connect.controller.spec.ts b/services/liquid-auth-api-js/src/connect/connect.controller.spec.ts new file mode 100644 index 0000000..d1c5217 --- /dev/null +++ b/services/liquid-auth-api-js/src/connect/connect.controller.spec.ts @@ -0,0 +1,107 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { AlgodService } from '../algod/algod.service'; +import { ConnectController } from './connect.controller'; +import { AuthService } from '../auth/auth.service'; +import * as crypto from 'node:crypto'; +import { Session } from './session.schema'; +import { accFixture } from '../../tests/constants'; + +describe('ConnectController', () => { + let connectController: ConnectController; + + beforeEach(async () => { + const moduleRef: TestingModule = await Test.createTestingModule({ + controllers: [ConnectController], + providers: [ + { + provide: AuthService, + useValue: { + init: jest.fn().mockResolvedValue({ + id: crypto.randomBytes(32).toString('base64url'), + wallet: accFixture.accs[0].addr, + credentials: [], + }), + }, + }, + { + provide: 'ACCOUNT_LINK_SERVICE', + useValue: { + emit: jest.fn(), + }, + }, + { + provide: AlgodService, + useValue: { + accountInformation: jest.fn().mockReturnThis(), + exclude: jest.fn().mockReturnThis(), + do: jest.fn().mockResolvedValue({ + 'auth-addr': accFixture.accs[1].addr, + }), + }, + }, + ], + }).compile(); + + connectController = moduleRef.get(ConnectController); + }); + + it('should be defined', () => { + expect(connectController).toBeDefined(); + }); + + describe('connect response', () => { + it('(CREATED) should return undefined when a valid account & signature requests to connect', async () => { + const session = new Session(); + + const linkResponseDTO = { + requestId: 0, + wallet: accFixture.accs[0].addr, + challenge: accFixture.challenge, + signature: accFixture.accs[0].signature, + credId: '', + }; + + const response = await connectController.linkWalletResponse( + session, + linkResponseDTO, + ); + + expect(response).toBe(undefined); + }); + + it("(FAIL) should throw an error when the signature doesn't match the account", async () => { + const session = new Session(); + + const linkResponseDTO = { + requestId: 0, + wallet: accFixture.accs[0].addr, + challenge: accFixture.challenge, + signature: accFixture.accs[2].signature, + credId: '', + }; + + await expect( + connectController.linkWalletResponse(session, linkResponseDTO), + ).rejects.toThrowError(); + }); + + it('(CREATED) should return undefined when a valid account & auth address signature requests to connect', async () => { + const session = new Session(); + + const linkResponseDTO = { + requestId: 0, + wallet: accFixture.accs[0].addr, + challenge: accFixture.challenge, + signature: accFixture.accs[1].signature, + credId: '', + }; + + const response = await connectController.linkWalletResponse( + session, + linkResponseDTO, + ); + + expect(response).toBe(undefined); + }); + }); +}); From 3edf17169889091a84854906e87c858368d195fe Mon Sep 17 00:00:00 2001 From: Kyle Breeding Date: Tue, 9 Apr 2024 14:31:39 -0700 Subject: [PATCH 2/4] added AuthService Mock --- .../src/connect/connect.controller.spec.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/services/liquid-auth-api-js/src/connect/connect.controller.spec.ts b/services/liquid-auth-api-js/src/connect/connect.controller.spec.ts index d1c5217..b0dbe19 100644 --- a/services/liquid-auth-api-js/src/connect/connect.controller.spec.ts +++ b/services/liquid-auth-api-js/src/connect/connect.controller.spec.ts @@ -5,6 +5,7 @@ import { AuthService } from '../auth/auth.service'; import * as crypto from 'node:crypto'; import { Session } from './session.schema'; import { accFixture } from '../../tests/constants'; +import { mockAuthService } from '../__mocks__/auth.service.mock'; describe('ConnectController', () => { let connectController: ConnectController; @@ -15,13 +16,7 @@ describe('ConnectController', () => { providers: [ { provide: AuthService, - useValue: { - init: jest.fn().mockResolvedValue({ - id: crypto.randomBytes(32).toString('base64url'), - wallet: accFixture.accs[0].addr, - credentials: [], - }), - }, + useValue:,mockAuthService, }, { provide: 'ACCOUNT_LINK_SERVICE', @@ -69,7 +64,7 @@ describe('ConnectController', () => { expect(response).toBe(undefined); }); - it("(FAIL) should throw an error when the signature doesn't match the account", async () => { + it("(FAIL) should throw an error when the signature doesn't match the account", async () => { const session = new Session(); const linkResponseDTO = { From f143bf9319a9f8d844aae02ed40d516e6ab1a5b2 Mon Sep 17 00:00:00 2001 From: Kyle Breeding Date: Tue, 9 Apr 2024 14:46:57 -0700 Subject: [PATCH 3/4] fixed typo in mockAuthService useValue --- .../liquid-auth-api-js/src/connect/connect.controller.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/liquid-auth-api-js/src/connect/connect.controller.spec.ts b/services/liquid-auth-api-js/src/connect/connect.controller.spec.ts index b0dbe19..e38ded5 100644 --- a/services/liquid-auth-api-js/src/connect/connect.controller.spec.ts +++ b/services/liquid-auth-api-js/src/connect/connect.controller.spec.ts @@ -16,7 +16,7 @@ describe('ConnectController', () => { providers: [ { provide: AuthService, - useValue:,mockAuthService, + useValue: mockAuthService, }, { provide: 'ACCOUNT_LINK_SERVICE', From 3dd5afe84336bb3404f98ba5e47294c57a1fc373 Mon Sep 17 00:00:00 2001 From: Kyle Breeding Date: Wed, 10 Apr 2024 08:54:57 -0700 Subject: [PATCH 4/4] final adjustments --- .../src/connect/connect.controller.spec.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/services/liquid-auth-api-js/src/connect/connect.controller.spec.ts b/services/liquid-auth-api-js/src/connect/connect.controller.spec.ts index e38ded5..0f9a6d1 100644 --- a/services/liquid-auth-api-js/src/connect/connect.controller.spec.ts +++ b/services/liquid-auth-api-js/src/connect/connect.controller.spec.ts @@ -2,10 +2,10 @@ import { Test, TestingModule } from '@nestjs/testing'; import { AlgodService } from '../algod/algod.service'; import { ConnectController } from './connect.controller'; import { AuthService } from '../auth/auth.service'; -import * as crypto from 'node:crypto'; import { Session } from './session.schema'; import { accFixture } from '../../tests/constants'; import { mockAuthService } from '../__mocks__/auth.service.mock'; +import { mockAccountLinkService } from '../__mocks__/account-link.service.mock'; describe('ConnectController', () => { let connectController: ConnectController; @@ -16,13 +16,11 @@ describe('ConnectController', () => { providers: [ { provide: AuthService, - useValue: mockAuthService, + useValue: { ...mockAuthService }, }, { provide: 'ACCOUNT_LINK_SERVICE', - useValue: { - emit: jest.fn(), - }, + useValue: { ...mockAccountLinkService }, }, { provide: AlgodService,