Skip to content

Commit

Permalink
Merge pull request #160 from boostcampwm2023/feat/156-shapes-list-api
Browse files Browse the repository at this point in the history
[Feat] 모양 목록 API 구현
  • Loading branch information
JoonSoo-Kim authored Nov 28, 2023
2 parents a3cfb18 + 79ebe8a commit 094e919
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
20 changes: 20 additions & 0 deletions BE/src/shapes/shapes.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,24 @@ export class ShapesController {
): Promise<string> {
return this.shapesService.getShapeFileByUuid(uuid, user);
}

@Get("/")
@UseGuards(JwtAuthGuard)
async getShapesByUser(@GetUser() user: User): Promise<object> {
const shapeLists = await this.shapesService.getShapesByUser(user);
const shapeUuidList = shapeLists[0];
const shapeFileList = shapeLists[1];

let result = {};

for (let i = 0; i < shapeUuidList.length; i++) {
const response = {
uuid: shapeUuidList[i],
svg: shapeFileList[i],
};
result[`shape${i + 1}`] = response;
}

return result;
}
}
11 changes: 11 additions & 0 deletions BE/src/shapes/shapes.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,15 @@ export class ShapesRepository {
}
return found;
}

async getShapesByUser(user: User): Promise<Shape[]> {
const shapeList: Shape[] = await Shape.find({
where: { user: { userId: user.userId } },
});
if (!shapeList) {
throw new NotFoundException("존재하지 않는 사용자입니다.");
}

return shapeList;
}
}
26 changes: 26 additions & 0 deletions BE/src/shapes/shapes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,30 @@ export class ShapesService {

return getShapeFromS3(shape.shapePath);
}

async getShapesByUser(user: User): Promise<string[][]> {
let shapeList = [];
const defaultShapeList = await this.getDefaultShapeFiles();

if (user.userId !== "commonUser") {
shapeList = defaultShapeList.concat(
await this.shapesRepository.getShapesByUser(user),
);
} else {
shapeList = defaultShapeList;
}

const shapeUuidList = shapeList.map((shape) => {
return shape.uuid;
});

// getShapeFileByUuid가 async라서 Promise.all로 await을 걸지 않으면 Promise<string[]>이 반환
const shapeFileList = await Promise.all(
shapeUuidList.map((uuid) => {
return this.getShapeFileByUuid(uuid, user);
}),
);

return [shapeUuidList, shapeFileList];
}
}
95 changes: 95 additions & 0 deletions BE/test/int/shape.read-list.int-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { Test, TestingModule } from "@nestjs/testing";
import { INestApplication } from "@nestjs/common";
import * as request from "supertest";
import { ValidationPipe } from "@nestjs/common";
import { typeORMTestConfig } from "src/configs/typeorm.test.config";
import { TypeOrmModule } from "@nestjs/typeorm";
import { RedisModule } from "@liaoliaots/nestjs-redis";
import { ShapesModule } from "src/shapes/shapes.module";

describe("[전체 모양 조회] /shapes GET 통합 테스트", () => {
let app: INestApplication;
let accessToken: string;

beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [
TypeOrmModule.forRoot(typeORMTestConfig),
RedisModule.forRoot({
readyLog: true,
config: {
host: "223.130.129.145",
port: 6379,
},
}),
ShapesModule,
],
}).compile();

app = moduleFixture.createNestApplication();
app.enableCors();
app.useGlobalPipes(new ValidationPipe());

await app.init();

const signInPost = await request(app.getHttpServer())
.post("/auth/signin")
.send({
userId: "commonUser",
password: process.env.COMMON_USER_PASS,
});

accessToken = signInPost.body.accessToken;
});

afterAll(async () => {
await app.close();
});

it("정상 요청 시 200 OK 응답", async () => {
const postResponse = await request(app.getHttpServer())
.get("/shapes")
.set("Authorization", `Bearer ${accessToken}`)
.expect(200);

expect(postResponse.body).toEqual([
"0c99bbc6-e404-464b-a310-5bf0fa0f0fa7",
"d4ffa969-a299-4e15-a700-dccd6ef89f25",
"acbb06cb-a6bf-4f3b-9f30-f59ad5c03c90",
]);
});

it("일반 유저 정상 요청 시 200 OK 응답", async () => {
const signInPost = await request(app.getHttpServer())
.post("/auth/signin")
.send({
userId: "userId",
password: "password",
});

accessToken = signInPost.body.accessToken;

const postResponse = await request(app.getHttpServer())
.get("/shapes")
.set("Authorization", `Bearer ${accessToken}`)
.expect(200);

expect(postResponse.body).toEqual([
"0c99bbc6-e404-464b-a310-5bf0fa0f0fa7",
"d4ffa969-a299-4e15-a700-dccd6ef89f25",
"acbb06cb-a6bf-4f3b-9f30-f59ad5c03c90",
]);
});

it("액세스 토큰 없이 요청 시 401 Unauthorized 응답", async () => {
const postResponse = await request(app.getHttpServer())
.get("/shapes")
.expect(401);

expect(postResponse.body).toEqual({
error: "Unauthorized",
message: "비로그인 상태의 요청입니다.",
statusCode: 401,
});
});
});

0 comments on commit 094e919

Please sign in to comment.