Archived
1
0
Fork 0

Setup basic test for public pages and database models

This commit is contained in:
Ethanell 2020-08-18 13:06:54 +02:00
parent 4aea919d34
commit 70af322056
7 changed files with 2095 additions and 15 deletions

4
app.js
View file

@ -3,7 +3,6 @@ let path = require("path");
let cookieParser = require("cookie-parser"); let cookieParser = require("cookie-parser");
let session = require("express-session"); let session = require("express-session");
let logger = require("morgan"); let logger = require("morgan");
let config = require("./config/config.json");
let indexRouter = require("./routes/index"); let indexRouter = require("./routes/index");
let registerRouter = require("./routes/register"); let registerRouter = require("./routes/register");
@ -18,7 +17,7 @@ let adminCommandsRouter = require("./routes/admin/commands");
let app = express(); let app = express();
let sess = { let sess = {
secret: config.secret, secret: process.env.NODE_ENV === "test" ? "Keyboard Cat" : require("./config/config.json").secret,
cookie: {} cookie: {}
} }
@ -51,6 +50,7 @@ app.use("/admin/commands", adminCommandsRouter);
// catch 404 and forward to error handler // catch 404 and forward to error handler
app.use((req, res) => { app.use((req, res) => {
res.status(404);
res.render("error", {message: "Page not found", "error": {}}) res.render("error", {message: "Page not found", "error": {}})
}); });

View file

@ -1,8 +1,10 @@
{ {
"username": "root", "secret": "keyboard cat",
"password": null, "database": {
"database": "database_development", "username": "root",
"host": "127.0.0.1", "password": null,
"dialect": "mysql", "database": "database_development",
"secret": "keyboard cat" "host": "127.0.0.1",
"dialect": "mysql"
}
} }

View file

@ -4,15 +4,16 @@ const fs = require("fs");
const path = require("path"); const path = require("path");
const Sequelize = require("sequelize"); const Sequelize = require("sequelize");
const basename = path.basename(__filename); const basename = path.basename(__filename);
const config = require(__dirname + "/../config/config.json");
const db = {}; const db = {};
let sequelize; let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config); if (process.env.NODE_ENV === "test")
} else { sequelize = new Sequelize("sqlite::memory:");
sequelize = new Sequelize(config.database, config.username, config.password, config); else
} sequelize = new Sequelize(require(__dirname + "/../config/config.json").database);
fs fs
.readdirSync(__dirname) .readdirSync(__dirname)

1770
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -3,7 +3,8 @@
"version": "0.0.0", "version": "0.0.0",
"private": true, "private": true,
"scripts": { "scripts": {
"start": "node ./bin/www" "start": "node ./bin/www",
"test": "NODE_ENV=test mocha"
}, },
"dependencies": { "dependencies": {
"cookie-parser": "~1.4.4", "cookie-parser": "~1.4.4",
@ -17,5 +18,11 @@
"pug": "2.0.0-beta11", "pug": "2.0.0-beta11",
"sequelize": "^6.3.4", "sequelize": "^6.3.4",
"sequelize-cli": "^6.2.0" "sequelize-cli": "^6.2.0"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^8.1.1",
"sqlite3": "^5.0.0",
"supertest": "^4.0.2"
} }
} }

231
test/database.js Normal file
View file

@ -0,0 +1,231 @@
let expect = require("chai").expect;
async function wipeDatabase(models) {
for (let model in models) {
if (["sequelize", "Sequelize"].indexOf(model) < 0)
await models[model].destroy({where: {}});
}
}
async function databaseEnter() {
let models = require("../models");
await models.sequelize.sync();
await wipeDatabase(models);
return models;
}
async function databaseExit(models) {
await wipeDatabase(models);
await models.sequelize.close();
delete require.cache[require.resolve("../models")];
}
describe("Database Department tests", () => {
let models;
before(async () => {
models = await databaseEnter();
});
after(async () => {
await databaseExit(models);
});
afterEach(async () => {
await wipeDatabase(models);
});
it("Department creation", async () => {
expect(await models.Department.create({name: "TestDepartment"})).to.be.instanceOf(models.Department);
});
it("Department validation", async () => {
await models.Department.create({name: "TestDepartment"});
try {
await models.Department.create({name: "TestDepartment"});
} catch (e) {
if (!(e instanceof models.Sequelize.ValidationError))
throw e;
}
});
});
describe("Database Sandwich tests", () => {
let models;
before(async () => {
models = await databaseEnter();
});
after(async () => {
await databaseExit(models);
});
afterEach(async () => {
await wipeDatabase(models);
});
it("Sandwich creation", async () => {
expect(await models.Sandwich.create({name: "TestSandwich", price: 1.5})).to.be.instanceOf(models.Sandwich);
});
it("Sandwich validation", async () => {
await models.Sandwich.create({name: "TestSandwich", price: 1.5});
try {
await models.Sandwich.create({name: "TestSandwich", price: 1.5});
} catch (e) {
if (!(e instanceof models.Sequelize.ValidationError))
throw e;
}
});
});
describe("Database User test", () => {
let models;
before(async () => {
models = await databaseEnter()
});
after(async () => {
await databaseExit(models);
});
afterEach(async () => {
await wipeDatabase(models);
});
it("User creation", async () => {
expect(await models.User.create({
username: "test",
email: "test@test.fr",
firstName: "Test",
lastName: "Test",
passwordHash: "test"
})).to.be.instanceOf(models.User);
});
it("User password", async () => {
let testPassword = "test";
let testUser = await models.User.create({
username: "test",
email: "test@test.fr",
firstName: "Test",
lastName: "Test",
passwordHash: testPassword});
expect(testUser.checkPassword(testPassword)).to.be.equal(true);
});
it("User validation", async () => {
await models.User.create({
username: "test",
email: "test@test.fr",
firstName: "Test",
lastName: "Test",
passwordHash: "test"});
//Username
try {
await models.User.create({
username: "test",
email: "test2@test.fr",
firstName: "Test2",
lastName: "Test2",
passwordHash: "test"
})
} catch (e) {
if (!(e instanceof models.Sequelize.ValidationError))
throw e;
}
//Email
try {
await models.User.create({
username: "test2",
email: "test@test.fr",
firstName: "Test2",
lastName: "Test2",
passwordHash: "test"
})
} catch (e) {
if (!(e instanceof models.Sequelize.ValidationError))
throw e;
}
//First & last name
try {
await models.User.create({
username: "test2",
email: "test2@test.fr",
firstName: "Test",
lastName: "Test",
passwordHash: "test"
})
} catch (e) {
if (!(e instanceof models.Sequelize.ValidationError))
throw e;
}
});
it("User associations", async () => {
let testUser = await models.User.create({
username: "test",
email: "test@test.fr",
firstName: "Test",
lastName: "Test",
passwordHash: "test"
}, {
include: models.Department
});
let testDepartment = await models.Department.create({name: "TestDepartment"});
await testUser.setDepartment(testDepartment);
await testUser.reload();
expect(testUser.Department).to.be.instanceOf(models.Department);
expect(testUser.Department.name).to.be.equal(testDepartment.name);
});
});
describe("Database Command tests", () => {
let models;
before(async () => {
models = await databaseEnter();
});
after(async () => {
await databaseExit(models);
});
afterEach(async () => {
await wipeDatabase(models);
});
it("Command creation", async () => {
expect(await models.Command.create({
firstName: "Test",
lastName: "Test",
price: 1.5
})).to.be.instanceOf(models.Command);
});
it("Command associations", async () => {
let testCommand = await models.Command.create({firstName: "Test", lastName: "Test", price: 1.5, },
{include: [models.Department, models.Sandwich, models.User]});
let testDepartment = await models.Department.create({name: "TestDepartment"});
let testSandwich = await models.Sandwich.create({name: "TestSandwich", price: 1.5});
let testUser = await models.User.create({
username: "test",
email: "test@test.fr",
firstName: "Test",
lastName: "Test",
passwordHash: "test"});
await testCommand.setDepartment(testDepartment);
await testCommand.reload();
expect(testCommand.Department).to.be.instanceOf(models.Department);
expect(testCommand.Department.name).to.be.equal(testDepartment.name);
await testCommand.addSandwiches(testSandwich);
await testCommand.reload();
expect(testCommand.Sandwiches[0]).to.be.instanceOf(models.Sandwich);
expect(testCommand.Sandwiches[0].name).to.be.equal(testSandwich.name);
await testCommand.setUser(testUser);
await testCommand.reload();
expect(testCommand.User).to.be.instanceOf(models.User);
expect(testCommand.User.username).to.be.equal(testUser.username);
});
});

69
test/pages.js Normal file
View file

@ -0,0 +1,69 @@
let request = require("supertest");
describe("Public pages test", () => {
let app;
let models;
before(async () => {
app = require("../app");
models = require("../models");
await models.sequelize.sync();
});
after( async () => {
await models.sequelize.close();
for (let e of ["../app", "../models"])
delete require.cache[require.resolve(e)];
});
it("Responds to /", (done) => {
request(app)
.get("/")
.expect(200, done);
});
it("Responds to /login", (done) => {
request(app)
.get("/login")
.expect(200, done);
});
it("Responds to /register", (done) => {
request(app)
.get("/register")
.expect(200, done);
});
it("Responds to /logout", (done) => {
request(app)
.get("/logout")
.expect(302, done);
});
it("Response to /profile", (done) => {
request(app)
.get("/profile")
.expect(302, done);
});
it("Response to /sandwiches", (done) => {
request(app)
.get("/sandwiches")
.expect(302, done);
});
it("Response to /commands", (done) => {
request(app)
.get("/commands")
.expect(302, done);
});
it("Response to /admin", (done) => {
request(app)
.get("/admin")
.expect(302, done);
});
it("Response to /admin/commands", (done) => {
request(app)
.get("/admin/commands")
.expect(302, done);
});
it("404 everything else", (done) => {
request(app)
.get("/foo/bar")
.expect(404, done);
});
});