From 43227d42ad40c2c217f6728e3c06b2eed7a5f25f Mon Sep 17 00:00:00 2001 From: flifloo Date: Tue, 25 Aug 2020 17:53:21 +0200 Subject: [PATCH] Reorganise test --- test/database.js | 442 ++++++++++++++++++++++++----------------------- test/pages.js | 416 ++++++++++++++++++++++---------------------- 2 files changed, 434 insertions(+), 424 deletions(-) diff --git a/test/database.js b/test/database.js index 0205877..10520cf 100644 --- a/test/database.js +++ b/test/database.js @@ -17,236 +17,242 @@ async function databaseExit(models) { delete require.cache[require.resolve("../models")]; } +describe("Database objects tests", () => { + describe("Database Department tests", () => { + let 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"}); - return expect(models.Department.create({name: "TestDepartment"})).to.be.rejectedWith(models.Sequelize.ValidationError); - }); -}); - -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}); - return expect(models.Sandwich.create({name: "TestSandwich", price: 1.5})).to.be.rejectedWith(models.Sequelize.ValidationError); - }); -}); - -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", - emailToken: "Keyboard Cat", - firstName: "Test", - lastName: "Test", - passwordHash: "test", - passwordToken: "Keyboard Cat" + before(async () => { + models = await databaseEnter(); + }); + after(async () => { + await databaseExit(models); }); - return Promise.all([ - //Username - expect(models.User.create({ + 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"}); + return expect(models.Department.create({name: "TestDepartment"})).to.be.rejectedWith(models.Sequelize.ValidationError); + }); + }); + + 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}); + return expect(models.Sandwich.create({ + name: "TestSandwich", + price: 1.5 + })).to.be.rejectedWith(models.Sequelize.ValidationError); + }); + }); + + 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: "test2@test.fr", - firstName: "Test2", - lastName: "Test2", - passwordHash: "test" - })).to.be.rejectedWith(models.Sequelize.ValidationError), - //Email - expect(models.User.create({ - username: "test2", email: "test@test.fr", - firstName: "Test2", - lastName: "Test2", - passwordHash: "test" - })).to.be.rejectedWith(models.Sequelize.ValidationError), - //First & last name - expect(models.User.create({ - username: "test2", - email: "test2@test.fr", firstName: "Test", lastName: "Test", passwordHash: "test" - })).to.be.rejectedWith(models.Sequelize.ValidationError), - //Email token - expect(models.User.create({ - username: "test2", - email: "test2@test.fr", + })).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", emailToken: "Keyboard Cat", - firstName: "Test2", - lastName: "Test2", - passwordHash: "test" - })).to.be.rejectedWith(models.Sequelize.ValidationError), - //Password token - expect(models.User.create({ - username: "test2", - email: "test2@test.fr", - firstName: "Test2", - lastName: "Test2", + firstName: "Test", + lastName: "Test", passwordHash: "test", passwordToken: "Keyboard Cat" - })).to.be.rejectedWith(models.Sequelize.ValidationError) - ]) - }); - 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 + }); + + return Promise.all([ + //Username + expect(models.User.create({ + username: "test", + email: "test2@test.fr", + firstName: "Test2", + lastName: "Test2", + passwordHash: "test" + })).to.be.rejectedWith(models.Sequelize.ValidationError), + //Email + expect(models.User.create({ + username: "test2", + email: "test@test.fr", + firstName: "Test2", + lastName: "Test2", + passwordHash: "test" + })).to.be.rejectedWith(models.Sequelize.ValidationError), + //First & last name + expect(models.User.create({ + username: "test2", + email: "test2@test.fr", + firstName: "Test", + lastName: "Test", + passwordHash: "test" + })).to.be.rejectedWith(models.Sequelize.ValidationError), + //Email token + expect(models.User.create({ + username: "test2", + email: "test2@test.fr", + emailToken: "Keyboard Cat", + firstName: "Test2", + lastName: "Test2", + passwordHash: "test" + })).to.be.rejectedWith(models.Sequelize.ValidationError), + //Password token + expect(models.User.create({ + username: "test2", + email: "test2@test.fr", + firstName: "Test2", + lastName: "Test2", + passwordHash: "test", + passwordToken: "Keyboard Cat" + })).to.be.rejectedWith(models.Sequelize.ValidationError) + ]) }); - let testDepartment = await models.Department.create({name: "TestDepartment"}); + 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); + 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 Order tests", () => { + let models; + + before(async () => { + models = await databaseEnter(); + }); + after(async () => { + await databaseExit(models); + }); + + afterEach(async () => { + await wipeDatabase(models); + }); + + it("Order creation", async () => { + expect(await models.Order.create({ + firstName: "Test", + lastName: "Test", + price: 1.5 + })).to.be.instanceOf(models.Order); + }); + it("Order associations", async () => { + let testOrder = await models.Order.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 testOrder.setDepartment(testDepartment); + await testOrder.reload(); + expect(testOrder.Department).to.be.instanceOf(models.Department); + expect(testOrder.Department.name).to.be.equal(testDepartment.name); + + await testOrder.addSandwiches(testSandwich); + await testOrder.reload(); + expect(testOrder.Sandwiches[0]).to.be.instanceOf(models.Sandwich); + expect(testOrder.Sandwiches[0].name).to.be.equal(testSandwich.name); + + await testOrder.setUser(testUser); + await testOrder.reload(); + expect(testOrder.User).to.be.instanceOf(models.User); + expect(testOrder.User.username).to.be.equal(testUser.username); + }); + }); + + describe("Database Data tests", () => { + let models; + + before(async () => { + models = await databaseEnter(); + }); + after(async () => { + await databaseExit(models); + }); + + afterEach(async () => { + await wipeDatabase(models); + }); + + it("Data creation", async () => { + expect(await models.Data.create({ + key: "test", + value: "test" + })).to.be.instanceOf(models.Data); + }); + it("Data validation", async () => { + await models.Data.create({key: "test", value: "test"}); + return expect(models.Data.create({ + key: "test", + value: "test" + })).to.be.rejectedWith(models.Sequelize.ValidationError); + }) }); }); - -describe("Database Order tests", () => { - let models; - - before(async () => { - models = await databaseEnter(); - }); - after(async () => { - await databaseExit(models); - }); - - afterEach(async () => { - await wipeDatabase(models); - }); - - it("Order creation", async () => { - expect(await models.Order.create({ - firstName: "Test", - lastName: "Test", - price: 1.5 - })).to.be.instanceOf(models.Order); - }); - it("Order associations", async () => { - let testOrder = await models.Order.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 testOrder.setDepartment(testDepartment); - await testOrder.reload(); - expect(testOrder.Department).to.be.instanceOf(models.Department); - expect(testOrder.Department.name).to.be.equal(testDepartment.name); - - await testOrder.addSandwiches(testSandwich); - await testOrder.reload(); - expect(testOrder.Sandwiches[0]).to.be.instanceOf(models.Sandwich); - expect(testOrder.Sandwiches[0].name).to.be.equal(testSandwich.name); - - await testOrder.setUser(testUser); - await testOrder.reload(); - expect(testOrder.User).to.be.instanceOf(models.User); - expect(testOrder.User.username).to.be.equal(testUser.username); - }); -}); - -describe("Database Data tests", () => { - let models; - - before(async () => { - models = await databaseEnter(); - }); - after(async () => { - await databaseExit(models); - }); - - afterEach(async () => { - await wipeDatabase(models); - }); - - it("Data creation", async () => { - expect(await models.Data.create({ - key: "test", - value: "test" - })).to.be.instanceOf(models.Data); - }); - it("Data validation", async () => { - await models.Data.create({key: "test", value: "test"}); - return expect(models.Data.create({ - key: "test", - value: "test" - })).to.be.rejectedWith(models.Sequelize.ValidationError); - }) -}); diff --git a/test/pages.js b/test/pages.js index f14f931..f4d0743 100644 --- a/test/pages.js +++ b/test/pages.js @@ -39,239 +39,243 @@ async function getLoginAgent(app) { } -describe("Public pages test", () => { - let app; - let models; - - before(async () => { - [app, models] = await setup(); - }); - after(() => { - return clean; - }); - - 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 /forget", (done) => { - request(app) - .get("/forget") - .expect(200, done) - }); - it("Responds to /register", (done) => { - request(app) - .get("/register") - .expect(200, done); - }); - it("Responds to /check", (done) => { - request(app) - .get("/check") - .expect(400, 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 /orders", (done) => { - request(app) - .get("/orders") - .expect(302, done); - }); - it("Response to /admin", (done) => { - request(app) - .get("/admin") - .expect(302, done); - }); - it("Response to /admin/orders", (done) => { - request(app) - .get("/admin/orders") - .expect(302, done); - }); - it("Response to /admin/orders/date", done => { - request(app) - .get("/admin/orders/date") - .expect(302, done); - }); - it("Reponse to /admin/sandwiches", done => { - request(app) - .get("/admin/sandwiches") - .expect(302, done); - }); - it("Reponse to /admin/sandwiches/add", done => { - request(app) - .get("/admin/sandwiches/add") - .expect(302, done); - }); - it("Reponse to /admin/sandwiches/edit", done => { - request(app) - .get("/admin/sandwiches/edit") - .expect(302, done); - }); - it("Reponse to /admin/departments", done => { - request(app) - .get("/admin/departments") - .expect(302, done); - }); - it("Reponse to /admin/departments/add", done => { - request(app) - .get("/admin/departments/add") - .expect(302, done); - }); - it("Reponse to /admin/departments/edit", done => { - request(app) - .get("/admin/departments/edit") - .expect(302, done); - }); - it("404 everything else", (done) => { - request(app) - .get("/foo/bar") - .expect(404, done); - }); -}); - -describe("Global logged user pages", () => { - let app; - let models; - - before(async () => { - [app, models] = await setup(); - await createTestUser(models); - }); - after( () => { - return clean; - }); - - it("Login user", async () => { - let res = await request(app) - .post("/login") - .send({username: "test", password: "test"}) - .expect(302); - expect(res.headers.location).to.be.equal("/"); - }); - it("Login error", async () => { - let res = await request(app) - .post("/login") - .send({username: "wrong", password: "wrong"}) - .expect(302); - expect(res.headers.location).to.be.equal("/login?err=true"); - }); - it("Forget page", async () => { - await (await getLoginAgent(app)) - .get("/forget") - .expect(302); - }); - it("Register page", async () => { - await (await getLoginAgent(app)) - .get("/register") - .expect(302); - }); - it("Logout page", async () => { - let agent = await getLoginAgent(app); - await agent - .get("/logout") - .expect(302); - - //Check if user is correctly logout - await agent - .get("/login") - .expect(200); - }); - it("Profile page", async () => { - await (await getLoginAgent(app)) - .get("/profile") - .expect(200); - }); -}); - -for (let [p, a] of Object.entries({ - 0: [403, 403, 403, 403, 403, 403, 403, 403, 403, 403, 403], - 1: [200, 403, 403, 403, 403, 403, 403, 403, 403, 403, 403], - 2: [200, 200, 403, 403, 403, 403, 403, 403, 403, 403, 403], - 3: [200, 200, 200, 200, 200, 200, 200, 400, 200, 200, 400] -})) - describe(`Permission ${p} pages`, () => { +describe("Pages tests", () => { + describe("Public pages test", () => { let app; let models; before(async () => { [app, models] = await setup(); - await createTestUser(models, p); }); - after( () => { + after(() => { return clean; }); - it("Sandwiches page", async () => { - await (await getLoginAgent(app)) + 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 /forget", (done) => { + request(app) + .get("/forget") + .expect(200, done) + }); + it("Responds to /register", (done) => { + request(app) + .get("/register") + .expect(200, done); + }); + it("Responds to /check", (done) => { + request(app) + .get("/check") + .expect(400, 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(a[0]); + .expect(302, done); }); - it("Orders page", async () => { - await (await getLoginAgent(app)) + it("Response to /orders", (done) => { + request(app) .get("/orders") - .expect(a[1]); + .expect(302, done); }); - it("Admin page", async () => { - await (await getLoginAgent(app)) + it("Response to /admin", (done) => { + request(app) .get("/admin") - .expect(a[2]); + .expect(302, done); }); - it("Orders administration page", async () => { - await (await getLoginAgent(app)) + it("Response to /admin/orders", (done) => { + request(app) .get("/admin/orders") - .expect(a[3]); + .expect(302, done); }); - it("Orders date administration page", async () => { - await (await getLoginAgent(app)) + it("Response to /admin/orders/date", done => { + request(app) .get("/admin/orders/date") - .expect(a[4]) + .expect(302, done); }); - it("Sandwiches administration page", async () => { - await (await getLoginAgent(app)) + it("Reponse to /admin/sandwiches", done => { + request(app) .get("/admin/sandwiches") - .expect(a[5]) + .expect(302, done); }); - it("Sandwiches add administration page", async () => { - await (await getLoginAgent(app)) + it("Reponse to /admin/sandwiches/add", done => { + request(app) .get("/admin/sandwiches/add") - .expect(a[6]) + .expect(302, done); }); - it("Sandwiches edit administration page", async () => { - await (await getLoginAgent(app)) - .get("/admin/sandwiches/edit?name=test") - .expect(a[7]) + it("Reponse to /admin/sandwiches/edit", done => { + request(app) + .get("/admin/sandwiches/edit") + .expect(302, done); }); - it("Departments administration page", async () => { - await (await getLoginAgent(app)) + it("Reponse to /admin/departments", done => { + request(app) .get("/admin/departments") - .expect(a[8]) + .expect(302, done); }); - it("Departments add administration page", async () => { - await (await getLoginAgent(app)) + it("Reponse to /admin/departments/add", done => { + request(app) .get("/admin/departments/add") - .expect(a[9]) + .expect(302, done); }); - it("Departments edit administration page", async () => { - await (await getLoginAgent(app)) - .get("/admin/departments/edit?name=test") - .expect(a[10]) + it("Reponse to /admin/departments/edit", done => { + request(app) + .get("/admin/departments/edit") + .expect(302, done); + }); + it("404 everything else", (done) => { + request(app) + .get("/foo/bar") + .expect(404, done); }); }); + + describe("Global logged user pages", () => { + let app; + let models; + + before(async () => { + [app, models] = await setup(); + await createTestUser(models); + }); + after(() => { + return clean; + }); + + it("Login user", async () => { + let res = await request(app) + .post("/login") + .send({username: "test", password: "test"}) + .expect(302); + expect(res.headers.location).to.be.equal("/"); + }); + it("Login error", async () => { + let res = await request(app) + .post("/login") + .send({username: "wrong", password: "wrong"}) + .expect(302); + expect(res.headers.location).to.be.equal("/login?err=true"); + }); + it("Forget page", async () => { + await (await getLoginAgent(app)) + .get("/forget") + .expect(302); + }); + it("Register page", async () => { + await (await getLoginAgent(app)) + .get("/register") + .expect(302); + }); + it("Logout page", async () => { + let agent = await getLoginAgent(app); + await agent + .get("/logout") + .expect(302); + + //Check if user is correctly logout + await agent + .get("/login") + .expect(200); + }); + it("Profile page", async () => { + await (await getLoginAgent(app)) + .get("/profile") + .expect(200); + }); + }); + + describe("Permissions pages tests", () => { + for (let [p, a] of Object.entries({ + 0: [403, 403, 403, 403, 403, 403, 403, 403, 403, 403, 403], + 1: [200, 403, 403, 403, 403, 403, 403, 403, 403, 403, 403], + 2: [200, 200, 403, 403, 403, 403, 403, 403, 403, 403, 403], + 3: [200, 200, 200, 200, 200, 200, 200, 400, 200, 200, 400] + })) + describe(`Permission ${p} pages`, () => { + let app; + let models; + + before(async () => { + [app, models] = await setup(); + await createTestUser(models, p); + }); + after(() => { + return clean; + }); + + it("Sandwiches page", async () => { + await (await getLoginAgent(app)) + .get("/sandwiches") + .expect(a[0]); + }); + it("Orders page", async () => { + await (await getLoginAgent(app)) + .get("/orders") + .expect(a[1]); + }); + it("Admin page", async () => { + await (await getLoginAgent(app)) + .get("/admin") + .expect(a[2]); + }); + it("Orders administration page", async () => { + await (await getLoginAgent(app)) + .get("/admin/orders") + .expect(a[3]); + }); + it("Orders date administration page", async () => { + await (await getLoginAgent(app)) + .get("/admin/orders/date") + .expect(a[4]) + }); + it("Sandwiches administration page", async () => { + await (await getLoginAgent(app)) + .get("/admin/sandwiches") + .expect(a[5]) + }); + it("Sandwiches add administration page", async () => { + await (await getLoginAgent(app)) + .get("/admin/sandwiches/add") + .expect(a[6]) + }); + it("Sandwiches edit administration page", async () => { + await (await getLoginAgent(app)) + .get("/admin/sandwiches/edit?name=test") + .expect(a[7]) + }); + it("Departments administration page", async () => { + await (await getLoginAgent(app)) + .get("/admin/departments") + .expect(a[8]) + }); + it("Departments add administration page", async () => { + await (await getLoginAgent(app)) + .get("/admin/departments/add") + .expect(a[9]) + }); + it("Departments edit administration page", async () => { + await (await getLoginAgent(app)) + .get("/admin/departments/edit?name=test") + .expect(a[10]) + }); + }); + }); +});