thread_post.js 24 KB

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