Skip to content

nsLittle's Node Eval#169

Open
nsLittle wants to merge 34 commits into
projectshft:masterfrom
nsLittle:master
Open

nsLittle's Node Eval#169
nsLittle wants to merge 34 commits into
projectshft:masterfrom
nsLittle:master

Conversation

@nsLittle
Copy link
Copy Markdown

@nsLittle nsLittle commented Aug 2, 2024

post /{name} is still wonky

Comment thread swagger.yaml Outdated
description: Brand names of sunglasses
get:
summary: GET brand names
description: GET brand names of sunglasses
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

missing a tag key to group apis together under a same category: brands, products, login/user, etc

Comment thread swagger.yaml
name: Mutsumi Hata
url: https://github.com/nsLittle
paths:
/:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this is not really an api, the home page is a FE concept and not a BE one.

Comment thread swagger.yaml
summary: GET brand names
description: GET brand names of sunglasses
operationId: get.brands
responses:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

missing a response schema reference, the whole point of swagger is to see what API needs and returns + description, etc. The real value is in the schemas!

Comment thread swagger.yaml
Comment on lines +35 to +39
- name: name
in: path
required: true
schema:
type: string
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

GREAT

Comment thread swagger.yaml
content:
application/json:
examples:
Oakley:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LOVE it! this should have been in every api, but why only in this one?

Comment thread app/server.js Outdated
};

const newItem= {
product: req.body.product || '',
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

check your code alignment

Comment thread app/server.js Outdated
const userCart = user.cart;
res.status(200).json(userCart);
} else {
res.status(401).send('Unauthorized');
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this is not accurate, if you didnt find the user, means that the user was not found (400), not that the request is unauthorized

Comment thread app/server.js Outdated
Comment on lines +40 to +47
const authenticate = (req, res, next) => {
const { username, password } = req.body;

if (username || password) {
return res.status(400).send(`Who are you?`);
};
next();
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

code alignment

Comment thread app/server.js Outdated
const authenticate = (req, res, next) => {
const { username, password } = req.body;

if (username || password) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

if there is a username or a password, then the request is invalid?

Comment thread app/server.js Outdated
});

// Authentication middleware
const authenticate = (req, res, next) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this is not really authenticating.
You need to basically accessToken that was returned in the /login api.
The login api needs to check the sent user & password and then generate that token that is checked on each api

Comment thread app/server.js

// BASIC MIDDLEWARE
app.use((req, res, next) => {
console.log('Basic Middleware Stuff...');
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

need this console log? use comment instead.

Comment thread app/server.js
Comment on lines +169 to +170
console.log('AUTHENTICATION');
console.log('AuthHeader: ', authHeader);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

clean up your code please

Comment thread app/server.js

if (authHeader) {
const token = authHeader.split(' ')[1];
console.log('AuthHeader Deconstructed: ', token);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

no need

Comment thread app/server.js
};

// AUTHENTICATED ROUTES
app.get('/users', authenticateJWT, (req, res) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

what is the purpose of this api?

Comment thread app/server.js
res.status(200).json({ users: userNames });
});

app.get('/:name', authenticateJWT, (req, res) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You defined well the cart routes in the swagger file but this is not matching your server.js file. Here the url is not containing the resource (cart). Make sure to always include it!

Comment thread app/server.js
Comment on lines +234 to +235
const userName = req.params.name.toLowerCase();
const user = users.find(user => user.name.first.toLowerCase() === userName);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

you are repeating this validation in almost every api, maybe can be part of the authenticationToken?

Comment thread test/server.test.js
Comment on lines +71 to +86
expect(res).to.have.status(200);
expect(res.body).to.be.an('object');
expect(res.body).to.have.property('Burberry');

const products = res.body['Burberry'];

expect(products).to.be.an('array');

products.forEach(product => {
expect(product).to.have.property('id');
expect(product).to.have.property('categoryId');
expect(product).to.have.property('name');
expect(product).to.have.property('description');
expect(product).to.have.property('price');
expect(product).to.have.property('imageUrls');
})
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

when this test fails, it will be VERY hard to find why or what happened

Comment thread test/server.test.js
.end((err, res) => {
if (err) return done(err);
expect(res).to.have.status(200);
expect(res.body).to.be.an('object');
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this is not really valuable for testing.
Becomes redundant for the next steps you do in the test

Comment thread test/server.test.js
Comment on lines +124 to +128
expect(productDetails).to.be.an('object');
expect(productDetails).to.have.property('name');
expect(productDetails).to.have.property('description');
expect(productDetails).to.have.property('price');
expect(productDetails).to.have.property('imageUrls');
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

instead of many expect, do one comparing productDetails to an mock object

Comment thread test/server.test.js
expect(userCart).to.have.property('items').that.is.an('array');
expect(userCart.items[0]).to.have.property('product');
expect(userCart.items[1]).to.have.property('quantity');
expect(userCart).to.have.property('total').that.is.a('number');
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

why not testing the actual number so you test the logic you did to update the total?

Suggested change
expect(userCart).to.have.property('total').that.is.a('number');
expect(userCart).to.have.property('total').that.equals(sumNumber);

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