diff --git a/test/activity.test.ts b/test/activity.test.ts new file mode 100644 index 0000000..dd12c64 --- /dev/null +++ b/test/activity.test.ts @@ -0,0 +1,50 @@ +import { GraphQLClient } from 'graphql-request'; + +import { getSdk } from '../../gatewaySdk/sources/GatewayV3'; +import { ActivityMockService } from '../__mocks__/v3/activity.mock'; +import { activityStub } from '../stubs/v3/activity.stub'; +import { Activity } from '../../src/v3/activity/activity'; + +let activity: Activity; + +beforeAll(() => { + activity = new Activity(getSdk(new GraphQLClient(''))); +}); + +afterAll(() => { + jest.resetAllMocks(); +}); + +describe('ACTIVITY SERVICE TESTING', () => { + it('get Activity count', async () => { + const { getActivityCountMock } = ActivityMockService(activity); + const { activitiesCount } = await activity.getActivityCount(); + + expect(activitiesCount).toBeGreaterThanOrEqual(10); + expect(getActivityCountMock).toHaveBeenCalled(); + }); + + it('get Activity by id', async () => { + const { getActivityMock } = ActivityMockService(activity); + const { activity: activityResult } = await activity.getActivity( + activityStub().id, + ); + expect(activityResult.id).toEqual(activityStub().id); + expect(getActivityMock).toHaveBeenCalled(); + }); + + it('get Activity by id => throw error', async () => { + const { getActivityMock } = ActivityMockService(activity); + expect(async () => await activity.getActivity('34')).rejects.toThrow( + ' should be atleast 2 length', + ); + expect(getActivityMock).toHaveBeenCalled(); + }); + + it('get Activitys', async () => { + const { getActivitysMock } = ActivityMockService(activity); + const { activities } = await activity.getActivities(); + expect(activities.length).toBeGreaterThanOrEqual(0); + expect(getActivitysMock).toHaveBeenCalled(); + }); +}); diff --git a/test/auth.test.ts b/test/auth.test.ts new file mode 100644 index 0000000..19899dd --- /dev/null +++ b/test/auth.test.ts @@ -0,0 +1,180 @@ +import { GraphQLClient } from 'graphql-request'; + +import { getSdk } from '../../gatewaySdk/sources/GatewayV3'; +import { AuthMockService } from '../__mocks__/v3/auth.mock'; +import { authStub } from '../stubs/v3/auth.stub'; +import { Auth } from '../../src/v3/auth/auth'; +import { SignCipherEnum } from '../../src/types'; + +let auth: Auth; + +beforeAll(() => { + auth = new Auth(getSdk(new GraphQLClient(''))); +}); + +afterAll(() => { + jest.resetAllMocks(); +}); + +describe('AUTH SERVICE TESTING', () => { + it('check username availability', async () => { + const { checkUsernameAvailabilityMock } = AuthMockService(auth); + + const result = await auth.checkUsernameAvailability(authStub().username); + + expect(result).toEqual(true); + expect(checkUsernameAvailabilityMock).toHaveBeenCalled(); + }); + + it('check username availability to throw error', async () => { + const { checkUsernameAvailabilityMock } = AuthMockService(auth); + + expect(async () => { + await auth.checkUsernameAvailability(authStub({ username: '' }).username); + }).rejects.toThrow(' should be atleast 2 length'); + expect(checkUsernameAvailabilityMock).toHaveBeenCalled(); + }); + + it('check did availability', async () => { + const { checkDIDAvailabilityMock } = AuthMockService(auth); + + const result = await auth.checkDIDAvailability(authStub().did); + + expect(result).toEqual(true); + expect(checkDIDAvailabilityMock).toHaveBeenCalled(); + }); + + it('check did availability to throw error', async () => { + const { checkDIDAvailabilityMock } = AuthMockService(auth); + + expect(async () => { + await auth.checkDIDAvailability(authStub({ did: '' }).did); + }).rejects.toThrow(` is not valid did`); + expect(checkDIDAvailabilityMock).toHaveBeenCalled(); + }); + + it('create user nonce', async () => { + const { createUserNonceMock } = AuthMockService(auth); + + const nonce = await auth.createUserNonce({ + did: authStub().did, + signingKey: authStub().wallet, + }); + + expect(nonce).toEqual(authStub().message); + expect(createUserNonceMock).toHaveBeenCalled(); + }); + + it('create user nonce to throw error', async () => { + const { createUserNonceMock } = AuthMockService(auth); + + expect(async () => { + await auth.createUserNonce({ + did: authStub({ did: '' }).did, + signingKey: authStub().wallet, + }); + }).rejects.toThrow(` is not valid did`); + expect(createUserNonceMock).toHaveBeenCalled(); + }); + + it('create user', async () => { + const { createUserMock } = AuthMockService(auth); + + const result = await auth.createUser({ + signature: authStub().signature, + signingKey: authStub().wallet, + }); + + expect(result).toEqual(authStub().jwt); + expect(createUserMock).toHaveBeenCalled(); + }); + + it('create user to throw error', async () => { + const { createUserMock } = AuthMockService(auth); + + expect(async () => { + await auth.createUser({ + signature: authStub().signature, + signingKey: authStub({ wallet: '' }).wallet, + }); + }).rejects.toThrow(` is invalid`); + expect(createUserMock).toHaveBeenCalled(); + }); + + it('create user to throw wrong wallet error', async () => { + const { createUserMock } = AuthMockService(auth); + + expect(async () => { + await auth.createUser({ + signature: authStub().signature, + signingKey: authStub().wallet, + signingCipher: SignCipherEnum.ED25519, + }); + }).rejects.toThrow(''); + expect(createUserMock).toHaveBeenCalled(); + }); + + it('generate nonce', async () => { + const { generateNonceMock } = AuthMockService(auth); + + const { generateNonce } = await auth.generateNonce(authStub().wallet); + + expect(generateNonce.message).toEqual(authStub().message); + expect(generateNonceMock).toHaveBeenCalled(); + }); + + it('generate nonce to throw error', async () => { + const { generateNonceMock } = AuthMockService(auth); + + expect(async () => { + await auth.generateNonce(authStub({ wallet: '' }).wallet); + }).rejects.toThrow(` is invalid`); + expect(generateNonceMock).toHaveBeenCalled(); + }); + + it('generate nonce to throw wrong wallet error', async () => { + const { generateNonceMock } = AuthMockService(auth); + + expect(async () => { + await auth.generateNonce(authStub().wallet, SignCipherEnum.ED25519); + }).rejects.toThrow(''); + expect(generateNonceMock).toHaveBeenCalled(); + }); + + it('refresh token', async () => { + const { refreshTokenMock } = AuthMockService(auth); + + const jwt = await auth.refreshToken({ + signature: authStub().signature, + signingKey: authStub().wallet, + }); + + expect(jwt).toEqual(authStub().jwt); + expect(refreshTokenMock).toHaveBeenCalled(); + }); + + it('refresh token to throw error', async () => { + const { refreshTokenMock } = AuthMockService(auth); + + expect(async () => { + await auth.refreshToken({ + signature: authStub({ signature: '' }).signature, + signingKey: authStub().wallet, + }); + }).rejects.toThrow(` should be atleast 2 length`); + expect(refreshTokenMock).toHaveBeenCalled(); + }); + + it('refresh token to throw wrong wallet error', async () => { + const { refreshTokenMock } = AuthMockService(auth); + + expect(async () => { + await auth.refreshToken({ + signature: authStub().signature, + signingKey: authStub().wallet, + cipher: 'ED25519', + }); + }).rejects.toThrow(''); + expect(refreshTokenMock).toHaveBeenCalled(); + }); +}); diff --git a/test/data-model.test.ts b/test/data-model.test.ts new file mode 100644 index 0000000..9188f8f --- /dev/null +++ b/test/data-model.test.ts @@ -0,0 +1,147 @@ +import { getSdk } from '../../gatewaySdk/sources/GatewayV3'; +import { DataModel } from '../../src/v3/data-model/data-model'; +import { + dataModelCreateStub, + dataModelStub, + dataModelMetaDataStub, +} from '../stubs/v3/data-model.stub'; +import { DataModelMockService } from '../__mocks__/v3/data-model.mock'; +import { GraphQLClient } from 'graphql-request'; + +let dataModel: DataModel; + +beforeAll(() => { + dataModel = new DataModel(getSdk(new GraphQLClient(''))); +}); + +afterAll(() => { + jest.resetAllMocks(); +}); + +describe('DATA MODEL CLASS METHODS TESTING', () => { + it('create data model ', async () => { + const { createDataModelMock } = DataModelMockService(dataModel); + + const { createDataModel } = await dataModel.createDataModel( + dataModelCreateStub(), + ); + + expect(createDataModel.title).toEqual(dataModelCreateStub().data.title); + expect(createDataModelMock).toHaveBeenCalled(); + }); + + it('create data model -> throw error ', async () => { + const { createDataModelMock } = DataModelMockService(dataModel); + + expect( + async () => + await dataModel.createDataModel({ + data: { + title: '', + description: '', + schema: undefined, + }, + signature: '', + signingKey: '', + signingCipher: 'ED25519', + }), + ).rejects.toThrow(' should be atleast 2 length'); + expect(createDataModelMock).toHaveBeenCalled(); + }); + + it('get data model', async () => { + const { getDataModelMock } = DataModelMockService(dataModel); + + const { dataModel: rdataModel } = await dataModel.getDataModel( + dataModelStub().id, + ); + expect(rdataModel).toEqual(dataModelStub()); + expect(getDataModelMock).toHaveBeenCalled(); + }); + + it('get data model -> throw error', async () => { + const { getDataModelMock } = DataModelMockService(dataModel); + + expect(async () => await dataModel.getDataModel('wrong')).rejects.toThrow( + 'wrong is not valid', + ); + expect(getDataModelMock).toHaveBeenCalled(); + }); + + it('get data models', async () => { + const { getDataModelsMock } = DataModelMockService(dataModel); + + const { dataModels } = await dataModel.getDataModels(); + + expect(dataModels.length).toBeGreaterThanOrEqual(0); + expect(getDataModelsMock).toHaveBeenCalled(); + }); + + it('get data models count', async () => { + const { getDataModelsCountMock } = DataModelMockService(dataModel); + const { dataModelsCount } = await dataModel.getDataModelsCount(); + expect(dataModelsCount).toBeGreaterThanOrEqual(0); + expect(getDataModelsCountMock).toHaveBeenCalled(); + }); + + it('get meta data of data models', async () => { + const { getDataModelsMetaDataMock } = DataModelMockService(dataModel); + const { dataModelsMetadata } = await dataModel.getDataModelsMetaData(); + expect(dataModelsMetadata).toEqual(dataModelMetaDataStub()); + expect(getDataModelsMetaDataMock).toHaveBeenCalled(); + }); + + it('get issuers by data model', async () => { + const { getIssuersByDataModelMock } = DataModelMockService(dataModel); + const { issuersByDataModel } = await dataModel.getIssuersByDataModel( + dataModelStub().id, + ); + + expect(issuersByDataModel.length).toBeGreaterThanOrEqual(0); + expect(getIssuersByDataModelMock).toHaveBeenCalled(); + }); + + it('get issuers by data model -> throw error', async () => { + const { getIssuersByDataModelMock } = DataModelMockService(dataModel); + expect( + async () => await dataModel.getIssuersByDataModel('wrong'), + ).rejects.toThrow('wrong is not valid'); + expect(getIssuersByDataModelMock).toHaveBeenCalled(); + }); + + it('get isssuers by data model count', async () => { + const { getIssuersDataModelCountMock } = DataModelMockService(dataModel); + const { issuersByDataModelCount } = + await dataModel.getIssuersByDataModelCount(dataModelStub().id); + expect(issuersByDataModelCount).toBeGreaterThanOrEqual(0); + expect(getIssuersDataModelCountMock).toHaveBeenCalled(); + }); + + it('get isssuers by data model count -> throw error', async () => { + const { getIssuersDataModelCountMock } = DataModelMockService(dataModel); + expect( + async () => await dataModel.getIssuersByDataModelCount('wrong'), + ).rejects.toThrow('wrong is not valid'); + expect(getIssuersDataModelCountMock).toHaveBeenCalled(); + }); + + it('get total issuers by data model ', async () => { + const { getTotalofIssuersByDataModelMock } = + DataModelMockService(dataModel); + const { getTotalofIssuersByDataModel } = + await dataModel.getTotalofIssuersByDataModel(dataModelStub().id); + + expect(getTotalofIssuersByDataModel).toBeGreaterThanOrEqual(0); + expect(getTotalofIssuersByDataModelMock).toHaveBeenCalled(); + }); + + it('get total issuers by data model -> throw error ', async () => { + const { getTotalofIssuersByDataModelMock } = + DataModelMockService(dataModel); + + expect( + async () => await dataModel.getTotalofIssuersByDataModel('wrong'), + ).rejects.toThrow('wrong is not valid'); + expect(getTotalofIssuersByDataModelMock).toHaveBeenCalled(); + }); +}); diff --git a/test/organization.test.ts b/test/organization.test.ts new file mode 100644 index 0000000..ac0d1de --- /dev/null +++ b/test/organization.test.ts @@ -0,0 +1,212 @@ +import { GraphQLClient } from 'graphql-request'; +import { OrganizationMockService } from '../__mocks__/v3/organization.mock'; +import { + organizationCreateStub, + organizationStub, +} from './stubs/organization.stub'; +import { authStub } from './stubs/auth.stub'; +import { userStub } from './stubs/user.stub'; + +let organization: Organization; + +beforeAll(() => { + organization = new Organization(getSdk(new GraphQLClient(''))); +}); + +afterAll(() => { + jest.resetAllMocks(); +}); + +describe('ORGANIZATION SERVICE TESTING', () => { + it('organization create', async () => { + const { createOrganizationMock } = OrganizationMockService(organization); + + const { createOrganization } = await organization.createOrganization( + organizationCreateStub(), + ); + + expect(createOrganization.name).toEqual(organizationStub().name); + expect(createOrganizationMock).toHaveBeenCalled(); + }); + + it('organization create to throw error', async () => { + const { createOrganizationMock } = OrganizationMockService(organization); + + expect( + async () => + await organization.createOrganization( + organizationCreateStub({ data: { username: '' } }), + ), + ).rejects.toThrow(' should be atleast 2 length'); + + expect(createOrganizationMock).toHaveBeenCalled(); + }); + + it('organization create to throw wallet error', async () => { + const { createOrganizationMock } = OrganizationMockService(organization); + + expect( + async () => + await organization.createOrganization( + organizationCreateStub({ signingCipher: 'ED25519' }), + ), + ).rejects.toThrow(''); + + expect(createOrganizationMock).toHaveBeenCalled(); + }); + + it('get single organization', async () => { + const { getOrganizationMock } = OrganizationMockService(organization); + + const res = await organization.getOrganization( + OrganizationIdentifierType.ORG_DID, + organizationStub().did, + ); + + expect(res.organization?.did).toEqual(organizationStub().did); + expect(getOrganizationMock).toHaveBeenCalled(); + }); + + it('get single organization to throw error', async () => { + const { getOrganizationMock } = OrganizationMockService(organization); + + expect( + async () => + await organization.getOrganization( + OrganizationIdentifierType.ORG_DID, + organizationStub({ did: '' }).did, + ), + ).rejects.toThrow(''); + + expect(getOrganizationMock).toHaveBeenCalled(); + }); + + it('update organization', async () => { + const { updateOrganizationMock } = OrganizationMockService(organization); + + let updatedOrgObj = { + description: 'updated description', + did: organizationStub().did, + }; + const { updateOrganization } = await organization.updateOrganization({ + data: updatedOrgObj, + signature: authStub().signature, + signingKey: authStub().wallet, + }); + + expect(updateOrganization.description).toEqual(updatedOrgObj.description); + expect(updateOrganizationMock).toHaveBeenCalled(); + }); + + it('update organization to throw error', async () => { + const { updateOrganizationMock } = OrganizationMockService(organization); + + let updatedOrgObj = { + description: 'updated description', + did: organizationStub({ did: '' }).did, + }; + expect( + async () => + await organization.updateOrganization({ + data: updatedOrgObj, + signature: authStub().signature, + signingKey: authStub().wallet, + }), + ).rejects.toThrow(''); + + expect(updateOrganizationMock).toHaveBeenCalled(); + }); + + it('update organization to throw wallet error', async () => { + const { updateOrganizationMock } = OrganizationMockService(organization); + + let updatedOrgObj = { + description: 'updated description', + did: organizationStub().did, + }; + expect( + async () => + await organization.updateOrganization({ + data: updatedOrgObj, + signature: authStub().signature, + signingKey: authStub().wallet, + signingCipher: 'ED25519', + }), + ).rejects.toThrow(''); + + expect(updateOrganizationMock).toHaveBeenCalled(); + }); + + // it('member crud organization', async () => { + // const { + // addMemberToOrganizationMock, + // changeMemberRoleMock, + // removeMemberFromOrganizationMock, + // } = OrganizationMockService(organization); + + // let addMemberObj = { + // data: { + // organization: { + // type: OrganizationIdentifierTypeV3.ORG_DID, + // value: organizationStub().did, + // }, + // user: { type: UserIdentifierTypeV3.USER_ID, value: 'testing_sdk' }, + // }, + // signature: '', + // signingKey: '', + // }; + // let changeMemberRoleObj = { + // ...addMemberObj, + // role: OrganizationRoleV3.ADMIN, + // }; + // const { addMemberToOrganization } = + // await organization.addMemberToOrganization(addMemberObj); + + // expect(addMemberToOrganization).toBeDefined(); + // expect(addMemberToOrganizationMock).toHaveBeenCalled(); + + // const { changeMemberRole } = + // await organization.changeMemberRole(changeMemberRoleObj); + + // expect(changeMemberRole.role).toBe(OrganizationRoleV3.ADMIN); + // expect(changeMemberRoleMock).toHaveBeenCalled(); + + // const { removeMemberFromOrganization } = + // await organization.removeMemberFromOrganization(addMemberObj); + + // expect(removeMemberFromOrganization).toBeTruthy(); + // expect(removeMemberFromOrganizationMock).toHaveBeenCalled(); + // }); + + // it('trasfer ownership', async () => { + // const { transferOwnershipOrganizationMock } = + // OrganizationMockService(organization); + + // const { transferOwnership } = await organization.transferOwnership({ + // data: { + // organization: { + // type: OrganizationIdentifierTypeV3.ORG_DID, + // value: organizationStub().did, + // }, + // user: { type: UserIdentifierTypeV3.USER_ID, value: 'testing_sdk' }, + // }, + // signature: '', + // signingKey: '', + // }); + + // expect(transferOwnership.user.did).toBe(userStub().did); + // expect(transferOwnershipOrganizationMock).toHaveBeenCalled(); + // }); + + it('organizations', async () => { + const { getOrganizationsMock } = OrganizationMockService(organization); + + const { organizations } = await organization.getOrganizations({ + skip: 0, + take: 10, + }); + + expect(organizations.length).toBeGreaterThanOrEqual(0); + expect(getOrganizationsMock).toHaveBeenCalled(); + }); +}); diff --git a/test/proof.test.ts b/test/proof.test.ts new file mode 100644 index 0000000..3833591 --- /dev/null +++ b/test/proof.test.ts @@ -0,0 +1,154 @@ +import { Proof } from '../../src/v3/proof/proof'; +import { getSdk } from '../../gatewaySdk/sources/GatewayV3'; +import { proofStub, createProofStub } from '../stubs/v3/proof.stub'; +import { ProofMockService } from '../__mocks__/v3/proof.mock'; +import { pdaStub } from '../stubs/v3/pda.stub'; +import { GraphQLClient } from 'graphql-request'; + +let proof: Proof; + +beforeAll(() => { + proof = new Proof(getSdk(new GraphQLClient(''))); +}); + +afterAll(() => { + jest.resetAllMocks(); +}); + +describe('PROOF SERVICE TESTING', () => { + it('proof create', async () => { + const { createProofMock } = ProofMockService(proof); + + const { createProof } = await proof.createProof(createProofStub()); + + expect(createProof.id).toBe(proofStub().id); + expect(createProofMock).toHaveBeenCalled(); + }); + + it('get proof', async () => { + const { getProofMock } = ProofMockService(proof); + const { proof: resultProof } = await proof.getProof(proofStub().id); + expect(resultProof.id).toBe(proofStub().id); + expect(getProofMock).toHaveBeenCalled(); + }); + + it('get proof -> throw error', async () => { + const { getProofMock } = ProofMockService(proof); + expect(async () => await proof.getProof('324324324')).rejects.toThrow( + '324324324 is not valid', + ); + expect(getProofMock).toHaveBeenCalled(); + }); + + it('get proofs', async () => { + const { getProofsMock } = ProofMockService(proof); + const { proofs: resultProofs } = await proof.getProofs(); + expect(resultProofs?.length).toBeGreaterThan(0); + expect(getProofsMock).toHaveBeenCalled(); + }); + + // it('get proofs by pda ids', async () => { + // const { getProofsByPDAIdsMock } = ProofMockService(proof); + // const { proofsByPDAIds } = await proof.getProofsByPDA({ + // pdaIds: [pdaStub().id], + // }); + + // expect(proofsByPDAIds.length).toBeGreaterThan(0); + // expect(getProofsByPDAIdsMock).toHaveBeenCalled(); + // }); + + // it('get proofs by pda ids -> single id', async () => { + // const { getProofsByPDAIdsMock } = ProofMockService(proof); + // const { proofsByPDAIds } = await proof.getProofsByPDAIds({ + // pdaIds: pdaStub().id, + // }); + + // expect(proofsByPDAIds.length).toBeGreaterThan(0); + // expect(getProofsByPDAIdsMock).toHaveBeenCalled(); + // }); + + // it('get proofs by pda ids -> throw error message', async () => { + // const { getProofsByPDAIdsMock } = ProofMockService(proof); + + // expect( + // async () => + // await proof.getProofsByPDAIds({ + // pdaIds: 'pdaStub().id,', + // }), + // ).rejects.toThrow(''); + + // expect(getProofsByPDAIdsMock).toHaveBeenCalled(); + // }); + + it('get received proofs', async () => { + const { getReceivedProofsMock } = ProofMockService(proof); + const { receivedProofs } = await proof.getReceivedProofs(); + + expect(receivedProofs.length).toBeGreaterThan(0); + expect(getReceivedProofsMock).toHaveBeenCalled(); + }); + + // it('get received proofs -> single id', async () => { + // const { getReceivedProofsMock } = ProofMockService(proof); + // const { receivedProofs } = await proof.getReceivedProofs({ + // organizationId: pdaStub().id, + // }); + + // expect(receivedProofs.length).toBeGreaterThan(0); + // expect(getReceivedProofsMock).toHaveBeenCalled(); + // }); + + it('get received proofs -> throw single error message', async () => { + const { getReceivedProofsMock } = ProofMockService(proof); + expect( + async () => + await proof.getReceivedProofs({ + organizationId: 'pdaStub().id', + }), + ).rejects.toThrow(''); + + expect(getReceivedProofsMock).toHaveBeenCalled(); + }); + + it('get received proofs count', async () => { + const { getReceivedProofsCountMock } = ProofMockService(proof); + const { receivedProofsCount } = await proof.getReceivedProofsCount(); + expect(receivedProofsCount).toBeGreaterThanOrEqual(0); + + expect(getReceivedProofsCountMock).toHaveBeenCalled(); + }); + + // it('get received proofs count --> single id', async () => { + // const { getReceivedProofsCountMock } = ProofMockService(proof); + // const { receivedProofsCount } = await proof.getReceivedProofsCount( + // pdaStub().id, + // ); + // expect(receivedProofsCount).toBeGreaterThanOrEqual(0); + + // expect(getReceivedProofsCountMock).toHaveBeenCalled(); + // }); + + it('get received proofs count --> throw error single id', async () => { + const { getReceivedProofsCountMock } = ProofMockService(proof); + expect( + async () => await proof.getReceivedProofsCount('pdaStub().id'), + ).rejects.toThrow(''); + + expect(getReceivedProofsCountMock).toHaveBeenCalled(); + }); + + it('get sent proofs', async () => { + const { getSentProofsMock } = ProofMockService(proof); + const { sentProofs } = await proof.getSentProofs(); + + expect(sentProofs.length).toBeGreaterThanOrEqual(0); + expect(getSentProofsMock).toHaveBeenCalled(); + }); + + it('get sent proofs count', async () => { + const { getSentProofsCountMock } = ProofMockService(proof); + const { sentProofsCount } = await proof.getSentProofsCount(); + expect(sentProofsCount).toBeGreaterThanOrEqual(0); + expect(getSentProofsCountMock).toHaveBeenCalled(); + }); +}); diff --git a/test/user.test.ts b/test/user.test.ts new file mode 100644 index 0000000..c965310 --- /dev/null +++ b/test/user.test.ts @@ -0,0 +1,125 @@ +import { GraphQLClient } from 'graphql-request'; +import { getSdk } from '../../gatewaySdk/sources/GatewayV3'; +import { User } from '../../src/v3/user/user'; +import { invalidUUID, userStub } from '../stubs/v3/user.stub'; +import { UserMockService } from '../__mocks__/v3/user.mock'; +import { UserIdentifierType, UserIdentifierTypeV3 } from '../../src/types'; + +let user: User; + +beforeAll(() => { + user = new User(getSdk(new GraphQLClient(''))); +}); + +afterAll(() => { + jest.clearAllMocks(); +}); + +describe('USER SERVICE TESTING', () => { + it('me', async () => { + const { meMock } = UserMockService(user); + + const { me } = await user.me(); + + expect(me.did).toEqual(userStub().did); + + expect(meMock).toHaveBeenCalled(); + }); + + it('single user', async () => { + const { getSingleUserMock } = UserMockService(user); + + const res = await user.getSingleUser({ + type: UserIdentifierTypeV3.USER_ID, + value: userStub().id, + }); + + expect(res.user?.did).toEqual(userStub().did); + + expect(getSingleUserMock).toHaveBeenCalled(); + }); + + it('single user to throw error', async () => { + const { getSingleUserMock } = UserMockService(user); + + expect( + async () => + await user.getSingleUser({ + type: UserIdentifierTypeV3.USER_DID, + value: userStub({ did: '' }).did, + }), + ).rejects.toThrow(''); + + expect(getSingleUserMock).toHaveBeenCalled(); + }); + + it('my pdas count', async () => { + const { myPDACountMock } = UserMockService(user); + + const count = await user.myPDACount(); + + expect(count).toBeGreaterThanOrEqual(0); + expect(myPDACountMock).toHaveBeenCalled(); + }); + + it('my pdas count to throw error', async () => { + const { myPDACountMock } = UserMockService(user); + + expect( + async () => + await user.myPDACount({ filter: { dataModelIds: [invalidUUID] } }), + ).rejects.toThrow(`${invalidUUID} is not valid uuid`); + + expect(myPDACountMock).toHaveBeenCalled(); + }); + + it('my pdas', async () => { + const { myPDAsMock } = UserMockService(user); + + const { myPDAs } = await user.myPDAs({ + skip: 0, + take: 10, + }); + + expect(myPDAs.length).toBeGreaterThanOrEqual(0); + expect(myPDAsMock).toHaveBeenCalled(); + }); + + it('my pdas to throw error', async () => { + const { myPDAsMock } = UserMockService(user); + + expect( + async () => + await user.myPDAs({ filter: { dataModelIds: [invalidUUID] } }), + ).rejects.toThrow(`${invalidUUID} is not valid uuid`); + + expect(myPDAsMock).toHaveBeenCalled(); + }); + + it('my data models count', async () => { + const { myDataModelsCountMock } = UserMockService(user); + + const count = await user.myDataModelsCount(); + + expect(count).toBeGreaterThanOrEqual(0); + expect(myDataModelsCountMock).toHaveBeenCalled(); + }); + + it('my activities count', async () => { + const { myActivitiesCountMock } = UserMockService(user); + + const count = await user.myActivitiesCount(); + + expect(count).toBeGreaterThanOrEqual(0); + expect(myActivitiesCountMock).toHaveBeenCalled(); + }); + + it('my activities', async () => { + const { myActivitiesMock } = UserMockService(user); + + const { myActivities } = await user.myActivities(); + + expect(myActivities.length).toBeGreaterThanOrEqual(0); + expect(myActivitiesMock).toHaveBeenCalled(); + }); +}); diff --git a/test/utils.test.ts b/test/utils.test.ts new file mode 100644 index 0000000..b06b5cc --- /dev/null +++ b/test/utils.test.ts @@ -0,0 +1,187 @@ +import forge from 'node-forge'; +import { Chain, UserIdentifierType } from '../src/types'; +import { errorHandler } from '../src/utils/errorHandler'; +import { + isDateValid, + isEmailValid, + isStringValid, + isUUIDValid, + isValidUrl, + isWalletAddressValid, + validateEtherumWallet, + validateObjectProperties, + validatePDAFilter, + validateSolanaWallet, +} from '../src/utils/validators'; +import { + decryptWithPKI, + encryptWithPKI, + generateDID, + generateNewEtherumWallet, + generateRSAKeyPair, + jsonEncoder, + sharedEncryptWithPKI, +} from '../src/utils/v3-crypto-helper'; +import { authStub } from './stubs/v2/auth.stub'; + +describe('UTILS VALIDATORS TESTING', () => { + it('error handler testing normal', () => { + const result = errorHandler('Something went wrong'); + expect(result).toBeDefined(); + }); + + it('error handler testing array', () => { + const result = errorHandler([{ message: 'cant acccess pda' }]); + expect(result).toBeDefined(); + }); + + it('error handler testing object', () => { + const result = errorHandler({ message: 'cant acccess pda' }); + expect(result).toBeDefined(); + }); + + it('email validator', () => { + const result = isEmailValid('test@gmail.com'); + expect(result).toBeDefined(); + expect(() => isEmailValid('wrong-email.com')).toThrow( + 'wrong-email.com is not valid', + ); + }); + + it('uuid validator', () => { + const result = isUUIDValid('f17ac10b-58cc-4372-a567-0e02b2c3d479'); + expect(result).toBeDefined(); + expect(() => isUUIDValid('f17ac10b-58cc-4372-a567')).toThrow( + 'f17ac10b-58cc-4372-a567 is not valid', + ); + }); + + it('url validator', () => { + const result = isValidUrl('https://fake-url.com'); + expect(result).toBeDefined(); + expect(() => isValidUrl('f17ac10b-58cc-4372-a567')).toThrow( + 'f17ac10b-58cc-4372-a567 is not valid', + ); + }); + + it('string validator', () => { + const result = isStringValid('test pda'); + expect(result).toBeDefined(); + expect(() => isStringValid('')).toThrow(' should be atleast 2 length'); + }); + + it('etherum validator', () => { + const result = validateEtherumWallet(authStub().wallet); + expect(result).toBeDefined(); + expect(() => + validateEtherumWallet('f17ac10b-58cc-4372-a567-0e02b2c3d479'), + ).toThrow('f17ac10b-58cc-4372-a567-0e02b2c3d479 is invalid'); + }); + + it('etherum & solana validator', () => { + const result = isWalletAddressValid( + '9aohAjd3okUogzGJT6N2cQUDwBbi2ay7oSzPPaQjQ22s', + Chain.SOL, + ); + expect(result).toBeDefined(); + expect(() => + isWalletAddressValid('f17ac10b-58cc-4372-a567-0e02b2c3d479', Chain.SOL), + ).toThrow('Non-base58 character'); + }); + + it('solana validator', () => { + const result = validateSolanaWallet( + '9aohAjd3okUogzGJT6N2cQUDwBbi2ay7oSzPPaQjQ22s', + ); + expect(result).toBeDefined(); + expect(() => + validateSolanaWallet('f17ac10b-58cc-4372-a567-0e02b2c3d479'), + ).toThrow('Non-base58 character'); + }); + + it('date validator', () => { + const result = isDateValid(new Date().toDateString()); + expect(result).toBeDefined(); + expect(() => isDateValid('f17ac10b-58cc-4372-a567-0e02b2c3d479')).toThrow( + 'f17ac10b-58cc-4372-a567-0e02b2c3d479 is not valid', + ); + }); + + it('validate object', () => { + let sampleObject = { + dataModelId: 'f17ac10b-58cc-4372-a567-0e02b2c3d479', + description: 'test', + title: 'test', + dateTest: new Date(), + owner: { + type: UserIdentifierType.GATEWAY_ID, + value: 'test', + }, + }; + const result = validateObjectProperties(sampleObject); + expect(result).toBeUndefined(); + }); + + it('validate v3 pda filter data model filter', async () => { + let samplePDAFilter = { + dataModelIds: ['111'], + }; + expect( + async () => await validatePDAFilter(samplePDAFilter), + ).rejects.toThrow('111 is not valid uuid'); + }); + + it('validate v3 pda filter ids filter', async () => { + let samplePDAFilter = { + ids: ['111'], + }; + expect( + async () => await validatePDAFilter(samplePDAFilter), + ).rejects.toThrow('111 is not valid uuid'); + }); +}); + +describe('UTILS CRYPTO V3 TESTING', () => { + it('generate did', () => { + const result = generateDID('test'); + expect(result).toBeDefined(); + }); + + it('generate new etherum wallet', () => { + const result = generateNewEtherumWallet(); + expect(result).toBeDefined(); + }); + + it('generate RSA primary key', () => { + const result = generateRSAKeyPair(); + expect(result).toBeDefined(); + }); + + it('whole encryption/decryption flow', async () => { + const { privateKey, publicPem } = generateRSAKeyPair(); + const did = generateDID('test'); + const result = sharedEncryptWithPKI('hello', { + did, + publicPem: publicPem, + }); + + const result2 = encryptWithPKI('hello ser', publicPem, did); + const decryptedData = await decryptWithPKI( + result, + did, + forge.util.decode64(privateKey), + ); + expect(result).toBeDefined(); + expect(result2).toBeDefined(); + expect(decryptedData).toBeDefined(); + expect(decryptedData).toBe('hello'); + }); + + it('json encoder', () => { + let sampleObject = { + key: 'value', + }; + const encodedObject = jsonEncoder(sampleObject); + expect(encodedObject).toBeDefined(); + }); +});