<commit_before>'use strict';

const expect = require('chai').expect;
const request = require('superagent');
const Promise = require('bluebird');
const Customer = require('../model/customer.js');
const CartOrder = require('../model/cart-order.js');
const CartProduct = require('../model/cart.js');

require('../server.js');

const url = `http://localhost:${process.env.PORT}`;

const sampleCustomer = {
  name: 'Test user',
  username: 'Test username',
  password: '<PASSWORD>',
  email: '<EMAIL>',
  address: 'Test address'
};

const sampleOrder = {
  products: []
};

const sampleProduct = {
  name: 'Test product',
  desc: 'Test description',
  quantity: 100
};

describe('Cart Product Routes', function() {
  after(done => {
    Promise.all([
      Customer.remove({}),
      CartOrder.remove({}),
      CartProduct.remove({})
    ])
    .then(() => done())
    .catch(done);
  });

  describe('POST: /api/orders/:cartOrderID/cart', () => {
    before(done => {
      new Customer(sampleCustomer).save()
      .then(customer => {
        this.tempCustomer = customer;
        sampleOrder.customerID = customer._id;
        return Customer.addCartOrder(customer._id, sampleOrder);
      })
      .then(order => {
        this.tempOrder = order;
        sampleProduct.orderID = order._id;
        done();
      })
      .catch(done);
    });

    describe('With a valid ID and body', () => {
      it('should return a product', done => {
        request
        .post(`${url}/api/orders/${this.tempOrder._id}/cart`)
        .send(sampleProduct)
        .end((err, response) => {
          if (err) return done(err);
          this.tempProduct = response.body;
          expect(response.status).to.equal(201);
          expect(response.body.name).to.equal(sampleProduct.name);
          expect(response.body.desc).to.equal(sampleProduct.desc);
          expect(response.body.quantity).to.equal(sampleProduct.quantity);
          expect(response.body.cartOrderID).to.equal(this.tempOrder._id.toString());
          done();
        });
      });
    });

    describe('With an invalid ID', () => {
      it('should return a 404 status', done => {
        request
        .post(`${url}/api/orders/69`)
        .send(sampleProduct)
        .end((err, response) => {
          expect(err).to.be.an('error');
          expect(response.status).to.equal(404);
          done();
        });
      });
    });

    describe('With a valid ID, but invalid body', () => {
      it('should return a 400 status', done => {
        request
        .post(`${url}/api/orders/${this.tempOrder._id}/cart`)
        .end((err, response) => {
          expect(err).to.be.an('error');
          expect(response.status).to.equal(400);
          done();
        });
      });
    });
  });

  describe('GET: /api/products/:productID', () => {
    describe('With a valid ID', () => {
      it('should return a product', done => {
        request
        .get(`${url}/api/products/${this.tempProduct._id}`)
        .end((err, response) => {
          if (err) return done(err);
          expect(response.status).to.equal(200);
          expect(response.body.name).to.equal(this.tempProduct.name);
          expect(response.body.desc).to.equal(this.tempProduct.desc);
          expect(response.body.quantity).to.equal(this.tempProduct.quantity);
          expect(response.body.cartOrderID).to.equal(this.tempOrder._id.toString());
          done();
        });
      });
    });

    describe('With an invalid ID', () => {
      it('should return a 404 error', done => {
        request
        .get(`${url}/api/products/69`)
        .end((err, response) => {
          expect(err).to.be.an('error');
          expect(response.status).to.equal(404);
          done();
        });
      });
    });
  });

  describe('PUT: /api/products/:productID', () => {
    describe('With a valid ID and body', () => {
      it('should return an updated product', done => {
        request
        .put(`${url}/api/products/${this.tempProduct._id}`)
        .send({name: 'New name'})
        .end((err, response) => {
          if (err) return done(err);
          expect(response.status).to.equal(200);
          expect(response.body.name).to.equal('New name');
          expect(response.body.desc).to.equal(this.tempProduct.desc);
          expect(response.body.quantity).to.equal(this.tempProduct.quantity);
          expect(response.body.cartOrderID).to.equal(this.tempProduct.cartOrderID.toString());
          done();
        });
      });
    });

    describe('With an invalid ID', () => {
      it('should return a 404 error', done => {
        request
        .put(`${url}/api/products/69`)
        .send({name: 'New name'})
        .end((err, response) => {
          expect(err).to.be.an('error');
          expect(response.status).to.equal(404);
          done();
        });
      });
    });

    describe('With a valid ID, but invalid body', () => {
      it('should return a 400 error', done => {
        request
        .put(`${url}/api/products/${this.tempProduct._id}`)
        .end((err, response) => {
          expect(err).to.be.an('error');
          expect(response.status).to.equal(400);
          done();
        });
      });
    });
  });
});<commit_msg>Added DELETE test with valid ID.<commit_after>'use strict';

const expect = require('chai').expect;
const request = require('superagent');
const Promise = require('bluebird');
const Customer = require('../model/customer.js');
const CartOrder = require('../model/cart-order.js');
const CartProduct = require('../model/cart.js');

require('../server.js');

const url = `http://localhost:${process.env.PORT}`;

const sampleCustomer = {
  name: '<NAME>',
  username: 'Test username',
  password: '<PASSWORD>',
  email: '<EMAIL>',
  address: 'Test address'
};

const sampleOrder = {
  products: []
};

const sampleProduct = {
  name: 'Test product',
  desc: 'Test description',
  quantity: 100
};

describe('Cart Product Routes', function() {
  after(done => {
    Promise.all([
      Customer.remove({}),
      CartOrder.remove({}),
      CartProduct.remove({})
    ])
    .then(() => done())
    .catch(done);
  });

  describe('POST: /api/orders/:cartOrderID/cart', () => {
    before(done => {
      new Customer(sampleCustomer).save()
      .then(customer => {
        this.tempCustomer = customer;
        sampleOrder.customerID = customer._id;
        return Customer.addCartOrder(customer._id, sampleOrder);
      })
      .then(order => {
        this.tempOrder = order;
        sampleProduct.orderID = order._id;
        done();
      })
      .catch(done);
    });

    describe('With a valid ID and body', () => {
      it('should return a product', done => {
        request
        .post(`${url}/api/orders/${this.tempOrder._id}/cart`)
        .send(sampleProduct)
        .end((err, response) => {
          if (err) return done(err);
          this.tempProduct = response.body;
          expect(response.status).to.equal(201);
          expect(response.body.name).to.equal(sampleProduct.name);
          expect(response.body.desc).to.equal(sampleProduct.desc);
          expect(response.body.quantity).to.equal(sampleProduct.quantity);
          expect(response.body.cartOrderID).to.equal(this.tempOrder._id.toString());
          done();
        });
      });
    });

    describe('With an invalid ID', () => {
      it('should return a 404 status', done => {
        request
        .post(`${url}/api/orders/69`)
        .send(sampleProduct)
        .end((err, response) => {
          expect(err).to.be.an('error');
          expect(response.status).to.equal(404);
          done();
        });
      });
    });

    describe('With a valid ID, but invalid body', () => {
      it('should return a 400 status', done => {
        request
        .post(`${url}/api/orders/${this.tempOrder._id}/cart`)
        .end((err, response) => {
          expect(err).to.be.an('error');
          expect(response.status).to.equal(400);
          done();
        });
      });
    });
  });

  describe('GET: /api/products/:productID', () => {
    describe('With a valid ID', () => {
      it('should return a product', done => {
        request
        .get(`${url}/api/products/${this.tempProduct._id}`)
        .end((err, response) => {
          if (err) return done(err);
          expect(response.status).to.equal(200);
          expect(response.body.name).to.equal(this.tempProduct.name);
          expect(response.body.desc).to.equal(this.tempProduct.desc);
          expect(response.body.quantity).to.equal(this.tempProduct.quantity);
          expect(response.body.cartOrderID).to.equal(this.tempOrder._id.toString());
          done();
        });
      });
    });

    describe('With an invalid ID', () => {
      it('should return a 404 error', done => {
        request
        .get(`${url}/api/products/69`)
        .end((err, response) => {
          expect(err).to.be.an('error');
          expect(response.status).to.equal(404);
          done();
        });
      });
    });
  });

  describe('PUT: /api/products/:productID', () => {
    describe('With a valid ID and body', () => {
      it('should return an updated product', done => {
        request
        .put(`${url}/api/products/${this.tempProduct._id}`)
        .send({name: 'New name'})
        .end((err, response) => {
          if (err) return done(err);
          expect(response.status).to.equal(200);
          expect(response.body.name).to.equal('New name');
          expect(response.body.desc).to.equal(this.tempProduct.desc);
          expect(response.body.quantity).to.equal(this.tempProduct.quantity);
          expect(response.body.cartOrderID).to.equal(this.tempProduct.cartOrderID.toString());
          done();
        });
      });
    });

    describe('With an invalid ID', () => {
      it('should return a 404 error', done => {
        request
        .put(`${url}/api/products/69`)
        .send({name: 'New name'})
        .end((err, response) => {
          expect(err).to.be.an('error');
          expect(response.status).to.equal(404);
          done();
        });
      });
    });

    describe('With a valid ID, but invalid body', () => {
      it('should return a 400 error', done => {
        request
        .put(`${url}/api/products/${this.tempProduct._id}`)
        .end((err, response) => {
          expect(err).to.be.an('error');
          expect(response.status).to.equal(400);
          done();
        });
      });
    });
  });

  describe('DELETE: /api/products/:productID', () => {
    describe('With a valid ID', () => {
      it('should return a 204 status', done => {
        request
        .delete(`${url}/api/products/${this.tempProduct._id}`)
        .end((err, response) => {
          if (err) return done(err);
          expect(response.status).to.equal(204);
          done();
        });
      });
    });
  });
});
{'fraction_non_alphanumeric': 0.18823421439060206, 'fraction_numerical': 0.009361233480176211, 'mean_word_length': 2.247020262216925, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 76, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1646403', 'n_tokens_mistral': 3317, 'n_tokens_neox': 3229, 'n_words': 554}