Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test/user/logout #85

Merged
merged 2 commits into from
Dec 12, 2022
Merged
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
1 change: 1 addition & 0 deletions server/src/Loader/Mongo.Loader.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import * as dotenv from "dotenv";
import mongoose from "mongoose";

Expand Down
1 change: 1 addition & 0 deletions server/src/Middlewares/Error.Middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import HttpException from "../Common/Exceptions/Http.Exception";
const errorMiddleware = (err: HttpException, req: Request, res: Response, next: NextFunction) => {
// render the error page
if (!err.status) {
// eslint-disable-next-line no-console
console.log(err);
}
const { status, message } = err;
Expand Down
2 changes: 1 addition & 1 deletion server/src/User/User.Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class UserService {
return res.data.access_token;
})
.catch((err) => {
throw UnauthorizedException;
throw new UnauthorizedException();
});
return githubAccessToken;
}
Expand Down
43 changes: 42 additions & 1 deletion server/src/User/User.Test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
/* eslint-disable import/no-extraneous-dependencies */
import request from "supertest";
import * as dotenv from "dotenv";
Expand All @@ -17,7 +18,7 @@ const testUser = {
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NTgsImlhdCI6MTY3MDgyODE2NH0.07bukMgpAcfDik8Umlg2UtTvVKmWFotf3AHy_legMWs",
};

const testUserRefreshToken = beforeAll(async () => {
beforeAll(async () => {
const myDataSource = new DataSource({
type: "mysql",
host: process.env.TYPEORM_HOST || "",
Expand All @@ -32,6 +33,14 @@ const testUserRefreshToken = beforeAll(async () => {
});
});

// 로그인 처리
beforeEach(async () => {
const targetUser = await User.findOneByID(testUser.id);
if (targetUser === null) throw new Error("mock invalid");
targetUser.refresh_token = testUser.infRefreshToken;
await targetUser.save();
});

// 본인 유저 정보
describe("GET api/users", () => {
test("로그인한 유저의 경우 유저 정보 반환", (done) => {
Expand Down Expand Up @@ -63,3 +72,35 @@ describe("GET api/users", () => {
});
});
});

describe("DELETE /api/users/logout", () => {
test("로그인한 유저의 경우 로그아웃 처리", (done) => {
request(app)
.delete("/api/users/logout")
.set("Cookie", [`accessToken=${testUser.infAccessToken}`, `refreshToken=${testUser.infRefreshToken}`])
.expect(204)
.expect(
"set-cookie",
"accessToken=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT,refreshToken=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT"
)
.then((res) => done())
.catch((err) => {
console.error("######Error >>", err);
done(err);
});
});

test("인증 불가 유저의 로그아웃 요청시 401에러 반환", (done) => {
request(app)
.delete("/api/users/logout")
.expect(401)
.then((res) => {
expect(res.body).toEqual({ status: 401, message: "Empty AccessToken" });
done();
})
.catch((err) => {
console.error("######Error >>", err);
done(err);
});
});
});