Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 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(done) {
  60. seedBlogData().then((data) => {
  61. console.log('=========');
  62. console.log('=========');
  63. console.log('=========');
  64. console.log(data);
  65. console.log('=========');
  66. console.log('=========');
  67. console.log('=========');
  68. done();
  69. });
  70. });
  71.  
  72. afterEach(function() {
  73. return tearDownDb();
  74. });
  75.  
  76. after(function() {
  77. return closeServer();
  78. })
  79.  
  80. describe('GET endpoint', function() {
  81. it('should return all existing blogs', function() {
  82. let res;
  83. return chai.request(app)
  84. .get('/api/posts')
  85. .then(function(_res) {
  86. res = _res;
  87. res.should.have.status(200);
  88. res.body.should.have.length.of.at.least(1);
  89. return BlogPost.count();
  90. })
  91. .then(function(count) {
  92. res.body.should.have.lengthOf(count);
  93. });
  94. });
  95.  
  96. it('should return blogs with right fields', function() {
  97. let resBlog;
  98. chai.request(app)
  99. .get('/posts')
  100. .then(function(res) {
  101. res.should.have.status(200);
  102. res.should.be.json;
  103. res.body.blogs.should.be.a('array');
  104. res.body.blogs.should.have.a.length.of.at.least(1);
  105.  
  106. res.body.blogs.forEach(function(blog) {
  107. blog.should.be.a('object');
  108. blog.should.include.keys(
  109. 'title', 'author', 'content', 'comments');
  110. });
  111. resBlog = res.body.blogs[0];
  112. return BlogPost.findById(resBlogPost.id);
  113. })
  114. .then(function(restaurant) {
  115. resBlogPost.title.should.equal(blog.title);
  116. resBlogPost.content.should.equal(blog.content);
  117. resBlogPost.author.should.equal(blog.authorName);
  118. });
  119. });
  120. });
  121.  
  122. describe('POST endpoint', function() {
  123. it('it responds with 401 status code if no authorization header', function(done) {
  124. request(app).get('/api/posts').expect(401).end(function(err, res) {
  125. if (err) return done(err);
  126. done();
  127. });
  128. });
  129.  
  130. it('it responds with JSON if no authorization header', function(done) {
  131. request(app).get('/api/posts').expect('Content-Type', /json/).end(function(err, res) {
  132. if (err) return done(err);
  133. done();
  134. });
  135. });
  136.  
  137. it('it responds with 200 status code if good authorization header', function(done) {
  138. var token = jwt.sign({
  139. id: 1,
  140. }, JWT_SECRET, { expiresIn: 60*60 });
  141. request(app)
  142. .get('/api/posts')
  143. .set('Authorization', token)
  144. .expect(200)
  145. .end(function(err, res) {
  146. if (err) return done(err);
  147. done();
  148. });
  149. });
  150.  
  151. it('it responds with JSON if good authorization header', function(done) {
  152. var token = jwt.sign({
  153. id: 1,
  154. }, JWT_SECRET, { expiresIn: 60*60 });
  155. request(app)
  156. .get('/api/posts')
  157. .set('Authorization', token)
  158. .expect('Content-Type', /json/)
  159. .end(function(err, res) {
  160. if (err) return done(err);
  161. done();
  162. });
  163. });
  164.  
  165. it('should add a new blog', function() {
  166. var token = jwt.sign({
  167. id: 1,
  168. }, JWT_SECRET, { expiresIn: 60*60 });
  169. request(app)
  170. .get('/api/posts')
  171. .set('Authorization', token)
  172. .expect('Content-Type', /json/)
  173. .end(function(err, res) {
  174. if (err) return done(err);
  175. done();
  176. });
  177. const newPost = {
  178. title: faker.lorem.sentence(),
  179. author: {
  180. firstName: faker.name.firstName(),
  181. lastName: faker.name.lastName(),
  182. },
  183. content: faker.lorem.text(),
  184. image: faker.image.image()
  185. };
  186. return chai.request(app)
  187. .post('/api/posts')
  188. .send(newPost)
  189. .then(function(res) {
  190. res.should.have.status(201);
  191. res.should.be.json;
  192. res.body.should.be.a('object');
  193. res.body.should.include.keys(
  194. 'id', 'title', 'author', 'content', 'image');
  195. res.body.title.should.equal(newPost.title);
  196. res.body.id.should.not.be.null;
  197. res.body.author.should.equal(
  198. `${newPost.author.firstName} ${newPost.author.lastName}`);
  199. res.body.content.should.equal(newPost.content);
  200. return BlogPost.findById(res.body.id);
  201. })
  202. .then(function(post) {
  203. post.title.should.equal(newPost.title);
  204. post.content.should.equal(newPost.content);
  205. post.author.firstName.should.equal(newPost.author.firstName);
  206. post.author.lastName.should.equal(newPost.author.lastName);
  207. });
  208. });
  209. });
  210.  
  211. describe('DELETE endpoint', function() {
  212. it('delete a blog by id', function() {
  213. let blog;
  214.  
  215. return BlogPost
  216. .findOne()
  217. .then(function(_blog) {
  218. blog = _blog;
  219. return chai.request(app).delete(`/api/posts/${blog.id}`);
  220. })
  221. .then(function(res) {
  222. res.should.have.status(204);
  223. return BlogPost.findById(blog.id);
  224. })
  225. .then(function(_blog) {
  226. should.not.exist(_blog);
  227. });
  228. });
  229. });
  230. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement