|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const superagent = require('superagent'); |
| 4 | +const mongoose = require('mongoose'); |
| 5 | +const Promise = require('bluebird'); |
| 6 | +const chai = require('chai'); |
| 7 | +const User = require('../model/user'); |
| 8 | +const Gallery = require('../model/gallery'); |
| 9 | +const Pic = require('../model/pic'); |
| 10 | +const expect = chai.expect; |
| 11 | +require('../server.js'); |
| 12 | +const url = `http://localhost:${process.env.PORT}`; |
| 13 | +mongoose.Promise = Promise; |
| 14 | + |
| 15 | +const testUser = { |
| 16 | + username: 'carlomari', |
| 17 | + email: 'carlomari@squidbilly.com', |
| 18 | + password: 'password', |
| 19 | +}; |
| 20 | + |
| 21 | +const testGallery = { |
| 22 | + name: 'this is only a test', |
| 23 | + desc: 'test all the things', |
| 24 | +}; |
| 25 | + |
| 26 | +describe('Pic routes', function() { |
| 27 | + let tempUser; |
| 28 | + let tempToken; |
| 29 | + let tempGallery; |
| 30 | + |
| 31 | + beforeEach(done => { |
| 32 | + new User(testUser) |
| 33 | + .generatePasswordHash(testUser.password) |
| 34 | + .then(user => user.save()) |
| 35 | + .then(user => { |
| 36 | + tempUser = user; |
| 37 | + return user.generateToken(); |
| 38 | + }) |
| 39 | + .then(token => { |
| 40 | + tempToken = token; |
| 41 | + }) |
| 42 | + .then(() => new Gallery(testGallery)) |
| 43 | + .then(gallery => { |
| 44 | + gallery.userId = tempUser._id; |
| 45 | + tempGallery = gallery; |
| 46 | + done(); |
| 47 | + }) |
| 48 | + .catch(err => done(err)); |
| 49 | + }); |
| 50 | + |
| 51 | + afterEach(done => { |
| 52 | + Promise.all([ |
| 53 | + User.remove({}), |
| 54 | + Gallery.remove({}), |
| 55 | + Pic.remove({}), |
| 56 | + ]) |
| 57 | + .then(() => done()) |
| 58 | + .catch(err => done(err)); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('POST method', function() { |
| 62 | + it('should post a picture and have status of 200', done => { |
| 63 | + superagent.post(`${url}/api/gallery/${tempGallery._id}/pic`) |
| 64 | + .set('Content-Type', 'application/json') |
| 65 | + .set('Authorization', `Bearer ${tempToken}`) |
| 66 | + .field('name', 'superman') |
| 67 | + .field('desc', 'superbatspider') |
| 68 | + .attach('image', `${__dirname}/testpic/superman.jpg`) |
| 69 | + .end((err, res) => { |
| 70 | + expect(res.body.name).to.equal('superman'); |
| 71 | + expect(res).to.have.status(200); |
| 72 | + done(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + it('should post as status of 401 on bad request', done => { |
| 76 | + superagent.post(`${url}/api/gallery/${tempGallery._id}/pic`) |
| 77 | + .set('Content-Type', 'application/json') |
| 78 | + .field('name', 'superman') |
| 79 | + .field('desc', 'superbatspider') |
| 80 | + .attach('image', `${__dirname}/testpic/superman.jpg`) |
| 81 | + .end((err, res) => { |
| 82 | + expect(res).to.have.status(401); |
| 83 | + done(); |
| 84 | + }); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments