-
Notifications
You must be signed in to change notification settings - Fork 71
refactor DTOs to domain modules with validation tests #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,37 @@ | ||
| const { config } = require('dotenv'); | ||
| config({ path: '.env.test' }); | ||
| import { config } from "dotenv"; | ||
|
|
||
| config({ path: ".env.test" }); | ||
|
|
||
| /** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
| module.exports = { | ||
| preset: 'ts-jest', | ||
| testEnvironment: 'node', | ||
| roots: ['<rootDir>/tests'], | ||
| export default { | ||
| preset: "ts-jest", | ||
| testEnvironment: "node", | ||
| roots: ["<rootDir>/tests", "<rootDir>/src"], | ||
| transform: { | ||
| '^.+\\.tsx?$': [ | ||
| 'ts-jest', | ||
| "^.+\\.tsx?$": [ | ||
| "ts-jest", | ||
| { | ||
| tsconfig: './tsconfig.json', | ||
| tsconfig: "./tsconfig.json", | ||
| }, | ||
| ], | ||
| }, | ||
| testMatch: [ | ||
| '**/tests/**/*.test.ts', | ||
| '**/__tests__/**/*.test.ts', | ||
| '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$', | ||
| "**/tests/**/*.test.ts", | ||
| "**/__tests__/**/*.test.ts", | ||
| "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$", | ||
| "**/src/**/__tests__/**/*.test.ts", | ||
| "**/src/**/*.test.ts", | ||
| ], | ||
| moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], | ||
| moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"], | ||
| moduleNameMapper: { | ||
| '^@/(.*)$': '<rootDir>/src/$1', | ||
| "^@/(.*)$": "<rootDir>/src/$1", | ||
| "^@prisma/client$": "<rootDir>/tests/__mocks__/prisma.ts", | ||
| }, | ||
| collectCoverageFrom: [ | ||
| 'src/**/*.{js,ts}', | ||
| '!**/node_modules/**', | ||
| '!**/tests/**', | ||
| "src/**/*.{js,ts}", | ||
| "!**/node_modules/**", | ||
| "!**/tests/**", | ||
| ], | ||
| coverageDirectory: 'coverage', | ||
| coverageDirectory: "coverage", | ||
| testTimeout: 30000, | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
src/modules/auth/__tests__/dto/emailVerification.dto.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { RegisterDTO } from "../../dto/emailVerification.dto"; | ||
|
|
||
| describe("RegisterDTO - DTO validation", () => { | ||
| it("should validate correct data", async () => { | ||
| const dto = new RegisterDTO({ | ||
| name: "John Doe", | ||
| email: "john.doe@example.com", | ||
| password: "SecurePassword123", | ||
| wallet: "0x1234567890abcdef", | ||
| }); | ||
| const errors = await RegisterDTO.validate(dto); | ||
| expect(errors).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("should validate required fields", async () => { | ||
| const dto = new RegisterDTO({ | ||
| email: "john.doe@example.com", | ||
| password: "SecurePassword123", | ||
| }); | ||
| const errors = await RegisterDTO.validate(dto); | ||
| expect(errors).toHaveLength(2); | ||
| expect(errors[0].property).toBe("name"); | ||
| expect(errors[1].property).toBe("wallet"); | ||
| }); | ||
|
|
||
| it("should validate email format", async () => { | ||
| const dto = new RegisterDTO({ | ||
| name: "John Doe", | ||
| email: "invalid-email", | ||
| password: "SecurePassword123", | ||
| wallet: "0x1234567890abcdef", | ||
| }); | ||
| const errors = await RegisterDTO.validate(dto); | ||
| expect(errors).toHaveLength(1); | ||
| expect(errors[0].property).toBe("email"); | ||
| }); | ||
|
|
||
| it("should validate password length", async () => { | ||
| const dto = new RegisterDTO({ | ||
| name: "John Doe", | ||
| email: "john.doe@example.com", | ||
| password: "short", | ||
| wallet: "0x1234567890abcdef", | ||
| }); | ||
| const errors = await RegisterDTO.validate(dto); | ||
| expect(errors).toHaveLength(1); | ||
| expect(errors[0].property).toBe("password"); | ||
| }); | ||
|
|
||
| it("should fail with invalid email", async () => { | ||
| const dto = new RegisterDTO(); | ||
| dto.email = "invalid-email"; | ||
| const errors = await RegisterDTO.validate(dto); | ||
| expect(errors.length).toBeGreaterThan(0); | ||
| }); | ||
| }); |
27 changes: 27 additions & 0 deletions
27
src/modules/auth/__tests__/dto/resendVerificationDTO.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { validate } from "class-validator"; | ||
| import { ResendVerificationDTO } from "../../dto/resendVerificationDTO"; | ||
|
|
||
| describe("ResendVerificationDTO - DTO validation", () => { | ||
| it("should validate correct email", async () => { | ||
| const dto = new ResendVerificationDTO(); | ||
| dto.email = "test@example.com"; | ||
| const errors = await validate(dto); | ||
| expect(errors).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("should fail with invalid email", async () => { | ||
| const dto = new ResendVerificationDTO(); | ||
| dto.email = "invalid-email"; | ||
| const errors = await validate(dto); | ||
| expect(errors).toHaveLength(1); | ||
| expect(errors[0].property).toBe("email"); | ||
| }); | ||
|
|
||
| it("should fail with empty email", async () => { | ||
| const dto = new ResendVerificationDTO(); | ||
| dto.email = ""; | ||
| const errors = await validate(dto); | ||
| expect(errors).toHaveLength(1); | ||
| expect(errors[0].property).toBe("email"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { validate } from "class-validator"; | ||
| import { VerifyEmailDTO } from "../../dto/verifyEmailDTO"; | ||
|
|
||
| describe("VerifyEmailDTO - DTO validation", () => { | ||
| it("should validate correct token", async () => { | ||
| const dto = new VerifyEmailDTO(); | ||
| dto.token = "valid-token-123"; | ||
| const errors = await validate(dto); | ||
| expect(errors).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("should fail with empty token", async () => { | ||
| const dto = new VerifyEmailDTO(); | ||
| dto.token = ""; | ||
| const errors = await validate(dto); | ||
| expect(errors).toHaveLength(1); | ||
| expect(errors[0].property).toBe("token"); | ||
| }); | ||
|
|
||
| it("should fail with invalid type", async () => { | ||
| const dto = new VerifyEmailDTO(); | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| dto.token = 123 as any; // Invalid type | ||
| const errors = await validate(dto); | ||
| expect(errors).toHaveLength(1); | ||
| expect(errors[0].property).toBe("token"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { | ||
| IsString, | ||
| IsEmail, | ||
| IsNotEmpty, | ||
| MinLength, | ||
| validate, | ||
| } from "class-validator"; | ||
|
|
||
| export class RegisterDTO { | ||
| @IsString() | ||
| @IsNotEmpty() | ||
| name: string; | ||
|
|
||
| @IsEmail() | ||
| @IsNotEmpty() | ||
| email: string; | ||
|
|
||
| @IsString() | ||
| @IsNotEmpty() | ||
| @MinLength(8) | ||
| password: string; | ||
|
|
||
| @IsString() | ||
| @IsNotEmpty() | ||
| wallet: string; | ||
|
|
||
| constructor(partial: Partial<RegisterDTO> = {}) { | ||
| Object.assign(this, partial); | ||
| this.name = this.name || ""; | ||
| this.email = this.email || ""; | ||
| this.password = this.password || ""; | ||
| this.wallet = this.wallet || ""; | ||
| } | ||
martinvibes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| static validate(dto: RegisterDTO) { | ||
| return validate(dto); | ||
| } | ||
| } | ||
|
|
||
| export interface VerifyEmailDTO { | ||
| token: string; | ||
| } | ||
|
|
||
| export interface ResendVerificationDTO { | ||
| email: string; | ||
| } | ||
martinvibes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { IsString, IsNotEmpty } from "class-validator"; | ||
|
|
||
| export class VerifyEmailDTO { | ||
| @IsString() | ||
| @IsNotEmpty() | ||
| token: string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export const PrismaClient = class { | ||
| constructor() {} | ||
| }; | ||
|
|
||
| export const prismaClientSingleton = () => new PrismaClient(); | ||
| export const prisma = prismaClientSingleton(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { validate } from "class-validator"; | ||
| import { RegisterDTO } from "../src/modules/auth/dto/emailVerification.dto"; | ||
|
|
||
| describe("DTO Validation", () => { | ||
| describe("RegisterDTO", () => { | ||
| it("should validate correct data", async () => { | ||
| const dto: RegisterDTO = { | ||
| name: "John Doe", | ||
| email: "john.doe@example.com", | ||
| password: "SecurePassword123", | ||
| wallet: "0x1234567890abcdef", | ||
| }; | ||
| const errors = await validate(dto); | ||
| expect(errors).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("should validate required fields", async () => { | ||
| const dto = new RegisterDTO({ | ||
| email: "john.doe@example.com", | ||
| password: "SecurePassword123", | ||
| }); | ||
| const errors = await validate(dto); | ||
| expect(errors).toHaveLength(2); | ||
| expect(errors[0].property).toBe("name"); | ||
| expect(errors[1].property).toBe("wallet"); | ||
| expect(errors[0].constraints?.required).toBe("Name is required"); | ||
| expect(errors[1].constraints?.required).toBe("Wallet is required"); | ||
martinvibes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| it("should validate email format", async () => { | ||
| const dto = new RegisterDTO({ | ||
| name: "John Doe", | ||
| email: "invalid-email", | ||
| password: "SecurePassword123", | ||
| wallet: "0x1234567890abcdef", | ||
| }); | ||
| const errors = await validate(dto); | ||
| expect(errors).toHaveLength(1); | ||
| expect(errors[0].property).toBe("email"); | ||
| expect(errors[0].constraints?.email).toBeDefined(); | ||
| }); | ||
|
|
||
| it("should validate password length", async () => { | ||
| const dto = new RegisterDTO({ | ||
| name: "John Doe", | ||
| email: "john.doe@example.com", | ||
| password: "short", // Too short | ||
| wallet: "0x1234567890abcdef", | ||
| }); | ||
| const errors = await validate(dto); | ||
| expect(errors).toHaveLength(1); | ||
| expect(errors[0].property).toBe("password"); | ||
| expect(errors[0].constraints?.minlength).toBe( | ||
| "Password must be at least 8 characters long" | ||
| ); | ||
martinvibes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.