Skip to content

Commit

Permalink
feat: update get method of file controller now include name of file i…
Browse files Browse the repository at this point in the history
…n path
  • Loading branch information
ToniDarodda committed Aug 4, 2024
1 parent b9a1371 commit 3d00df8
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 34 deletions.
7 changes: 4 additions & 3 deletions dist/guards/auth.guard.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { CanActivate, ExecutionContext } from '@nestjs/common';
import { AccountService } from 'modules/account/service/account.service';
import { Account } from 'entities/account';
import { Repository } from 'typeorm';
export declare class JwtAuthGuard implements CanActivate {
private readonly accountService;
constructor(accountService: AccountService);
private readonly accountRepository;
constructor(accountRepository: Repository<Account>);
canActivate(context: ExecutionContext): Promise<boolean>;
}
16 changes: 11 additions & 5 deletions dist/guards/auth.guard.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/guards/auth.guard.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/modules/file/controller/file.controller.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export declare class FileController {
private readonly fileService;
constructor(fileService: FileService);
uploadFile({ sub }: DecodedUserToken, file: Express.Multer.File): Promise<string>;
getFile({ sub }: DecodedUserToken): Promise<string>;
getFile(fileName: string): Promise<string>;
}
14 changes: 9 additions & 5 deletions dist/modules/file/controller/file.controller.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/modules/file/controller/file.controller.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions dist/modules/file/file.module.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/modules/file/file.module.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/tsconfig.build.tsbuildinfo

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions src/guards/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import {
ExecutionContext,
UnauthorizedException,
} from '@nestjs/common';
import { AccountService } from 'modules/account/service/account.service';
import { InjectRepository } from '@nestjs/typeorm';
import { Account } from 'entities/account';
import { Repository } from 'typeorm';
import { decodeUserToken } from 'utils/parseCookie';

@Injectable()
export class JwtAuthGuard implements CanActivate {
constructor(private readonly accountService: AccountService) {}
constructor(
@InjectRepository(Account)
private readonly accountRepository: Repository<Account>,
) {}

async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest();
Expand All @@ -23,7 +28,7 @@ export class JwtAuthGuard implements CanActivate {

const { sub } = decodeUserToken(cookies[COOKIE_TOKEN_NAME]);

const user = await this.accountService.get(sub);
const user = await this.accountRepository.findOneBy({ id: sub });

if (!user) {
throw new UnauthorizedException();
Expand Down
43 changes: 35 additions & 8 deletions src/modules/account/controller/account.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AccountController } from './account.controller';
import { AccountService } from '../service/account.service';
import { CacheInterceptor, CACHE_MANAGER } from '@nestjs/cache-manager';
import { JwtAuthGuard } from 'guards/auth.guard';
import { RolesGuard } from 'guards/roles.guard';
import { Reflector } from '@nestjs/core';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Account } from 'entities/account';
import { CACHE_MANAGER, CacheInterceptor } from '@nestjs/cache-manager';

const mockAccountService = () => ({
// mock implementation of the AccountService methods
create: jest.fn(),
get: jest.fn(),
getByEmail: jest.fn(),
patch: jest.fn(),
delete: jest.fn(),
});

const mockJwtAuthGuard = {
canActivate: jest.fn(() => true),
};

const mockRolesGuard = {
canActivate: jest.fn(() => true),
};

const mockAccountRepository = {
findOne: jest.fn(),
};

const mockCacheManager = {
// mock implementation of the cache manager methods
get: jest.fn(),
set: jest.fn(),
};

describe('AccountController', () => {
let controller: AccountController;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let service: AccountService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
Expand All @@ -25,17 +44,25 @@ describe('AccountController', () => {
provide: AccountService,
useFactory: mockAccountService,
},
{
provide: getRepositoryToken(Account),
useValue: mockAccountRepository,
},
{
provide: CACHE_MANAGER,
useValue: mockCacheManager,
},
CacheInterceptor,
Reflector,
CacheInterceptor,
],
}).compile();
})
.overrideGuard(JwtAuthGuard)
.useValue(mockJwtAuthGuard)
.overrideGuard(RolesGuard)
.useValue(mockRolesGuard)
.compile();

controller = module.get<AccountController>(AccountController);
service = module.get<AccountService>(AccountService);
});

it('should be defined', () => {
Expand Down
23 changes: 21 additions & 2 deletions src/modules/file/controller/file.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { Test, TestingModule } from '@nestjs/testing';
import { FileController } from './file.controller';
import { FileService } from '../service/file.service';
import { JwtAuthGuard } from 'guards/auth.guard';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Account } from 'entities/account';

const mockFileService = {
// mock implementation of the FileService methods
upload: jest.fn(),
get: jest.fn(),
};

const mockJwtAuthGuard = {
canActivate: jest.fn(() => true),
};

const mockAccountRepository = {
findOne: jest.fn(),
};

describe('FileController', () => {
Expand All @@ -17,8 +29,15 @@ describe('FileController', () => {
provide: FileService,
useValue: mockFileService,
},
{
provide: getRepositoryToken(Account),
useValue: mockAccountRepository,
},
],
}).compile();
})
.overrideGuard(JwtAuthGuard)
.useValue(mockJwtAuthGuard)
.compile();

controller = module.get<FileController>(FileController);
});
Expand Down
12 changes: 9 additions & 3 deletions src/modules/file/controller/file.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import {
Get,
HttpCode,
HttpStatus,
Param,
Post,
UploadedFile,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
Expand All @@ -15,6 +17,8 @@ import { Role } from 'types/role';
import { DecodedUserToken } from 'utils/parseCookie';
import { FileService } from '../service/file.service';
import { CacheKey, CacheTTL } from '@nestjs/cache-manager';
import { JwtAuthGuard } from 'guards/auth.guard';
import { RolesGuard } from 'guards/roles.guard';

@ApiTags('File')
@Controller('file')
Expand All @@ -41,14 +45,15 @@ export class FileController {
description: 'File uploaded successfully',
})
@Roles(Role.USER)
@UseGuards(JwtAuthGuard, RolesGuard)
uploadFile(
@AuthToken() { sub }: DecodedUserToken,
@UploadedFile() file: Express.Multer.File,
) {
return this.fileService.upload(sub, file);
}

@Get()
@Get(':fileName')
@HttpCode(HttpStatus.OK)
@ApiResponse({
status: HttpStatus.OK,
Expand All @@ -59,9 +64,10 @@ export class FileController {
description: 'User file not found',
})
@Roles(Role.USER)
@UseGuards(JwtAuthGuard, RolesGuard)
@CacheKey('get_file_key')
@CacheTTL(5)
getFile(@AuthToken() { sub }: DecodedUserToken) {
return this.fileService.get(sub);
getFile(@Param('fileName') fileName: string) {
return this.fileService.get(fileName);
}
}
3 changes: 3 additions & 0 deletions src/modules/file/file.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Module } from '@nestjs/common';
import { FileService } from './service/file.service';
import { FileController } from './controller/file.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Account } from 'entities/account';

@Module({
imports: [TypeOrmModule.forFeature([Account])],
providers: [FileService],
controllers: [FileController],
})
Expand Down
10 changes: 10 additions & 0 deletions src/modules/file/service/file.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Test, TestingModule } from '@nestjs/testing';
import { FileService } from './file.service';
import { ConfigService } from '@nestjs/config';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Account } from 'entities/account';

const mockAccountRepository = () => ({
// mock implementation of the repository methods
});

const mockConfigService = {
getOrThrow: jest.fn((key: string) => {
Expand All @@ -25,6 +31,10 @@ describe('FileService', () => {
provide: ConfigService,
useValue: mockConfigService,
},
{
provide: getRepositoryToken(Account),
useFactory: mockAccountRepository,
},
],
}).compile();

Expand Down

0 comments on commit 3d00df8

Please sign in to comment.