Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { AiModule } from './ai/openai.module';
import { AppController } from './app.controller';
import { AopModule } from './common/aop/aop.module';
import { InfraModule } from './infra/infra.module';
import { TestModule } from './test/test.module';

@Module({
imports: [
Expand Down Expand Up @@ -108,6 +109,7 @@ import { InfraModule } from './infra/infra.module';
}),
AopModule,
InfraModule,
TestModule,
],
controllers: [AppController],
providers: [],
Expand Down
2 changes: 1 addition & 1 deletion src/categories/category.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ import { CategoryV2Controller } from './v2/category.v2.controller';
],
controllers: [CategoryController, CategoryV2Controller],
providers: [CategoryService, ContentRepository, CategoryRepository],
exports: [CategoryRepository],
exports: [CategoryRepository, CategoryService],
})
export class CategoryModule {}
43 changes: 43 additions & 0 deletions src/test/is-admin.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class IsAdminGuard implements CanActivate {
constructor(private readonly configService: ConfigService) {}

canActivate(context: ExecutionContext) {
const request = context.switchToHttp().getRequest();
const authorization = request.headers.authorization;
if (!authorization) {
return false;
}

if (this.validateAuthorizationHeader(authorization)) {
return false;
}

const [username, password] = this.decodeAuthorization(authorization);

if (
username === this.configService.get('ADMIN_USERNAME') &&
password === this.configService.get('ADMIN_PASSWORD')
) {
return true;
}

return false;
}

private validateAuthorizationHeader(authorization: string) {
const [bearer, token] = authorization.split(' ');
if (bearer !== 'Basic' || !token) {
return false;
}
}

private decodeAuthorization(authorization: string) {
const [_, token] = authorization.split(' ');
const basicAuth = Buffer.from(token, 'base64').toString('utf-8');
return basicAuth.split(':');
}
}
15 changes: 14 additions & 1 deletion src/test/test.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Controller, Post, Body } from '@nestjs/common';
import { Controller, Post, Body, UseGuards } from '@nestjs/common';
import {
ApiTags,
ApiOperation,
ApiOkResponse,
ApiBadRequestResponse,
ApiExcludeEndpoint,
} from '@nestjs/swagger';
import { ErrorOutput } from '../common/dtos/output.dto';
import { ContentsService } from '../contents/contents.service';
Expand All @@ -12,13 +13,17 @@ import {
SummarizeContentBodyDto,
} from '../contents/dtos/content.dto';
import { CategoryService } from '../categories/category.service';
import { TestService } from './test.service';
import { IsAdminGuard } from './is-admin.guard';

@Controller('test')
@ApiTags('Test')
@UseGuards(IsAdminGuard)
export class TestController {
constructor(
private readonly contentsService: ContentsService,
private readonly categoryService: CategoryService,
private readonly testService: TestService,
) {}

@ApiOperation({
Expand All @@ -39,4 +44,12 @@ export class TestController {
): Promise<SummarizeContentOutput> {
return this.contentsService.testSummarizeContent(content);
}

@ApiExcludeEndpoint()
@Post('user')
async createTester(
@Body() { email, password }: { email: string; password: string },
) {
return this.testService.createTester({ email, password });
}
}
7 changes: 7 additions & 0 deletions src/test/test.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { Module } from '@nestjs/common';
import { CategoryModule } from '../categories/category.module';
import { ContentsModule } from '../contents/contents.module';
import { TestService } from './test.service';
import { UserRepository } from '../users/repository/user.repository';
import { CategoryRepository } from '../categories/category.repository';
import { IsAdminGuard } from './is-admin.guard';
import { TestController } from './test.controller';

@Module({
imports: [CategoryModule, ContentsModule],
controllers: [TestController],
providers: [TestService, UserRepository, CategoryRepository, IsAdminGuard],
})
export class TestModule {}
28 changes: 28 additions & 0 deletions src/test/test.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Injectable } from '@nestjs/common';
import { UserRepository } from '../users/repository/user.repository';
import { CategoryRepository } from '../categories/category.repository';
import { User } from '../users/entities/user.entity';

@Injectable()
export class TestService {
constructor(
private readonly userRepository: UserRepository,
private readonly categoryRepository: CategoryRepository,
) {}

async createTester({
email,
password,
}: {
email: string;
password: string;
}): Promise<void> {
const user = new User();
user.email = email;
user.password = password;
user.name = email.split('@')[0];

await this.userRepository.createOne(user);
await this.categoryRepository.createDefaultCategories(user);
}
}