-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from boostcampwm2023/feat/131-signout-api-res…
…ponse-handling [Feat] 로그아웃 API에 대한 서버 응답 수정 및 통합 테스트 작성
- Loading branch information
Showing
4 changed files
with
124 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: "[email protected]", | ||
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, | ||
}); | ||
}); | ||
}); |