thread_post.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. process.env.NODE_ENV = 'test'
  2. let chai = require('chai')
  3. let server = require('../server')
  4. let should = chai.should()
  5. let expect = chai.expect
  6. let { sequelize } = require('../models')
  7. const Errors = require('../lib/errors.js')
  8. let PAGINATION_THREAD_ID
  9. chai.use(require('chai-http'))
  10. chai.use(require('chai-things'))
  11. describe('Thread and post', () => {
  12. let userAgent, replyAgent
  13. //Wait for app to start before commencing
  14. before((done) => {
  15. if(server.locals.appStarted) mockData()
  16. server.on('appStarted', () => {
  17. mockData()
  18. })
  19. function mockData() {
  20. userAgent = chai.request.agent(server)
  21. replyAgent = chai.request.agent(server)
  22. userAgent
  23. .post('/api/v1/user')
  24. .set('content-type', 'application/json')
  25. .send({
  26. username: 'username',
  27. password: 'password',
  28. admin: true
  29. })
  30. .then(() => {
  31. userAgent
  32. .post('/api/v1/category')
  33. .set('content-type', 'application/json')
  34. .send({ name: 'category_name' })
  35. .then(() => {
  36. return userAgent
  37. .post('/api/v1/category')
  38. .set('content-type', 'application/json')
  39. .send({ name: 'category with spaces' })
  40. })
  41. .then(() => { done() })
  42. .catch(done)
  43. })
  44. .catch(done)
  45. }
  46. })
  47. //Delete all rows in table after
  48. //tess completed
  49. after(() => {
  50. sequelize.sync({ force: true })
  51. })
  52. describe('POST /thread', () => {
  53. it('should create a thread if logged in', async () => {
  54. let res = await userAgent
  55. .post('/api/v1/thread')
  56. .set('content-type', 'application/json')
  57. .send({
  58. name: 'thread',
  59. category: 'CATEGORY_NAME'
  60. })
  61. res.should.have.status(200)
  62. res.should.be.json
  63. res.body.should.have.property('name', 'thread')
  64. res.body.should.have.property('postsCount', 0)
  65. res.body.should.have.property('slug', 'thread')
  66. res.body.should.have.deep.property('User.username', 'username')
  67. res.body.should.have.deep.property('Category.name', 'category_name')
  68. })
  69. it('should create a thread for a category with spaces in', async () => {
  70. let res = await userAgent
  71. .post('/api/v1/thread')
  72. .set('content-type', 'application/json')
  73. .send({
  74. name: 'thread123',
  75. category: 'CATEGORY_WITH_SPACES'
  76. })
  77. res.should.have.status(200)
  78. res.should.be.json
  79. res.body.should.have.property('name', 'thread123')
  80. res.body.should.have.property('postsCount', 0)
  81. res.body.should.have.property('slug', 'thread123')
  82. res.body.should.have.deep.property('User.username', 'username')
  83. res.body.should.have.deep.property('Category.name', 'category with spaces')
  84. })
  85. it('should add a slug from the thread name', async () => {
  86. let res = await userAgent
  87. .post('/api/v1/thread')
  88. .set('content-type', 'application/json')
  89. .send({
  90. name: ' à long thrËad, with lØts of àccents!!! ',
  91. category: 'CATEGORY_NAME'
  92. })
  93. res.should.have.status(200)
  94. res.should.be.json
  95. res.body.should.have.property('name', ' à long thrËad, with lØts of àccents!!! ')
  96. res.body.should.have.property('slug', 'a-long-thread-with-lots-of-accents')
  97. res.body.should.have.deep.property('User.username', 'username')
  98. res.body.should.have.deep.property('Category.name', 'category_name')
  99. })
  100. it('should return an error if not logged in', async () => {
  101. try {
  102. let res = await chai.request(server)
  103. .post('/api/v1/thread')
  104. .set('content-type', 'application/json')
  105. .send({
  106. name: 'thread',
  107. category: 'CATEGORY_NAME'
  108. })
  109. } catch (res) {
  110. res.should.have.status(401)
  111. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  112. }
  113. })
  114. it('should return an error if missing title', async () => {
  115. try {
  116. let res = await userAgent
  117. .post('/api/v1/thread')
  118. .send({
  119. category: 'CATEGORY_NAME'
  120. })
  121. } catch (res) {
  122. let body = JSON.parse(res.response.text)
  123. res.should.have.status(400)
  124. body.errors.should.contain.something.that.has.property('message', 'name cannot be null')
  125. }
  126. })
  127. it('should return an error if name has no length', done => {
  128. userAgent
  129. .post('/api/v1/thread')
  130. .set('content-type', 'application/json')
  131. .send({
  132. name: '',
  133. category: 'CATEGORY_NAME'
  134. })
  135. .end((err, res) => {
  136. res.should.be.json
  137. res.should.have.status(400)
  138. res.body.errors.should.contain.something.that.has.property('message', 'The title cannot be empty')
  139. done()
  140. })
  141. })
  142. it('should return an error if invalid types', async () => {
  143. try {
  144. let res = await userAgent
  145. .post('/api/v1/thread')
  146. .set('content-type', 'application/json')
  147. .send({
  148. name: 123,
  149. category: 'CATEGORY_NAME'
  150. })
  151. } catch (res) {
  152. let body = JSON.parse(res.response.text)
  153. res.should.have.status(400)
  154. body.errors.should.contain.something.that.has.property('message', 'The title must be a string')
  155. }
  156. })
  157. it('should return an error if category does not exist', async () => {
  158. try {
  159. let res = await userAgent
  160. .post('/api/v1/thread')
  161. .set('content-type', 'application/json')
  162. .send({
  163. name: 'thread1',
  164. category: 'non-existent'
  165. })
  166. } catch (res) {
  167. res.should.have.status(400)
  168. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.invalidCategory)
  169. }
  170. })
  171. })
  172. describe('PUT /thread', () => {
  173. let threadId
  174. let normalUserAgent = chai.request.agent(server)
  175. before(done => {
  176. userAgent
  177. .post('/api/v1/thread')
  178. .set('content-type', 'application/json')
  179. .send({
  180. name: 'thread_lock',
  181. category: 'CATEGORY_NAME'
  182. })
  183. .then(res => {
  184. threadId = res.body.id
  185. return normalUserAgent
  186. .post('/api/v1/user')
  187. .set('content-type', 'application/json')
  188. .send({
  189. username: 'normaluseragent',
  190. password: 'password'
  191. })
  192. })
  193. .then(_ => {
  194. done()
  195. })
  196. .catch(done)
  197. })
  198. it('should lock the thread', async () => {
  199. let res = await userAgent
  200. .put('/api/v1/thread/' + threadId)
  201. .set('content-type', 'application/json')
  202. .send({
  203. locked: true
  204. })
  205. res.should.be.json
  206. res.should.have.status(200)
  207. res.body.should.have.property('success', true)
  208. let thread = await userAgent.get('/api/v1/thread/' + threadId)
  209. thread.body.should.have.property('locked', true)
  210. })
  211. it('should unlock the thread', async () => {
  212. let res = await userAgent
  213. .put('/api/v1/thread/' + threadId)
  214. .set('content-type', 'application/json')
  215. .send({
  216. locked: false
  217. })
  218. res.should.be.json
  219. res.should.have.status(200)
  220. res.body.should.have.property('success', true)
  221. let thread = await userAgent.get('/api/v1/thread/' + threadId)
  222. thread.body.should.have.property('locked', false)
  223. })
  224. it('should return an error if thread does not exist', done => {
  225. userAgent
  226. .put('/api/v1/thread/not_a_thread')
  227. .set('content-type', 'application/json')
  228. .send({
  229. locked: false
  230. })
  231. .end((err, res) => {
  232. res.should.be.json
  233. res.should.have.status(400)
  234. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('threadId', 'thread does not exist'))
  235. done()
  236. })
  237. })
  238. it('should return an error if not logged in', done => {
  239. chai.request(server)
  240. .put('/api/v1/thread/' + threadId)
  241. .set('content-type', 'application/json')
  242. .send({
  243. locked: false
  244. })
  245. .end((err, res) => {
  246. res.should.be.json
  247. res.should.have.status(401)
  248. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  249. done()
  250. })
  251. })
  252. it('should return an error if not an administrator', done => {
  253. normalUserAgent
  254. .put('/api/v1/thread/' + threadId)
  255. .set('content-type', 'application/json')
  256. .send({
  257. locked: false
  258. })
  259. .end((err, res) => {
  260. res.should.be.json
  261. res.should.have.status(401)
  262. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  263. done()
  264. })
  265. })
  266. it('should not allow new posts if locked', done => {
  267. userAgent
  268. .put('/api/v1/thread/' + threadId)
  269. .set('content-type', 'application/json')
  270. .send({
  271. locked: true
  272. })
  273. .end(_ => {
  274. userAgent
  275. .post('/api/v1/post')
  276. .set('content-type', 'application/json')
  277. .send({
  278. content: 'new post',
  279. threadId
  280. })
  281. .end((err, res) => {
  282. res.should.be.json
  283. res.should.have.status(400)
  284. res.body.errors.should.contain.something.that.deep.equals(Errors.threadLocked)
  285. done()
  286. })
  287. })
  288. })
  289. })
  290. describe('POST /post', () => {
  291. it('should create a post if logged in', async () => {
  292. let res = await userAgent
  293. .post('/api/v1/post')
  294. .set('content-type', 'application/json')
  295. .send({
  296. content: 'content',
  297. threadId: 1
  298. })
  299. res.should.be.json
  300. res.should.have.status(200)
  301. res.body.should.have.property('content', '<p>content</p>\n')
  302. res.body.should.have.property('postNumber', 0)
  303. res.body.should.have.deep.property('User.username', 'username')
  304. res.body.should.have.deep.property('Thread.name', 'thread')
  305. res.body.should.have.deep.property('Thread.postsCount', 1)
  306. })
  307. it('should return an error if not logged in', async () => {
  308. try {
  309. let res = await chai.request(server)
  310. .post('/api/v1/post')
  311. .set('content-type', 'application/json')
  312. .send({
  313. content: 'content',
  314. threadId: 1
  315. })
  316. res.should.be.json
  317. res.should.have.status(401)
  318. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  319. } catch (res) {
  320. res.should.have.status(401)
  321. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  322. }
  323. })
  324. it('should return an error if missing parameters', async () => {
  325. try {
  326. let res = await userAgent
  327. .post('/api/v1/post')
  328. res.should.be.json
  329. res.should.have.status(400)
  330. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('content'))
  331. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('threadId'))
  332. } catch (res) {
  333. let body = JSON.parse(res.response.text)
  334. res.should.have.status(400)
  335. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('content'))
  336. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('threadId'))
  337. }
  338. })
  339. it('should return an error if invalid types', async () => {
  340. try {
  341. let res = await userAgent
  342. .post('/api/v1/post')
  343. .set('content-type', 'application/json')
  344. .send({
  345. content: 123,
  346. threadId: 'string',
  347. replyingToId: 'string'
  348. })
  349. res.should.be.json
  350. res.should.have.status(400)
  351. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('content', 'string'))
  352. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('threadId', 'integer'))
  353. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('replyingToId', 'integer'))
  354. } catch (res) {
  355. let body = JSON.parse(res.response.text)
  356. res.should.have.status(400)
  357. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('content', 'string'))
  358. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('threadId', 'integer'))
  359. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('replyingToId', 'integer'))
  360. }
  361. })
  362. it('should return an error if thread id does not exist', async () => {
  363. try {
  364. let res = await userAgent
  365. .post('/api/v1/post')
  366. .set('content-type', 'application/json')
  367. .send({
  368. content: 'content',
  369. threadId: 10
  370. })
  371. res.should.be.json
  372. res.should.have.status(400)
  373. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('threadId', 'thread does not exist'))
  374. } catch (res) {
  375. let body = JSON.parse(res.response.text)
  376. res.should.have.status(400)
  377. body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('threadId', 'thread does not exist'))
  378. }
  379. })
  380. it('should be able to reply to a post', async () => {
  381. await replyAgent
  382. .post('/api/v1/user')
  383. .set('content-type', 'application/json')
  384. .send({
  385. username: 'username1',
  386. password: 'password'
  387. })
  388. let res = await replyAgent
  389. .post('/api/v1/post')
  390. .set('content-type', 'application/json')
  391. .send({
  392. content: 'another post',
  393. threadId: 1,
  394. replyingToId: 1
  395. })
  396. res.should.be.json
  397. res.should.have.status(200)
  398. res.body.should.have.property('postNumber', 1)
  399. res.body.should.have.property('content', '<p>another post</p>\n')
  400. res.body.should.have.deep.property('User.username', 'username1')
  401. res.body.should.have.deep.property('Thread.name', 'thread')
  402. res.body.should.have.deep.property('Thread.postsCount', 2)
  403. res.body.should.have.property('replyingToUsername', 'username')
  404. res.body.should.have.property('Replies').that.deep.equals([])
  405. })
  406. it('should return any replies to a post', async () => {
  407. let res = await replyAgent.get('/api/v1/post/1')
  408. res.should.be.json
  409. res.should.have.status(200)
  410. res.body.should.have.deep.property('replyingToUsername', null)
  411. res.body.should.have.deep.property('Replies.0.content', '<p>another post</p>\n')
  412. })
  413. it('should return an error if reply id does not exist', async () => {
  414. try {
  415. let res = await replyAgent
  416. .post('/api/v1/post')
  417. .set('content-type', 'application/json')
  418. .send({
  419. content: 'yet another post',
  420. threadId: 1,
  421. replyingToId: 10
  422. })
  423. res.should.have.status(400)
  424. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
  425. } catch (res) {
  426. let body = JSON.parse(res.response.text)
  427. res.should.have.status(400)
  428. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
  429. }
  430. })
  431. it('should return an error if post reply not in same thread', async () => {
  432. try {
  433. let threadId = (await replyAgent
  434. .post('/api/v1/thread')
  435. .set('content-type', 'application/json')
  436. .send({
  437. name: 'another thread',
  438. category: 'CATEGORY_NAME'
  439. })).body.id
  440. let res = await replyAgent
  441. .post('/api/v1/post')
  442. .set('content-type', 'application/json')
  443. .send({
  444. content: 'yet another post',
  445. threadId: threadId,
  446. replyingToId: 1
  447. })
  448. res.should.have.status(400)
  449. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
  450. } catch (res) {
  451. let body = JSON.parse(res.response.text)
  452. res.should.have.status(400)
  453. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
  454. }
  455. })
  456. })
  457. describe('GET /thread/:id', () => {
  458. it('should return the thread and corresponding posts', async () => {
  459. let res = await chai.request(server).get('/api/v1/thread/1')
  460. res.should.have.status(200)
  461. res.should.be.json
  462. res.body.should.have.property('name', 'thread')
  463. res.body.should.have.deep.property('Category.name', 'category_name')
  464. res.body.should.have.deep.property('User.username', 'username')
  465. res.body.should.have.property('Posts')
  466. res.body.Posts.should.have.property('length', 2)
  467. res.body.Posts.should.contain.something.that.has.property('content', '<p>content</p>\n')
  468. res.body.Posts.should.contain.something.that.has.deep.property('User.username', 'username')
  469. res.body.Posts.should.contain.something.that.has.property('content', '<p>another post</p>\n')
  470. res.body.Posts.should.contain.something.that.has.deep.property('User.username', 'username1')
  471. })
  472. it('should allow pagination', async () => {
  473. let thread = await userAgent
  474. .post('/api/v1/thread')
  475. .set('content-type', 'application/json')
  476. .send({ category: 'CATEGORY_NAME', name: 'pagination' })
  477. let threadOther = await userAgent
  478. .post('/api/v1/thread')
  479. .set('content-type', 'application/json')
  480. .send({ category: 'CATEGORY_NAME', name: 'pagination_other' })
  481. PAGINATION_THREAD_ID = thread.body.id
  482. for(var i = 0; i < 30; i++) {
  483. let post = await userAgent
  484. .post('/api/v1/post')
  485. .set('content-type', 'application/json')
  486. .send({ threadId: thread.body.id, content: `POST ${i}` })
  487. if(i === 3) {
  488. await userAgent
  489. .post('/api/v1/post')
  490. .set('content-type', 'application/json')
  491. .send({ threadId: threadOther.body.id, content: `POST OTHER ${i}` })
  492. }
  493. }
  494. let pageOne = await userAgent.get('/api/v1/thread/' + thread.body.id)
  495. let pageTwo = await userAgent.get(pageOne.body.meta.nextURL)
  496. let pageThree = await userAgent.get(pageTwo.body.meta.nextURL)
  497. let pageInvalid = await userAgent.get('/api/v1/thread/' + thread.body.id + '?from=' + 100)
  498. pageOne.body.Posts.should.have.length(10)
  499. pageOne.body.meta.should.have.property('postsRemaining', 20)
  500. pageOne.body.meta.should.have.property('previousPostsCount', 0)
  501. pageOne.body.meta.should.have.property('nextPostsCount', 10)
  502. pageOne.body.Posts[0].should.have.property('content', '<p>POST 0</p>\n')
  503. pageTwo.body.Posts.should.have.length(10)
  504. pageTwo.body.meta.should.have.property('postsRemaining', 10)
  505. pageTwo.body.meta.should.have.property('previousPostsCount', 10)
  506. pageTwo.body.meta.should.have.property('nextPostsCount', 10)
  507. pageTwo.body.Posts[0].should.have.property('content', '<p>POST 10</p>\n')
  508. pageTwo.body.meta.should.have.property('previousURL')
  509. pageThree.body.Posts.should.have.length(10)
  510. pageThree.body.meta.should.have.property('postsRemaining', 0)
  511. pageThree.body.meta.should.have.property('previousPostsCount', 10)
  512. pageThree.body.meta.should.have.property('nextPostsCount', 0)
  513. pageThree.body.Posts[0].should.have.property('content', '<p>POST 20</p>\n')
  514. pageThree.body.Posts[9].should.have.property('content', '<p>POST 29</p>\n')
  515. expect(pageThree.body.meta.nextURL).to.be.null
  516. pageInvalid.body.Posts.should.have.length(0)
  517. })
  518. it('should allow you to get an individual and surrounding posts', async () => {
  519. let http = chai.request(server)
  520. let pageOne = await http.get(`/api/v1/thread/${PAGINATION_THREAD_ID}?postNumber=15`)
  521. let pageZero = await http.get(pageOne.body.meta.previousURL)
  522. let pageTwo = await http.get(pageOne.body.meta.nextURL)
  523. pageOne.body.Posts.should.have.length(10)
  524. pageOne.body.Posts[0].should.have.property('content', '<p>POST 11</p>\n')
  525. pageOne.body.Posts[4].should.have.property('content', '<p>POST 15</p>\n')
  526. pageOne.body.Posts[9].should.have.property('content', '<p>POST 20</p>\n')
  527. pageOne.body.meta.should.have.property('postsRemaining', 9)
  528. pageOne.body.meta.should.have.property('previousPostsCount', 10)
  529. pageOne.body.meta.should.have.property('nextPostsCount', 9)
  530. pageTwo.body.Posts.should.have.length(9)
  531. pageTwo.body.Posts[0].should.have.property('content', '<p>POST 21</p>\n')
  532. pageTwo.body.Posts[8].should.have.property('content', '<p>POST 29</p>\n')
  533. pageTwo.body.meta.should.have.property('nextURL', null)
  534. pageTwo.body.meta.should.have.property('postsRemaining', 0)
  535. pageTwo.body.meta.should.have.property('previousPostsCount', 10)
  536. pageTwo.body.meta.should.have.property('nextPostsCount', 0)
  537. pageZero.body.Posts.should.have.length(10)
  538. pageZero.body.Posts[0].should.have.property('content', '<p>POST 1</p>\n')
  539. pageZero.body.Posts[9].should.have.property('content', '<p>POST 10</p>\n')
  540. pageZero.body.meta.should.have.property('postsRemaining', 19)
  541. pageZero.body.meta.should.have.property('previousPostsCount', 1)
  542. pageZero.body.meta.should.have.property('nextPostsCount', 10)
  543. let pageFirst = await http.get(pageZero.body.meta.previousURL)
  544. pageFirst.body.Posts[0].should.have.property('content', '<p>POST 0</p>\n')
  545. pageFirst.body.meta.should.have.property('previousURL', null)
  546. pageFirst.body.meta.should.have.property('postsRemaining', 29)
  547. pageFirst.body.meta.should.have.property('previousPostsCount', 0)
  548. })
  549. it('should return an error if :id is invalid', async () => {
  550. try {
  551. let res = await chai.request(server).get('/api/v1/thread/invalid')
  552. res.should.have.status(400)
  553. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  554. } catch (res) {
  555. let body = JSON.parse(res.response.text)
  556. res.should.have.status(400)
  557. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  558. }
  559. })
  560. })
  561. describe('GET /post/:id', () => {
  562. it('should return the post', async () => {
  563. let res = await chai.request(server).get('/api/v1/post/1')
  564. res.should.have.status(200)
  565. res.should.be.json
  566. res.body.should.have.property('content', '<p>content</p>\n')
  567. res.body.should.have.deep.property('User.username', 'username')
  568. res.body.should.have.deep.property('Thread.name', 'thread')
  569. res.body.should.have.deep.property('Thread.Category.name', 'category_name')
  570. res.body.should.have.deep.property('Replies.0.User.username', 'username1')
  571. })
  572. it('should return an error if invalid post id', async () => {
  573. try {
  574. let res = await chai.request(server).get('/api/v1/post/invalid')
  575. res.should.have.status(400)
  576. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'post does not exist'))
  577. } catch (res) {
  578. let body = JSON.parse(res.response.text)
  579. res.should.have.status(400)
  580. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'post does not exist'))
  581. }
  582. })
  583. })
  584. describe('DELETE /post/:id', () => {
  585. let threadId
  586. let postId
  587. let normalUserAgent = chai.request.agent(server)
  588. before(done => {
  589. userAgent
  590. .post('/api/v1/thread')
  591. .set('content-type', 'application/json')
  592. .send({
  593. name: 'delete_post_thread',
  594. category: 'CATEGORY_NAME'
  595. })
  596. .then(res => {
  597. threadId = res.body.id
  598. return userAgent
  599. .post('/api/v1/post')
  600. .set('content-type', 'application/json')
  601. .send({
  602. content: 'test content here',
  603. threadId
  604. })
  605. })
  606. .then(res => {
  607. postId = res.body.id
  608. return normalUserAgent
  609. .post('/api/v1/user')
  610. .set('content-type', 'application/json')
  611. .send({
  612. username: 'delete_post_non_admin',
  613. password: 'password'
  614. })
  615. })
  616. .then(_ => {
  617. done()
  618. })
  619. .catch(done)
  620. })
  621. it('should remove the post', async () => {
  622. let res = await userAgent.delete('/api/v1/post/' + postId)
  623. res.should.be.json
  624. res.should.have.status(200)
  625. res.body.should.have.property('success', true)
  626. let post = await userAgent.get('/api/v1/post/' + postId)
  627. post.body.should.have.property('removed', true)
  628. post.body.should.have.property('content', '<p>[This post has been removed by an administrator]</p>\n')
  629. })
  630. it('should return an error if trying to reply to a removed post', async () => {
  631. replyAgent
  632. .post('/api/v1/post')
  633. .set('content-type', 'application/json')
  634. .send({
  635. content: 'reply to deleted post',
  636. replyId: postId,
  637. threadId
  638. })
  639. .end((err, res) => {
  640. res.should.be.json
  641. res.should.have.status(400)
  642. res.body.errors.should.include.something.that.deep.equals(Errors.postRemoved)
  643. })
  644. })
  645. it('should return an error if post does not exist', done => {
  646. userAgent
  647. .delete('/api/v1/post/not_a_post')
  648. .end((err, res) => {
  649. res.should.be.json
  650. res.should.have.status(400)
  651. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('postId', 'post does not exist'))
  652. done()
  653. })
  654. })
  655. it('should return an error if not an admin', done => {
  656. normalUserAgent
  657. .delete('/api/v1/post/' + postId)
  658. .end((err, res) => {
  659. res.should.be.json
  660. res.should.have.status(401)
  661. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  662. done()
  663. })
  664. })
  665. })
  666. })