diff --git a/backend/src/auth-module/auth.controller.spec.ts b/backend/src/auth-module/auth.controller.spec.ts index 517a882..bf27d7b 100644 --- a/backend/src/auth-module/auth.controller.spec.ts +++ b/backend/src/auth-module/auth.controller.spec.ts @@ -1,20 +1,22 @@ -import { Controller, Post, Body, HttpCode, HttpStatus } from '@nestjs/common'; +import { Test, TestingModule } from '@nestjs/testing'; +import { AuthController } from './auth.controller'; import { AuthService } from './auth.service'; -import { RegisterDto } from './dto/register.dto'; -import { LoginDto } from './dto/login.dto'; -@Controller('auth') -export class AuthController { - constructor(private authService: AuthService) {} +describe('AuthController', () => { + let controller: AuthController; - @Post('register') - async register(@Body() registerDto: RegisterDto) { - return this.authService.register(registerDto); - } + const mockAuthService = {}; - @Post('login') - @HttpCode(HttpStatus.OK) - async login(@Body() loginDto: LoginDto) { - return this.authService.login(loginDto); - } -} \ No newline at end of file + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [AuthController], + providers: [{ provide: AuthService, useValue: mockAuthService }], + }).compile(); + + controller = module.get(AuthController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/backend/src/auth-module/auth.service.spec.ts b/backend/src/auth-module/auth.service.spec.ts index 800ab66..0f00c72 100644 --- a/backend/src/auth-module/auth.service.spec.ts +++ b/backend/src/auth-module/auth.service.spec.ts @@ -1,15 +1,24 @@ import { Test, TestingModule } from '@nestjs/testing'; import { AuthService } from './auth.service'; +import { UsersService } from '../users/users.service'; describe('AuthService', () => { let service: AuthService; + const mockUsersService = { + findOne: jest.fn(), + }; + beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [AuthService], + providers: [ + AuthService, + { provide: UsersService, useValue: mockUsersService }, + ], }).compile(); service = module.get(AuthService); + jest.clearAllMocks(); }); it('should be defined', () => { diff --git a/backend/src/auth-module/auth.service.ts b/backend/src/auth-module/auth.service.ts index 05e9bf8..4eb1ecf 100644 --- a/backend/src/auth-module/auth.service.ts +++ b/backend/src/auth-module/auth.service.ts @@ -1,9 +1,21 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, NotFoundException } from '@nestjs/common'; import { RegisterDto } from './dto/register.dto'; +import { UsersService } from '../users/users.service'; @Injectable() export class AuthService { + constructor(private readonly usersService: UsersService) {} + register(registerDto: RegisterDto) { throw new Error('Method not implemented.'); } + + async validateUser(userId: string) { + try { + return await this.usersService.findOne(userId); + } catch (e) { + if (e instanceof NotFoundException) return null; + throw e; + } + } } diff --git a/backend/src/auth-module/strategies/jwt.strategy.ts b/backend/src/auth-module/strategies/jwt.strategy.ts index 32e83c4..eabd659 100644 --- a/backend/src/auth-module/strategies/jwt.strategy.ts +++ b/backend/src/auth-module/strategies/jwt.strategy.ts @@ -13,7 +13,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), ignoreExpiration: false, - secretOrKey: configService.get('JWT_SECRET'), + secretOrKey: configService.getOrThrow('JWT_SECRET'), }); } diff --git a/backend/src/social-link/social-links.dto.ts b/backend/src/social-link/social-links.dto.ts index d636c13..9bdf924 100644 --- a/backend/src/social-link/social-links.dto.ts +++ b/backend/src/social-link/social-links.dto.ts @@ -1,7 +1,7 @@ import { ApiPropertyOptional } from '@nestjs/swagger'; import { IsOptional, IsString, MaxLength } from 'class-validator'; import { Transform } from 'class-transformer'; -import { IsSafeUrl, IsSocialHandle, sanitizeUrl, normalizeHandle } from '../common/social-links.validator'; +import { IsSafeUrl, IsSocialHandle, sanitizeUrl, normalizeHandle } from './social-links.validator'; /** * SocialLinksDto @@ -18,7 +18,10 @@ export class SocialLinksDto { @IsString() @MaxLength(500) @IsSafeUrl({ message: 'website_url must be a valid http or https URL' }) - @Transform(({ value }) => sanitizeUrl(value)) + @Transform(({ value }) => { + const sanitized = sanitizeUrl(value); + return sanitized !== null ? sanitized : value ?? null; + }) websiteUrl?: string | null; @ApiPropertyOptional({ @@ -51,6 +54,9 @@ export class SocialLinksDto { @IsString() @MaxLength(500) @IsSafeUrl({ message: 'other_link must be a valid http or https URL' }) - @Transform(({ value }) => sanitizeUrl(value)) + @Transform(({ value }) => { + const sanitized = sanitizeUrl(value); + return sanitized !== null ? sanitized : value ?? null; + }) otherLink?: string | null; } diff --git a/backend/src/social-link/social-links.service.spec.ts b/backend/src/social-link/social-links.service.spec.ts index 0a5dac1..f82c154 100644 --- a/backend/src/social-link/social-links.service.spec.ts +++ b/backend/src/social-link/social-links.service.spec.ts @@ -1,5 +1,5 @@ -import { SocialLinksService } from '../social-links.service'; -import { SocialLinksResponseDto } from '../dto/user-profile.dto'; +import { SocialLinksService } from './social-links.service'; +import { SocialLinksResponseDto } from './user-profile.dto'; describe('SocialLinksService', () => { let service: SocialLinksService; diff --git a/backend/src/social-link/social-links.service.ts b/backend/src/social-link/social-links.service.ts index 53b76f7..8483b65 100644 --- a/backend/src/social-link/social-links.service.ts +++ b/backend/src/social-link/social-links.service.ts @@ -1,8 +1,8 @@ import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; -import { SocialLinksDto } from '../dto/social-links.dto'; -import { SocialLinksResponseDto } from '../dto/user-profile.dto'; +import { SocialLinksDto } from './social-links.dto'; +import { SocialLinksResponseDto } from './user-profile.dto'; /** * SocialLinksService diff --git a/backend/src/social-link/social-links.validator.spec.ts b/backend/src/social-link/social-links.validator.spec.ts index 79e6ede..4968518 100644 --- a/backend/src/social-link/social-links.validator.spec.ts +++ b/backend/src/social-link/social-links.validator.spec.ts @@ -5,8 +5,8 @@ import { IsSocialHandleConstraint, sanitizeUrl, normalizeHandle, -} from '../common/social-links.validator'; -import { SocialLinksDto } from '../dto/social-links.dto'; +} from './social-links.validator'; +import { SocialLinksDto } from './social-links.dto'; // ─── IsSafeUrlConstraint ─────────────────────────────────────────────────────