From a4901a8affe46d270fe37b66eab1b933fc78d722 Mon Sep 17 00:00:00 2001 From: jeongmin Date: Thu, 23 Nov 2023 19:14:57 +0900 Subject: [PATCH] =?UTF-8?q?Test:=20=EB=A1=9C=EA=B7=B8=EC=95=84=EC=9B=83=20?= =?UTF-8?q?/auth/signout=20POST=20=ED=86=B5=ED=95=A9=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 올바른 토큰으로 요청 시 204 No Content 응답 - 유효하지 않은 액세스 토큰이 포함된 상태로 로그아웃 요청 시 401 Unauthorized 응답 - 토큰이 존재하지 않은 상태로 로그아웃 요청 시 401 Unauthorized 응답 --- BE/test/int/auth.signin.int-spec.ts | 5 +- BE/test/int/auth.signout.int-spec.ts | 108 +++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 BE/test/int/auth.signout.int-spec.ts diff --git a/BE/test/int/auth.signin.int-spec.ts b/BE/test/int/auth.signin.int-spec.ts index 04a2175..7ddee04 100644 --- a/BE/test/int/auth.signin.int-spec.ts +++ b/BE/test/int/auth.signin.int-spec.ts @@ -1,14 +1,12 @@ import { Test, TestingModule } from "@nestjs/testing"; import { INestApplication } from "@nestjs/common"; import * as request from "supertest"; -import { AppModule } from "../../src/app.module"; import { ValidationPipe } from "@nestjs/common"; import { AuthModule } from "src/auth/auth.module"; import { TypeOrmModule } from "@nestjs/typeorm"; import { typeORMTestConfig } from "src/configs/typeorm.test.config"; -import { typeORMConfig } from "src/configs/typeorm.config"; -describe("/auth/signin (e2e)", () => { +describe("[로그인] /auth/signin POST 통합 테스트", () => { let app: INestApplication; beforeAll(async () => { @@ -22,6 +20,7 @@ describe("/auth/signin (e2e)", () => { await app.init(); }); + afterAll(async () => { await app.close(); }); diff --git a/BE/test/int/auth.signout.int-spec.ts b/BE/test/int/auth.signout.int-spec.ts new file mode 100644 index 0000000..2836e07 --- /dev/null +++ b/BE/test/int/auth.signout.int-spec.ts @@ -0,0 +1,108 @@ +import { Test, TestingModule } from "@nestjs/testing"; +import { INestApplication } from "@nestjs/common"; +import * as request from "supertest"; +import { ValidationPipe } from "@nestjs/common"; +import { AuthModule } from "src/auth/auth.module"; +import { TypeOrmModule } from "@nestjs/typeorm"; +import { typeORMTestConfig } from "src/configs/typeorm.test.config"; +import { User } from "src/auth/users.entity"; +import { UsersRepository } from "src/auth/users.repository"; + +describe("[로그아웃] /auth/signout POST 통합 테스트", () => { + let app: INestApplication; + + beforeAll(async () => { + const moduleFixture: TestingModule = await Test.createTestingModule({ + imports: [TypeOrmModule.forRoot(typeORMTestConfig), AuthModule], + providers: [UsersRepository], + }).compile(); + + app = moduleFixture.createNestApplication(); + app.enableCors(); + app.useGlobalPipes(new ValidationPipe()); + + await app.init(); + + await request(app.getHttpServer()) + .post("/auth/signup") + .send({ + userId: "SignoutTestUserId", + password: "SignoutTestPassword", + email: "signouttestemail@naver.com", + nickname: "SignoutTestUser", + }) + .expect(201); + }); + + afterAll(async () => { + const usersRepository = new UsersRepository(); + + const testUser = await usersRepository.getUserByUserId("SignoutTestUserId"); + if (testUser) { + await User.remove(testUser); + } + + await app.close(); + }); + + it("올바른 토큰으로 요청 시 204 No Content 응답", async () => { + const signInResponse = await request(app.getHttpServer()) + .post("/auth/signin") + .send({ + userId: "SignoutTestUserId", + password: "SignoutTestPassword", + }) + .expect(201); + + expect(signInResponse.body).toHaveProperty("accessToken"); + + const { accessToken } = signInResponse.body; + + await request(app.getHttpServer()) + .post("/auth/signout") + .set("Authorization", `Bearer ${accessToken}`) + .expect(204); + }); + + it("유효하지 않은 액세스 토큰이 포함된 상태로 로그아웃 요청 시 401 Unauthorized 응답", async () => { + await request(app.getHttpServer()) + .post("/auth/signin") + .send({ + userId: "SignoutTestUserId", + password: "SignoutTestPassword", + }) + .expect(201); + + const invalidAccessToken = "1234"; + const postResponse = await request(app.getHttpServer()) + .post("/auth/signout") + .set("Authorization", `Bearer ${invalidAccessToken}`) + .expect(401); + + expect(postResponse.body).toEqual({ + error: "Unauthorized", + message: "유효하지 않은 토큰입니다.", + statusCode: 401, + }); + }); + + it("토큰이 존재하지 않은 상태로 로그아웃 요청 시 401 Unauthorized 응답", async () => { + await request(app.getHttpServer()) + .post("/auth/signin") + .send({ + userId: "SignoutTestUserId", + password: "SignoutTestPassword", + }) + .expect(201); + + const postResponse = await request(app.getHttpServer()) + .post("/auth/signout") + .expect(401); + + expect(postResponse.body).toEqual({ + error: "Unauthorized", + message: "비로그인 상태의 요청입니다.", + statusCode: 401, + }); + }); +});