thread_post.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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(() => { done() })
  36. .catch(done)
  37. })
  38. .catch(done)
  39. }
  40. })
  41. //Delete all rows in table after
  42. //tess completed
  43. after(() => {
  44. sequelize.sync({ force: true })
  45. })
  46. describe('POST /thread', () => {
  47. it('should create a thread if logged in', async () => {
  48. let res = await userAgent
  49. .post('/api/v1/thread')
  50. .set('content-type', 'application/json')
  51. .send({
  52. name: 'thread',
  53. category: 'category_name'
  54. })
  55. res.should.have.status(200)
  56. res.should.be.json
  57. res.body.should.have.property('name', 'thread')
  58. res.body.should.have.property('postsCount', 0)
  59. res.body.should.have.property('slug', 'thread')
  60. res.body.should.have.deep.property('User.username', 'username')
  61. res.body.should.have.deep.property('Category.name', 'category_name')
  62. })
  63. it('should add a slug from the thread name', async () => {
  64. let res = await userAgent
  65. .post('/api/v1/thread')
  66. .set('content-type', 'application/json')
  67. .send({
  68. name: ' à long thrËad, with lØts of àccents!!! ',
  69. category: 'category_name'
  70. })
  71. res.should.have.status(200)
  72. res.should.be.json
  73. res.body.should.have.property('name', ' à long thrËad, with lØts of àccents!!! ')
  74. res.body.should.have.property('slug', 'a-long-thread-with-lots-of-accents')
  75. res.body.should.have.deep.property('User.username', 'username')
  76. res.body.should.have.deep.property('Category.name', 'category_name')
  77. })
  78. it('should return an error if not logged in', async () => {
  79. try {
  80. let res = await chai.request(server)
  81. .post('/api/v1/thread')
  82. .set('content-type', 'application/json')
  83. .send({
  84. name: 'thread',
  85. category: 'category_name'
  86. })
  87. res.should.be.json
  88. res.should.have.status(401)
  89. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  90. } catch (res) {
  91. res.should.have.status(401)
  92. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  93. }
  94. })
  95. it('should return an error if missing parameters', async () => {
  96. try {
  97. let res = await userAgent
  98. .post('/api/v1/thread')
  99. res.should.be.json
  100. res.should.have.status(400)
  101. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('name'))
  102. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('category'))
  103. } catch (res) {
  104. let body = JSON.parse(res.response.text)
  105. res.should.have.status(400)
  106. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('name'))
  107. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('category'))
  108. }
  109. })
  110. it('should return an error if invalid types', async () => {
  111. try {
  112. let res = await userAgent
  113. .post('/api/v1/thread')
  114. .set('content-type', 'application/json')
  115. .send({
  116. name: 123,
  117. category: 123
  118. })
  119. res.should.be.json
  120. res.should.have.status(400)
  121. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('name', 'string'))
  122. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('category', 'string'))
  123. } catch (res) {
  124. let body = JSON.parse(res.response.text)
  125. res.should.have.status(400)
  126. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('name', 'string'))
  127. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('category', 'string'))
  128. }
  129. })
  130. it('should return an error if category does not exist', async () => {
  131. try {
  132. let res = await userAgent
  133. .post('/api/v1/thread')
  134. .set('content-type', 'application/json')
  135. .send({
  136. name: 'thread1',
  137. category: 'non-existent'
  138. })
  139. res.should.be.json
  140. res.should.have.status(400)
  141. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidCategory)
  142. } catch (res) {
  143. res.should.have.status(400)
  144. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.invalidCategory)
  145. }
  146. })
  147. })
  148. describe('POST /post', () => {
  149. it('should create a post if logged in', async () => {
  150. let res = await userAgent
  151. .post('/api/v1/post')
  152. .set('content-type', 'application/json')
  153. .send({
  154. content: 'content',
  155. threadId: 1
  156. })
  157. res.should.be.json
  158. res.should.have.status(200)
  159. res.body.should.have.property('content', '<p>content</p>\n')
  160. res.body.should.have.property('postNumber', 0)
  161. res.body.should.have.deep.property('User.username', 'username')
  162. res.body.should.have.deep.property('Thread.name', 'thread')
  163. res.body.should.have.deep.property('Thread.postsCount', 1)
  164. })
  165. it('should return an error if not logged in', async () => {
  166. try {
  167. let res = await chai.request(server)
  168. .post('/api/v1/post')
  169. .set('content-type', 'application/json')
  170. .send({
  171. content: 'content',
  172. threadId: 1
  173. })
  174. res.should.be.json
  175. res.should.have.status(401)
  176. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  177. } catch (res) {
  178. res.should.have.status(401)
  179. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  180. }
  181. })
  182. it('should return an error if missing parameters', async () => {
  183. try {
  184. let res = await userAgent
  185. .post('/api/v1/post')
  186. res.should.be.json
  187. res.should.have.status(400)
  188. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('content'))
  189. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('threadId'))
  190. } catch (res) {
  191. let body = JSON.parse(res.response.text)
  192. res.should.have.status(400)
  193. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('content'))
  194. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('threadId'))
  195. }
  196. })
  197. it('should return an error if invalid types', async () => {
  198. try {
  199. let res = await userAgent
  200. .post('/api/v1/post')
  201. .set('content-type', 'application/json')
  202. .send({
  203. content: 123,
  204. threadId: 'string',
  205. replyingToId: 'string'
  206. })
  207. res.should.be.json
  208. res.should.have.status(400)
  209. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('content', 'string'))
  210. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('threadId', 'integer'))
  211. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('replyingToId', 'integer'))
  212. } catch (res) {
  213. let body = JSON.parse(res.response.text)
  214. res.should.have.status(400)
  215. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('content', 'string'))
  216. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('threadId', 'integer'))
  217. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('replyingToId', 'integer'))
  218. }
  219. })
  220. it('should return an error if thread id does not exist', async () => {
  221. try {
  222. let res = await userAgent
  223. .post('/api/v1/post')
  224. .set('content-type', 'application/json')
  225. .send({
  226. content: 'content',
  227. threadId: 10
  228. })
  229. res.should.be.json
  230. res.should.have.status(400)
  231. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('threadId', 'thread does not exist'))
  232. } catch (res) {
  233. let body = JSON.parse(res.response.text)
  234. res.should.have.status(400)
  235. body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('threadId', 'thread does not exist'))
  236. }
  237. })
  238. it('should be able to reply to a post', async () => {
  239. await replyAgent
  240. .post('/api/v1/user')
  241. .set('content-type', 'application/json')
  242. .send({
  243. username: 'username1',
  244. password: 'password'
  245. })
  246. let res = await replyAgent
  247. .post('/api/v1/post')
  248. .set('content-type', 'application/json')
  249. .send({
  250. content: 'another post',
  251. threadId: 1,
  252. replyingToId: 1
  253. })
  254. res.should.be.json
  255. res.should.have.status(200)
  256. res.body.should.have.property('postNumber', 1)
  257. res.body.should.have.property('content', '<p>another post</p>\n')
  258. res.body.should.have.deep.property('User.username', 'username1')
  259. res.body.should.have.deep.property('Thread.name', 'thread')
  260. res.body.should.have.deep.property('Thread.postsCount', 2)
  261. res.body.should.have.property('replyingToUsername', 'username')
  262. res.body.should.have.property('Replies').that.deep.equals([])
  263. })
  264. it('should return any replies to a post', async () => {
  265. let res = await replyAgent.get('/api/v1/post/1')
  266. res.should.be.json
  267. res.should.have.status(200)
  268. res.body.should.have.deep.property('replyingToUsername', null)
  269. res.body.should.have.deep.property('Replies.0.content', '<p>another post</p>\n')
  270. })
  271. it('should return an error if reply id does not exist', async () => {
  272. try {
  273. let res = await replyAgent
  274. .post('/api/v1/post')
  275. .set('content-type', 'application/json')
  276. .send({
  277. content: 'yet another post',
  278. threadId: 1,
  279. replyingToId: 10
  280. })
  281. res.should.have.status(400)
  282. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
  283. } catch (res) {
  284. let body = JSON.parse(res.response.text)
  285. res.should.have.status(400)
  286. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
  287. }
  288. })
  289. it('should return an error if post reply not in same thread', async () => {
  290. try {
  291. let threadId = (await replyAgent
  292. .post('/api/v1/thread')
  293. .set('content-type', 'application/json')
  294. .send({
  295. name: 'another thread',
  296. category: 'category_name'
  297. })).body.id
  298. let res = await replyAgent
  299. .post('/api/v1/post')
  300. .set('content-type', 'application/json')
  301. .send({
  302. content: 'yet another post',
  303. threadId: threadId,
  304. replyingToId: 1
  305. })
  306. res.should.have.status(400)
  307. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
  308. } catch (res) {
  309. let body = JSON.parse(res.response.text)
  310. res.should.have.status(400)
  311. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
  312. }
  313. })
  314. })
  315. describe('GET /thread/:id', () => {
  316. it('should return the thread and corresponding posts', async () => {
  317. let res = await chai.request(server).get('/api/v1/thread/1')
  318. res.should.have.status(200)
  319. res.should.be.json
  320. res.body.should.have.property('name', 'thread')
  321. res.body.should.have.deep.property('Category.name', 'category_name')
  322. res.body.should.have.deep.property('User.username', 'username')
  323. res.body.should.have.property('Posts')
  324. res.body.Posts.should.have.property('length', 2)
  325. res.body.Posts.should.contain.something.that.has.property('content', '<p>content</p>\n')
  326. res.body.Posts.should.contain.something.that.has.deep.property('User.username', 'username')
  327. res.body.Posts.should.contain.something.that.has.property('content', '<p>another post</p>\n')
  328. res.body.Posts.should.contain.something.that.has.deep.property('User.username', 'username1')
  329. })
  330. it('should allow pagination', async () => {
  331. let thread = await userAgent
  332. .post('/api/v1/thread')
  333. .set('content-type', 'application/json')
  334. .send({ category: 'category_name', name: 'pagination' })
  335. let threadOther = await userAgent
  336. .post('/api/v1/thread')
  337. .set('content-type', 'application/json')
  338. .send({ category: 'category_name', name: 'pagination_other' })
  339. PAGINATION_THREAD_ID = thread.body.id
  340. for(var i = 0; i < 30; i++) {
  341. let post = await userAgent
  342. .post('/api/v1/post')
  343. .set('content-type', 'application/json')
  344. .send({ threadId: thread.body.id, content: `POST ${i}` })
  345. if(i === 3) {
  346. await userAgent
  347. .post('/api/v1/post')
  348. .set('content-type', 'application/json')
  349. .send({ threadId: threadOther.body.id, content: `POST OTHER ${i}` })
  350. }
  351. }
  352. let pageOne = await userAgent.get('/api/v1/thread/' + thread.body.id)
  353. let pageTwo = await userAgent.get(pageOne.body.meta.nextURL)
  354. let pageThree = await userAgent.get(pageTwo.body.meta.nextURL)
  355. let pageInvalid = await userAgent.get('/api/v1/thread/' + thread.body.id + '?from=' + 100)
  356. pageOne.body.Posts.should.have.length(10)
  357. pageOne.body.meta.should.have.property('postsRemaining', 20)
  358. pageOne.body.meta.should.have.property('previousPostsCount', 0)
  359. pageOne.body.meta.should.have.property('nextPostsCount', 10)
  360. pageOne.body.Posts[0].should.have.property('content', '<p>POST 0</p>\n')
  361. pageTwo.body.Posts.should.have.length(10)
  362. pageTwo.body.meta.should.have.property('postsRemaining', 10)
  363. pageTwo.body.meta.should.have.property('previousPostsCount', 10)
  364. pageTwo.body.meta.should.have.property('nextPostsCount', 10)
  365. pageTwo.body.Posts[0].should.have.property('content', '<p>POST 10</p>\n')
  366. pageTwo.body.meta.should.have.property('previousURL')
  367. pageThree.body.Posts.should.have.length(10)
  368. pageThree.body.meta.should.have.property('postsRemaining', 0)
  369. pageThree.body.meta.should.have.property('previousPostsCount', 10)
  370. pageThree.body.meta.should.have.property('nextPostsCount', 0)
  371. pageThree.body.Posts[0].should.have.property('content', '<p>POST 20</p>\n')
  372. pageThree.body.Posts[9].should.have.property('content', '<p>POST 29</p>\n')
  373. expect(pageThree.body.meta.nextURL).to.be.null
  374. pageInvalid.body.Posts.should.have.length(0)
  375. })
  376. it('should allow you to get an individual and surrounding posts', async () => {
  377. let http = chai.request(server)
  378. let pageOne = await http.get(`/api/v1/thread/${PAGINATION_THREAD_ID}?postNumber=15`)
  379. let pageZero = await http.get(pageOne.body.meta.previousURL)
  380. let pageTwo = await http.get(pageOne.body.meta.nextURL)
  381. pageOne.body.Posts.should.have.length(10)
  382. pageOne.body.Posts[0].should.have.property('content', '<p>POST 11</p>\n')
  383. pageOne.body.Posts[4].should.have.property('content', '<p>POST 15</p>\n')
  384. pageOne.body.Posts[9].should.have.property('content', '<p>POST 20</p>\n')
  385. pageOne.body.meta.should.have.property('postsRemaining', 9)
  386. pageOne.body.meta.should.have.property('previousPostsCount', 10)
  387. pageOne.body.meta.should.have.property('nextPostsCount', 9)
  388. pageTwo.body.Posts.should.have.length(9)
  389. pageTwo.body.Posts[0].should.have.property('content', '<p>POST 21</p>\n')
  390. pageTwo.body.Posts[8].should.have.property('content', '<p>POST 29</p>\n')
  391. pageTwo.body.meta.should.have.property('nextURL', null)
  392. pageTwo.body.meta.should.have.property('postsRemaining', 0)
  393. pageTwo.body.meta.should.have.property('previousPostsCount', 10)
  394. pageTwo.body.meta.should.have.property('nextPostsCount', 0)
  395. pageZero.body.Posts.should.have.length(10)
  396. pageZero.body.Posts[0].should.have.property('content', '<p>POST 1</p>\n')
  397. pageZero.body.Posts[9].should.have.property('content', '<p>POST 10</p>\n')
  398. pageZero.body.meta.should.have.property('postsRemaining', 19)
  399. pageZero.body.meta.should.have.property('previousPostsCount', 1)
  400. pageZero.body.meta.should.have.property('nextPostsCount', 10)
  401. let pageFirst = await http.get(pageZero.body.meta.previousURL)
  402. pageFirst.body.Posts[0].should.have.property('content', '<p>POST 0</p>\n')
  403. pageFirst.body.meta.should.have.property('previousURL', null)
  404. pageFirst.body.meta.should.have.property('postsRemaining', 29)
  405. pageFirst.body.meta.should.have.property('previousPostsCount', 0)
  406. })
  407. it('should return an error if :id is invalid', async () => {
  408. try {
  409. let res = await chai.request(server).get('/api/v1/thread/invalid')
  410. res.should.have.status(400)
  411. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  412. } catch (res) {
  413. let body = JSON.parse(res.response.text)
  414. res.should.have.status(400)
  415. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  416. }
  417. })
  418. })
  419. describe('GET /post/:id', () => {
  420. it('should return the post', async () => {
  421. let res = await chai.request(server).get('/api/v1/post/1')
  422. res.should.have.status(200)
  423. res.should.be.json
  424. res.body.should.have.property('content', '<p>content</p>\n')
  425. res.body.should.have.deep.property('User.username', 'username')
  426. res.body.should.have.deep.property('Thread.name', 'thread')
  427. res.body.should.have.deep.property('Thread.Category.name', 'category_name')
  428. res.body.should.have.deep.property('Replies.0.User.username', 'username1')
  429. })
  430. it('should return an error if invalid post id', async () => {
  431. try {
  432. let res = await chai.request(server).get('/api/v1/post/invalid')
  433. res.should.have.status(400)
  434. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'post does not exist'))
  435. } catch (res) {
  436. let body = JSON.parse(res.response.text)
  437. res.should.have.status(400)
  438. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'post does not exist'))
  439. }
  440. })
  441. })
  442. })