Advertisement
Guest User

Untitled

a guest
Dec 1st, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1.  
  2. const app = require('../app');
  3.  
  4. let chai = require('chai');
  5. let chaiHttp = require('chai-http');
  6.  
  7. chai.use(chaiHttp);
  8.  
  9. // tests
  10. const Thread = require('../models/thread');
  11. const User = require('../models/user');
  12. const expect = chai.expect;
  13. const assert = chai.assert;
  14.  
  15. const dummyUser = {
  16. Username: "test_thread",
  17. Password: "hunter2"
  18. };
  19. const dummyUser2 = {
  20. Username: "test_thread2",
  21. Password: "hunter2"
  22. };
  23.  
  24. const dummyThread = {
  25. id: "",
  26. username: dummyUser.Username,
  27. title: "Test Thread",
  28. content: "This is a test thread."
  29. };
  30.  
  31. const dummyThread2 = {
  32. id: "",
  33. Username: dummyUser2.Username,
  34. Title: "Test Thread 2",
  35. Content: "This is a second test thread."
  36. };
  37.  
  38. const dummyComment = {
  39. username: dummyUser.Username,
  40. content: "This is an test comment"
  41. };
  42.  
  43. before(function(){
  44. User.create(dummyUser);
  45. })
  46.  
  47. describe('/api/threads end-point', function() {
  48. it('should create a new thread', function(done) {
  49. chai.request(app)
  50. .post('/api/threads')
  51. .send(dummyThread)
  52. .end(function(err, res) {
  53. assert(res.status, 201);
  54. dummyThread.id = res.body._id;
  55. done();
  56. });
  57. });
  58.  
  59. it('responds with list of threads', function (done) {
  60. chai.request(app)
  61. .get('/api/threads')
  62. .end(function(err, res) {
  63. expect(err).to.be.null;
  64. expect(res).to.have.status(200);
  65.  
  66. assert.property(res.body[0], 'Votes');
  67. assert.property(res.body[0], '_id');
  68. assert.property(res.body[0], 'User');
  69. assert.property(res.body[0], 'Title');
  70. assert.property(res.body[0], 'Content');
  71. done();
  72. });
  73. });
  74.  
  75. it('responds with specific thread', function (done) {
  76. Thread.findOne({ _id: dummyThread.id }, function(error, thread) {
  77. chai.request(app)
  78. .get('/api/threads/' + thread._id)
  79. .end(function(err, res) {
  80. expect(err).to.be.null;
  81. expect(res).to.have.status(200);
  82.  
  83. //console.log(res.body);
  84.  
  85. assert.property(res.body, 'Comments');
  86. assert.property(res.body, 'Votes');
  87. assert.property(res.body, '_id');
  88. assert.property(res.body, 'User');
  89. assert.property(res.body, 'Title');
  90. assert.property(res.body, 'Content');
  91. done();
  92. });
  93. })
  94. });
  95.  
  96. it('updates an existing thread content', function (done) {
  97. dummyThread.newcontent = "This dummy thread has been updated!";
  98.  
  99. Thread.findOne({ _id: dummyThread.id }, function(error, thread) {
  100. chai.request(app)
  101. .put('/api/threads/' + thread._id)
  102. .send(dummyThread)
  103. .end(function(err, res) {
  104. expect(err).to.be.null;
  105. expect(res).to.have.status(202);
  106.  
  107.  
  108. dummyThread.content = dummyThread.newcontent;
  109. done();
  110. });
  111. })
  112. });
  113.  
  114. it('adds an comment to a thread', function (done) {
  115. Thread.findOne({ _id: dummyThread.id }, function(error, thread) {
  116. chai.request(app)
  117. .post('/api/threads/' + thread._id + "/comments")
  118. .send(dummyComment)
  119. .end(function(err, res) {
  120. expect(err).to.be.null;
  121. expect(res).to.have.status(202);
  122. done();
  123. });
  124. })
  125. });
  126.  
  127. it('adds a vote to an thread', function (done) {
  128. Thread.findOne({ _id: dummyThread.id }, function(error, thread) {
  129. chai.request(app)
  130. .post('/api/threads/' + thread._id + "/votes")
  131. .send({
  132. username: dummyUser.Username,
  133. votestatus: false
  134. })
  135. .end(function(err, res) {
  136. expect(err).to.be.null;
  137. expect(res).to.have.status(202);
  138. done();
  139. });
  140. })
  141. });
  142.  
  143. it('doesnt add duplicate votes' , function (done) {
  144. Thread.findOne({ _id: dummyThread.id }, function(error, thread) {
  145. chai.request(app)
  146. .post('/api/threads/' + thread._id + "/votes")
  147. .send({
  148. username: dummyUser.Username,
  149. votestatus: false
  150. })
  151. .end(function(err, res) {
  152. expect(err).to.be.null;
  153. expect(res.body.Votes).to.have.length(1);
  154. done();
  155. });
  156. })
  157. });
  158.  
  159. it('updates vote status of existing vote', function(done){
  160. Thread.findOne({ _id: dummyThread.id }, function(error, thread) {
  161. chai.request(app)
  162. .post('/api/threads/' + thread._id + "/votes")
  163. .send({
  164. username: dummyUser.Username,
  165. votestatus: true
  166. })
  167. .end(function(err, res) {
  168. expect(err).to.be.null;
  169. expect(res).to.have.status(202);
  170. expect(res.body.Votes[0].VoteStatus).to.be.equal(true);
  171. done();
  172. });
  173. })
  174. })
  175.  
  176. it('gets a list of all threads sorted by most upvotes', function(done){
  177. Thread.create(dummyThread2);
  178. chai.request(app)
  179. .get('/api/threads')
  180. .set('sortBy', 'upvotes')
  181. .end(function(err, res){
  182. expect(err).to.be.null;
  183. expect(res.body[0]._id).to.be.equal(dummyThread.id);
  184. done();
  185. })
  186. })
  187.  
  188. it('gets a list of all threads sorted by highest score', function(done){
  189. chai.request(app)
  190. .get('/api/threads')
  191. .set('sortBy', 'score')
  192. .end(function(err, res){
  193. expect(err).to.be.null;
  194. expect(res.body[0]._id).to.be.equal(dummyThread.id);
  195. done();
  196. })
  197. })
  198.  
  199.  
  200. it('deactivates an existing thread', function (done) {
  201. Thread.findOne({ _id: dummyThread.id }, function(error, thread) {
  202. chai.request(app)
  203. .delete('/api/threads/' + thread._id)
  204.  
  205. .end(function(err, res) {
  206. expect(err).to.be.null;
  207. expect(res).to.have.status(200);
  208. done();
  209. });
  210. })
  211. });
  212. });
  213.  
  214.  
  215. // removing dummy data from database.
  216. after(function() {
  217. Thread.findOneAndDelete({ _id: dummyThread.id }, function (err) {});
  218. Thread.findOneAndDelete({ _id: dummyThread2.id }, function (err) {});
  219. User.findOneAndDelete({ Username: dummyUser.Username }, function (err) {});
  220. User.findOneAndDelete({ Username: dummyUser2.Username }, function (err) {});
  221. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement