Skip to content

Eval submission#171

Open
colormethanh wants to merge 11 commits intoprojectshft:masterfrom
colormethanh:master
Open

Eval submission#171
colormethanh wants to merge 11 commits intoprojectshft:masterfrom
colormethanh:master

Conversation

@colormethanh
Copy link

No description provided.

const getUserCart = (username, userStore) => {
// check if user exist in userStore
const user = userStore.find((user) => user.login.username === username);
if (!user || !user.cart) return {isFound: false, err: 500, message: errorMessages[500]}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check your code alignment and spacing

Suggested change
if (!user || !user.cart) return {isFound: false, err: 500, message: errorMessages[500]}
if (!user || !user.cart) return { isFound: false, err: 500, message: errorMessages[500] }


const getUserCart = (username, userStore) => {
// check if user exist in userStore
const user = userStore.find((user) => user.login.username === username);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you dont need the brackets.

Suggested change
const user = userStore.find((user) => user.login.username === username);
const user = userStore.find(user => user.login.username === username);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will actually crash your server if for some reason your store has no users, protect yourself with optional

Suggested change
const user = userStore.find((user) => user.login.username === username);
const user = userStore.find((user) => user?.login?.username === username);

const user = userStore.find((user) => user.login.username === username);
if (!user || !user.cart) return {isFound: false, err: 500, message: errorMessages[500]}

return {isFound: true, cart: user.cart};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spacing


const user = userStore.find((user) => user.login.username === username && user.login.password === password);

if (!user || !username || !password) return {loginSuccess: false, err: 401, message: errorMessages[401]}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by this point consider using a lint rule for the spacing

it("should GET all brands", (done) => {
sendChaiGet(server, "/api/brands", done, (err, res, done) => {
res.should.have.status(200)
res.body.should.be.an("array")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not really testing anything, if you change your actual code to return an empty array, this test wont fail and you will be introducing a severe production bug. Always test agains mocks to make sure you are returning the proper data.

Comment on lines +32 to +37
res.body[0].should.have.property("id");
res.body[0].should.have.property("categoryId", "1");
res.body[0].should.have.property("name");
res.body[0].should.have.property("description");
res.body[0].should.have.property("price");
res.body[0].should.have.property("imageUrls");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above, here its better but still can let pass some bugs.

Comment on lines +117 to +121
const token = req.header("token");

// Validate token
const tokenValidation = validateToken(token, ACCESS_TOKENS);
if (!tokenValidation.isValid) return res.status(tokenValidation.err).send(tokenValidation.message);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DRY, you are repeating the same over and over in your authenticated apis.
For an idea, check step 5 in here https://medium.com/@prashantramnyc/authenticate-rest-apis-in-node-js-using-jwt-json-web-tokens-f0e97669aad3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants