diff --git a/.meshrc.yml b/.meshrc.yml index 1fffbb9..1431ed2 100644 --- a/.meshrc.yml +++ b/.meshrc.yml @@ -15,4 +15,4 @@ customFetch: ./custom-fetch.ts sdk: generateOperations: - selectionSetDepth: 2 \ No newline at end of file + selectionSetDepth: 2 diff --git a/__mocks__/auth.mock.ts b/__mocks__/auth.mock.ts new file mode 100644 index 0000000..2f047f0 --- /dev/null +++ b/__mocks__/auth.mock.ts @@ -0,0 +1,23 @@ +import { Auth } from '../src/auth/auth'; +import { authStub } from '../test/stubs/auth.stub'; +import { pdaStub } from '../test/stubs/pda.stub'; + +export const AuthMockService = (auth: Auth) => ({ + checkUsernameAvailabilityMock: jest + .spyOn(auth.sdk, 'checkUsernameAvailability_query') + .mockResolvedValue({ + checkUsernameAvailability: true, + }), + addEmailMock: jest.spyOn(auth.sdk, 'addEmail_mutation').mockResolvedValue({ + addEmail: authStub(), + }), + addWalletMock: jest + .spyOn(auth.sdk, 'addWallet_mutation') + .mockResolvedValue({ addWallet: authStub() }), + getPDAMock: jest.spyOn(auth.sdk, 'PDA_query').mockResolvedValue({ + PDA: pdaStub(), + }), + createWalletNonceMock: jest + .spyOn(auth.sdk, 'createWalletNonce_mutation') + .mockResolvedValue({ createWalletNonce: authStub() }), +}); diff --git a/package.json b/package.json index ac28c9b..d7bc7e4 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "eslint-plugin-promise": "^6.1.1", "jest": "^29.7.0", "prettier": "^3.1.1", + "ts-jest": "^29.1.1", "typescript": "^5.3.3" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index effec8a..a2bb9f7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,7 +9,6 @@ specifiers: '@types/node': ^20.10.4 '@typescript-eslint/eslint-plugin': ^6.13.2 '@whatwg-node/fetch': ^0.9.14 - dotenv: ^16.3.1 eslint: ^8.55.0 eslint-config-prettier: ^9.1.0 eslint-config-standard-with-typescript: ^39.1.1 @@ -21,7 +20,6 @@ specifiers: jest: ^29.7.0 prettier: ^3.1.1 ts-jest: ^29.1.1 - ts-node: ^10.9.2 typescript: ^5.3.3 dependencies: @@ -36,7 +34,6 @@ devDependencies: '@types/jest': 29.5.11 '@types/node': 20.10.4 '@typescript-eslint/eslint-plugin': 6.13.2_7hj7rehet5x3fvq7nqub5sammm - dotenv: 16.3.1 eslint: 8.55.0 eslint-config-prettier: 9.1.0_eslint@8.55.0 eslint-config-standard-with-typescript: 39.1.1_mka63c7brje3imjaenl3c5gusi @@ -44,10 +41,9 @@ devDependencies: eslint-plugin-n: 16.3.1_eslint@8.55.0 eslint-plugin-prettier: 5.0.1_kavvf57s7md676dhxydp3dqm2e eslint-plugin-promise: 6.1.1_eslint@8.55.0 - jest: 29.7.0_a56qyaxzmzl4f2pjmodmqi2bye + jest: 29.7.0_@types+node@20.10.4 prettier: 3.1.1 ts-jest: 29.1.1_ft44a4vuo3yhmyfgh7dialedhm - ts-node: 10.9.2_yn7bv5jzkfjmbt2js7kurghsqq typescript: 5.3.3 packages: @@ -778,6 +774,7 @@ packages: engines: {node: '>=12'} dependencies: '@jridgewell/trace-mapping': 0.3.9 + dev: false /@envelop/core/5.0.0: resolution: {integrity: sha512-aJdnH/ptv+cvwfvciCBe7TSvccBwo9g0S5f6u35TBVzRVqIGkK03lFlIL+x1cnfZgN9EfR2b1PH2galrT1CdCQ==} @@ -1921,7 +1918,7 @@ packages: slash: 3.0.0 dev: true - /@jest/core/29.7.0_ts-node@10.9.2: + /@jest/core/29.7.0: resolution: {integrity: sha512-n7aeXWKMnGtDA48y8TLWJPJmLmmZ642Ceo78cYWEpiD7FzDgmNDV/GCVRorPABdXLJZ/9wzzgZAlHjXjxDHGsg==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} peerDependencies: @@ -1942,7 +1939,7 @@ packages: exit: 0.1.2 graceful-fs: 4.2.11 jest-changed-files: 29.7.0 - jest-config: 29.7.0_a56qyaxzmzl4f2pjmodmqi2bye + jest-config: 29.7.0_@types+node@20.10.4 jest-haste-map: 29.7.0 jest-message-util: 29.7.0 jest-regex-util: 29.6.3 @@ -2153,6 +2150,7 @@ packages: dependencies: '@jridgewell/resolve-uri': 3.1.1 '@jridgewell/sourcemap-codec': 1.4.15 + dev: false /@kamilkisiela/fast-url-parser/1.1.4: resolution: {integrity: sha512-gbkePEBupNydxCelHCESvFSFM8XPh1Zs/OAVRW/rKpEqPAl5PbOM90Si8mv9bvnR53uPD2s/FiRxdvSejpRJew==} @@ -2257,15 +2255,19 @@ packages: /@tsconfig/node10/1.0.9: resolution: {integrity: sha512-jNsYVVxU8v5g43Erja32laIDHXeoNvFEpX33OK4d6hljo3jDhCBDhx5dhCCTMWUojscpAagGiRkBKxpdl9fxqA==} + dev: false /@tsconfig/node12/1.0.11: resolution: {integrity: sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==} + dev: false /@tsconfig/node14/1.0.3: resolution: {integrity: sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==} + dev: false /@tsconfig/node16/1.0.4: resolution: {integrity: sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==} + dev: false /@types/babel__core/7.20.5: resolution: {integrity: sha512-qoQprZvz5wQFJwMDqeseRXWv3rqMvhgpbXFfVyWhbx9X47POIA6i/+dXefEmZKoAgOaTdaIgNSMqMIU61yRyzA==} @@ -2583,6 +2585,7 @@ packages: /acorn-walk/8.3.1: resolution: {integrity: sha512-TgUZgYvqZprrl7YldZNoa9OciCAyZR+Ejm9eXzKCmjsF5IKp/wgQ7Z/ZpjpGTIUPwrHQIcYeI8qDh4PsEwxMbw==} engines: {node: '>=0.4.0'} + dev: false /acorn/8.11.2: resolution: {integrity: sha512-nc0Axzp/0FILLEVsm4fNwLCwMttvhEI263QtVPQcbpfZZ3ts0hLsZGOpE6czNlid7CJ9MlyH8reXkpsf3YUY4w==} @@ -2698,6 +2701,7 @@ packages: /arg/4.1.3: resolution: {integrity: sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==} + dev: false /argparse/1.0.10: resolution: {integrity: sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==} @@ -3220,7 +3224,7 @@ packages: typescript: 5.3.3 dev: false - /create-jest/29.7.0_a56qyaxzmzl4f2pjmodmqi2bye: + /create-jest/29.7.0_@types+node@20.10.4: resolution: {integrity: sha512-Adz2bdH0Vq3F53KEMJOoftQFutWCukm6J24wbPWRO4k1kMY7gS7ds/uoJkNuV8wDCtWWnuwGcJwpWcih+zEW1Q==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} hasBin: true @@ -3229,7 +3233,7 @@ packages: chalk: 4.1.2 exit: 0.1.2 graceful-fs: 4.2.11 - jest-config: 29.7.0_a56qyaxzmzl4f2pjmodmqi2bye + jest-config: 29.7.0_@types+node@20.10.4 jest-util: 29.7.0 prompts: 2.4.2 transitivePeerDependencies: @@ -3241,6 +3245,7 @@ packages: /create-require/1.1.1: resolution: {integrity: sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==} + dev: false /cross-fetch/3.1.8: resolution: {integrity: sha512-cvA+JwZoU0Xq+h6WkMvAUqPEYy92Obet6UdKLfW60qn99ftItKjB5T+BkyWOFWe2pUyfQ+IJHmpOTznqk1M6Kg==} @@ -3385,6 +3390,7 @@ packages: /diff/4.0.2: resolution: {integrity: sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==} engines: {node: '>=0.3.1'} + dev: false /dir-glob/3.0.1: resolution: {integrity: sha512-WkrWp9GR4KXfKGYzOLmTuGVi1UWFfws377n9cc55/tb6DuqyF6pcQ5AbiHEshaDpY9v6oaSr2XCDidGmMwdzIA==} @@ -3416,6 +3422,7 @@ packages: /dotenv/16.3.1: resolution: {integrity: sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==} engines: {node: '>=12'} + dev: false /dset/3.1.3: resolution: {integrity: sha512-20TuZZHCEZ2O71q9/+8BwKwZ0QtD9D8ObhrihJPr+vLLYlSuAU3/zL4cSlgbfeoGHTjCSJBa7NGcrF9/Bx/WJQ==} @@ -4806,7 +4813,7 @@ packages: - supports-color dev: true - /jest-cli/29.7.0_a56qyaxzmzl4f2pjmodmqi2bye: + /jest-cli/29.7.0_@types+node@20.10.4: resolution: {integrity: sha512-OVVobw2IubN/GSYsxETi+gOe7Ka59EFMR/twOU3Jb2GnKKeMGJB5SGUUrEz3SFVmJASUdZUzy83sLNNQ2gZslg==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} hasBin: true @@ -4816,14 +4823,14 @@ packages: node-notifier: optional: true dependencies: - '@jest/core': 29.7.0_ts-node@10.9.2 + '@jest/core': 29.7.0 '@jest/test-result': 29.7.0 '@jest/types': 29.6.3 chalk: 4.1.2 - create-jest: 29.7.0_a56qyaxzmzl4f2pjmodmqi2bye + create-jest: 29.7.0_@types+node@20.10.4 exit: 0.1.2 import-local: 3.1.0 - jest-config: 29.7.0_a56qyaxzmzl4f2pjmodmqi2bye + jest-config: 29.7.0_@types+node@20.10.4 jest-util: 29.7.0 jest-validate: 29.7.0 yargs: 17.7.2 @@ -4834,7 +4841,7 @@ packages: - ts-node dev: true - /jest-config/29.7.0_a56qyaxzmzl4f2pjmodmqi2bye: + /jest-config/29.7.0_@types+node@20.10.4: resolution: {integrity: sha512-uXbpfeQ7R6TZBqI3/TxCU4q4ttk3u0PJeC+E0zbfSoSjq6bJ7buBPxzQPL0ifrkY4DNu4JUdk0ImlBUYi840eQ==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} peerDependencies: @@ -4869,7 +4876,6 @@ packages: pretty-format: 29.7.0 slash: 3.0.0 strip-json-comments: 3.1.1 - ts-node: 10.9.2_yn7bv5jzkfjmbt2js7kurghsqq transitivePeerDependencies: - babel-plugin-macros - supports-color @@ -5158,7 +5164,7 @@ packages: supports-color: 8.1.1 dev: true - /jest/29.7.0_a56qyaxzmzl4f2pjmodmqi2bye: + /jest/29.7.0_@types+node@20.10.4: resolution: {integrity: sha512-NIy3oAFp9shda19hy4HK0HRTWKtPJmGdnvywu01nOqNC2vZg+Z+fvJDxpMQA88eb2I9EcafcdjYgsDthnYTvGw==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} hasBin: true @@ -5168,10 +5174,10 @@ packages: node-notifier: optional: true dependencies: - '@jest/core': 29.7.0_ts-node@10.9.2 + '@jest/core': 29.7.0 '@jest/types': 29.6.3 import-local: 3.1.0 - jest-cli: 29.7.0_a56qyaxzmzl4f2pjmodmqi2bye + jest-cli: 29.7.0_@types+node@20.10.4 transitivePeerDependencies: - '@types/node' - babel-plugin-macros @@ -6591,7 +6597,7 @@ packages: dependencies: bs-logger: 0.2.6 fast-json-stable-stringify: 2.1.0 - jest: 29.7.0_a56qyaxzmzl4f2pjmodmqi2bye + jest: 29.7.0_@types+node@20.10.4 jest-util: 29.7.0 json5: 2.2.3 lodash.memoize: 4.1.2 @@ -6630,6 +6636,7 @@ packages: typescript: 5.3.3 v8-compile-cache-lib: 3.0.1 yn: 3.1.1 + dev: false /tsconfig-paths/3.14.2: resolution: {integrity: sha512-o/9iXgCYc5L/JxCHPe3Hvh8Q/2xm5Z+p18PESBU6Ff33695QnCHBEjcytY2q19ua7Mbl/DavtBOLq+oG0RCL+g==} @@ -6817,6 +6824,7 @@ packages: /v8-compile-cache-lib/3.0.1: resolution: {integrity: sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==} + dev: false /v8-to-istanbul/9.2.0: resolution: {integrity: sha512-/EH/sDgxU2eGxajKdwLCDmQ4FWq+kpi3uCmBGpw1xJtnAxEjlD8j8PEiGWpCIMIs3ciNAgH0d3TTJiUkYzyZjA==} @@ -6995,6 +7003,7 @@ packages: /yn/3.1.1: resolution: {integrity: sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==} engines: {node: '>=6'} + dev: false /yocto-queue/0.1.0: resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==} diff --git a/src/auth/auth.ts b/src/auth/auth.ts index d4b7f16..b3a40e3 100644 --- a/src/auth/auth.ts +++ b/src/auth/auth.ts @@ -1,9 +1,15 @@ import { AddWalletConfirmationInput, Sdk } from '../../.mesh'; import { AuthType, Chain } from '../types'; import { errorHandler } from '../utils/errorHandler'; +import { + isEmailValid, + isStringValid, + isUUIDValid, + isWalletAddressvalid, +} from '../utils/validators'; export class Auth { - private sdk: Sdk; + public sdk: Sdk; constructor(sdk: Sdk) { this.sdk = sdk; @@ -33,6 +39,7 @@ export class Auth { */ async addEmail(email: string) { try { + isEmailValid(email); return (await this.sdk.addEmail_mutation({ input: { email } })).addEmail; } catch (error) { throw new Error(errorHandler(error)); @@ -48,6 +55,7 @@ export class Auth { */ async addEmailConfirmation(email: string, code: number) { try { + isEmailValid(email); return ( await this.sdk.addEmailConfirmation_mutation({ input: { code, email }, @@ -66,6 +74,7 @@ export class Auth { */ async addWallet(wallet: string, chain?: Chain) { try { + isWalletAddressvalid(wallet); return (await this.sdk.addWallet_mutation({ input: { wallet, chain } })) .addWallet; } catch (error) { @@ -99,6 +108,7 @@ export class Auth { */ async createWalletNonce(wallet: string, chain?: Chain) { try { + isWalletAddressvalid(wallet); return ( await this.sdk.createWalletNonce_mutation({ input: { wallet, chain } }) ).createWalletNonce; @@ -114,6 +124,7 @@ export class Auth { */ async deleteAccount(id: string) { try { + isUUIDValid(id); return (await this.sdk.deleteAccount_mutation({ id })).deleteAccount; } catch (error) { throw new Error(errorHandler(error)); @@ -128,6 +139,7 @@ export class Auth { */ async loginEmail(email: string, code: number) { try { + isEmailValid(email); return (await this.sdk.loginEmail_mutation({ input: { email, code } })) .loginEmail; } catch (error) { @@ -143,6 +155,7 @@ export class Auth { */ async loginWallet(wallet: string, signature: string) { try { + isWalletAddressvalid(wallet); return ( await this.sdk.loginWallet_mutation({ input: { wallet, signature }, @@ -160,6 +173,7 @@ export class Auth { */ async refreshToken(existingRefreshToken: string) { try { + isStringValid(existingRefreshToken); return ( await this.sdk.refreshToken_mutation({ input: { refresh_token: existingRefreshToken }, @@ -179,6 +193,7 @@ export class Auth { */ async unregisterAuthMethod(data: string, type: AuthType) { try { + isStringValid(data); return await this.sdk.unregisterAuthMethod_mutation({ input: { data, type }, }); diff --git a/test/auth.test.ts b/test/auth.test.ts index 3e5cb24..d4c6687 100644 --- a/test/auth.test.ts +++ b/test/auth.test.ts @@ -1,7 +1,8 @@ import { Auth } from '../src/auth/auth'; import { getMeshSDK } from '../.mesh'; import { Chain } from '../src/types'; -jest.mock('../src/auth/auth'); +import { AuthMockService } from '../__mocks__/auth.mock'; +import { authStub } from './stubs/auth.stub'; let auth: Auth; @@ -9,62 +10,44 @@ beforeAll(() => { auth = new Auth(getMeshSDK()); }); -afterEach(() => { +afterAll(() => { jest.resetAllMocks(); }); describe('auth test', () => { it('check username availability', async () => { - const mockCheckUsernameAvailability = jest.fn().mockResolvedValue(false); - (auth.checkUsernameAvailability as jest.Mock).mockImplementation( - mockCheckUsernameAvailability, - ); + const { checkUsernameAvailabilityMock } = AuthMockService(auth); - const result = await auth.checkUsernameAvailability('test'); + const result = await auth.checkUsernameAvailability(authStub().username); - expect(result).toEqual(false); - // expect(mockCheckUsernameAvailability).toHaveBeenCalled(); - // expect(mockCheckUsernameAvailability).toHaveBeenCalledWith('test'); + expect(result).toEqual(true); + expect(checkUsernameAvailabilityMock).toHaveBeenCalled(); }); it('add email', async () => { - const mockAddEmail = jest - .fn() - .mockResolvedValue({ email: '', code: '000000' }); - (auth.addEmail as jest.Mock).mockImplementation(mockAddEmail); + const { addEmailMock } = AuthMockService(auth); - const { email, code } = await auth.addEmail(''); + const { email, code } = await auth.addEmail(authStub().email); - expect(email).toBe(''); - expect(code).toBe('000000'); - expect(mockAddEmail).toHaveBeenCalled(); - expect(mockAddEmail).toHaveBeenCalledWith(''); + expect(email).toBe(authStub().email); + expect(code).toBe(authStub().code); + expect(addEmailMock).toHaveBeenCalled(); }); it('add wallet', async () => { - const mockAddWallet = jest - .fn() - .mockResolvedValue({ message: 'wallet added' }); - (auth.addWallet as jest.Mock).mockImplementation(mockAddWallet); + const { addWalletMock } = AuthMockService(auth); - const { message } = await auth.addWallet('dummy wallet'); + const { message } = await auth.addWallet(authStub().wallet); - expect(message).toBeDefined(); - expect(mockAddWallet).toHaveBeenCalled(); - expect(mockAddWallet).toHaveBeenCalledWith('dummy wallet'); + expect(message).toBe(authStub().message); + expect(addWalletMock).toHaveBeenCalled(); }); it('create wallet nounce', async () => { - const mockCreateWalletNonce = jest - .fn() - .mockResolvedValue({ message: 'nonce created' }); + const { createWalletNonceMock } = AuthMockService(auth); - (auth.createWalletNonce as jest.Mock).mockImplementation( - mockCreateWalletNonce, - ); - - const { message } = await auth.createWalletNonce('dummy wallet'); - expect(message).toBeDefined(); - expect(mockCreateWalletNonce).toHaveBeenCalled(); + const { message } = await auth.createWalletNonce(authStub().wallet); + expect(message).toBe(authStub().message); + expect(createWalletNonceMock).toHaveBeenCalled(); }); }); diff --git a/test/stubs/auth.stub.ts b/test/stubs/auth.stub.ts new file mode 100644 index 0000000..2cb53f6 --- /dev/null +++ b/test/stubs/auth.stub.ts @@ -0,0 +1,18 @@ +import { Chain } from '../../src/types'; + +export const authStub = (overrideAuth?: any) => ({ + email: 'testuser01@mygateway.xyz', + username: 'testuser01', + id: 'f17ac10b-58cc-4372-a567-0e02b2c3d479', + issuanceDate: new Date('2021-01-01T12:00:00Z'), + lastUpdated: new Date('2021-01-01T12:00:00Z'), + code: 111111, + expirationDate: new Date('2021-01-01T12:00:00Z'), + wallet: '0x8887d10B02C9b785Ca54A2e60c3Ce68DC6dDcdb1', + chain: Chain.EVM, + existingRefreshToken: + 'a86d21a712aebd1e6d5a4cb02a6a4a30f41e319a670b401c0bf0fc04c617e0f1', + signature: '65f18a8b37cbf73d8b201345ed1ccf983e0a571b69b2eef17465a26e3b187700', + message: 'message is valid', + ...overrideAuth, +}); diff --git a/test/stubs/pda.stub.ts b/test/stubs/pda.stub.ts index 3229ca9..04538db 100644 --- a/test/stubs/pda.stub.ts +++ b/test/stubs/pda.stub.ts @@ -13,6 +13,10 @@ export const pdaStub = (overridePDA?: any): PrivateDataAsset => ({ title: 'test', claim: { gatewayUse: 'test' }, expirationDate: new Date('2021-01-01T12:00:00Z'), + claimArray: [], + dataModel: {}, + issuer: {}, + owner: {}, }, expirationDate: new Date('2021-01-01T12:00:00Z'), hash: '9c10c15ea609ca3598a35b551e2ebe4827e8fb99b5da8443c0cf84ff20872a1b',