From 772d3ee96a51f46a61dd8175e63353b5f9c39a33 Mon Sep 17 00:00:00 2001 From: Chloe Date: Sat, 18 Apr 2020 16:56:06 +0100 Subject: [PATCH] add comments Relates #54 --- .prettierrc | 2 +- handlers/examples-handlers.js | 3 +- middleware/error.js | 1 - model/examples-model.js | 1 + model/users-model.js | 4 +- server.js | 4 +- tests/model.test.js | 104 +++++++++++++--------------------- 7 files changed, 48 insertions(+), 71 deletions(-) diff --git a/.prettierrc b/.prettierrc index c08bd90..931981e 100644 --- a/.prettierrc +++ b/.prettierrc @@ -9,4 +9,4 @@ "jsxBracketSameLine": false, "arrowParens": "avoid", "proseWrap": "never" -} \ No newline at end of file +} diff --git a/handlers/examples-handlers.js b/handlers/examples-handlers.js index ff32312..f5190f6 100644 --- a/handlers/examples-handlers.js +++ b/handlers/examples-handlers.js @@ -45,10 +45,11 @@ function getExample(req, res, next) { } function updateExample(req, res, next) { - const id = Number(req.params.id); + const id = Number(req.params.id); //req.params.id comes back as a string const userID = req.user.id; const newdata = req.body; if (id === NaN) { + // if id was not a number then throws an error (prevents SQL injections) const err = new Error("This is not a valid ID"); err.status = 401; next(err); diff --git a/middleware/error.js b/middleware/error.js index e8fd24c..dfac49c 100644 --- a/middleware/error.js +++ b/middleware/error.js @@ -1,7 +1,6 @@ function handleError(err, req, res, next) { const errorStatus = err.status || 400; res.status(errorStatus).send({error: err}); - // console.error(err); } module.exports = handleError; diff --git a/model/examples-model.js b/model/examples-model.js index a3c47e4..699c294 100644 --- a/model/examples-model.js +++ b/model/examples-model.js @@ -38,6 +38,7 @@ function createExample(example) { function deleteExample(exampleId, user) { return getExample(exampleId).then(exampleObjectFromDB => { if (exampleObjectFromDB.id === user.id || user.adminusr) { + // check if user is authorised return db .query("DELETE FROM examples WHERE id = ($1);", [exampleId]) .then(result => true) diff --git a/model/users-model.js b/model/users-model.js index e068ae0..0bc9116 100644 --- a/model/users-model.js +++ b/model/users-model.js @@ -3,16 +3,18 @@ const db = require("../db/connection.js"); function createUser(user) { return db .query( - "INSERT INTO users(username, email, user_password) VALUES($1, $2, $3) RETURNING id;", + "INSERT INTO users(username, email, user_password) VALUES($1, $2, $3) RETURNING *;", [user.username, user.email, user.password] ) .then(res => res.rows[0].id); } +// Get every row from the users table. function getUsers() { return db.query("SELECT * FROM users").then(res => res.rows); } +// Get a particular user by email function getUser(email) { return db.query("SELECT * FROM users WHERE email = ($1);", [email]).then(res => { if (res.rows.length < 1) throw new Error("User does not exist"); diff --git a/server.js b/server.js index e546ab4..27798e2 100644 --- a/server.js +++ b/server.js @@ -13,13 +13,13 @@ server.get("/", examples.getAllExamples); server.post("/examples", auth, examples.postExample); server.get("/examples/:id", examples.getExample); server.delete("/examples/:id", auth, examples.deleteExample); -server.put("/examples/:id", auth, examples.updateExample); //NEED TO TEST - +server.put("/examples/:id", auth, examples.updateExample); server.post("/signup", users.signup); server.post("/login", users.login); server.use(handleError); +// If this env exists we are in testing mode so don't start the server if (process.env.PGDATABASE !== "localtest") { server.listen(PORT, () => console.log(`Listening on http://localhost:${PORT}`)); } diff --git a/tests/model.test.js b/tests/model.test.js index 16680db..9c67716 100644 --- a/tests/model.test.js +++ b/tests/model.test.js @@ -33,6 +33,15 @@ test("Can create new user", t => { }); }); +test("Returns error if no user found", t => { + build().then(() => { + getUser("hello@iscool.com").catch(err => { + t.equals(err.message, "User does not exist"); + t.end(); + }); + }); +}); + test("Returns user with a given email address", t => { build().then(() => { getUser("admin@iscool.com") @@ -84,69 +93,34 @@ test("Can get update an example by id without all values", t => { }); }); -// test.only("Can update an example by id", (t) => { -// build().then(() => { -// const data = { -// language: "sql", -// example: "This is an example of SQL", -// }; -// updateExample(4, data) -// .then((res) => { -// t.equal(res.language, "sql"); -// // t.equal(res.title, 'SQL example snippet') -// t.equal(res.title, null); -// t.equal(res.example, "This is an example of SQL"); -// t.end(); -// }) -// .catch((err) => { -// t.error(err); -// t.end(); -// }); -// }); -// }); - -// test("Returns a users row by id", (t) => { -// build().then(() => { -// getUserById("2") -// .then((res) => { -// t.equal(res.username, "Tom"); -// t.equal(res.adminusr, false); -// t.end(); -// }) -// .catch((err) => { -// t.error(err); -// t.end(); -// }); -// }); -// }); - -// test("Returns error if no user found", (t) => { -// build().then(() => { -// t.throws(() => getUser("hello@iscool.com")) -// t.end(); -// }) -// }); +test("Returns a users row by id", t => { + build().then(() => { + getUserById("2") + .then(res => { + t.equal(res.username, "Tom"); + t.equal(res.adminusr, false); + t.end(); + }) + .catch(err => { + t.error(err); + t.end(); + }); + }); +}); -// test("Does not allow duplicate users when email is already in use", (t) => { -// build() -// .then(() => { -// const user = { -// username: "Tommy", -// email: "tom@iscool.com", -// password: -// "$2a$10$3IAfxI7ekmnHqMv1T8a46O./avVNcq/YYk6SGkRwxEHsy9cQasuUy", -// }; -// createUser(user).then(() => { -// getUsers().then((res) => { -// console.log("hello"); -// t.equal(res[res.length - 1].username, "Roger"); -// t.equal(res.length, 5); -// t.end(); -// }); -// }); -// }) -// .catch((err) => { -// t.error(err); -// t.end(); -// }); -// }); +test("Does not allow duplicate users when email is already in use", t => { + build().then(() => { + const user = { + username: "Tommy", + email: "tom@iscool.com", + password: "password" + }; + createUser(user).catch(() => { + getUsers().then(res => { + t.equal(res[res.length - 1].username, "Roger", "Database has not changed"); + // t.equal(res.length, 5); + t.end(); + }); + }); + }); +});