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

Testing Infastructure #93

Merged
merged 3 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions @types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
export type user_token = { username: string, admin: string }
export type env_file = {
PORT: number,
URI?: string,
DB?: string,
TOKEN_KEY: string,
SECRET_KEY: string,
ENCRYPT_KEY: string,
IV_KEY: string,
DOMAIN: string,
PROD: boolean
}
28 changes: 28 additions & 0 deletions __tests__/survey.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Express } from "express"
import { Server } from "http";
import supertest = require("supertest");
import { create_app } from "../app";
import { start_server } from "../server";
const request = require('supertest');

let app: Express
let server: Server

beforeAll(async() => {
app = await create_app()
})


test("should return OK response generating survey", (done) => {
const survey_url = "https://raw.githubusercontent.com/Watts-Lab/surveyor/main/surveys/CRT.csv"

request(app)
.get(`/s/?url=${survey_url}`)
.expect(200)
.end((err, res) => {
if (err) return done(err);
return done();
})

})

46 changes: 39 additions & 7 deletions app.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
/** @format */
require("dotenv").config();
import { startServer } from "./server";
import express = require("express");
import cors = require("cors");
import { json, urlencoded } from "body-parser";
import session = require("express-session");
import { env_config } from "./config";
import { Express } from "express"
var cookieParser = require("cookie-parser")
// Router Imports
const links_router = require("./routes/links")
const survey_router = require("./routes/survey")
const auth_router = require("./routes/auth")
const encrypt_router = require("./routes/encrypt")
const validate_router = require("./routes/validation")

async function main(): Promise<void> {
// should be updated to Load CSV from URL (we can hard code that URL for now, and can deal with dynamic coding later)
startServer();
}

main();
export async function create_app():Promise<Express> {
const app = express()
app.use(cors())
app.use(
session({
secret: env_config.TOKEN_KEY, // just a long random string
resave: false,
saveUninitialized: true,
})
);

app.set("view engine", "pug");
app.use(express.static("public")); // More info on this: http://expressjs.com/en/starter/static-files.html
app.use(json()); // for parsing application/json
app.use(urlencoded({ extended: true })); // for parsing url
app.use(cookieParser())

/** ROUTES */
app.use("/", survey_router)
app.use("/", links_router)
app.use("/", auth_router)
app.use("/", encrypt_router)
app.use("/validate", validate_router)

return app
}
31 changes: 14 additions & 17 deletions config.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import { Database_Wrapper } from "./interfaces";
import Mongo from "./databases/prod_db";

import Mongo from "./databases/db";
import { env_file } from "./@types";
import { MongoMemoryServer } from 'mongodb-memory-server';


const env_config = {
PORT: parseInt(process.env.PORT),
URI: process.env.URI,
DB: process.env.PROD.toLowerCase() == "true" ? process.env.PROD_DB : process.env.TEST_DB,
RANDOM: process.env.RANDOM,
TOKEN_KEY: process.env.TOKEN_KEY,
SECRET_KEY: process.env.SECRET_KEY,
ENCRYPT_KEY: process.env.ENCRYPT_KEY,
IV_KEY: process.env.IV_KEY,
DOMAIN: process.env.DOMAIN
const env_config: env_file = {
PORT: parseInt(process.env.PORT),
URI: process.env.URI,
DB: process.env.PROD.toLowerCase() == "true" ? process.env.PROD_DB : process.env.TEST_DB,
TOKEN_KEY: process.env.TOKEN_KEY,
SECRET_KEY: process.env.SECRET_KEY,
ENCRYPT_KEY: process.env.ENCRYPT_KEY,
IV_KEY: process.env.IV_KEY,
DOMAIN: process.env.DOMAIN,
PROD: process.env.PROD.toLowerCase() == "true"
}

if (env_config.URI === undefined || env_config.DB === undefined) {
throw new Error("Please set mongo db uri or db")
}

const Db_Wrapper: Database_Wrapper = new Mongo(env_config);
Db_Wrapper.set_db(env_config.DB);

export {Db_Wrapper, env_config}
export {env_config}
32 changes: 26 additions & 6 deletions databases/prod_db.ts → databases/db.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
let mongodb, { MongoClient, collection, ObjectID } = require("mongodb");
import { MongoMemoryServer } from 'mongodb-memory-server';
import { env_file } from '../@types';
import { Database_Wrapper } from '../interfaces'

export default class Mongo implements Database_Wrapper {
readonly db_uri: string;
db_uri: string;
collection: string;
client: any
db: string

constructor(env_config) {
this.db_uri = env_config.URI;
console.log(this.db_uri)
constructor(db_uri: string, db: string) {
this.db_uri = db_uri
this.db = db
this.client = new MongoClient(this.db_uri);
this.test_database()
this.test_database()
}

static async create_mongo_memory_server(db: string) {
const mongod = await MongoMemoryServer.create({
instance: {
dbName: db
}
})

return mongod.getUri()
}

set_db(db: string): void {
Expand All @@ -23,7 +35,6 @@ export default class Mongo implements Database_Wrapper {
// Connect the client to the server
await this.client.connect();
await this.client.db("admin").command({ ping: 1 });
console.log("Connected successfully to mongodb server");
} catch {
console.dir
} finally {
Expand Down Expand Up @@ -74,3 +85,12 @@ export default class Mongo implements Database_Wrapper {
}
}

export let Db_Wrapper: Database_Wrapper = null

export const start_db_server = async (env_config: env_file) => {
if (!env_config.PROD) {
const uri = await Mongo.create_mongo_memory_server(env_config.DB)
env_config.URI = uri
}
Db_Wrapper = new Mongo(env_config.URI, env_config.DB)
}
61 changes: 0 additions & 61 deletions databases/test_db.ts

This file was deleted.

8 changes: 5 additions & 3 deletions helpers/encrypt_utils.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import crypto = require("crypto")
import {Db_Wrapper, env_config} from "../config"
import { env_config } from "../config"

const private_key = crypto.scryptSync(env_config.ENCRYPT_KEY, "salt", 32)
const iv = Buffer.from(env_config.IV_KEY, 'hex')
const algorithm = 'aes-256-cbc';

export const encrypt = (text) => {
const private_key = crypto.scryptSync(env_config.ENCRYPT_KEY, "salt", 32)
const iv = Buffer.from(env_config.IV_KEY, 'hex')
const cipher = crypto.createCipheriv(algorithm, private_key, iv)
let crypted = cipher.update(text,'utf8','hex')
crypted += cipher.final('hex')
return crypted
}

export const decrypt = (text) => {
const private_key = crypto.scryptSync(env_config.ENCRYPT_KEY, "salt", 32)
const iv = Buffer.from(env_config.IV_KEY, 'hex')
let decipher = crypto.createDecipheriv(algorithm, private_key, iv)
let dec = decipher.update(text,'hex','utf8')
dec += decipher.final('utf8')
Expand Down
2 changes: 1 addition & 1 deletion helpers/survey_helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { response } from "express";
import { Request, Response } from "express-serve-static-core";
import {Db_Wrapper, env_config} from "../config"
import { Db_Wrapper } from "../databases/db"

export const setPageNums = (survey) => {
var pagefinal = 0
Expand Down
2 changes: 2 additions & 0 deletions interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { env_file } from "./@types";

export interface Database_Wrapper {
set_db(db: any): void;

Expand Down
9 changes: 9 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns : [
"<rootDir>/built/"
],
globalTeardown: '<rootDir>/teardown.js',
};
2 changes: 1 addition & 1 deletion markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import marked = require("marked");

// Gets MD content from a tasks table
function get_task_content(tasks_to_extract: any): Promise<any[]> {
const tasks_with_promises = tasks_to_extract
const tasks_with_promises: [] = tasks_to_extract
.filter(
(task) =>
task["task summary url"].includes(".md") &&
Expand Down
4 changes: 3 additions & 1 deletion middlewares/auth.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {Db_Wrapper, env_config} from "../config"
import { env_config } from "../config"
import { Db_Wrapper } from "../databases/db"

const jwt = require('jsonwebtoken');

export const verify_token = (req, res, next) => {
Expand Down
Loading