From a04f395db96252c9b1763dfdfbd05c88bf69f0a3 Mon Sep 17 00:00:00 2001 From: Vic Date: Mon, 23 May 2022 01:15:10 +0200 Subject: [PATCH] added products_spec handler tests --- REQUIREMENTS.md | 2 +- jasmine.json | 3 +- src/handlers/orders.ts | 13 +++--- src/handlers/products.ts | 6 +-- src/handlers/users.ts | 4 +- src/handlers/utils.ts | 3 -- src/models/order.ts | 2 +- src/tests/handlers/products_spec.ts | 64 ++++++++++++++++++++++++++++- src/tests/models/product_spec.ts | 36 ++++++++++------ 9 files changed, 103 insertions(+), 30 deletions(-) diff --git a/REQUIREMENTS.md b/REQUIREMENTS.md index 8f0020e..fbea93e 100644 --- a/REQUIREMENTS.md +++ b/REQUIREMENTS.md @@ -14,7 +14,7 @@ These are the notes from a meeting with the frontend developer that describe wha #### Users - Index [token required] - Show [token required] -- Create N[token required] +- Create [token required] #### Orders - Current Order by user (args: user id)[token required] diff --git a/jasmine.json b/jasmine.json index e935887..803e06e 100644 --- a/jasmine.json +++ b/jasmine.json @@ -1,7 +1,8 @@ { "spec_dir": "src/tests", "spec_files": [ - "**/*_spec.ts" + "models/*_[sS]pec.ts", + "handlers/*_[sS]pec.ts" ], "helpers": [ "helpers/**/*.js" diff --git a/src/handlers/orders.ts b/src/handlers/orders.ts index bb74b57..37a0b97 100644 --- a/src/handlers/orders.ts +++ b/src/handlers/orders.ts @@ -5,8 +5,8 @@ import { verifyAuthToken } from './utils' const orderRoutes = (app: express.Application) => { app.get('/orders', index) app.get('/orders/:id', read) - app.post('/orders', create) - app.post('/orders/:id/products', addProduct) + app.post('/orders', verifyAuthToken, create) + app.post('/orders/:id/products', verifyAuthToken, addProduct) } const store = new OrderStore() @@ -36,8 +36,10 @@ const create = async (req: Request, res: Response) => { try { const orderInfo: Order = { status: req.body.status, - userId: req.body.userId + userId: parseInt(req.body.userId) } + + console.log('orderInfo', orderInfo) const newOrder = await store.create(orderInfo) res.json(newOrder) @@ -50,10 +52,11 @@ const create = async (req: Request, res: Response) => { const addProduct = async (req: Request, res: Response) => { try { const orderProductInfo: OrderProduct = { - quantity: parseInt(req.body.quantity), orderId: parseInt(req.params.id), - productId: req.body.productId + quantity: parseInt(req.body.quantity), + productId: parseInt(req.body.productId) } + console.log(orderProductInfo) const addedProduct = await store.addProduct(orderProductInfo) res.json(addedProduct) } catch (err) { diff --git a/src/handlers/products.ts b/src/handlers/products.ts index 6dc6c8a..0ecc45f 100644 --- a/src/handlers/products.ts +++ b/src/handlers/products.ts @@ -5,9 +5,9 @@ import { verifyAuthToken } from './utils' const productRoutes = (app: express.Application) => { app.get('/products', index) app.get('/products/:id', read) - app.post('/products', create) - app.put('/products/:id', update) - app.delete('/products/:id', destroy) + app.post('/products', verifyAuthToken, create) + app.put('/products/:id', verifyAuthToken, update) + app.delete('/products/:id', verifyAuthToken, destroy) } const store = new ProductStore() diff --git a/src/handlers/users.ts b/src/handlers/users.ts index 53e3d01..d090ab5 100644 --- a/src/handlers/users.ts +++ b/src/handlers/users.ts @@ -4,8 +4,8 @@ import { verifyAuthToken, verifyUserToken} from './utils' const userRoutes = (app: express.Application) => { - app.get('/users', index) - app.get('/users/:id', read) + app.get('/users', verifyAuthToken, index) + app.get('/users/:id', verifyAuthToken, read) app.post('/users', create) app.put('/users/:id', verifyAuthToken, update) app.delete('/users/:id', verifyAuthToken, destroy) diff --git a/src/handlers/utils.ts b/src/handlers/utils.ts index 8af9ffc..66319b2 100644 --- a/src/handlers/utils.ts +++ b/src/handlers/utils.ts @@ -13,10 +13,7 @@ export const verifyAuthToken = (req: Request, res: Response, next: NextFunction) } try { - console.log("req.headers.authorization", req.headers.authorization) - console.log(typeof(req.headers.authorization)) const token = req.headers.authorization.split(' ')[1] - console.log("token", token) jwt.verify(token, SECRET) next() } catch (err) { diff --git a/src/models/order.ts b/src/models/order.ts index a25e243..48ec6e4 100644 --- a/src/models/order.ts +++ b/src/models/order.ts @@ -71,7 +71,7 @@ export class OrderStore { try { const conn = await client.connect() - const sql = 'INSERT INTO order_products (quantity, order_id, product_id) VALUES ($1, $2) RETURNING *' + const sql = 'INSERT INTO order_products (quantity, order_id, product_id) VALUES ($1, $2, $3) RETURNING *' const result = await conn.query(sql, [o.quantity, o.orderId, o.productId]) const order = result.rows[0] diff --git a/src/tests/handlers/products_spec.ts b/src/tests/handlers/products_spec.ts index a2aee43..6673a12 100644 --- a/src/tests/handlers/products_spec.ts +++ b/src/tests/handlers/products_spec.ts @@ -1 +1,63 @@ -import sueprtest from "supertest" \ No newline at end of file +import supertest from "supertest" +import app from "../../server" +import { Product } from '../../models/product' + + + +const token = process.env.TOKEN_SECRET_TEST as string +const request = supertest(app); + +const testProduct: Product = { + name: "metro", + price: 10 +} + +const updatedProduct: Product = { + name: "1984", + price: 5 +} + + + +describe("Product handler", () => { + + it('Should create a new product', async () => { + const response = await request + .post("/products") + .auth(token, { type: "bearer" }) + .send(testProduct); + + expect(response.status).toBe(200); + }) + + it('Should index products', async () => { + const response = await request + .get("/products") + + expect(response.status).toBe(200); + }) + + it('Should read product with id', async () => { + const response = await request + .get("/products/1") + + expect(response.status).toBe(200); + }) + + it('Should update product with id', async () => { + const response = await request + .put("/products/1") + .auth(token, { type: 'bearer'}) + .send(updatedProduct) + + expect(response.status).toBe(200); + }) + + it('Should delete product with id', async () => { + const response = await request + .delete("/products/1") + .auth(token, { type: 'bearer'}) + + expect(response.status).toBe(200); + }) +}) \ No newline at end of file diff --git a/src/tests/models/product_spec.ts b/src/tests/models/product_spec.ts index 95b6bf4..f88ca06 100644 --- a/src/tests/models/product_spec.ts +++ b/src/tests/models/product_spec.ts @@ -2,25 +2,20 @@ import { Product, ProductStore } from '../../models/product' const store = new ProductStore() -describe("Products store Model", () => { - it('should have an index method', () => { +describe("Product model", () => { + it('Should have an index method', () => { expect(store.index).toBeDefined(); }); - it('index menthod should return a list of products', async () => { - const result = await store.index(); - expect(result).toEqual([]); - }); - - it('should have a create method', () => { + it('Should have a create method', () => { expect(store.index).toBeDefined; }); - it('should have a update method', () => { + it('Should have a update method', () => { expect(store.index).toBeDefined; }); - it('create method should add a product', async () => { + it('Create method should add a product', async () => { const result = await store.create({ name: '1984', price: 5, @@ -33,7 +28,7 @@ describe("Products store Model", () => { }); }); - it('index method should return a list of products', async () => { + it('Index method should return a list of products', async () => { const result = await store.index(); expect(result).toEqual([{ id: 1, @@ -43,7 +38,7 @@ describe("Products store Model", () => { }); - it('show method should return a product', async () => { + it('Show method should return a product', async () => { const result = await store.read(1); expect(result).toEqual({ id: 1, @@ -53,7 +48,22 @@ describe("Products store Model", () => { }); - it('delete method should remove the product', async () => { + it('Update method should update a product', async () => { + const updatedProduct: Product = { + id: 1, + name: 'The Dark Tower', + price: 7 + } + const result = await store.update(updatedProduct); + expect(result).toEqual({ + id: 1, + name: 'The Dark Tower', + price: 7, + }); + + }); + + it('Delete method should remove the product', async () => { store.delete(1); const result = await store.index()