Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
Relates #54
  • Loading branch information
Chloeh24 committed Apr 18, 2020
1 parent f9a08b2 commit 772d3ee
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
"jsxBracketSameLine": false,
"arrowParens": "avoid",
"proseWrap": "never"
}
}
3 changes: 2 additions & 1 deletion handlers/examples-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion middleware/error.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions model/examples-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion model/users-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`));
}
Expand Down
104 changes: 39 additions & 65 deletions tests/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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();
});
});
});
});

0 comments on commit 772d3ee

Please sign in to comment.