mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 19:59:20 +00:00
chore: adding user creation tests
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
test("sum two number", () => {
|
||||
expect(1 + 2).toBe(3);
|
||||
});
|
||||
41
backend/src/__tests__/unit/CreateUserService.spec.ts
Normal file
41
backend/src/__tests__/unit/CreateUserService.spec.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { disconnect, truncate } from "../utils/database";
|
||||
// import User from "../../models/User";
|
||||
// import app from "../../app";
|
||||
import CreateUserService from "../../services/UserServices/CreateUserService";
|
||||
import AppError from "../../errors/AppError";
|
||||
|
||||
describe("User", () => {
|
||||
beforeEach(async () => {
|
||||
await truncate();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await disconnect();
|
||||
});
|
||||
|
||||
it("should be able to create a new user", async () => {
|
||||
const user = await CreateUserService({
|
||||
name: "dasdas",
|
||||
email: "tesssst@test.com",
|
||||
password: "passwo22221131rd"
|
||||
});
|
||||
|
||||
expect(user).toHaveProperty("id");
|
||||
});
|
||||
|
||||
it("should not be able to create a user with duplicated email", async () => {
|
||||
await CreateUserService({
|
||||
name: "dasdas",
|
||||
email: "tesssst@test.com",
|
||||
password: "passwo22221131rd"
|
||||
});
|
||||
|
||||
expect(
|
||||
CreateUserService({
|
||||
name: "dasdas",
|
||||
email: "tesssst@test.com",
|
||||
password: "passwo22221131rd"
|
||||
})
|
||||
).rejects.toBeInstanceOf(AppError);
|
||||
});
|
||||
});
|
||||
11
backend/src/__tests__/utils/database.ts
Normal file
11
backend/src/__tests__/utils/database.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import database from "../../database";
|
||||
|
||||
const truncate = async (): Promise<void> => {
|
||||
await database.sync({ force: true });
|
||||
};
|
||||
|
||||
const disconnect = async (): Promise<void> => {
|
||||
return database.connectionManager.close();
|
||||
};
|
||||
|
||||
export { truncate, disconnect };
|
||||
44
backend/src/app.ts
Normal file
44
backend/src/app.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import "./bootstrap";
|
||||
import "reflect-metadata";
|
||||
import "express-async-errors";
|
||||
import express, { Request, Response, NextFunction } from "express";
|
||||
import cors from "cors";
|
||||
import cookieParser from "cookie-parser";
|
||||
import multer from "multer";
|
||||
import * as Sentry from "@sentry/node";
|
||||
|
||||
import "./database";
|
||||
import uploadConfig from "./config/upload";
|
||||
import AppError from "./errors/AppError";
|
||||
import routes from "./routes";
|
||||
|
||||
Sentry.init({ dsn: process.env.SENTRY_DSN });
|
||||
|
||||
const upload = multer(uploadConfig);
|
||||
const app = express();
|
||||
|
||||
app.use(
|
||||
cors({
|
||||
credentials: true,
|
||||
origin: process.env.FRONTEND_URL
|
||||
})
|
||||
);
|
||||
app.use(cookieParser());
|
||||
app.use(express.json());
|
||||
app.use(Sentry.Handlers.requestHandler());
|
||||
app.use(upload.single("media"));
|
||||
app.use("/public", express.static(uploadConfig.directory));
|
||||
app.use(routes);
|
||||
|
||||
app.use(Sentry.Handlers.errorHandler());
|
||||
|
||||
app.use(async (err: Error, req: Request, res: Response, _: NextFunction) => {
|
||||
if (err instanceof AppError) {
|
||||
return res.status(err.statusCode).json({ error: err.message });
|
||||
}
|
||||
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: "Internal server error" });
|
||||
});
|
||||
|
||||
export default app;
|
||||
@@ -1,51 +1,10 @@
|
||||
import "./bootstrap";
|
||||
import "reflect-metadata";
|
||||
import "express-async-errors";
|
||||
import express, { Request, Response, NextFunction } from "express";
|
||||
import cors from "cors";
|
||||
import cookieParser from "cookie-parser";
|
||||
import multer from "multer";
|
||||
import * as Sentry from "@sentry/node";
|
||||
|
||||
import "./database";
|
||||
import uploadConfig from "./config/upload";
|
||||
import AppError from "./errors/AppError";
|
||||
import routes from "./routes";
|
||||
import app from "./app";
|
||||
import { initIO } from "./libs/socket";
|
||||
import { StartWhatsAppSessions } from "./services/WbotServices/StartWhatsAppSessions";
|
||||
|
||||
Sentry.init({ dsn: process.env.SENTRY_DSN });
|
||||
|
||||
const upload = multer(uploadConfig);
|
||||
const app = express();
|
||||
|
||||
app.use(
|
||||
cors({
|
||||
credentials: true,
|
||||
origin: process.env.FRONTEND_URL
|
||||
})
|
||||
);
|
||||
app.use(cookieParser());
|
||||
app.use(express.json());
|
||||
app.use(Sentry.Handlers.requestHandler());
|
||||
app.use(upload.single("media"));
|
||||
app.use("/public", express.static(uploadConfig.directory));
|
||||
app.use(routes);
|
||||
|
||||
const server = app.listen(process.env.PORT, () => {
|
||||
console.log(`Server started on port: ${process.env.PORT}`);
|
||||
});
|
||||
|
||||
initIO(server);
|
||||
StartWhatsAppSessions();
|
||||
|
||||
app.use(Sentry.Handlers.errorHandler());
|
||||
|
||||
app.use(async (err: Error, req: Request, res: Response, _: NextFunction) => {
|
||||
if (err instanceof AppError) {
|
||||
return res.status(err.statusCode).json({ error: err.message });
|
||||
}
|
||||
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: "Internal server error" });
|
||||
});
|
||||
|
||||
@@ -32,13 +32,10 @@ const CreateUserService = async ({
|
||||
"Check-email",
|
||||
"An user with this email already exists.",
|
||||
async value => {
|
||||
if (value) {
|
||||
const emailExists = await User.findOne({
|
||||
where: { email: value }
|
||||
});
|
||||
return !emailExists;
|
||||
}
|
||||
return false;
|
||||
const emailExists = await User.findOne({
|
||||
where: { email: value! }
|
||||
});
|
||||
return !emailExists;
|
||||
}
|
||||
),
|
||||
password: Yup.string().required().min(5)
|
||||
|
||||
Reference in New Issue
Block a user