Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const chai = require('chai');
- const chaiHttp = require('chai-http');
- const faker = require('faker');
- const mongoose = require('mongoose');
- const jwt = require('jsonwebtoken');
- const request = require('supertest');
- const {app, runServer, closeServer} = require('../server');
- const {BlogPost} = require('../models/posts');
- const User = require('../models/user');
- const should = chai.should();
- const {TEST_DATABASE_URL} = require('../config');
- const {JWT_SECRET} = require('../config');
- chai.use(chaiHttp);
- function tearDownDb() {
- console.warn('Deleting database');
- return mongoose.connection.dropDatabase();
- }
- function seedBlogData() {
- console.info('seeding blog data');
- const seedData = [];
- for (let i=1; i<=10; i++) {
- seedData.push({
- author: {
- firstName: faker.name.firstName(),
- lastName: faker.name.lastName()
- },
- title: faker.lorem.sentence(),
- content: faker.lorem.text(),
- image: faker.image.image()
- });
- }
- const blogDatas = BlogPost.insertMany(seedData);
- const newUser = new User({
- email: 'abc@123.com',
- password: 'password',
- confirmPassword: 'password',
- firstName: 'Billy',
- lastName: 'Bob',
- username: 'username',
- });
- const user = newUser.save();
- return Promise.all([blogDatas, newUser]);
- }
- describe('Blog API resource', function() {
- let user;
- before(function() {
- return runServer(TEST_DATABASE_URL);
- });
- beforeEach(function() {
- return seedBlogData();
- });
- afterEach(function() {
- return tearDownDb();
- });
- after(function() {
- return closeServer();
- })
- describe('GET endpoint', function(first, ...topRest) {
- console.log('===============');
- console.log('===============');
- console.log('===============');
- console.log('first', first);
- console.log(topRest);
- console.log('===============');
- console.log('===============');
- console.log('===============');
- it('should return all existing blogs', function(second, ...rest) {
- console.log('.................');
- console.log('.................');
- console.log('.................');
- console.log('second', second);
- console.log(rest);
- console.log('.................');
- console.log('.................');
- console.log('.................');
- let res;
- return chai.request(app)
- .get('/api/posts')
- .then(function(_res) {
- res = _res;
- res.should.have.status(200);
- res.body.should.have.length.of.at.least(1);
- return BlogPost.count();
- })
- .then(function(count) {
- res.body.should.have.lengthOf(count);
- });
- });
- it('should return blogs with right fields', function() {
- let resBlog;
- chai.request(app)
- .get('/posts')
- .then(function(res) {
- res.should.have.status(200);
- res.should.be.json;
- res.body.blogs.should.be.a('array');
- res.body.blogs.should.have.a.length.of.at.least(1);
- res.body.blogs.forEach(function(blog) {
- blog.should.be.a('object');
- blog.should.include.keys(
- 'title', 'author', 'content', 'comments');
- });
- resBlog = res.body.blogs[0];
- return BlogPost.findById(resBlogPost.id);
- })
- .then(function(restaurant) {
- resBlogPost.title.should.equal(blog.title);
- resBlogPost.content.should.equal(blog.content);
- resBlogPost.author.should.equal(blog.authorName);
- });
- });
- });
- describe('POST endpoint', function() {
- it('it responds with 401 status code if no authorization header', function(done) {
- request(app).get('/api/posts').expect(401).end(function(err, res) {
- if (err) return done(err);
- done();
- });
- });
- it('it responds with JSON if no authorization header', function(done) {
- request(app).get('/api/posts').expect('Content-Type', /json/).end(function(err, res) {
- if (err) return done(err);
- done();
- });
- });
- it('it responds with 200 status code if good authorization header', function(done) {
- var token = jwt.sign({
- id: 1,
- }, JWT_SECRET, { expiresIn: 60*60 });
- request(app)
- .get('/api/posts')
- .set('Authorization', token)
- .expect(200)
- .end(function(err, res) {
- if (err) return done(err);
- done();
- });
- });
- it('it responds with JSON if good authorization header', function(done) {
- var token = jwt.sign({
- id: 1,
- }, JWT_SECRET, { expiresIn: 60*60 });
- request(app)
- .get('/api/posts')
- .set('Authorization', token)
- .expect('Content-Type', /json/)
- .end(function(err, res) {
- if (err) return done(err);
- done();
- });
- });
- it('should add a new blog', function() {
- var token = jwt.sign({
- id: 1,
- }, JWT_SECRET, { expiresIn: 60*60 });
- request(app)
- .get('/api/posts')
- .set('Authorization', token)
- .expect('Content-Type', /json/)
- .end(function(err, res) {
- if (err) return done(err);
- done();
- });
- const newPost = {
- title: faker.lorem.sentence(),
- author: {
- firstName: faker.name.firstName(),
- lastName: faker.name.lastName(),
- },
- content: faker.lorem.text(),
- image: faker.image.image()
- };
- return chai.request(app)
- .post('/api/posts')
- .send(newPost)
- .then(function(res) {
- res.should.have.status(201);
- res.should.be.json;
- res.body.should.be.a('object');
- res.body.should.include.keys(
- 'id', 'title', 'author', 'content', 'image');
- res.body.title.should.equal(newPost.title);
- res.body.id.should.not.be.null;
- res.body.author.should.equal(
- `${newPost.author.firstName} ${newPost.author.lastName}`);
- res.body.content.should.equal(newPost.content);
- return BlogPost.findById(res.body.id);
- })
- .then(function(post) {
- post.title.should.equal(newPost.title);
- post.content.should.equal(newPost.content);
- post.author.firstName.should.equal(newPost.author.firstName);
- post.author.lastName.should.equal(newPost.author.lastName);
- });
- });
- });
- describe('DELETE endpoint', function() {
- it('delete a blog by id', function() {
- let blog;
- return BlogPost
- .findOne()
- .then(function(_blog) {
- blog = _blog;
- return chai.request(app).delete(`/api/posts/${blog.id}`);
- })
- .then(function(res) {
- res.should.have.status(204);
- return BlogPost.findById(blog.id);
- })
- .then(function(_blog) {
- should.not.exist(_blog);
- });
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement