Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. const chai = require('chai');
  2. const chaiHttp = require('chai-http');
  3. const faker = require('faker');
  4. const mongoose = require('mongoose');
  5. const jwt = require('jsonwebtoken');
  6. const request = require('supertest');
  7.  
  8. const {app, runServer, closeServer} = require('../server');
  9. const {BlogPost} = require('../models/posts');
  10. const User = require('../models/user');
  11. const should = chai.should();
  12. const {TEST_DATABASE_URL} = require('../config');
  13. const {JWT_SECRET} = require('../config');
  14.  
  15. chai.use(chaiHttp);
  16.  
  17. function tearDownDb() {
  18. console.warn('Deleting database');
  19. return mongoose.connection.dropDatabase();
  20. }
  21.  
  22. function seedBlogData() {
  23. console.info('seeding blog data');
  24. const seedData = [];
  25. for (let i=1; i<=10; i++) {
  26. seedData.push({
  27. author: {
  28. firstName: faker.name.firstName(),
  29. lastName: faker.name.lastName()
  30. },
  31. title: faker.lorem.sentence(),
  32. content: faker.lorem.text(),
  33. image: faker.image.image()
  34. });
  35. }
  36. const blogDatas = BlogPost.insertMany(seedData);
  37.  
  38. const newUser = new User({
  39. email: 'abc@123.com',
  40. password: 'password',
  41. confirmPassword: 'password',
  42. firstName: 'Billy',
  43. lastName: 'Bob',
  44. username: 'username',
  45. });
  46.  
  47. const user = newUser.save();
  48.  
  49. return Promise.all([blogDatas, newUser]);
  50. }
  51.  
  52. describe('Blog API resource', function() {
  53. let user;
  54.  
  55. before(function() {
  56. return runServer(TEST_DATABASE_URL);
  57. });
  58.  
  59. beforeEach(function() {
  60. return seedBlogData();
  61. });
  62.  
  63. afterEach(function() {
  64. return tearDownDb();
  65. });
  66.  
  67. after(function() {
  68. return closeServer();
  69. })
  70.  
  71. describe('GET endpoint', function(first, ...topRest) {
  72. console.log('===============');
  73. console.log('===============');
  74. console.log('===============');
  75. console.log('first', first);
  76. console.log(topRest);
  77. console.log('===============');
  78. console.log('===============');
  79. console.log('===============');
  80. it('should return all existing blogs', function(second, ...rest) {
  81. console.log('.................');
  82. console.log('.................');
  83. console.log('.................');
  84. console.log('second', second);
  85. console.log(rest);
  86. console.log('.................');
  87. console.log('.................');
  88. console.log('.................');
  89. let res;
  90. return chai.request(app)
  91. .get('/api/posts')
  92. .then(function(_res) {
  93. res = _res;
  94. res.should.have.status(200);
  95. res.body.should.have.length.of.at.least(1);
  96. return BlogPost.count();
  97. })
  98. .then(function(count) {
  99. res.body.should.have.lengthOf(count);
  100. });
  101. });
  102.  
  103. it('should return blogs with right fields', function() {
  104. let resBlog;
  105. chai.request(app)
  106. .get('/posts')
  107. .then(function(res) {
  108. res.should.have.status(200);
  109. res.should.be.json;
  110. res.body.blogs.should.be.a('array');
  111. res.body.blogs.should.have.a.length.of.at.least(1);
  112.  
  113. res.body.blogs.forEach(function(blog) {
  114. blog.should.be.a('object');
  115. blog.should.include.keys(
  116. 'title', 'author', 'content', 'comments');
  117. });
  118. resBlog = res.body.blogs[0];
  119. return BlogPost.findById(resBlogPost.id);
  120. })
  121. .then(function(restaurant) {
  122. resBlogPost.title.should.equal(blog.title);
  123. resBlogPost.content.should.equal(blog.content);
  124. resBlogPost.author.should.equal(blog.authorName);
  125. });
  126. });
  127. });
  128.  
  129. describe('POST endpoint', function() {
  130. it('it responds with 401 status code if no authorization header', function(done) {
  131. request(app).get('/api/posts').expect(401).end(function(err, res) {
  132. if (err) return done(err);
  133. done();
  134. });
  135. });
  136.  
  137. it('it responds with JSON if no authorization header', function(done) {
  138. request(app).get('/api/posts').expect('Content-Type', /json/).end(function(err, res) {
  139. if (err) return done(err);
  140. done();
  141. });
  142. });
  143.  
  144. it('it responds with 200 status code if good authorization header', function(done) {
  145. var token = jwt.sign({
  146. id: 1,
  147. }, JWT_SECRET, { expiresIn: 60*60 });
  148. request(app)
  149. .get('/api/posts')
  150. .set('Authorization', token)
  151. .expect(200)
  152. .end(function(err, res) {
  153. if (err) return done(err);
  154. done();
  155. });
  156. });
  157.  
  158. it('it responds with JSON if good authorization header', function(done) {
  159. var token = jwt.sign({
  160. id: 1,
  161. }, JWT_SECRET, { expiresIn: 60*60 });
  162. request(app)
  163. .get('/api/posts')
  164. .set('Authorization', token)
  165. .expect('Content-Type', /json/)
  166. .end(function(err, res) {
  167. if (err) return done(err);
  168. done();
  169. });
  170. });
  171.  
  172. it('should add a new blog', function() {
  173. var token = jwt.sign({
  174. id: 1,
  175. }, JWT_SECRET, { expiresIn: 60*60 });
  176. request(app)
  177. .get('/api/posts')
  178. .set('Authorization', token)
  179. .expect('Content-Type', /json/)
  180. .end(function(err, res) {
  181. if (err) return done(err);
  182. done();
  183. });
  184. const newPost = {
  185. title: faker.lorem.sentence(),
  186. author: {
  187. firstName: faker.name.firstName(),
  188. lastName: faker.name.lastName(),
  189. },
  190. content: faker.lorem.text(),
  191. image: faker.image.image()
  192. };
  193. return chai.request(app)
  194. .post('/api/posts')
  195. .send(newPost)
  196. .then(function(res) {
  197. res.should.have.status(201);
  198. res.should.be.json;
  199. res.body.should.be.a('object');
  200. res.body.should.include.keys(
  201. 'id', 'title', 'author', 'content', 'image');
  202. res.body.title.should.equal(newPost.title);
  203. res.body.id.should.not.be.null;
  204. res.body.author.should.equal(
  205. `${newPost.author.firstName} ${newPost.author.lastName}`);
  206. res.body.content.should.equal(newPost.content);
  207. return BlogPost.findById(res.body.id);
  208. })
  209. .then(function(post) {
  210. post.title.should.equal(newPost.title);
  211. post.content.should.equal(newPost.content);
  212. post.author.firstName.should.equal(newPost.author.firstName);
  213. post.author.lastName.should.equal(newPost.author.lastName);
  214. });
  215. });
  216. });
  217.  
  218. describe('DELETE endpoint', function() {
  219. it('delete a blog by id', function() {
  220. let blog;
  221.  
  222. return BlogPost
  223. .findOne()
  224. .then(function(_blog) {
  225. blog = _blog;
  226. return chai.request(app).delete(`/api/posts/${blog.id}`);
  227. })
  228. .then(function(res) {
  229. res.should.have.status(204);
  230. return BlogPost.findById(blog.id);
  231. })
  232. .then(function(_blog) {
  233. should.not.exist(_blog);
  234. });
  235. });
  236. });
  237. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement