From 4db3cdde29b4bc9e44c788fc5e9cfa5a03894345 Mon Sep 17 00:00:00 2001 From: Gregory Campbell Date: Thu, 25 Nov 2021 16:07:40 -0500 Subject: [PATCH] Updated controller logic and greatly expanded on unit tests even though they timeout running through github actions --- controllers/msg.js | 10 ++- test/msg.js | 179 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 143 insertions(+), 46 deletions(-) diff --git a/controllers/msg.js b/controllers/msg.js index d34aaf7..8fc0261 100644 --- a/controllers/msg.js +++ b/controllers/msg.js @@ -54,10 +54,12 @@ const getOneMsg = (req, res) => { //find the specific msg with that id Msg.findOne({id:req.params.id}, (err, data) => { - if(err || !data) { - return res.status(400).json({message: "Message can't be found, it doesn't exist."}); - } - else return res.status(200).json(data); //return the msg object if found + if(data) { + return res.status(200).json(data); + }else{ + if(err) return res.status(404).json(`Something went wrong, please try again. ${err}`); + return res.status(400).json({message: "Message can't be found, it doesn't exist."}); + } }); }; diff --git a/test/msg.js b/test/msg.js index 1139162..781e6b5 100644 --- a/test/msg.js +++ b/test/msg.js @@ -9,44 +9,12 @@ const app = express(); app.use(express.urlencoded({ extended: false })) app.use("/", routes) -describe('GET /msg', function () { - it('respond with json containing a list of all users', function (done) { - request(app) - .get('/msg') - .set('Accept', 'application/json') - .expect('Content-Type', /json/) - .expect(200, done); - }); -});; - -describe('GET /msg/:id', function () { - it('respond with json containing a single message', function (done) { - request(app) - .get('/msg/1') - .set('Accept', 'application/json') - .expect('Content-Type', /json/) - .expect(200, done); - }); - - it('respond with json message not found', function (done) { - request(app) - .get('/msg/idisnonexisting') - .set('Accept', 'application/json') - .expect(400) //expecting HTTP status code - .expect('Content-Type', /json/) // expecting content value - .end((err) => { - if (err) return done(err); - done(); - }); - }); -}); - /** - * Testing post message endpoint + * Testing post message logic */ -describe('POST /msg', function () { +describe('POST /msg/:id', function () { const data = { - Message: "test", + message: "test", } it('respond with 201 created', function (done) { @@ -54,13 +22,9 @@ describe('POST /msg', function () { .post('/msg/1') .type('form') .send(data) - .then(() => { - request(app) - .get('/msg/1') - .set('Accept', 'application/json') - .expect('Content-Type', /json/) - .expect(200, done) - }); + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(201, done); }); it('respond with 400 not updated', function (done) { @@ -74,4 +38,135 @@ describe('POST /msg', function () { done(); }); }); + + it('respond with 400 not updated', function (done) { + request(app) + .post('/msg/idisnonexisting') + .set('Accept', 'application/json') + .expect(404) + .expect('Content-Type', /json/) + .end((err) => { + if (err) return done(err); + done(); + }); + }); +}); + +/** + * Testing get message logic + */ +describe('GET /msg', function () { + it('respond with json containing a list of all users', function (done) { + request(app) + .get('/msg') + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(200, done); + }); +}); + +describe('GET /msg/:id', function () { + it('respond with json containing a single message', function (done) { + request(app) + .get('/msg/1') + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(200, done); + }); + + it('respond with json message not found', function (done) { + request(app) + .get('/msg/2') + .set('Accept', 'application/json') + .expect(400) //expecting HTTP status code + .expect('Content-Type', /json/) // expecting content value + .end((err) => { + if (err) return done(err); + done(); + }); + }); + + it('respond with json message not found', function (done) { + request(app) + .get('/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(); + }); + }); +}); + +/** + * Testing put message logic + */ +describe('PUT /msg/:id', function () { + it('respond with json containing a single message', function (done) { + request(app) + .put('/msg/1') + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(201, done); + }); + + it('respond with json message not found', function (done) { + request(app) + .put('/msg/2') + .set('Accept', 'application/json') + .expect(400) //expecting HTTP status code + .expect('Content-Type', /json/) // expecting content value + .end((err) => { + if (err) return done(err); + done(); + }); + }); + + it('respond with json message not found', function (done) { + request(app) + .put('/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(); + }); + }); +}); + +/** + * Testing delete message logic + */ +describe('DELETE /msg/:id', function () { + it('respond with json containing a single message', function (done) { + request(app) + .delete('/msg/1') + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(200, done); + }); + + it('respond with json message not found', function (done) { + request(app) + .delete('/msg/1') + .set('Accept', 'application/json') + .expect(400) //expecting HTTP status code + .expect('Content-Type', /json/) // expecting content value + .end((err) => { + if (err) return done(err); + done(); + }); + }); +}); + +describe('DELETE /msg', function () { + it('respond with json containing a list of all users', function (done) { + request(app) + .delete('/msg') + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(200, done); + }); }); \ No newline at end of file