thread_post.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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. res.should.be.json
  110. res.should.have.status(401)
  111. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  112. } catch (res) {
  113. res.should.have.status(401)
  114. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  115. }
  116. })
  117. it('should return an error if missing parameters', async () => {
  118. try {
  119. let res = await userAgent
  120. .post('/api/v1/thread')
  121. res.should.be.json
  122. res.should.have.status(400)
  123. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('name'))
  124. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('category'))
  125. } catch (res) {
  126. let body = JSON.parse(res.response.text)
  127. res.should.have.status(400)
  128. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('name'))
  129. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('category'))
  130. }
  131. })
  132. it('should return an error if name has no length', done => {
  133. userAgent
  134. .post('/api/v1/thread')
  135. .set('content-type', 'application/json')
  136. .send({
  137. name: '',
  138. category: 'CATEGORY_NAME'
  139. })
  140. .end((err, res) => {
  141. res.should.be.json
  142. res.should.have.status(400)
  143. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('name'))
  144. done()
  145. })
  146. })
  147. it('should return an error if invalid types', async () => {
  148. try {
  149. let res = await userAgent
  150. .post('/api/v1/thread')
  151. .set('content-type', 'application/json')
  152. .send({
  153. name: 123,
  154. category: 123
  155. })
  156. res.should.be.json
  157. res.should.have.status(400)
  158. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('name', 'string'))
  159. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('category', 'string'))
  160. } catch (res) {
  161. let body = JSON.parse(res.response.text)
  162. res.should.have.status(400)
  163. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('name', 'string'))
  164. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('category', 'string'))
  165. }
  166. })
  167. it('should return an error if category does not exist', async () => {
  168. try {
  169. let res = await userAgent
  170. .post('/api/v1/thread')
  171. .set('content-type', 'application/json')
  172. .send({
  173. name: 'thread1',
  174. category: 'non-existent'
  175. })
  176. res.should.be.json
  177. res.should.have.status(400)
  178. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidCategory)
  179. } catch (res) {
  180. res.should.have.status(400)
  181. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.invalidCategory)
  182. }
  183. })
  184. })
  185. describe('PUT /thread', () => {
  186. let threadId
  187. before(done => {
  188. userAgent
  189. .post('/api/v1/thread')
  190. .set('content-type', 'application/json')
  191. .send({
  192. name: 'thread_lock',
  193. category: 'CATEGORY_NAME'
  194. })
  195. .then(res => {
  196. threadId = res.body.id
  197. done()
  198. })
  199. .catch(done)
  200. })
  201. it('should lock the thread', async () => {
  202. let res = await userAgent
  203. .put('/api/v1/thread/' + threadId)
  204. .set('content-type', 'application/json')
  205. .send({
  206. locked: true
  207. })
  208. res.should.be.json
  209. res.should.have.status(200)
  210. res.body.should.have.property('success', true)
  211. let thread = await userAgent.get('/api/v1/thread/' + threadId)
  212. thread.body.should.have.property('locked', true)
  213. })
  214. it('should unlock the thread', async () => {
  215. let res = await userAgent
  216. .put('/api/v1/thread/' + threadId)
  217. .set('content-type', 'application/json')
  218. .send({
  219. locked: false
  220. })
  221. res.should.be.json
  222. res.should.have.status(200)
  223. res.body.should.have.property('success', true)
  224. let thread = await userAgent.get('/api/v1/thread/' + threadId)
  225. thread.body.should.have.property('locked', false)
  226. })
  227. it('should return an error if thread does not exist', done => {
  228. userAgent
  229. .put('/api/v1/thread/not_a_thread')
  230. .set('content-type', 'application/json')
  231. .send({
  232. locked: false
  233. })
  234. .end((err, res) => {
  235. res.should.be.json
  236. res.should.have.status(400)
  237. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('threadId', 'thread does not exist'))
  238. done()
  239. })
  240. })
  241. it('should return an error if not logged in', done => {
  242. chai.request(server)
  243. .put('/api/v1/thread/' + threadId)
  244. .set('content-type', 'application/json')
  245. .send({
  246. locked: false
  247. })
  248. .end((err, res) => {
  249. res.should.be.json
  250. res.should.have.status(401)
  251. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  252. done()
  253. })
  254. })
  255. it('should not allow new posts if locked', done => {
  256. userAgent
  257. .put('/api/v1/thread/' + threadId)
  258. .set('content-type', 'application/json')
  259. .send({
  260. locked: true
  261. })
  262. .end(_ => {
  263. userAgent
  264. .post('/api/v1/post')
  265. .set('content-type', 'application/json')
  266. .send({
  267. content: 'new post',
  268. threadId
  269. })
  270. .end((err, res) => {
  271. res.should.be.json
  272. res.should.have.status(400)
  273. res.body.errors.should.contain.something.that.deep.equals(Errors.threadLocked)
  274. done()
  275. })
  276. })
  277. })
  278. })
  279. describe('POST /post', () => {
  280. it('should create a post if logged in', async () => {
  281. let res = await userAgent
  282. .post('/api/v1/post')
  283. .set('content-type', 'application/json')
  284. .send({
  285. content: 'content',
  286. threadId: 1
  287. })
  288. res.should.be.json
  289. res.should.have.status(200)
  290. res.body.should.have.property('content', '<p>content</p>\n')
  291. res.body.should.have.property('postNumber', 0)
  292. res.body.should.have.deep.property('User.username', 'username')
  293. res.body.should.have.deep.property('Thread.name', 'thread')
  294. res.body.should.have.deep.property('Thread.postsCount', 1)
  295. })
  296. it('should return an error if not logged in', async () => {
  297. try {
  298. let res = await chai.request(server)
  299. .post('/api/v1/post')
  300. .set('content-type', 'application/json')
  301. .send({
  302. content: 'content',
  303. threadId: 1
  304. })
  305. res.should.be.json
  306. res.should.have.status(401)
  307. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  308. } catch (res) {
  309. res.should.have.status(401)
  310. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  311. }
  312. })
  313. it('should return an error if missing parameters', async () => {
  314. try {
  315. let res = await userAgent
  316. .post('/api/v1/post')
  317. res.should.be.json
  318. res.should.have.status(400)
  319. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('content'))
  320. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('threadId'))
  321. } catch (res) {
  322. let body = JSON.parse(res.response.text)
  323. res.should.have.status(400)
  324. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('content'))
  325. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('threadId'))
  326. }
  327. })
  328. it('should return an error if invalid types', async () => {
  329. try {
  330. let res = await userAgent
  331. .post('/api/v1/post')
  332. .set('content-type', 'application/json')
  333. .send({
  334. content: 123,
  335. threadId: 'string',
  336. replyingToId: 'string'
  337. })
  338. res.should.be.json
  339. res.should.have.status(400)
  340. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('content', 'string'))
  341. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('threadId', 'integer'))
  342. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('replyingToId', 'integer'))
  343. } catch (res) {
  344. let body = JSON.parse(res.response.text)
  345. res.should.have.status(400)
  346. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('content', 'string'))
  347. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('threadId', 'integer'))
  348. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('replyingToId', 'integer'))
  349. }
  350. })
  351. it('should return an error if thread id does not exist', async () => {
  352. try {
  353. let res = await userAgent
  354. .post('/api/v1/post')
  355. .set('content-type', 'application/json')
  356. .send({
  357. content: 'content',
  358. threadId: 10
  359. })
  360. res.should.be.json
  361. res.should.have.status(400)
  362. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('threadId', 'thread does not exist'))
  363. } catch (res) {
  364. let body = JSON.parse(res.response.text)
  365. res.should.have.status(400)
  366. body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('threadId', 'thread does not exist'))
  367. }
  368. })
  369. it('should be able to reply to a post', async () => {
  370. await replyAgent
  371. .post('/api/v1/user')
  372. .set('content-type', 'application/json')
  373. .send({
  374. username: 'username1',
  375. password: 'password'
  376. })
  377. let res = await replyAgent
  378. .post('/api/v1/post')
  379. .set('content-type', 'application/json')
  380. .send({
  381. content: 'another post',
  382. threadId: 1,
  383. replyingToId: 1
  384. })
  385. res.should.be.json
  386. res.should.have.status(200)
  387. res.body.should.have.property('postNumber', 1)
  388. res.body.should.have.property('content', '<p>another post</p>\n')
  389. res.body.should.have.deep.property('User.username', 'username1')
  390. res.body.should.have.deep.property('Thread.name', 'thread')
  391. res.body.should.have.deep.property('Thread.postsCount', 2)
  392. res.body.should.have.property('replyingToUsername', 'username')
  393. res.body.should.have.property('Replies').that.deep.equals([])
  394. })
  395. it('should return any replies to a post', async () => {
  396. let res = await replyAgent.get('/api/v1/post/1')
  397. res.should.be.json
  398. res.should.have.status(200)
  399. res.body.should.have.deep.property('replyingToUsername', null)
  400. res.body.should.have.deep.property('Replies.0.content', '<p>another post</p>\n')
  401. })
  402. it('should return an error if reply id does not exist', async () => {
  403. try {
  404. let res = await replyAgent
  405. .post('/api/v1/post')
  406. .set('content-type', 'application/json')
  407. .send({
  408. content: 'yet another post',
  409. threadId: 1,
  410. replyingToId: 10
  411. })
  412. res.should.have.status(400)
  413. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
  414. } catch (res) {
  415. let body = JSON.parse(res.response.text)
  416. res.should.have.status(400)
  417. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
  418. }
  419. })
  420. it('should return an error if post reply not in same thread', async () => {
  421. try {
  422. let threadId = (await replyAgent
  423. .post('/api/v1/thread')
  424. .set('content-type', 'application/json')
  425. .send({
  426. name: 'another thread',
  427. category: 'CATEGORY_NAME'
  428. })).body.id
  429. let res = await replyAgent
  430. .post('/api/v1/post')
  431. .set('content-type', 'application/json')
  432. .send({
  433. content: 'yet another post',
  434. threadId: threadId,
  435. replyingToId: 1
  436. })
  437. res.should.have.status(400)
  438. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
  439. } catch (res) {
  440. let body = JSON.parse(res.response.text)
  441. res.should.have.status(400)
  442. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
  443. }
  444. })
  445. })
  446. describe('GET /thread/:id', () => {
  447. it('should return the thread and corresponding posts', async () => {
  448. let res = await chai.request(server).get('/api/v1/thread/1')
  449. res.should.have.status(200)
  450. res.should.be.json
  451. res.body.should.have.property('name', 'thread')
  452. res.body.should.have.deep.property('Category.name', 'category_name')
  453. res.body.should.have.deep.property('User.username', 'username')
  454. res.body.should.have.property('Posts')
  455. res.body.Posts.should.have.property('length', 2)
  456. res.body.Posts.should.contain.something.that.has.property('content', '<p>content</p>\n')
  457. res.body.Posts.should.contain.something.that.has.deep.property('User.username', 'username')
  458. res.body.Posts.should.contain.something.that.has.property('content', '<p>another post</p>\n')
  459. res.body.Posts.should.contain.something.that.has.deep.property('User.username', 'username1')
  460. })
  461. it('should allow pagination', async () => {
  462. let thread = await userAgent
  463. .post('/api/v1/thread')
  464. .set('content-type', 'application/json')
  465. .send({ category: 'CATEGORY_NAME', name: 'pagination' })
  466. let threadOther = await userAgent
  467. .post('/api/v1/thread')
  468. .set('content-type', 'application/json')
  469. .send({ category: 'CATEGORY_NAME', name: 'pagination_other' })
  470. PAGINATION_THREAD_ID = thread.body.id
  471. for(var i = 0; i < 30; i++) {
  472. let post = await userAgent
  473. .post('/api/v1/post')
  474. .set('content-type', 'application/json')
  475. .send({ threadId: thread.body.id, content: `POST ${i}` })
  476. if(i === 3) {
  477. await userAgent
  478. .post('/api/v1/post')
  479. .set('content-type', 'application/json')
  480. .send({ threadId: threadOther.body.id, content: `POST OTHER ${i}` })
  481. }
  482. }
  483. let pageOne = await userAgent.get('/api/v1/thread/' + thread.body.id)
  484. let pageTwo = await userAgent.get(pageOne.body.meta.nextURL)
  485. let pageThree = await userAgent.get(pageTwo.body.meta.nextURL)
  486. let pageInvalid = await userAgent.get('/api/v1/thread/' + thread.body.id + '?from=' + 100)
  487. pageOne.body.Posts.should.have.length(10)
  488. pageOne.body.meta.should.have.property('postsRemaining', 20)
  489. pageOne.body.meta.should.have.property('previousPostsCount', 0)
  490. pageOne.body.meta.should.have.property('nextPostsCount', 10)
  491. pageOne.body.Posts[0].should.have.property('content', '<p>POST 0</p>\n')
  492. pageTwo.body.Posts.should.have.length(10)
  493. pageTwo.body.meta.should.have.property('postsRemaining', 10)
  494. pageTwo.body.meta.should.have.property('previousPostsCount', 10)
  495. pageTwo.body.meta.should.have.property('nextPostsCount', 10)
  496. pageTwo.body.Posts[0].should.have.property('content', '<p>POST 10</p>\n')
  497. pageTwo.body.meta.should.have.property('previousURL')
  498. pageThree.body.Posts.should.have.length(10)
  499. pageThree.body.meta.should.have.property('postsRemaining', 0)
  500. pageThree.body.meta.should.have.property('previousPostsCount', 10)
  501. pageThree.body.meta.should.have.property('nextPostsCount', 0)
  502. pageThree.body.Posts[0].should.have.property('content', '<p>POST 20</p>\n')
  503. pageThree.body.Posts[9].should.have.property('content', '<p>POST 29</p>\n')
  504. expect(pageThree.body.meta.nextURL).to.be.null
  505. pageInvalid.body.Posts.should.have.length(0)
  506. })
  507. it('should allow you to get an individual and surrounding posts', async () => {
  508. let http = chai.request(server)
  509. let pageOne = await http.get(`/api/v1/thread/${PAGINATION_THREAD_ID}?postNumber=15`)
  510. let pageZero = await http.get(pageOne.body.meta.previousURL)
  511. let pageTwo = await http.get(pageOne.body.meta.nextURL)
  512. pageOne.body.Posts.should.have.length(10)
  513. pageOne.body.Posts[0].should.have.property('content', '<p>POST 11</p>\n')
  514. pageOne.body.Posts[4].should.have.property('content', '<p>POST 15</p>\n')
  515. pageOne.body.Posts[9].should.have.property('content', '<p>POST 20</p>\n')
  516. pageOne.body.meta.should.have.property('postsRemaining', 9)
  517. pageOne.body.meta.should.have.property('previousPostsCount', 10)
  518. pageOne.body.meta.should.have.property('nextPostsCount', 9)
  519. pageTwo.body.Posts.should.have.length(9)
  520. pageTwo.body.Posts[0].should.have.property('content', '<p>POST 21</p>\n')
  521. pageTwo.body.Posts[8].should.have.property('content', '<p>POST 29</p>\n')
  522. pageTwo.body.meta.should.have.property('nextURL', null)
  523. pageTwo.body.meta.should.have.property('postsRemaining', 0)
  524. pageTwo.body.meta.should.have.property('previousPostsCount', 10)
  525. pageTwo.body.meta.should.have.property('nextPostsCount', 0)
  526. pageZero.body.Posts.should.have.length(10)
  527. pageZero.body.Posts[0].should.have.property('content', '<p>POST 1</p>\n')
  528. pageZero.body.Posts[9].should.have.property('content', '<p>POST 10</p>\n')
  529. pageZero.body.meta.should.have.property('postsRemaining', 19)
  530. pageZero.body.meta.should.have.property('previousPostsCount', 1)
  531. pageZero.body.meta.should.have.property('nextPostsCount', 10)
  532. let pageFirst = await http.get(pageZero.body.meta.previousURL)
  533. pageFirst.body.Posts[0].should.have.property('content', '<p>POST 0</p>\n')
  534. pageFirst.body.meta.should.have.property('previousURL', null)
  535. pageFirst.body.meta.should.have.property('postsRemaining', 29)
  536. pageFirst.body.meta.should.have.property('previousPostsCount', 0)
  537. })
  538. it('should return an error if :id is invalid', async () => {
  539. try {
  540. let res = await chai.request(server).get('/api/v1/thread/invalid')
  541. res.should.have.status(400)
  542. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  543. } catch (res) {
  544. let body = JSON.parse(res.response.text)
  545. res.should.have.status(400)
  546. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  547. }
  548. })
  549. })
  550. describe('GET /post/:id', () => {
  551. it('should return the post', async () => {
  552. let res = await chai.request(server).get('/api/v1/post/1')
  553. res.should.have.status(200)
  554. res.should.be.json
  555. res.body.should.have.property('content', '<p>content</p>\n')
  556. res.body.should.have.deep.property('User.username', 'username')
  557. res.body.should.have.deep.property('Thread.name', 'thread')
  558. res.body.should.have.deep.property('Thread.Category.name', 'category_name')
  559. res.body.should.have.deep.property('Replies.0.User.username', 'username1')
  560. })
  561. it('should return an error if invalid post id', async () => {
  562. try {
  563. let res = await chai.request(server).get('/api/v1/post/invalid')
  564. res.should.have.status(400)
  565. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'post does not exist'))
  566. } catch (res) {
  567. let body = JSON.parse(res.response.text)
  568. res.should.have.status(400)
  569. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'post does not exist'))
  570. }
  571. })
  572. })
  573. describe('DELETE /post/:id', () => {
  574. let postId
  575. before(done => {
  576. userAgent
  577. .post('/api/v1/thread')
  578. .set('content-type', 'application/json')
  579. .send({
  580. name: 'delete_post_thread',
  581. category: 'CATEGORY_NAME'
  582. })
  583. .then(res => {
  584. let threadId = res.body.id
  585. return userAgent
  586. .post('/api/v1/post')
  587. .set('content-type', 'application/json')
  588. .send({
  589. content: 'test content here',
  590. threadId
  591. })
  592. })
  593. .then(res => {
  594. postId = res.body.id
  595. done()
  596. })
  597. .catch(done)
  598. })
  599. it('should remove the post', async () => {
  600. let res = await userAgent.delete('/api/v1/post/' + postId)
  601. res.should.be.json
  602. res.should.have.status(200)
  603. res.body.should.have.property('success', true)
  604. let post = await userAgent.get('/api/v1/post/' + postId)
  605. post.body.should.have.property('removed', true)
  606. post.body.should.have.property('content', '<p>[This post has been removed by an administrator]</p>\n')
  607. })
  608. it('should return an error if post does not exist', done => {
  609. userAgent
  610. .delete('/api/v1/post/not_a_post')
  611. .end((err, res) => {
  612. res.should.be.json
  613. res.should.have.status(400)
  614. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('postId', 'post does not exist'))
  615. done()
  616. })
  617. })
  618. it('should return an error if not an admin', done => {
  619. replyAgent
  620. .delete('/api/v1/post/' + postId)
  621. .end((err, res) => {
  622. res.should.be.json
  623. res.should.have.status(401)
  624. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  625. done()
  626. })
  627. })
  628. })
  629. })