2021-11-25 15:25:29 -05:00
|
|
|
require("../mongoConfig")
|
|
|
|
|
|
|
|
|
|
const routes = require('../routes/msg')
|
2021-11-25 10:48:28 -05:00
|
|
|
const request = require('supertest');
|
2021-11-25 15:25:29 -05:00
|
|
|
const express = require('express');
|
2021-11-25 21:34:43 -05:00
|
|
|
|
2021-11-25 15:25:29 -05:00
|
|
|
const app = express();
|
|
|
|
|
|
|
|
|
|
app.use(express.urlencoded({ extended: false }))
|
|
|
|
|
app.use("/", routes)
|
2021-11-25 10:48:28 -05:00
|
|
|
|
2021-11-25 16:07:40 -05:00
|
|
|
/**
|
2021-11-25 22:02:18 -05:00
|
|
|
* Testing post message logic and response status codes
|
2021-11-25 16:07:40 -05:00
|
|
|
*/
|
|
|
|
|
describe('POST /msg/:id', function () {
|
|
|
|
|
const data = {
|
|
|
|
|
message: "test",
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-25 22:02:18 -05:00
|
|
|
// Send basic message with id 1
|
2021-11-25 16:07:40 -05:00
|
|
|
it('respond with 201 created', function (done) {
|
|
|
|
|
request(app)
|
2021-11-25 21:23:42 -05:00
|
|
|
.post('/msg/1')
|
|
|
|
|
.type('form')
|
|
|
|
|
.send(data)
|
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
|
.expect(201, done);
|
2021-11-25 16:07:40 -05:00
|
|
|
});
|
|
|
|
|
|
2021-11-25 22:02:18 -05:00
|
|
|
// Tries to send a message that already exists
|
2021-11-25 21:34:43 -05:00
|
|
|
it('respond with 400 not posted', function (done) {
|
2021-11-25 16:07:40 -05:00
|
|
|
request(app)
|
|
|
|
|
.post('/msg/1')
|
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
|
.expect(400)
|
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
|
.end((err) => {
|
|
|
|
|
if (err) return done(err);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-25 22:02:18 -05:00
|
|
|
// Tries to send a message with an id that won't work
|
2021-11-25 21:34:43 -05:00
|
|
|
it('respond with 404 error', function (done) {
|
2021-11-25 16:07:40 -05:00
|
|
|
request(app)
|
|
|
|
|
.post('/msg/idisnonexisting')
|
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
|
.expect(404)
|
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
|
.end((err) => {
|
|
|
|
|
if (err) return done(err);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-25 22:02:18 -05:00
|
|
|
* Testing get message logic and response status codes
|
2021-11-25 16:07:40 -05:00
|
|
|
*/
|
2021-11-25 10:48:28 -05:00
|
|
|
describe('GET /msg', function () {
|
2021-11-25 22:02:18 -05:00
|
|
|
|
|
|
|
|
// Get info of entire database
|
2021-11-25 21:34:43 -05:00
|
|
|
it('respond with 200 info recieved', function (done) {
|
2021-11-25 15:25:29 -05:00
|
|
|
request(app)
|
2021-11-25 10:48:28 -05:00
|
|
|
.get('/msg')
|
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
|
.expect(200, done);
|
|
|
|
|
});
|
2021-11-25 16:07:40 -05:00
|
|
|
});
|
2021-11-25 10:48:28 -05:00
|
|
|
|
|
|
|
|
describe('GET /msg/:id', function () {
|
2021-11-25 22:02:18 -05:00
|
|
|
|
|
|
|
|
//Get info of a message with id 1
|
2021-11-25 21:34:43 -05:00
|
|
|
it('respond with 200 info recieved for one message', function (done) {
|
2021-11-25 15:25:29 -05:00
|
|
|
request(app)
|
2021-11-25 10:48:28 -05:00
|
|
|
.get('/msg/1')
|
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
|
.expect(200, done);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-25 22:02:18 -05:00
|
|
|
// Get info of a message that doesnt exist
|
2021-11-25 21:34:43 -05:00
|
|
|
it('respond with 400 message not found', function (done) {
|
2021-11-25 15:25:29 -05:00
|
|
|
request(app)
|
2021-11-25 16:07:40 -05:00
|
|
|
.get('/msg/2')
|
2021-11-25 15:25:29 -05:00
|
|
|
.set('Accept', 'application/json')
|
2021-11-25 21:34:43 -05:00
|
|
|
.expect(400)
|
|
|
|
|
.expect('Content-Type', /json/)
|
2021-11-25 10:48:28 -05:00
|
|
|
.end((err) => {
|
|
|
|
|
if (err) return done(err);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-11-25 16:07:40 -05:00
|
|
|
|
2021-11-25 22:02:18 -05:00
|
|
|
// Get info of a message with an id that won't work
|
2021-11-25 21:34:43 -05:00
|
|
|
it('respond with 404 error', function (done) {
|
2021-11-25 16:07:40 -05:00
|
|
|
request(app)
|
|
|
|
|
.get('/msg/idisnonexisting')
|
|
|
|
|
.set('Accept', 'application/json')
|
2021-11-25 21:34:43 -05:00
|
|
|
.expect(404)
|
|
|
|
|
.expect('Content-Type', /json/)
|
2021-11-25 16:07:40 -05:00
|
|
|
.end((err) => {
|
|
|
|
|
if (err) return done(err);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-11-25 10:48:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-25 22:02:18 -05:00
|
|
|
* Testing update message logic and response status codes
|
2021-11-25 10:48:28 -05:00
|
|
|
*/
|
2021-11-25 16:07:40 -05:00
|
|
|
describe('PUT /msg/:id', function () {
|
2021-11-25 22:02:18 -05:00
|
|
|
|
|
|
|
|
// Update the info of a message with id 1
|
2021-11-25 21:34:43 -05:00
|
|
|
it('respond with 201 message updated', function (done) {
|
2021-11-25 16:07:40 -05:00
|
|
|
request(app)
|
|
|
|
|
.put('/msg/1')
|
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
|
.expect(201, done);
|
|
|
|
|
});
|
2021-11-25 15:25:29 -05:00
|
|
|
|
2021-11-25 22:02:18 -05:00
|
|
|
// Update the info of a message that doesn't exist
|
2021-11-25 21:34:43 -05:00
|
|
|
it('respond with 400 message not found', function (done) {
|
2021-11-25 15:25:29 -05:00
|
|
|
request(app)
|
2021-11-25 16:07:40 -05:00
|
|
|
.put('/msg/2')
|
|
|
|
|
.set('Accept', 'application/json')
|
2021-11-25 21:34:43 -05:00
|
|
|
.expect(400)
|
|
|
|
|
.expect('Content-Type', /json/)
|
2021-11-25 16:07:40 -05:00
|
|
|
.end((err) => {
|
|
|
|
|
if (err) return done(err);
|
|
|
|
|
done();
|
|
|
|
|
});
|
2021-11-25 10:48:28 -05:00
|
|
|
});
|
|
|
|
|
|
2021-11-25 22:02:18 -05:00
|
|
|
|
|
|
|
|
// Update the info of a message with an id that won't work
|
2021-11-25 21:34:43 -05:00
|
|
|
it('respond with 404 error', function (done) {
|
2021-11-25 15:25:29 -05:00
|
|
|
request(app)
|
2021-11-25 16:07:40 -05:00
|
|
|
.put('/msg/idisnonexisting')
|
|
|
|
|
.set('Accept', 'application/json')
|
2021-11-25 21:34:43 -05:00
|
|
|
.expect(404)
|
|
|
|
|
.expect('Content-Type', /json/)
|
2021-11-25 16:07:40 -05:00
|
|
|
.end((err) => {
|
|
|
|
|
if (err) return done(err);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-25 22:02:18 -05:00
|
|
|
* Testing delete message logic and response status codes
|
2021-11-25 16:07:40 -05:00
|
|
|
*/
|
|
|
|
|
describe('DELETE /msg/:id', function () {
|
2021-11-25 22:02:18 -05:00
|
|
|
|
|
|
|
|
// Delete a message with id of 1
|
2021-11-25 21:34:43 -05:00
|
|
|
it('respond with 200 single message deleted', function (done) {
|
2021-11-25 16:07:40 -05:00
|
|
|
request(app)
|
|
|
|
|
.delete('/msg/1')
|
2021-11-25 10:48:28 -05:00
|
|
|
.set('Accept', 'application/json')
|
2021-11-25 15:25:29 -05:00
|
|
|
.expect('Content-Type', /json/)
|
2021-11-25 16:07:40 -05:00
|
|
|
.expect(200, done);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-25 22:02:18 -05:00
|
|
|
// Delete a message that doesn't exist
|
2021-11-25 21:34:43 -05:00
|
|
|
it('respond with 400 message not found', function (done) {
|
2021-11-25 16:07:40 -05:00
|
|
|
request(app)
|
|
|
|
|
.delete('/msg/1')
|
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
|
.expect(400) //expecting HTTP status code
|
|
|
|
|
.expect('Content-Type', /json/) // expecting content value
|
2021-11-25 10:48:28 -05:00
|
|
|
.end((err) => {
|
|
|
|
|
if (err) return done(err);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-11-25 21:40:34 -05:00
|
|
|
|
2021-11-25 22:02:18 -05:00
|
|
|
// Delete a message with an id that won't work
|
2021-11-25 21:40:34 -05:00
|
|
|
it('respond with 404 error', function (done) {
|
|
|
|
|
request(app)
|
|
|
|
|
.delete('/msg/idisnonexisting')
|
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
|
.expect(404) //expecting HTTP status code
|
|
|
|
|
.expect('Content-Type', /json/) // expecting content value
|
|
|
|
|
.end((err) => {
|
|
|
|
|
if (err) return done(err);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-11-25 16:07:40 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('DELETE /msg', function () {
|
2021-11-25 22:02:18 -05:00
|
|
|
|
|
|
|
|
// Delete all messages within database
|
2021-11-25 21:34:43 -05:00
|
|
|
it('respond with 200 all messages deleted', function (done) {
|
2021-11-25 16:07:40 -05:00
|
|
|
request(app)
|
|
|
|
|
.delete('/msg')
|
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
|
.expect(200, done);
|
|
|
|
|
});
|
2021-11-25 10:48:28 -05:00
|
|
|
});
|