thread_post.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. 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 parameters', async () => {
  338. try {
  339. let res = await userAgent
  340. .post('/api/v1/post')
  341. res.should.be.json
  342. res.should.have.status(400)
  343. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('content'))
  344. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('threadId'))
  345. } catch (res) {
  346. let body = JSON.parse(res.response.text)
  347. res.should.have.status(400)
  348. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('content'))
  349. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('threadId'))
  350. }
  351. })
  352. it('should return an error if invalid types', async () => {
  353. try {
  354. let res = await userAgent
  355. .post('/api/v1/post')
  356. .set('content-type', 'application/json')
  357. .send({
  358. content: 123,
  359. threadId: 'string',
  360. replyingToId: 'string'
  361. })
  362. res.should.be.json
  363. res.should.have.status(400)
  364. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('content', 'string'))
  365. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('threadId', 'integer'))
  366. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('replyingToId', 'integer'))
  367. } catch (res) {
  368. let body = JSON.parse(res.response.text)
  369. res.should.have.status(400)
  370. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('content', 'string'))
  371. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('threadId', 'integer'))
  372. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('replyingToId', 'integer'))
  373. }
  374. })
  375. it('should return an error if thread id does not exist', async () => {
  376. try {
  377. let res = await userAgent
  378. .post('/api/v1/post')
  379. .set('content-type', 'application/json')
  380. .send({
  381. content: 'content',
  382. threadId: 10
  383. })
  384. res.should.be.json
  385. res.should.have.status(400)
  386. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('threadId', 'thread does not exist'))
  387. } catch (res) {
  388. let body = JSON.parse(res.response.text)
  389. res.should.have.status(400)
  390. body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('threadId', 'thread does not exist'))
  391. }
  392. })
  393. it('should be able to reply to a post', async () => {
  394. await replyAgent
  395. .post('/api/v1/user')
  396. .set('content-type', 'application/json')
  397. .send({
  398. username: 'username1',
  399. password: 'password'
  400. })
  401. let res = await replyAgent
  402. .post('/api/v1/post')
  403. .set('content-type', 'application/json')
  404. .send({
  405. content: 'another post',
  406. threadId: 1,
  407. replyingToId: 1
  408. })
  409. res.should.be.json
  410. res.should.have.status(200)
  411. res.body.should.have.property('postNumber', 1)
  412. res.body.should.have.property('content', '<p>another post</p>\n')
  413. res.body.should.have.deep.property('User.username', 'username1')
  414. res.body.should.have.deep.property('Thread.name', 'thread')
  415. res.body.should.have.deep.property('Thread.postsCount', 2)
  416. res.body.should.have.property('replyingToUsername', 'username')
  417. res.body.should.have.property('Replies').that.deep.equals([])
  418. })
  419. it('should return any replies to a post', async () => {
  420. let res = await replyAgent.get('/api/v1/post/1')
  421. res.should.be.json
  422. res.should.have.status(200)
  423. res.body.should.have.deep.property('replyingToUsername', null)
  424. res.body.should.have.deep.property('Replies.0.content', '<p>another post</p>\n')
  425. })
  426. it('should return an error if reply id does not exist', async () => {
  427. try {
  428. let res = await replyAgent
  429. .post('/api/v1/post')
  430. .set('content-type', 'application/json')
  431. .send({
  432. content: 'yet another post',
  433. threadId: 1,
  434. replyingToId: 10
  435. })
  436. res.should.have.status(400)
  437. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
  438. } catch (res) {
  439. let body = JSON.parse(res.response.text)
  440. res.should.have.status(400)
  441. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
  442. }
  443. })
  444. it('should return an error if post reply not in same thread', async () => {
  445. try {
  446. let threadId = (await replyAgent
  447. .post('/api/v1/thread')
  448. .set('content-type', 'application/json')
  449. .send({
  450. name: 'another thread',
  451. category: 'CATEGORY_NAME'
  452. })).body.id
  453. let res = await replyAgent
  454. .post('/api/v1/post')
  455. .set('content-type', 'application/json')
  456. .send({
  457. content: 'yet another post',
  458. threadId: threadId,
  459. replyingToId: 1
  460. })
  461. res.should.have.status(400)
  462. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
  463. } catch (res) {
  464. let body = JSON.parse(res.response.text)
  465. res.should.have.status(400)
  466. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
  467. }
  468. })
  469. })
  470. describe('GET /thread/:id', () => {
  471. it('should return the thread and corresponding posts', async () => {
  472. let res = await chai.request(server).get('/api/v1/thread/1')
  473. res.should.have.status(200)
  474. res.should.be.json
  475. res.body.should.have.property('name', 'thread')
  476. res.body.should.have.deep.property('Category.name', 'category_name')
  477. res.body.should.have.deep.property('User.username', 'username')
  478. res.body.should.have.property('Posts')
  479. res.body.Posts.should.have.property('length', 2)
  480. res.body.Posts.should.contain.something.that.has.property('content', '<p>content</p>\n')
  481. res.body.Posts.should.contain.something.that.has.deep.property('User.username', 'username')
  482. res.body.Posts.should.contain.something.that.has.property('content', '<p>another post</p>\n')
  483. res.body.Posts.should.contain.something.that.has.deep.property('User.username', 'username1')
  484. })
  485. it('should allow pagination', async () => {
  486. let thread = await userAgent
  487. .post('/api/v1/thread')
  488. .set('content-type', 'application/json')
  489. .send({ category: 'CATEGORY_NAME', name: 'pagination' })
  490. let threadOther = await userAgent
  491. .post('/api/v1/thread')
  492. .set('content-type', 'application/json')
  493. .send({ category: 'CATEGORY_NAME', name: 'pagination_other' })
  494. PAGINATION_THREAD_ID = thread.body.id
  495. for(var i = 0; i < 30; i++) {
  496. let post = await userAgent
  497. .post('/api/v1/post')
  498. .set('content-type', 'application/json')
  499. .send({ threadId: thread.body.id, content: `POST ${i}` })
  500. if(i === 3) {
  501. await userAgent
  502. .post('/api/v1/post')
  503. .set('content-type', 'application/json')
  504. .send({ threadId: threadOther.body.id, content: `POST OTHER ${i}` })
  505. }
  506. }
  507. let pageOne = await userAgent.get('/api/v1/thread/' + thread.body.id)
  508. let pageTwo = await userAgent.get(pageOne.body.meta.nextURL)
  509. let pageThree = await userAgent.get(pageTwo.body.meta.nextURL)
  510. let pageInvalid = await userAgent.get('/api/v1/thread/' + thread.body.id + '?from=' + 100)
  511. pageOne.body.Posts.should.have.length(10)
  512. pageOne.body.meta.should.have.property('postsRemaining', 20)
  513. pageOne.body.meta.should.have.property('previousPostsCount', 0)
  514. pageOne.body.meta.should.have.property('nextPostsCount', 10)
  515. pageOne.body.Posts[0].should.have.property('content', '<p>POST 0</p>\n')
  516. pageTwo.body.Posts.should.have.length(10)
  517. pageTwo.body.meta.should.have.property('postsRemaining', 10)
  518. pageTwo.body.meta.should.have.property('previousPostsCount', 10)
  519. pageTwo.body.meta.should.have.property('nextPostsCount', 10)
  520. pageTwo.body.Posts[0].should.have.property('content', '<p>POST 10</p>\n')
  521. pageTwo.body.meta.should.have.property('previousURL')
  522. pageThree.body.Posts.should.have.length(10)
  523. pageThree.body.meta.should.have.property('postsRemaining', 0)
  524. pageThree.body.meta.should.have.property('previousPostsCount', 10)
  525. pageThree.body.meta.should.have.property('nextPostsCount', 0)
  526. pageThree.body.Posts[0].should.have.property('content', '<p>POST 20</p>\n')
  527. pageThree.body.Posts[9].should.have.property('content', '<p>POST 29</p>\n')
  528. expect(pageThree.body.meta.nextURL).to.be.null
  529. pageInvalid.body.Posts.should.have.length(0)
  530. })
  531. it('should allow you to get an individual and surrounding posts', async () => {
  532. let http = chai.request(server)
  533. let pageOne = await http.get(`/api/v1/thread/${PAGINATION_THREAD_ID}?postNumber=15`)
  534. let pageZero = await http.get(pageOne.body.meta.previousURL)
  535. let pageTwo = await http.get(pageOne.body.meta.nextURL)
  536. pageOne.body.Posts.should.have.length(10)
  537. pageOne.body.Posts[0].should.have.property('content', '<p>POST 11</p>\n')
  538. pageOne.body.Posts[4].should.have.property('content', '<p>POST 15</p>\n')
  539. pageOne.body.Posts[9].should.have.property('content', '<p>POST 20</p>\n')
  540. pageOne.body.meta.should.have.property('postsRemaining', 9)
  541. pageOne.body.meta.should.have.property('previousPostsCount', 10)
  542. pageOne.body.meta.should.have.property('nextPostsCount', 9)
  543. pageTwo.body.Posts.should.have.length(9)
  544. pageTwo.body.Posts[0].should.have.property('content', '<p>POST 21</p>\n')
  545. pageTwo.body.Posts[8].should.have.property('content', '<p>POST 29</p>\n')
  546. pageTwo.body.meta.should.have.property('nextURL', null)
  547. pageTwo.body.meta.should.have.property('postsRemaining', 0)
  548. pageTwo.body.meta.should.have.property('previousPostsCount', 10)
  549. pageTwo.body.meta.should.have.property('nextPostsCount', 0)
  550. pageZero.body.Posts.should.have.length(10)
  551. pageZero.body.Posts[0].should.have.property('content', '<p>POST 1</p>\n')
  552. pageZero.body.Posts[9].should.have.property('content', '<p>POST 10</p>\n')
  553. pageZero.body.meta.should.have.property('postsRemaining', 19)
  554. pageZero.body.meta.should.have.property('previousPostsCount', 1)
  555. pageZero.body.meta.should.have.property('nextPostsCount', 10)
  556. let pageFirst = await http.get(pageZero.body.meta.previousURL)
  557. pageFirst.body.Posts[0].should.have.property('content', '<p>POST 0</p>\n')
  558. pageFirst.body.meta.should.have.property('previousURL', null)
  559. pageFirst.body.meta.should.have.property('postsRemaining', 29)
  560. pageFirst.body.meta.should.have.property('previousPostsCount', 0)
  561. })
  562. it('should return an error if :id is invalid', async () => {
  563. try {
  564. let res = await chai.request(server).get('/api/v1/thread/invalid')
  565. res.should.have.status(400)
  566. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  567. } catch (res) {
  568. let body = JSON.parse(res.response.text)
  569. res.should.have.status(400)
  570. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  571. }
  572. })
  573. })
  574. describe('GET /post/:id', () => {
  575. it('should return the post', async () => {
  576. let res = await chai.request(server).get('/api/v1/post/1')
  577. res.should.have.status(200)
  578. res.should.be.json
  579. res.body.should.have.property('content', '<p>content</p>\n')
  580. res.body.should.have.deep.property('User.username', 'username')
  581. res.body.should.have.deep.property('Thread.name', 'thread')
  582. res.body.should.have.deep.property('Thread.Category.name', 'category_name')
  583. res.body.should.have.deep.property('Replies.0.User.username', 'username1')
  584. })
  585. it('should return an error if invalid post id', async () => {
  586. try {
  587. let res = await chai.request(server).get('/api/v1/post/invalid')
  588. res.should.have.status(400)
  589. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'post does not exist'))
  590. } catch (res) {
  591. let body = JSON.parse(res.response.text)
  592. res.should.have.status(400)
  593. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'post does not exist'))
  594. }
  595. })
  596. })
  597. })