Advertisement
Guest User

Untitled

a guest
Mar 9th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require('should');
  2.  
  3. const request = require('supertest');
  4. const app = require('./helpers/mock.app');
  5. const config = require('../src/config/config');
  6. var token = '';
  7. var aCard = require('./helpers/newCard');
  8. var newCard = JSON.parse(JSON.stringify(aCard));
  9. var updatedCard = JSON.parse(JSON.stringify(newCard));
  10. updatedCard.idName = (JSON.parse(JSON.stringify(updatedCard.name.toLowerCase()))).replace(/ /g, '-').replace(/\./g, '');
  11.  
  12. describe('Cards.', function () {
  13.   this.timeout(5000);
  14.  
  15.   describe('/api/cards', () => {
  16.     before((done) => {
  17.         request(app)
  18.           .post('/api/authenticate')
  19.           .set('Accept', 'application/x-www-form-urlencoded')
  20.           .send({ username: config.ADMIN_USERNAME, password: config.ADMIN_PASSWORD})
  21.           .end((err, res) => {
  22.             token = res.text;
  23.             done();
  24.           });
  25.     });
  26.     describe('POST /', () => {
  27.       it('should create a new Card', done => {
  28.         request(app)
  29.           .post('/api/cards')
  30.           .set('Accept', 'application/json')
  31.           .set('Authorization', token)
  32.           .send(newCard)
  33.           .expect('Content-Type', /json/)
  34.           .end((err, res) => {
  35.             res.status.should.eql(201);
  36.             newCard.idName = res.body.idName;
  37.             done();
  38.           });
  39.       });
  40.  
  41.       it('should try to create a duplicated card', done => {
  42.         request(app)
  43.           .post('/api/cards')
  44.           .set('Accept', 'application/json')
  45.           .set('Authorization', token)
  46.           .send(newCard)
  47.           .end((err, res) => {
  48.             res.status.should.eql(500);
  49.             done();
  50.           });
  51.       });
  52.  
  53.       it('should try to create a new Card with no token', done => {
  54.         request(app)
  55.           .post('/api/cards')
  56.           .set('Accept', 'application/json')
  57.           .send(newCard)
  58.           .expect('Content-Type', /json/)
  59.           .end((err, res) => {
  60.             res.status.should.eql(401);
  61.             done();
  62.           });
  63.       });
  64.  
  65.       it('should try to create a new Card with an invalid token', done => {
  66.         request(app)
  67.           .post('/api/cards')
  68.           .set('Accept', 'application/json')
  69.           .set('Authorization', '111111111111111111111111111111111111.111111111111111111111111111111111111111111111111111111111111111111111111.1111111111111111111111111111111111111111111')
  70.           .send(newCard)
  71.           .expect('Content-Type', /json/)
  72.           .end((err, res) => {
  73.             res.status.should.eql(401);
  74.             done();
  75.           });
  76.       });
  77.     });
  78.  
  79.     describe('GET /', () => {
  80.       it('should find all cards', done => {
  81.         request(app)
  82.           .get('/api/cards')
  83.           .set('Authorization', token)
  84.           .expect('Content-Type', /json/)
  85.           .end((err, res) => {
  86.             newCard._id = res.body[0]._id;
  87.             res.body[0].name.should.eql(newCard.name);
  88.             res.body[0].rarity.should.eql(newCard.rarity);
  89.             res.body[0].type.should.eql(newCard.type);
  90.             res.body[0].description.should.eql(newCard.description);
  91.             res.body[0].arena.should.eql(newCard.arena);
  92.             res.body[0].elixirCost.should.eql(newCard.elixirCost);
  93.             res.status.should.eql(200);
  94.             done();
  95.           });
  96.       });
  97.  
  98.       it('should find one card by id', done => {
  99.         request(app)
  100.           .get('/api/cards/' + newCard._id)
  101.           .set('Authorization', token)
  102.           .expect('Content-Type', /json/)
  103.           .end((err, res) => {
  104.             res.body.name.should.eql(newCard.name);
  105.             res.body.rarity.should.eql(newCard.rarity);
  106.             res.body.type.should.eql(newCard.type);
  107.             res.body.description.should.eql(newCard.description);
  108.             res.body.arena.should.eql(newCard.arena);
  109.             res.body.elixirCost.should.eql(newCard.elixirCost);
  110.             res.status.should.eql(200);
  111.             done();
  112.           });
  113.       });
  114.  
  115.       it('should find one card by idName', done => {
  116.         request(app)
  117.           .get('/api/cards/' + newCard.idName)
  118.           .set('Authorization', token)
  119.           .expect('Content-Type', /json/)
  120.           .end((err, res) => {
  121.             res.body.name.should.eql(newCard.name);
  122.             res.body.rarity.should.eql(newCard.rarity);
  123.             res.body.type.should.eql(newCard.type);
  124.             res.body.description.should.eql(newCard.description);
  125.             res.body.arena.should.eql(newCard.arena);
  126.             res.body.elixirCost.should.eql(newCard.elixirCost);
  127.             res.status.should.eql(200);
  128.             done();
  129.           });
  130.       });
  131.  
  132.       it('should not find one card by id', done => {
  133.         request(app)
  134.           .get('/api/cards/111111111111111111111111')
  135.           .set('Authorization', token)
  136.           .expect('Content-Type', /json/)
  137.           .end((err, res) => {
  138.             res.status.should.eql(404);
  139.             done();
  140.           });
  141.       });
  142.  
  143.       it('should not find one card by idName', done => {
  144.         request(app)
  145.           .get('/api/cards/some-name-here')
  146.           .set('Authorization', token)
  147.           .expect('Content-Type', /json/)
  148.           .end((err, res) => {
  149.             res.status.should.eql(404);
  150.             done();
  151.           });
  152.       });
  153.  
  154.       it('should find card by type', done => {
  155.         request(app)
  156.           .get('/api/cards?rarity='  + newCard.rarity)
  157.           .set('Authorization', token)
  158.           .expect('Content-Type', /json/)
  159.           .end((err, res) => {
  160.             res.body[0].name.should.eql(newCard.name);
  161.             res.body[0].rarity.should.eql(newCard.rarity);
  162.             res.body[0].type.should.eql(newCard.type);
  163.             res.body[0].description.should.eql(newCard.description);
  164.             res.body[0].arena.should.eql(newCard.arena);
  165.             res.body[0].elixirCost.should.eql(newCard.elixirCost);
  166.             res.status.should.eql(200);
  167.             done();
  168.           });
  169.       });
  170.     });
  171.  
  172.     describe('PUT /', () => {
  173.       it('should update a card', done => {
  174.         updatedCard.name = 'New Name';
  175.         request(app)
  176.           .put('/api/cards/' + newCard._id)
  177.           .set('Accept', 'application/json')
  178.           .set('Authorization', token)
  179.           .send(updatedCard)
  180.           .expect('Content-Type', /json/)
  181.           .end((err, res) => {
  182.             newCard._id = res.body._id;
  183.             res.body.name.should.eql(updatedCard.name);
  184.             res.body.rarity.should.eql(newCard.rarity);
  185.             res.body.type.should.eql(newCard.type);
  186.             res.body.description.should.eql(newCard.description);
  187.             res.body.arena.should.eql(newCard.arena);
  188.             res.body.elixirCost.should.eql(newCard.elixirCost);
  189.             res.status.should.eql(200);
  190.             done();
  191.           });
  192.       });
  193.  
  194.       it('should try to delete an invalid card', done => {
  195.         updatedCard._id = '111111111111111111111111';
  196.         request(app)
  197.           .put('/api/cards/' + updatedCard._id)
  198.           .set('Accept', 'application/json')
  199.           .set('Authorization', token)
  200.           .send(updatedCard)
  201.           .expect('Content-Type', /json/)
  202.           .end((err, res) => {
  203.             res.status.should.eql(404);
  204.             done();
  205.           });
  206.       });
  207.     });
  208.  
  209.     describe('DELETE /', () => {
  210.       it('should delete a card', done => {
  211.         request(app)
  212.         .delete('/api/cards/' + newCard._id)
  213.           .set('Accept', 'application/json')
  214.           .set('Authorization', token)
  215.           .expect('Content-Type', /json/)
  216.           .end((err, res) => {
  217.             res.status.should.eql(204);
  218.             done();
  219.           });
  220.       });
  221.     });
  222.  
  223.     describe('DELETE /', () => {
  224.       it('should try to delete an invalid card', done => {
  225.         request(app)
  226.           .delete('/api/cards/' + newCard._id)
  227.           .set('Accept', 'application/json')
  228.           .set('Authorization', token)
  229.           .expect('Content-Type', /json/)
  230.           .end((err, res) => {
  231.             res.status.should.eql(404);
  232.             done();
  233.           });
  234.       });
  235.     });
  236.  
  237.   });
  238. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement