From 03d338264ac4e3d1d2de4957b40b5f58698e2755 Mon Sep 17 00:00:00 2001 From: minggas Date: Sun, 30 Jun 2019 14:10:45 -0300 Subject: [PATCH 1/2] feat: add GET for threads and replies w/ test --- routes/api.js | 34 +++++++++++++++++++++++++++ tests/2_functional-tests.js | 47 +++++++++++++++++++++++++++++++------ 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/routes/api.js b/routes/api.js index 946886b..7e7b375 100644 --- a/routes/api.js +++ b/routes/api.js @@ -36,6 +36,23 @@ module.exports = function(app) { return res.redirect(`/b/${board}`); }); }) + .get((req, res) => { + const board = req.params.board; + Thread.find( + { board }, + { __v: 0, delete_password: 0, reported: 0 }, + { sort: { bumped_on: -1 }, limit: 10 }, + ) + .populate({ + path: "replies", + select: "-delete_password -reported -__v", + options: { limit: 3 }, + }) + .exec((err, result) => { + if (err) return res.status(506).send(err); + return res.status(200).json(result); + }); + }); app.route("/api/replies/:board").post((req, res) => { if (!req.body.text.trim()) @@ -61,4 +78,21 @@ module.exports = function(app) { }, ); }); + }) + .get((req, res) => { + const thread_id = req.query.thread_id; + Thread.findOne( + { _id: thread_id }, + { __v: 0, delete_password: 0, reported: 0 }, + { sort: { bumped_on: -1 } }, + ) + .populate({ + path: "replies", + select: "-delete_password -reported -__v", + }) + .exec((err, result) => { + if (err) return res.status(506).send(err); + return res.status(200).json(result); + }); + }); }; diff --git a/tests/2_functional-tests.js b/tests/2_functional-tests.js index 221f524..be70998 100644 --- a/tests/2_functional-tests.js +++ b/tests/2_functional-tests.js @@ -12,6 +12,7 @@ var assert = chai.assert; var server = require("../server"); chai.use(chaiHttp); +let test_thread_id = ""; suite("Functional Tests", function() { suite("API ROUTING FOR /api/threads/:board", function() { @@ -62,8 +63,21 @@ suite("Functional Tests", function() { }); }); - suite('GET', function() { - + suite("GET", function() { + test("Show recent threads", function(done) { + chai + .request(server) + .get("/api/threads/test-board") + .end(function(err, res) { + test_thread_id = res.body[0]._id; + assert.equal(res.status, 200); + assert.isArray(res.body, "res should be an array"); + assert.isObject(res.body[0]); + assert.notProperty(res.body[0], "delete_password"); + assert.notProperty(res.body[0], "reported"); + done(); + }); + }); }); /*suite("DELETE", function() {}); @@ -78,7 +92,7 @@ suite("Functional Tests", function() { .request(server) .post("/api/replies/test-board") .send({ - thread_id: "5d17d37af205d2493dc39e5b", + thread_id: test_thread_id, text: "This is my first reply to message board - 54321", delete_password: "0d&2", }) @@ -99,19 +113,38 @@ suite("Functional Tests", function() { delete_password: "02$d&2", }) .end(function(err, res) { - assert.equal(res.status, 500); + assert.equal(res.status, 403); assert.equal( res.text, - 'Cannot update thread. error: CastError: Cast to ObjectId failed for value "Th1si2aInval1d" at path "_id" for model "Thread"', + "invalid Thread id", "Invalid thread id error", ); done(); }); }); }); - /* - suite("GET", function() {}); + suite("GET", function() { + test("Show entire test-board thread with all its replies", function(done) { + chai + .request(server) + .get("/api/replies/test-board") + .query({ + thread_id: test_thread_id, + }) + .end(function(err, res) { + assert.equal(res.status, 200); + assert.isObject(res.body); + assert.notProperty(res.body, "delete_password"); + assert.notProperty(res.body, "reported"); + assert.property(res.body, text); + assert.property(res.body, _id); + assert.isArray(res.body.replies, "Replies should be an array"); + done(); + }); + }); + }); + /* suite("PUT", function() {}); suite("DELETE", function() {});*/ From b14bac57e21df8f35092654a1ef710cbe1dc6e9a Mon Sep 17 00:00:00 2001 From: minggas Date: Sun, 30 Jun 2019 14:11:21 -0300 Subject: [PATCH 2/2] fix: add thread_id field to reply model --- models/Reply.js | 1 + 1 file changed, 1 insertion(+) diff --git a/models/Reply.js b/models/Reply.js index 81a7c9a..74f1ac6 100644 --- a/models/Reply.js +++ b/models/Reply.js @@ -1,6 +1,7 @@ const mongoose = require("mongoose"); const ReplySchema = new mongoose.Schema({ + thread_id: { type: mongoose.Schema.Types.ObjectId, ref: "Thread" }, text: { type: String, required: true,