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

Tab/title #84

Merged
merged 10 commits into from
Dec 12, 2022
2 changes: 1 addition & 1 deletion client/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>React App</title>
<title>BoostForm</title>
</head>
<body>
<div id="root"></div>
Expand Down
6 changes: 6 additions & 0 deletions server/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
testRegex: ".Test.ts$",
preset: "ts-jest",
testEnvironment: "node",
};
14,174 changes: 9,898 additions & 4,276 deletions server/package-lock.json

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"version": "0.0.0",
"private": true,
"scripts": {
"start": "tsc -p . && node ./dist/app.js",
"dev": "nodemon -e ts --watch \"./src\" --exec \"npm start\""
"start": "tsc -p . && node ./dist/server.js",
"dev": "nodemon -e ts --watch \"./src\" --exec \"npm start\"",
"test": "jest"
},
"dependencies": {
"axios": "^1.1.2",
Expand All @@ -29,19 +30,24 @@
"@types/cors": "^2.8.12",
"@types/express": "^4.17.14",
"@types/http-errors": "^1.8.2",
"@types/jest": "^29.2.4",
"@types/jsonwebtoken": "^8.5.9",
"@types/morgan": "^1.9.3",
"@types/mysql": "^2.15.21",
"@types/node": "^18.11.9",
"@types/node-schedule": "^2.1.0",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^5.42.1",
"@typescript-eslint/parser": "^5.42.1",
"eslint": "^8.27.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.3.1",
"prettier": "^2.7.1",
"supertest": "^6.3.3",
"ts-jest": "^29.0.3",
"ts-node": "^10.9.1",
"typescript": "^4.8.4"
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/Board/Board.Controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response, NextFunction } from "express";
import { redisCli } from "../app";
import redisCli from "../Loader/Redis.Loader";

import BoardService from "./Board.Service";
import { searchKeyList, sortKeyList } from "./Board.Constants";
Expand Down
2 changes: 1 addition & 1 deletion server/src/Form/Form.Caching.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextFunction, Request, Response } from "express";
import { redisCli } from "../app";
import redisCli from "../Loader/Redis.Loader";

const formCaching = async (req: Request, res: Response, next: NextFunction) => {
const cachingResult = await redisCli.get(`form:${req.params.formId}`);
Expand Down
2 changes: 1 addition & 1 deletion server/src/Form/Form.Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import InteranServerException from "../Common/Exceptions/InternalServer.Exceptio
import BadRequestException from "../Common/Exceptions/BadRequest.Exception";
import FormService from "./Form.Service";
import { FormInDB } from "./Form.Interface";
import { redisCli } from "../app";
import redisCli from "../Loader/Redis.Loader";

class FormController {
static async createNewForm(req: Request, res: Response, next: NextFunction) {
Expand Down
17 changes: 17 additions & 0 deletions server/src/Loader/Mongo.Loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as dotenv from "dotenv";
import mongoose from "mongoose";

dotenv.config();

export default function connectMongoDB() {
mongoose.connect(
`mongodb+srv://${process.env.MONGODB_ID}:${process.env.MONGODB_PASSWORD}@cluster0.a7vmgdw.mongodb.net/database0?`,
(err) => {
if (err) {
console.log(err);
} else {
console.log("mongoDB is connected...");
}
}
);
}
16 changes: 16 additions & 0 deletions server/src/Loader/MySQL.Loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as dotenv from "dotenv";
import "reflect-metadata";
import { DataSource } from "typeorm";

dotenv.config();
const myDataSource = new DataSource({
type: "mysql",
host: process.env.TYPEORM_HOST || "",
port: Number(process.env.TYPEORM_PORT),
username: process.env.TYPEORM_USERNAME || "",
password: process.env.TYPEORM_PASSWORD || "",
database: process.env.TYPEORM_DATABASE || "",
entities: [`${__dirname}/../**/*.Model{.ts,.js}`],
});

export default myDataSource;
18 changes: 18 additions & 0 deletions server/src/Loader/Redis.Loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as redis from "redis";

const redisClient = redis.createClient({
url: `redis://@${process.env.REDIS_HOST}:${process.env.REDIS_PORT}/0`,
legacyMode: true,
});

redisClient.on("connect", () => {
console.info("Redis connected!");
});
redisClient.on("error", (err) => {
console.error("Redis Client Error", err);
});

redisClient.connect();
const redisCli = redisClient.v4;

export default redisCli;
3 changes: 3 additions & 0 deletions server/src/Middlewares/Error.Middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import HttpException from "../Common/Exceptions/Http.Exception";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const errorMiddleware = (err: HttpException, req: Request, res: Response, next: NextFunction) => {
// render the error page
if (!err.status) {
console.log(err);
}
const { status, message } = err;
res.status(err.status).send({
status,
Expand Down
2 changes: 1 addition & 1 deletion server/src/Response/Response.Service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import FormResponse from "./Response.Model";
import { redisCli } from "../app";
import redisCli from "../Loader/Redis.Loader";
import { AnswerInterface, AnswerFromRequest } from "./Response.Interface";

class ResponseService {
Expand Down
84 changes: 84 additions & 0 deletions server/src/Result/Result.Test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import request from "supertest";
import * as dotenv from "dotenv";
import { BeforeRecover } from "typeorm";
import app from "../app";
import connectMongoDB from "../Loader/Mongo.Loader";

dotenv.config();
beforeAll(async () => {
await connectMongoDB();
});
describe("GET api/results/:formId", () => {
const testFormId = "6396ab6baa73534ba0a86b12";
const testResult = {
formTitle: "TEST",
totalResponseCount: 9,
acceptResponse: true,
questionResultDict: {
"1": {
type: "checkbox",
questionTitle: "q1",
responseCount: 9,
answerTotal: {
a1: 2,
a2: 1,
a3: 1,
a4: 2,
a5: 3,
},
},
"2": {
type: "multiple",
questionTitle: "q2",
responseCount: 9,
answerTotal: {
a1: 6,
a2: 9,
a3: 9,
a4: 9,
a5: 9,
},
},
"3": {
type: "paragraph",
questionTitle: "q3",
responseCount: 9,
answerTotal: {
"2": 1,
"3": 2,
"4": 1,
"5": 2,
"6": 2,
a1: 1,
},
},
},
};
test("유효한 설문에 대한 결과 요청", (done) => {
request(app)
.get(`/api/results/${testFormId}`)
.expect(200)
.then((res) => {
expect(res.body).toEqual(testResult);
done();
})
.catch((err) => {
console.error("######Error >>", err);
done(err);
});
});

test("유효하지 않은 설문에 대한 결과 요청", (done) => {
request(app)
.get(`/api/results/invalidForm`)
.expect(400)
.then((res) => {
expect(res.body).toEqual({ status: 400, message: "Invalid formId" });
done();
})
.catch((err) => {
console.error("######Error >>", err);
done(err);
});
});
});
2 changes: 1 addition & 1 deletion server/src/User/User.Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class UserController {
userService
.logout(userID)
.then(() => {
res.status(204).clearCookie("accessToken").clearCookie("refreshToken");
res.status(204).clearCookie("accessToken").clearCookie("refreshToken").end();
})
.catch((err) => {
next(err);
Expand Down
65 changes: 65 additions & 0 deletions server/src/User/User.Test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* eslint-disable import/no-extraneous-dependencies */
import request from "supertest";
import * as dotenv from "dotenv";
import "reflect-metadata";
import { DataSource } from "typeorm";
import User from "./User.Model";
import app from "../app";

dotenv.config();

const testUser = {
name: "testUser",
id: 58,
infAccessToken:
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NTgsImlhdCI6MTY3MDgyODE2MH0.vQrK4E6RbQfPxpTt0cjBmhYVtlihWJelKSdssm2W45E",
infRefreshToken:
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NTgsImlhdCI6MTY3MDgyODE2NH0.07bukMgpAcfDik8Umlg2UtTvVKmWFotf3AHy_legMWs",
};

const testUserRefreshToken = beforeAll(async () => {
const myDataSource = new DataSource({
type: "mysql",
host: process.env.TYPEORM_HOST || "",
port: Number(process.env.TYPEORM_PORT),
username: process.env.TYPEORM_USERNAME || "",
password: process.env.TYPEORM_PASSWORD || "",
database: process.env.TYPEORM_DATABASE || "",
entities: [User],
});
await myDataSource.initialize().then(() => {
console.log("Data Source has been initialized!");
});
});

// 본인 유저 정보
describe("GET api/users", () => {
test("로그인한 유저의 경우 유저 정보 반환", (done) => {
request(app)
.get("/api/users")
.set("Cookie", [`accessToken=${testUser.infAccessToken}`, `refreshToken=${testUser.infRefreshToken}`])
.expect(200)
.then((res) => {
expect(res.body).toEqual({ userID: testUser.id, userName: testUser.name });
done();
})
.catch((err) => {
console.error("######Error >>", err);
done(err);
});
});

test("로그인 안한 유저의 경우 401에러 반환", (done) => {
request(app)
.get("/api/users")
.expect(401)
.then((res) => {
expect(res.body).toEqual({ status: 401, message: "Empty AccessToken" });
done();
})
.catch((err) => {
console.error("######Error >>", err);
done(err);
});
});
});
53 changes: 0 additions & 53 deletions server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import path from "path";
import cookieParser from "cookie-parser";
import logger from "morgan";
import * as dotenv from "dotenv";
import mongoose from "mongoose";
import cors from "cors";
import "reflect-metadata";
import { DataSource } from "typeorm";
import * as redis from "redis";

import indexRouter from "./routes/index";
import errorMiddleware from "./Middlewares/Error.Middleware";
Expand All @@ -24,54 +21,6 @@ app.use(
credentials: true,
})
);

const myDataSource = new DataSource({
type: "mysql",
host: process.env.TYPEORM_HOST || "",
port: Number(process.env.TYPEORM_PORT),
username: process.env.TYPEORM_USERNAME || "",
password: process.env.TYPEORM_PASSWORD || "",
database: process.env.TYPEORM_DATABASE || "",
entities: [`${__dirname}/**/*.Model{.ts,.js}`],
});

myDataSource.initialize().then(() => {
console.log("Data Source has been initialized!");
});

// MongoDB 연결
function connectDB() {
mongoose.connect(
`mongodb+srv://${process.env.MONGODB_ID}:${process.env.MONGODB_PASSWORD}@cluster0.a7vmgdw.mongodb.net/database0?`,
(err) => {
if (err) {
console.log(err);
} else {
console.log("mongoDB is connected...");
}
}
);
}

connectDB();

// redis 연결
const redisClient = redis.createClient({
url: `redis://@${process.env.REDIS_HOST}:${process.env.REDIS_PORT}/0`,
legacyMode: true,
});

redisClient.on("connect", () => {
console.info("Redis connected!");
});
redisClient.on("error", (err) => {
console.error("Redis Client Error", err);
});

redisClient.connect();

export const redisCli = redisClient.v4;

// view engine setup
app.use(logger("dev"));
app.use(express.json());
Expand All @@ -88,6 +37,4 @@ app.use((req: Request, res: Response, next: NextFunction) => {
// error handler
app.use(errorMiddleware);

app.listen(process.env.PORT);

export default app;
11 changes: 11 additions & 0 deletions server/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import app from "./app";
import myDataSource from "./Loader/MySQL.Loader";
import connectMongoDB from "./Loader/Mongo.Loader";

myDataSource.initialize().then(() => {
console.log("Data Source has been initialized!");
});

connectMongoDB();

app.listen(process.env.PORT);