From 679811c47812701ba1da1f4551f645150071164e Mon Sep 17 00:00:00 2001 From: Siddharth9890 Date: Mon, 8 Jul 2024 11:27:44 +0530 Subject: [PATCH] feat: added requests test --- __mocks__/request.mock.ts | 49 +++++++++ __mocks__/user.mock.ts | 3 + src/services/validator-service.ts | 4 - test/auth.test.ts | 2 - test/proof.test.ts | 10 ++ test/request.test.ts | 162 ++++++++++++++++++++++++++++++ test/stubs/request.stub.ts | 48 +++++++++ test/user.test.ts | 13 ++- 8 files changed, 283 insertions(+), 8 deletions(-) create mode 100644 __mocks__/request.mock.ts create mode 100644 test/request.test.ts create mode 100644 test/stubs/request.stub.ts diff --git a/__mocks__/request.mock.ts b/__mocks__/request.mock.ts new file mode 100644 index 0000000..ac18c76 --- /dev/null +++ b/__mocks__/request.mock.ts @@ -0,0 +1,49 @@ +import { Sdk } from '../gatewaySdk/sources/Gateway'; +import { requestStub } from '../test/stubs/request.stub'; + +export const RequestMockService = (sdk: Sdk) => ({ + createDataRequestMock: jest + .spyOn(sdk, 'createDataRequestMutation') + .mockResolvedValue({ + createDataRequest: requestStub(), + }), + updateDataRequestMock: jest + .spyOn(sdk, 'updateDataRequestMutation') + .mockResolvedValue({ + updateDataRequest: requestStub(), + }), + getDataRequestMock: jest.spyOn(sdk, 'dataRequestQuery').mockResolvedValue({ + dataRequest: requestStub(), + }), + getDataRequestCountMock: jest + .spyOn(sdk, 'dataRequestCountQuery') + .mockResolvedValue({ + dataRequestCount: 10, + }), + getDataRequestStatusMock: jest + .spyOn(sdk, 'dataRequestStatusQuery') + .mockResolvedValue({ + dataRequestStatus: requestStub().status, + }), + getDataRequestsMock: jest.spyOn(sdk, 'dataRequestsQuery').mockResolvedValue({ + dataRequests: [requestStub()], + }), + getRequestsReceivedMock: jest + .spyOn(sdk, 'requestsReceivedQuery') + .mockResolvedValue({ + requestsReceived: [requestStub()], + }), + getRequestReceivedCountMock: jest + .spyOn(sdk, 'requestsReceivedCountQuery') + .mockResolvedValue({ + requestsReceivedCount: 10, + }), + + getRequestsSentMock: jest.spyOn(sdk, 'requestsSentQuery').mockResolvedValue({ + requestsSent: [requestStub()], + }), + + getRequestsSentCountMock: jest + .spyOn(sdk, 'requestsSentCountQuery') + .mockResolvedValue({ requestsSentCount: 10 }), +}); diff --git a/__mocks__/user.mock.ts b/__mocks__/user.mock.ts index 71e4910..c2293ce 100644 --- a/__mocks__/user.mock.ts +++ b/__mocks__/user.mock.ts @@ -25,4 +25,7 @@ export const UserMockService = (sdk: Sdk) => ({ myActivitiesMock: jest .spyOn(sdk, 'myActivitiesQuery') .mockResolvedValue({ myActivities: activitiesStub() }), + updateMeMock: jest + .spyOn(sdk, 'updateMyUserMutation') + .mockResolvedValue({ updateMyUser: userStub() }), }); diff --git a/src/services/validator-service.ts b/src/services/validator-service.ts index c655c2f..ad1859d 100644 --- a/src/services/validator-service.ts +++ b/src/services/validator-service.ts @@ -107,10 +107,6 @@ export class ValidationService { }; validateFileName = (fileName: string) => { - if (!fileName) { - throw new Error('Invalid file path. File name is missing.'); - } - const parts = fileName.split('.'); if (parts.length < 2) { diff --git a/test/auth.test.ts b/test/auth.test.ts index ff57555..57f0ccf 100644 --- a/test/auth.test.ts +++ b/test/auth.test.ts @@ -6,11 +6,9 @@ import { authStub } from './stubs/auth.stub'; import { AuthMockService } from '../__mocks__/auth.mock'; import { ValidationService } from '../src/services/validator-service'; import { SignCipherEnum } from '../src/common/enums'; -import { ethers } from 'ethers'; let sdk: Sdk; let auth: Auth; -let wallet: ethers.Wallet; beforeAll(() => { sdk = getSdk(new GraphQLClient('')); diff --git a/test/proof.test.ts b/test/proof.test.ts index 32ec177..9fec975 100644 --- a/test/proof.test.ts +++ b/test/proof.test.ts @@ -37,6 +37,16 @@ describe('PROOF SERVICE TESTING', () => { expect(createProofMock).toHaveBeenCalled(); }); + it('proof create -> throw error', async () => { + const { createProofMock } = ProofMockService(sdk); + + expect( + async () => await proof.createProof(createProofStub({ requestId: '' })), + ).rejects.toThrow(''); + + expect(createProofMock).toHaveBeenCalled(); + }); + it('get proof', async () => { const { getProofMock } = ProofMockService(sdk); const { proof: resultProof } = await proof.getProof(proofStub().id); diff --git a/test/request.test.ts b/test/request.test.ts new file mode 100644 index 0000000..c76ed68 --- /dev/null +++ b/test/request.test.ts @@ -0,0 +1,162 @@ +import { GraphQLClient } from 'graphql-request'; +import { getSdk, Sdk } from '../gatewaySdk/sources/Gateway'; +import { ethers } from 'ethers'; +import { Request } from '../src/modules/request/request'; +import { ValidationService } from '../src/services/validator-service'; +import { WalletService } from '../src/services/wallet-service'; +import { + createRequestStub, + requestStub, + updateRequestStub, +} from './stubs/request.stub'; +import { RequestMockService } from '../__mocks__/request.mock'; + +let request: Request; +let sdk: Sdk; +let wallet: ethers.Wallet; + +beforeAll(() => { + sdk = getSdk(new GraphQLClient('')); + wallet = ethers.Wallet.createRandom(); + request = new Request( + sdk, + new ValidationService(), + { + apiKey: '', + token: '', + url: '', + walletPrivateKey: wallet.privateKey, + }, + new WalletService({ walletPrivateKey: wallet.privateKey }), + ); +}); + +afterAll(() => { + jest.resetAllMocks(); +}); + +describe('REQUEST SERVICE TESTING', () => { + it('data request create', async () => { + const { createDataRequestMock } = RequestMockService(sdk); + + const { createDataRequest } = + await request.createDataRequest(createRequestStub()); + + expect(createDataRequest.id).toBe(requestStub().id); + expect(createDataRequestMock).toHaveBeenCalled(); + }); + + it('data request create -> throw error', async () => { + const { createDataRequestMock } = RequestMockService(sdk); + + expect( + async () => + await request.createDataRequest(createRequestStub({ id: '' })), + ).rejects.toThrow(''); + expect(createDataRequestMock).toHaveBeenCalled(); + }); + + it('data request update', async () => { + const { updateDataRequestMock } = RequestMockService(sdk); + + const { updateDataRequest } = + await request.updateDataRequest(updateRequestStub()); + + expect(updateDataRequest.id).toBe(requestStub().id); + expect(updateDataRequestMock).toHaveBeenCalled(); + }); + + it('data request create -> throw error', async () => { + const { updateDataRequestMock } = RequestMockService(sdk); + + expect( + async () => + await request.updateDataRequest(updateRequestStub({ id: '' })), + ).rejects.toThrow(''); + expect(updateDataRequestMock).toHaveBeenCalled(); + }); + + it('get data request', async () => { + const { getDataRequestMock } = RequestMockService(sdk); + const { dataRequest } = await request.getDataRequest(requestStub().id); + + expect(dataRequest.id).toBe(requestStub().id); + expect(getDataRequestMock).toHaveBeenCalled(); + }); + + it('get data request -> throw error', async () => { + const { getDataRequestMock } = RequestMockService(sdk); + + expect(async () => await request.getDataRequest('wrong')).rejects.toThrow( + 'wrong is not valid', + ); + expect(getDataRequestMock).toHaveBeenCalled(); + }); + + it('get data request count', async () => { + const { getDataRequestCountMock } = RequestMockService(sdk); + const { dataRequestCount } = await request.getDataRequestCount(); + + expect(dataRequestCount).toBeGreaterThanOrEqual(0); + expect(getDataRequestCountMock).toHaveBeenCalled(); + }); + + it('get data request status', async () => { + const { getDataRequestStatusMock } = RequestMockService(sdk); + const { dataRequestStatus } = await request.getDataRequestStatus( + requestStub().id, + ); + + expect(dataRequestStatus).toEqual(requestStub().status); + expect(getDataRequestStatusMock).toHaveBeenCalled(); + }); + + it('get data request status -> throww message', async () => { + const { getDataRequestStatusMock } = RequestMockService(sdk); + + expect( + async () => await request.getDataRequestStatus('wrong'), + ).rejects.toThrow('wrong is not valid'); + expect(getDataRequestStatusMock).toHaveBeenCalled(); + }); + + it('get data requests', async () => { + const { getDataRequestMock } = RequestMockService(sdk); + const { dataRequests } = await request.getDataRequests(); + + expect(dataRequests.length).toBeGreaterThanOrEqual(0); + expect(getDataRequestMock).toHaveBeenCalled(); + }); + + it('get requests received', async () => { + const { getRequestsReceivedMock } = RequestMockService(sdk); + const { requestsReceived } = await request.getRequestsReceived(); + + expect(requestsReceived.length).toBeGreaterThanOrEqual(0); + expect(getRequestsReceivedMock).toHaveBeenCalled(); + }); + + it('get requests recieved count', async () => { + const { getRequestReceivedCountMock } = RequestMockService(sdk); + const { requestsReceivedCount } = await request.getRequestsReceivedCount(); + + expect(requestsReceivedCount).toBeGreaterThanOrEqual(0); + expect(getRequestReceivedCountMock).toHaveBeenCalled(); + }); + + it('get requests sent', async () => { + const { getRequestsSentMock } = RequestMockService(sdk); + const { requestsSent } = await request.getRequestsSent(); + + expect(requestsSent.length).toBeGreaterThanOrEqual(0); + expect(getRequestsSentMock).toHaveBeenCalled(); + }); + + it('get requests sent count', async () => { + const { getDataRequestCountMock } = RequestMockService(sdk); + const { requestsSentCount } = await request.getRequestsSentCount(); + + expect(requestsSentCount).toBeGreaterThanOrEqual(0); + expect(getDataRequestCountMock).toHaveBeenCalled(); + }); +}); diff --git a/test/stubs/request.stub.ts b/test/stubs/request.stub.ts new file mode 100644 index 0000000..a388995 --- /dev/null +++ b/test/stubs/request.stub.ts @@ -0,0 +1,48 @@ +import { + DataRequest, + DataRequestSchemaInput, + UpdateDataRequestData, +} from '../../gatewaySdk/sources/Gateway'; + +export const requestStub = (overrideDataRequest?: any): DataRequest => ({ + id: '6188e5e1-dc9e-4b4a-837a-63e18c5f1ae5', + arweaveUrl: 'https://arweave.net/Mrs7eYW4366sIOM4nZ9mKHU-XnftQ_RbfDS1Hx1EQWM', + createdAt: '2023-12-12T18:46:12.932Z', + data: { + dataUse: + 'Give you a discount for the next "Taylor Swift - The Eras Tour" concert!', + proofData: {}, + schema: [{ id: '6188e5e1-dc9e-4b4a-837a-63e18c5f1ae5' }], + }, + + owner: { + did: 'did:gatewayId:rawatmanish12', + }, + verifierOrganization: true, + verifier: { id: '6188e5e1-dc9e-4b4a-837a-63e18c5f1ae5' }, + ...overrideDataRequest, +}); + +export const createRequestStub = ( + overrideDataRequest?: any, +): DataRequestSchemaInput => ({ + id: '6188e5e1-dc9e-4b4a-837a-63e18c5f1ae5', + data: { + dataUse: 'Example data usage for the user.', + owner: { type: 'USER_DID', value: 'did:gatewayid:mygateway:rawatmanish12' }, + schema: [ + { + id: '6188e5e1-dc9e-4b4a-837a-63e18c5f1ae5', + }, + ], + }, + ...overrideDataRequest, +}); + +export const updateRequestStub = ( + overrideDataRequest?: any, +): UpdateDataRequestData => ({ + requestId: '6188e5e1-dc9e-4b4a-837a-63e18c5f1ae5', + status: 'EXPIRED', + ...overrideDataRequest +}); diff --git a/test/user.test.ts b/test/user.test.ts index f301f8f..c713a9f 100644 --- a/test/user.test.ts +++ b/test/user.test.ts @@ -25,10 +25,20 @@ describe('USER SERVICE TESTING', () => { const { me } = await user.me(); expect(me.did).toEqual(userStub().did); - expect(meMock).toHaveBeenCalled(); }); + it('update me', async () => { + const { updateMeMock } = UserMockService(sdk); + + const { updateMyUser } = await user.updateMe({ + username: userStub().username, + }); + + expect(updateMyUser.did).toEqual(userStub().did); + expect(updateMeMock).toHaveBeenCalled(); + }); + it('single user', async () => { const { getSingleUserMock } = UserMockService(sdk); @@ -38,7 +48,6 @@ describe('USER SERVICE TESTING', () => { }); expect(res.user?.did).toEqual(userStub().did); - expect(getSingleUserMock).toHaveBeenCalled(); });