thread_post.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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',
  310. threadId: 1
  311. })
  312. res.should.be.json
  313. res.should.have.status(200)
  314. res.body.should.have.property('content', '<p>content</p>\n')
  315. res.body.should.have.property('postNumber', 0)
  316. res.body.should.have.deep.property('User.username', 'username')
  317. res.body.should.have.deep.property('Thread.name', 'thread')
  318. res.body.should.have.deep.property('Thread.postsCount', 1)
  319. })
  320. it('should return an error if not logged in', async () => {
  321. try {
  322. let res = await chai.request(server)
  323. .post('/api/v1/post')
  324. .set('content-type', 'application/json')
  325. .send({
  326. content: 'content',
  327. threadId: 1
  328. })
  329. res.should.be.json
  330. res.should.have.status(401)
  331. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  332. } catch (res) {
  333. res.should.have.status(401)
  334. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  335. }
  336. })
  337. it('should return an error if missing content', done => {
  338. userAgent
  339. .post('/api/v1/post')
  340. .send({
  341. threadId: 1
  342. })
  343. .end((err, res) => {
  344. res.should.be.json
  345. res.should.have.status(400)
  346. res.body.errors.should.contain.something.that.has.property('message', 'content must be a string')
  347. done()
  348. })
  349. })
  350. it('should return an error if missing threadId', done => {
  351. userAgent
  352. .post('/api/v1/post')
  353. .send({
  354. content: 'content'
  355. })
  356. .end((err, res) => {
  357. res.should.be.json
  358. res.should.have.status(400)
  359. res.body.errors.should.contain.something.that.has.property('message', 'thread does not exist')
  360. done()
  361. })
  362. })
  363. it('should return an error if thread id does not exist', done => {
  364. userAgent
  365. .post('/api/v1/post')
  366. .set('content-type', 'application/json')
  367. .send({
  368. content: 'content',
  369. threadId: 10
  370. })
  371. .end((err, res) => {
  372. res.should.be.json
  373. res.should.have.status(400)
  374. res.body.errors.should.contain.something.that.has.property('message', 'thread does not exist')
  375. done()
  376. })
  377. })
  378. it('should return an error if mentions are invalid type', done => {
  379. userAgent
  380. .post('/api/v1/post')
  381. .set('content-type', 'application/json')
  382. .send({
  383. content: 'content',
  384. threadId: 1,
  385. mentions: 'string'
  386. })
  387. .end((err, res) => {
  388. res.should.be.json
  389. res.should.have.status(400)
  390. res.body.errors.should.contain.something.that.has.property('message', 'mentions must be an array of strings')
  391. userAgent
  392. .post('/api/v1/post')
  393. .set('content-type', 'application/json')
  394. .send({
  395. content: 'content',
  396. threadId: 1,
  397. mentions: ['string', false, 3]
  398. })
  399. .end((err, res) => {
  400. res.should.be.json
  401. res.should.have.status(400)
  402. res.body.errors.should.contain.something.that.has.property('message', 'mentions must be an array of strings')
  403. done()
  404. })
  405. })
  406. })
  407. it('should be able to reply to a post', async () => {
  408. await replyAgent
  409. .post('/api/v1/user')
  410. .set('content-type', 'application/json')
  411. .send({
  412. username: 'username1',
  413. password: 'password'
  414. })
  415. let res = await replyAgent
  416. .post('/api/v1/post')
  417. .set('content-type', 'application/json')
  418. .send({
  419. content: 'another post',
  420. threadId: 1,
  421. replyingToId: 1
  422. })
  423. res.should.be.json
  424. res.should.have.status(200)
  425. res.body.should.have.property('postNumber', 1)
  426. res.body.should.have.property('content', '<p>another post</p>\n')
  427. res.body.should.have.deep.property('User.username', 'username1')
  428. res.body.should.have.deep.property('Thread.name', 'thread')
  429. res.body.should.have.deep.property('Thread.postsCount', 2)
  430. res.body.should.have.property('replyingToUsername', 'username')
  431. res.body.should.have.property('Replies').that.deep.equals([])
  432. })
  433. it('should return any replies to a post', async () => {
  434. let res = await replyAgent.get('/api/v1/post/1')
  435. res.should.be.json
  436. res.should.have.status(200)
  437. res.body.should.have.deep.property('replyingToUsername', null)
  438. res.body.should.have.deep.property('Replies.0.content', '<p>another post</p>\n')
  439. })
  440. it('should return an error if reply id does not exist', async () => {
  441. try {
  442. let res = await replyAgent
  443. .post('/api/v1/post')
  444. .set('content-type', 'application/json')
  445. .send({
  446. content: 'yet another post',
  447. threadId: 1,
  448. replyingToId: 10
  449. })
  450. res.should.have.status(400)
  451. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
  452. } catch (res) {
  453. let body = JSON.parse(res.response.text)
  454. res.should.have.status(400)
  455. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
  456. }
  457. })
  458. it('should return an error if post reply not in same thread', async () => {
  459. try {
  460. let threadId = (await replyAgent
  461. .post('/api/v1/thread')
  462. .set('content-type', 'application/json')
  463. .send({
  464. name: 'another thread',
  465. category: 'CATEGORY_NAME'
  466. })).body.id
  467. let res = await replyAgent
  468. .post('/api/v1/post')
  469. .set('content-type', 'application/json')
  470. .send({
  471. content: 'yet another post',
  472. threadId: threadId,
  473. replyingToId: 1
  474. })
  475. res.should.have.status(400)
  476. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
  477. } catch (res) {
  478. let body = JSON.parse(res.response.text)
  479. res.should.have.status(400)
  480. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
  481. }
  482. })
  483. })
  484. describe('GET /thread/:id', () => {
  485. it('should return the thread and corresponding posts', async () => {
  486. let res = await chai.request(server).get('/api/v1/thread/1')
  487. res.should.have.status(200)
  488. res.should.be.json
  489. res.body.should.have.property('name', 'thread')
  490. res.body.should.have.deep.property('Category.name', 'category_name')
  491. res.body.should.have.deep.property('User.username', 'username')
  492. res.body.should.have.property('Posts')
  493. res.body.Posts.should.have.property('length', 2)
  494. res.body.Posts.should.contain.something.that.has.property('content', '<p>content</p>\n')
  495. res.body.Posts.should.contain.something.that.has.deep.property('User.username', 'username')
  496. res.body.Posts.should.contain.something.that.has.property('content', '<p>another post</p>\n')
  497. res.body.Posts.should.contain.something.that.has.deep.property('User.username', 'username1')
  498. })
  499. it('should allow pagination', async () => {
  500. let thread = await userAgent
  501. .post('/api/v1/thread')
  502. .set('content-type', 'application/json')
  503. .send({ category: 'CATEGORY_NAME', name: 'pagination' })
  504. let threadOther = await userAgent
  505. .post('/api/v1/thread')
  506. .set('content-type', 'application/json')
  507. .send({ category: 'CATEGORY_NAME', name: 'pagination_other' })
  508. PAGINATION_THREAD_ID = thread.body.id
  509. for(var i = 0; i < 30; i++) {
  510. let post = await userAgent
  511. .post('/api/v1/post')
  512. .set('content-type', 'application/json')
  513. .send({ threadId: thread.body.id, content: `POST ${i}` })
  514. if(i === 3) {
  515. await userAgent
  516. .post('/api/v1/post')
  517. .set('content-type', 'application/json')
  518. .send({ threadId: threadOther.body.id, content: `POST OTHER ${i}` })
  519. }
  520. }
  521. let pageOne = await userAgent.get('/api/v1/thread/' + thread.body.id)
  522. let pageTwo = await userAgent.get(pageOne.body.meta.nextURL)
  523. let pageThree = await userAgent.get(pageTwo.body.meta.nextURL)
  524. let pageInvalid = await userAgent.get('/api/v1/thread/' + thread.body.id + '?from=' + 100)
  525. pageOne.body.Posts.should.have.length(10)
  526. pageOne.body.meta.should.have.property('postsRemaining', 20)
  527. pageOne.body.meta.should.have.property('previousPostsCount', 0)
  528. pageOne.body.meta.should.have.property('nextPostsCount', 10)
  529. pageOne.body.Posts[0].should.have.property('content', '<p>POST 0</p>\n')
  530. pageTwo.body.Posts.should.have.length(10)
  531. pageTwo.body.meta.should.have.property('postsRemaining', 10)
  532. pageTwo.body.meta.should.have.property('previousPostsCount', 10)
  533. pageTwo.body.meta.should.have.property('nextPostsCount', 10)
  534. pageTwo.body.Posts[0].should.have.property('content', '<p>POST 10</p>\n')
  535. pageTwo.body.meta.should.have.property('previousURL')
  536. pageThree.body.Posts.should.have.length(10)
  537. pageThree.body.meta.should.have.property('postsRemaining', 0)
  538. pageThree.body.meta.should.have.property('previousPostsCount', 10)
  539. pageThree.body.meta.should.have.property('nextPostsCount', 0)
  540. pageThree.body.Posts[0].should.have.property('content', '<p>POST 20</p>\n')
  541. pageThree.body.Posts[9].should.have.property('content', '<p>POST 29</p>\n')
  542. expect(pageThree.body.meta.nextURL).to.be.null
  543. pageInvalid.body.Posts.should.have.length(0)
  544. })
  545. it('should allow you to get an individual and surrounding posts', async () => {
  546. let http = chai.request(server)
  547. let pageOne = await http.get(`/api/v1/thread/${PAGINATION_THREAD_ID}?postNumber=15`)
  548. let pageZero = await http.get(pageOne.body.meta.previousURL)
  549. let pageTwo = await http.get(pageOne.body.meta.nextURL)
  550. pageOne.body.Posts.should.have.length(10)
  551. pageOne.body.Posts[0].should.have.property('content', '<p>POST 11</p>\n')
  552. pageOne.body.Posts[4].should.have.property('content', '<p>POST 15</p>\n')
  553. pageOne.body.Posts[9].should.have.property('content', '<p>POST 20</p>\n')
  554. pageOne.body.meta.should.have.property('postsRemaining', 9)
  555. pageOne.body.meta.should.have.property('previousPostsCount', 10)
  556. pageOne.body.meta.should.have.property('nextPostsCount', 9)
  557. pageTwo.body.Posts.should.have.length(9)
  558. pageTwo.body.Posts[0].should.have.property('content', '<p>POST 21</p>\n')
  559. pageTwo.body.Posts[8].should.have.property('content', '<p>POST 29</p>\n')
  560. pageTwo.body.meta.should.have.property('nextURL', null)
  561. pageTwo.body.meta.should.have.property('postsRemaining', 0)
  562. pageTwo.body.meta.should.have.property('previousPostsCount', 10)
  563. pageTwo.body.meta.should.have.property('nextPostsCount', 0)
  564. pageZero.body.Posts.should.have.length(10)
  565. pageZero.body.Posts[0].should.have.property('content', '<p>POST 1</p>\n')
  566. pageZero.body.Posts[9].should.have.property('content', '<p>POST 10</p>\n')
  567. pageZero.body.meta.should.have.property('postsRemaining', 19)
  568. pageZero.body.meta.should.have.property('previousPostsCount', 1)
  569. pageZero.body.meta.should.have.property('nextPostsCount', 10)
  570. let pageFirst = await http.get(pageZero.body.meta.previousURL)
  571. pageFirst.body.Posts[0].should.have.property('content', '<p>POST 0</p>\n')
  572. pageFirst.body.meta.should.have.property('previousURL', null)
  573. pageFirst.body.meta.should.have.property('postsRemaining', 29)
  574. pageFirst.body.meta.should.have.property('previousPostsCount', 0)
  575. })
  576. it('should return an error if :id is invalid', async () => {
  577. try {
  578. let res = await chai.request(server).get('/api/v1/thread/invalid')
  579. res.should.have.status(400)
  580. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  581. } catch (res) {
  582. let body = JSON.parse(res.response.text)
  583. res.should.have.status(400)
  584. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  585. }
  586. })
  587. })
  588. describe('GET /post/:id', () => {
  589. it('should return the post', async () => {
  590. let res = await chai.request(server).get('/api/v1/post/1')
  591. res.should.have.status(200)
  592. res.should.be.json
  593. res.body.should.have.property('content', '<p>content</p>\n')
  594. res.body.should.have.deep.property('User.username', 'username')
  595. res.body.should.have.deep.property('Thread.name', 'thread')
  596. res.body.should.have.deep.property('Thread.Category.name', 'category_name')
  597. res.body.should.have.deep.property('Replies.0.User.username', 'username1')
  598. })
  599. it('should return an error if invalid post id', async () => {
  600. try {
  601. let res = await chai.request(server).get('/api/v1/post/invalid')
  602. res.should.have.status(400)
  603. res.body.errors.should.contain.something.that.has.property('message', 'post does not exist')
  604. } catch (res) {
  605. let body = JSON.parse(res.response.text)
  606. res.should.have.status(400)
  607. body.errors.should.contain.something.that.has.property('message', 'post does not exist')
  608. }
  609. })
  610. })
  611. describe('POST utf8', () => {
  612. it('should allow emojis', async () => {
  613. let res = await userAgent
  614. .post('/api/v1/post')
  615. .set('content-type', 'application/json')
  616. .send({
  617. content: '😂😀',
  618. threadId: 1
  619. })
  620. res.should.be.json
  621. res.should.have.status(200)
  622. res.body.should.have.property('content', '<p>😂😀</p>\n')
  623. })
  624. })
  625. describe('DELETE /post/:id', () => {
  626. let threadId
  627. let postId
  628. let normalUserAgent = chai.request.agent(server)
  629. before(done => {
  630. userAgent
  631. .post('/api/v1/thread')
  632. .set('content-type', 'application/json')
  633. .send({
  634. name: 'delete_post_thread',
  635. category: 'CATEGORY_NAME'
  636. })
  637. .then(res => {
  638. threadId = res.body.id
  639. return userAgent
  640. .post('/api/v1/post')
  641. .set('content-type', 'application/json')
  642. .send({
  643. content: 'test content here',
  644. threadId
  645. })
  646. })
  647. .then(res => {
  648. postId = res.body.id
  649. return normalUserAgent
  650. .post('/api/v1/user')
  651. .set('content-type', 'application/json')
  652. .send({
  653. username: 'delete_post_non_admin',
  654. password: 'password'
  655. })
  656. })
  657. .then(_ => {
  658. done()
  659. })
  660. .catch(done)
  661. })
  662. it('should remove the post', async () => {
  663. let res = await userAgent.delete('/api/v1/post/' + postId)
  664. res.should.be.json
  665. res.should.have.status(200)
  666. res.body.should.have.property('success', true)
  667. let post = await userAgent.get('/api/v1/post/' + postId)
  668. post.body.should.have.property('removed', true)
  669. post.body.should.have.property('content', '<p>[This post has been removed by an administrator]</p>\n')
  670. })
  671. it('should return an error if trying to reply to a removed post', async () => {
  672. replyAgent
  673. .post('/api/v1/post')
  674. .set('content-type', 'application/json')
  675. .send({
  676. content: 'reply to deleted post',
  677. replyId: postId,
  678. threadId
  679. })
  680. .end((err, res) => {
  681. res.should.be.json
  682. res.should.have.status(400)
  683. res.body.errors.should.include.something.that.deep.equals(Errors.postRemoved)
  684. })
  685. })
  686. it('should return an error if post does not exist', done => {
  687. userAgent
  688. .delete('/api/v1/post/not_a_post')
  689. .end((err, res) => {
  690. res.should.be.json
  691. res.should.have.status(400)
  692. res.body.errors.should.include.something.that.has.property('message', 'post does not exist')
  693. done()
  694. })
  695. })
  696. it('should return an error if not an admin', done => {
  697. normalUserAgent
  698. .delete('/api/v1/post/' + postId)
  699. .end((err, res) => {
  700. res.should.be.json
  701. res.should.have.status(401)
  702. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  703. done()
  704. })
  705. })
  706. })
  707. })