Skip to content

Commit

Permalink
adding it to app-escrita submodules
Browse files Browse the repository at this point in the history
  • Loading branch information
seanvert committed Nov 25, 2023
1 parent 92c3d1f commit e7ecbec
Show file tree
Hide file tree
Showing 42 changed files with 7,250 additions and 1,024 deletions.
110 changes: 59 additions & 51 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,86 @@
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const passport = require('passport');
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import express from 'express';
import mongoose from 'mongoose';
import bodyParser from 'body-parser';
import passport from 'passport';
// TODO: https://expressjs.com/en/advanced/best-practice-security.html#use-cookies-securely
// tirar o expreess-session e usar o cookie-session no lugar
const session = require('express-session');
const cookieParser = require('cookie-parser');
const cors = require('cors');
const morgan = require('morgan');
const populateDb = require('./populateDb');
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const textsRouter = require('./routes/texts');
const authRouter = require('./routes/auth');
const exercisesRouter = require('./routes/exercises');
const quotesRouter = require('./routes/quotes');
import session from 'express-session';
import cookieParser from 'cookie-parser';
import cors from 'cors';
import morgan from 'morgan';
import { addNumbers } from './mathUtils';
const result = addNumbers(2, 3);
console.log(result); // Output: 5
import populateDb from './populateDb';
import indexRouter from './routes/index';
import usersRouter from './routes/users';
import textsRouter from './routes/texts';
import authRouter from './routes/auth';
import exercisesRouter from './routes/exercises';
import quotesRouter from './routes/quotes';
require('dotenv').config();

const { DB_USER } = process.env;
const { DB_PASS } = process.env;
const { DB_URL } = process.env;
const { SECRET_KEY } = process.env;
const { dburl } = process.env;
const DB_URL = dburl;
const { secretkey } = process.env;
const SECRET_KEY = secretkey;
const { DEBUG } = process.env;
const options = {
user: DB_USER,
pass: DB_PASS,
useNewUrlParser: true,
autoIndex: false, // Don't build indexes
connectTimeoutMS: 10000, // Give up initial connection after 10 seconds
socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
family: 4, // Use IPv4, skip trying IPv6
user: DB_USER,
pass: DB_PASS,
useNewUrlParser: true,
autoIndex: false,
connectTimeoutMS: 10000,
socketTimeoutMS: 45000,
family: 4, // Use IPv4, skip trying IPv6
};

async function dbConnect() {
mongoose.connect(DB_URL, options);
mongoose.connection.useDb('escrita');
function dbConnect() {
return __awaiter(this, void 0, void 0, function* () {
mongoose.connect(DB_URL, options);
mongoose.connection.useDb('escrita');
});
}
dbConnect();

if (DEBUG) {
populateDb();
populateDb();
}

// route do healthcheck da api
const serverStatus = () => ({
state: 'up',
dbState: mongoose.STATES[mongoose.connection.readyState],
state: 'up',
dbState: mongoose.STATES[mongoose.connection.readyState],
});

const app = express();
app.use(morgan('tiny'));

app.use(cors({
origin: ['http://192.168.0.100:3000', 'http://192.168.0.101:3000'],
allowedHeaders: ['Content-Type', 'Authorization',
'access-control-allow-credentials',
'upgrade-insecure-requests'],
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: 'false',
credentials: true,
origin: ['http://192.168.0.100:3000', 'http://192.168.0.101:3000'],
allowedHeaders: ['Content-Type', 'Authorization',
'access-control-allow-credentials',
'upgrade-insecure-requests'],
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
credentials: true,
}));

app.use(cookieParser(SECRET_KEY));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(session({
// TODO: change secret key
// TODO: insert an environment variable here
secret: SECRET_KEY,
resave: false,
saveUninitialized: false,
// TODO: insert sessions into database
// TODO: change secret key
// TODO: insert an environment variable here
secret: SECRET_KEY,
resave: false,
saveUninitialized: false,
// TODO: insert sessions into database
}));

app.use(passport.initialize());
app.use(passport.session());
app.use('/', indexRouter);
Expand Down
95 changes: 46 additions & 49 deletions app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,69 @@ const options = {
// If not connected, return errors immediately rather than waiting for reconnect
connectTimeoutMS: 10000, // Give up initial connection after 10 seconds
socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
family: 4 // Use IPv4, skip trying IPv6
family: 4, // Use IPv4, skip trying IPv6
};

const DB_URL = 'mongodb://escritaApp:teste@localhost:27017/escrita';

beforeAll(done => {
beforeAll((done) => {
// mongoose.connect(DB_URL, options);
done()
done();
});




// TODO: ver como rodar estes testes
describe("testa a raiz", () => {
test("método GET", async () => {
//const response = await request(app).get("/");
//expect(response.statusCode).toBe(200);
});
describe('testa a raiz', () => {
test('método GET', async () => {
// const response = await request(app).get("/");
// expect(response.statusCode).toBe(200);
});
});

describe("testa a raiz dos users", () => {
test("método GET", async () => {
//const response = await request(app).get("/users");
//expect(response.statusCode).toBe(200);
});
describe('testa a raiz dos users', () => {
test('método GET', async () => {
// const response = await request(app).get("/users");
// expect(response.statusCode).toBe(200);
});

// TODO: criar um usuário
// TODO: apagar um usuário
// TODO: ler um usuário
// TODO: editar um usuário
// TODO: criar um usuário
// TODO: apagar um usuário
// TODO: ler um usuário
// TODO: editar um usuário
});

describe("testa a raiz dos texts", () => {
test("método GET", async () => {
//const response = await request(app).get("/texts");
//expect(response.statusCode).toBe(200);
});
describe('testa a raiz dos texts', () => {
test('método GET', async () => {
// const response = await request(app).get("/texts");
// expect(response.statusCode).toBe(200);
});

// TODO: criar um texto
// TODO: ler um texto
// TODO: apagar um texto
// TODO: editar um texto
// TODO: criar um texto
// TODO: ler um texto
// TODO: apagar um texto
// TODO: editar um texto
});

describe("Testa as rotas de autenticação", () => {
test("método POST em signup", async () => {
//const response = await request(app)
// .post("/signup")
// .send({
// name: "Teste",
// last_name: "Do teste",
// email: "teste@jest.com",
// username: "testesjest",
// password: "testesjest"
// });
//expect(response.statusCode).toBe(200);
});
describe('Testa as rotas de autenticação', () => {
test('método POST em signup', async () => {
// const response = await request(app)
// .post("/signup")
// .send({
// name: "Teste",
// last_name: "Do teste",
// email: "teste@jest.com",
// username: "testesjest",
// password: "testesjest"
// });
// expect(response.statusCode).toBe(200);
});

// TODO: criar um usuário
// TODO: apagar um usuário
// TODO: ler um usuário
// TODO: editar um usuário
// TODO: criar um usuário
// TODO: apagar um usuário
// TODO: ler um usuário
// TODO: editar um usuário
});

afterAll(done => {
//mongoose.connection.close();
done();
afterAll((done) => {
// mongoose.connection.close();
done();
});
90 changes: 90 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import express from 'express';
import mongoose from 'mongoose';
import bodyParser from 'body-parser';
import passport from 'passport';
// TODO: https://expressjs.com/en/advanced/best-practice-security.html#use-cookies-securely
// tirar o expreess-session e usar o cookie-session no lugar
import session from 'express-session';
import cookieParser from 'cookie-parser';
import cors from 'cors';
import morgan from 'morgan';

import populateDb from './populateDb.ts';
import indexRouter from './routes/index.ts';
import usersRouter from './routes/users.ts';
import textsRouter from './routes/texts.ts';
import authRouter from './routes/auth.ts';
import exercisesRouter from './routes/exercises.ts';
import quotesRouter from './routes/quotes.ts';
import { populate } from 'dotenv';
require('dotenv').config();

const { DB_USER } = process.env;
const { DB_PASS } = process.env;
const { dburl } = process.env;
const DB_URL = dburl!;
const { secretkey } = process.env;
const SECRET_KEY = secretkey!;
const { DEBUG } = process.env;
const options = {
user: DB_USER,
pass: DB_PASS,
useNewUrlParser: true,
autoIndex: false, // Don't build indexes
connectTimeoutMS: 10000, // Give up initial connection after 10 seconds
socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
family: 4, // Use IPv4, skip trying IPv6
};

async function dbConnect() {
mongoose.connect(DB_URL, options);
mongoose.connection.useDb('escrita');
}
dbConnect();

if (DEBUG) {
populateDb();
}

// route do healthcheck da api
const serverStatus = () => ({
state: 'up',
dbState: mongoose.STATES[mongoose.connection.readyState],
});
const app = express();
export default app;

app.use(morgan('tiny'));

app.use(cors({
origin: ['http://192.168.0.100:3000', 'http://192.168.0.101:3000'],
allowedHeaders: ['Content-Type', 'Authorization',
'access-control-allow-credentials',
'upgrade-insecure-requests'],
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
credentials: true,
}));

app.use(cookieParser(SECRET_KEY));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(session({
// TODO: change secret key
// TODO: insert an environment variable here
secret: SECRET_KEY,
resave: false,
saveUninitialized: false,
// TODO: insert sessions into database
}));

app.use(passport.initialize());
app.use(passport.session());
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/exercises', exercisesRouter);
app.use('/auth', authRouter);
app.use('/texts', textsRouter);
app.use('/texts', textsRouter);
app.use('/quotes', quotesRouter);
module.exports = app;
Loading

0 comments on commit e7ecbec

Please sign in to comment.