Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import express from 'express';
- import * as ProductController from '../controllers/product';
- import checkToken from '../middlewares/checkToken';
- import getUser from '../middlewares/getUser';
- const router = express.Router();
- router.post('/products', checkToken, getUser, ProductController.create);
- router.put('/products', checkToken, getUser, ProductController.update);
- router.get('/products/:categoryUrl', ProductController.getProductsByCategoryUrl);
- router.get('/products/:categoryUrl/:productUrl', checkToken, getUser, ProductController.getProductByUrl);
- router.get('/products', ProductController.getAll);
- export default router;
- import Product from '../models/product';
- import Category from '../models/category';
- import * as PageStatusService from '../services/pageStatusService';
- import * as CategoryService from '../services/categoryService';
- import urlCheck from '../helpres/check-is-valid-url';
- export async function create(req, res, next) {
- const productData = req.body;
- const userId = req.user._id;
- let product;
- let isStatusExist;
- let isCategoryExist;
- productData.user = userId;
- if (!urlCheck(productData.url)) {
- return next({
- status: 400,
- message: 'Invalid url value'
- });
- }
- try {
- isStatusExist = await PageStatusService.isExistStatus(productData.status);
- } catch ({ message }) {
- return next({
- status: 400,
- message
- });
- }
- if (!isStatusExist) {
- return next({
- status: 400,
- message: 'Invalid status value'
- });
- }
- // TODO: add check for category, product belong to last level category
- // if categories with parent category productData.category is empty
- try {
- isCategoryExist = await CategoryService.isExistCategoryById(productData.category);
- } catch ({ message }) {
- return next({
- status: 400,
- message
- });
- }
- if (!isCategoryExist) {
- return next({
- status: 400,
- message: 'Invalid category value'
- });
- }
- try {
- product = await Product.create(productData);
- } catch ({ message }) {
- return next({
- status: 400,
- message
- });
- }
- res.json(product);
- }
- ...
- var should = require('should');
- var assert = require('assert');
- var request = require('supertest');
- var mongoose = require('mongoose');
- var winston = require('winston');
- var config = require('./config-debug');
- describe('Account', function() {
- it('should return error trying to save duplicate username', function(done) {
- var profile = {
- username: 'vgheri',
- password: 'test',
- firstName: 'Valerio',
- lastName: 'Gheri'
- };
- // once we have specified the info we want to send to the server via POST verb,
- // we need to actually perform the action on the resource, in this case we want to
- // POST on /api/profiles and we want to send some info
- // We do this using the request object, requiring supertest!
- request(url)
- .post('/api/profiles')
- .send(profile)
- // end handles the response
- .end(function(err, res) {
- if (err) {
- throw err;
- }
- // this is should.js syntax, very clear
- res.should.have.status(400);
- done();
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement