added products_spec handler tests
This commit is contained in:
parent
372a5ee059
commit
a04f395db9
@ -14,7 +14,7 @@ These are the notes from a meeting with the frontend developer that describe wha
|
|||||||
#### Users
|
#### Users
|
||||||
- Index [token required]
|
- Index [token required]
|
||||||
- Show [token required]
|
- Show [token required]
|
||||||
- Create N[token required]
|
- Create [token required]
|
||||||
|
|
||||||
#### Orders
|
#### Orders
|
||||||
- Current Order by user (args: user id)[token required]
|
- Current Order by user (args: user id)[token required]
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
{
|
{
|
||||||
"spec_dir": "src/tests",
|
"spec_dir": "src/tests",
|
||||||
"spec_files": [
|
"spec_files": [
|
||||||
"**/*_spec.ts"
|
"models/*_[sS]pec.ts",
|
||||||
|
"handlers/*_[sS]pec.ts"
|
||||||
],
|
],
|
||||||
"helpers": [
|
"helpers": [
|
||||||
"helpers/**/*.js"
|
"helpers/**/*.js"
|
||||||
|
@ -5,8 +5,8 @@ import { verifyAuthToken } from './utils'
|
|||||||
const orderRoutes = (app: express.Application) => {
|
const orderRoutes = (app: express.Application) => {
|
||||||
app.get('/orders', index)
|
app.get('/orders', index)
|
||||||
app.get('/orders/:id', read)
|
app.get('/orders/:id', read)
|
||||||
app.post('/orders', create)
|
app.post('/orders', verifyAuthToken, create)
|
||||||
app.post('/orders/:id/products', addProduct)
|
app.post('/orders/:id/products', verifyAuthToken, addProduct)
|
||||||
}
|
}
|
||||||
|
|
||||||
const store = new OrderStore()
|
const store = new OrderStore()
|
||||||
@ -36,8 +36,10 @@ const create = async (req: Request, res: Response) => {
|
|||||||
try {
|
try {
|
||||||
const orderInfo: Order = {
|
const orderInfo: Order = {
|
||||||
status: req.body.status,
|
status: req.body.status,
|
||||||
userId: req.body.userId
|
userId: parseInt(req.body.userId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('orderInfo', orderInfo)
|
||||||
|
|
||||||
const newOrder = await store.create(orderInfo)
|
const newOrder = await store.create(orderInfo)
|
||||||
res.json(newOrder)
|
res.json(newOrder)
|
||||||
@ -50,10 +52,11 @@ const create = async (req: Request, res: Response) => {
|
|||||||
const addProduct = async (req: Request, res: Response) => {
|
const addProduct = async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
const orderProductInfo: OrderProduct = {
|
const orderProductInfo: OrderProduct = {
|
||||||
quantity: parseInt(req.body.quantity),
|
|
||||||
orderId: parseInt(req.params.id),
|
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)
|
const addedProduct = await store.addProduct(orderProductInfo)
|
||||||
res.json(addedProduct)
|
res.json(addedProduct)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -5,9 +5,9 @@ import { verifyAuthToken } from './utils'
|
|||||||
const productRoutes = (app: express.Application) => {
|
const productRoutes = (app: express.Application) => {
|
||||||
app.get('/products', index)
|
app.get('/products', index)
|
||||||
app.get('/products/:id', read)
|
app.get('/products/:id', read)
|
||||||
app.post('/products', create)
|
app.post('/products', verifyAuthToken, create)
|
||||||
app.put('/products/:id', update)
|
app.put('/products/:id', verifyAuthToken, update)
|
||||||
app.delete('/products/:id', destroy)
|
app.delete('/products/:id', verifyAuthToken, destroy)
|
||||||
}
|
}
|
||||||
|
|
||||||
const store = new ProductStore()
|
const store = new ProductStore()
|
||||||
|
@ -4,8 +4,8 @@ import { verifyAuthToken, verifyUserToken} from './utils'
|
|||||||
|
|
||||||
|
|
||||||
const userRoutes = (app: express.Application) => {
|
const userRoutes = (app: express.Application) => {
|
||||||
app.get('/users', index)
|
app.get('/users', verifyAuthToken, index)
|
||||||
app.get('/users/:id', read)
|
app.get('/users/:id', verifyAuthToken, read)
|
||||||
app.post('/users', create)
|
app.post('/users', create)
|
||||||
app.put('/users/:id', verifyAuthToken, update)
|
app.put('/users/:id', verifyAuthToken, update)
|
||||||
app.delete('/users/:id', verifyAuthToken, destroy)
|
app.delete('/users/:id', verifyAuthToken, destroy)
|
||||||
|
@ -13,10 +13,7 @@ export const verifyAuthToken = (req: Request, res: Response, next: NextFunction)
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log("req.headers.authorization", req.headers.authorization)
|
|
||||||
console.log(typeof(req.headers.authorization))
|
|
||||||
const token = req.headers.authorization.split(' ')[1]
|
const token = req.headers.authorization.split(' ')[1]
|
||||||
console.log("token", token)
|
|
||||||
jwt.verify(token, SECRET)
|
jwt.verify(token, SECRET)
|
||||||
next()
|
next()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -71,7 +71,7 @@ export class OrderStore {
|
|||||||
try {
|
try {
|
||||||
|
|
||||||
const conn = await client.connect()
|
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 result = await conn.query(sql, [o.quantity, o.orderId, o.productId])
|
||||||
const order = result.rows[0]
|
const order = result.rows[0]
|
||||||
|
@ -1 +1,63 @@
|
|||||||
import sueprtest from "supertest"
|
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);
|
||||||
|
})
|
||||||
|
})
|
@ -2,25 +2,20 @@ import { Product, ProductStore } from '../../models/product'
|
|||||||
|
|
||||||
const store = new ProductStore()
|
const store = new ProductStore()
|
||||||
|
|
||||||
describe("Products store Model", () => {
|
describe("Product model", () => {
|
||||||
it('should have an index method', () => {
|
it('Should have an index method', () => {
|
||||||
expect(store.index).toBeDefined();
|
expect(store.index).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('index menthod should return a list of products', async () => {
|
it('Should have a create method', () => {
|
||||||
const result = await store.index();
|
|
||||||
expect(result).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should have a create method', () => {
|
|
||||||
expect(store.index).toBeDefined;
|
expect(store.index).toBeDefined;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should have a update method', () => {
|
it('Should have a update method', () => {
|
||||||
expect(store.index).toBeDefined;
|
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({
|
const result = await store.create({
|
||||||
name: '1984',
|
name: '1984',
|
||||||
price: 5,
|
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();
|
const result = await store.index();
|
||||||
expect(result).toEqual([{
|
expect(result).toEqual([{
|
||||||
id: 1,
|
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);
|
const result = await store.read(1);
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
id: 1,
|
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);
|
store.delete(1);
|
||||||
const result = await store.index()
|
const result = await store.index()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user