Updated controller logic and greatly expanded on unit tests even though they timeout running through github actions

This commit is contained in:
Gregory Campbell
2021-11-25 16:07:40 -05:00
parent 25cc71f485
commit 4db3cdde29
2 changed files with 143 additions and 46 deletions
+6 -4
View File
@@ -54,10 +54,12 @@ const getOneMsg = (req, res) => {
//find the specific msg with that id //find the specific msg with that id
Msg.findOne({id:req.params.id}, (err, data) => { Msg.findOne({id:req.params.id}, (err, data) => {
if(err || !data) { if(data) {
return res.status(400).json({message: "Message can't be found, it doesn't exist."}); return res.status(200).json(data);
} }else{
else return res.status(200).json(data); //return the msg object if found 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."});
}
}); });
}; };
+137 -42
View File
@@ -9,44 +9,12 @@ const app = express();
app.use(express.urlencoded({ extended: false })) app.use(express.urlencoded({ extended: false }))
app.use("/", routes) 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 = { const data = {
Message: "test", message: "test",
} }
it('respond with 201 created', function (done) { it('respond with 201 created', function (done) {
@@ -54,13 +22,9 @@ describe('POST /msg', function () {
.post('/msg/1') .post('/msg/1')
.type('form') .type('form')
.send(data) .send(data)
.then(() => { .set('Accept', 'application/json')
request(app) .expect('Content-Type', /json/)
.get('/msg/1') .expect(201, done);
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200, done)
});
}); });
it('respond with 400 not updated', function (done) { it('respond with 400 not updated', function (done) {
@@ -74,4 +38,135 @@ describe('POST /msg', function () {
done(); 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);
});
}); });