thread_post.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. let MID_PAGINATION_POST_ID
  10. chai.use(require('chai-http'))
  11. chai.use(require('chai-things'))
  12. describe('Thread and post', () => {
  13. let userAgent, replyAgent
  14. //Wait for app to start before commencing
  15. before((done) => {
  16. if(server.locals.appStarted) mockData()
  17. server.on('appStarted', () => {
  18. mockData()
  19. })
  20. function mockData() {
  21. userAgent = chai.request.agent(server)
  22. replyAgent = chai.request.agent(server)
  23. userAgent
  24. .post('/api/v1/user')
  25. .set('content-type', 'application/json')
  26. .send({
  27. username: 'username',
  28. password: 'password',
  29. admin: true
  30. })
  31. .then(() => {
  32. userAgent
  33. .post('/api/v1/category')
  34. .set('content-type', 'application/json')
  35. .send({ name: 'category_name' })
  36. .then(() => { done() })
  37. .catch(done)
  38. })
  39. .catch(done)
  40. }
  41. })
  42. //Delete all rows in table after
  43. //tess completed
  44. after(() => {
  45. sequelize.sync({ force: true })
  46. })
  47. describe('POST /thread', () => {
  48. it('should create a thread if logged in', async () => {
  49. let res = await userAgent
  50. .post('/api/v1/thread')
  51. .set('content-type', 'application/json')
  52. .send({
  53. name: 'thread',
  54. category: 'category_name'
  55. })
  56. res.should.have.status(200)
  57. res.should.be.json
  58. res.body.should.have.property('name', 'thread')
  59. res.body.should.have.property('postsCount', 0)
  60. res.body.should.have.property('slug', 'thread')
  61. res.body.should.have.deep.property('User.username', 'username')
  62. res.body.should.have.deep.property('Category.name', 'category_name')
  63. })
  64. it('should add a slug from the thread name', async () => {
  65. let res = await userAgent
  66. .post('/api/v1/thread')
  67. .set('content-type', 'application/json')
  68. .send({
  69. name: ' à long thrËad, with lØts of àccents!!! ',
  70. category: 'category_name'
  71. })
  72. res.should.have.status(200)
  73. res.should.be.json
  74. res.body.should.have.property('name', ' à long thrËad, with lØts of àccents!!! ')
  75. res.body.should.have.property('slug', 'a-long-thread-with-lots-of-accents')
  76. res.body.should.have.deep.property('User.username', 'username')
  77. res.body.should.have.deep.property('Category.name', 'category_name')
  78. })
  79. it('should return an error if not logged in', async () => {
  80. try {
  81. let res = await chai.request(server)
  82. .post('/api/v1/thread')
  83. .set('content-type', 'application/json')
  84. .send({
  85. name: 'thread',
  86. category: 'category_name'
  87. })
  88. res.should.be.json
  89. res.should.have.status(401)
  90. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  91. } catch (res) {
  92. res.should.have.status(401)
  93. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  94. }
  95. })
  96. it('should return an error if missing parameters', async () => {
  97. try {
  98. let res = await userAgent
  99. .post('/api/v1/thread')
  100. res.should.be.json
  101. res.should.have.status(400)
  102. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('name'))
  103. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('category'))
  104. } catch (res) {
  105. let body = JSON.parse(res.response.text)
  106. res.should.have.status(400)
  107. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('name'))
  108. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('category'))
  109. }
  110. })
  111. it('should return an error if invalid types', async () => {
  112. try {
  113. let res = await userAgent
  114. .post('/api/v1/thread')
  115. .set('content-type', 'application/json')
  116. .send({
  117. name: 123,
  118. category: 123
  119. })
  120. res.should.be.json
  121. res.should.have.status(400)
  122. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('name', 'string'))
  123. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('category', 'string'))
  124. } catch (res) {
  125. let body = JSON.parse(res.response.text)
  126. res.should.have.status(400)
  127. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('name', 'string'))
  128. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('category', 'string'))
  129. }
  130. })
  131. it('should return an error if category does not exist', async () => {
  132. try {
  133. let res = await userAgent
  134. .post('/api/v1/thread')
  135. .set('content-type', 'application/json')
  136. .send({
  137. name: 'thread1',
  138. category: 'non-existent'
  139. })
  140. res.should.be.json
  141. res.should.have.status(400)
  142. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidCategory)
  143. } catch (res) {
  144. res.should.have.status(400)
  145. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.invalidCategory)
  146. }
  147. })
  148. })
  149. describe('POST /post', () => {
  150. it('should create a post if logged in', async () => {
  151. let res = await userAgent
  152. .post('/api/v1/post')
  153. .set('content-type', 'application/json')
  154. .send({
  155. content: 'content',
  156. threadId: 1
  157. })
  158. res.should.be.json
  159. res.should.have.status(200)
  160. res.body.should.have.property('content', '<p>content</p>\n')
  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('content', '<p>another post</p>\n')
  257. res.body.should.have.deep.property('User.username', 'username1')
  258. res.body.should.have.deep.property('Thread.name', 'thread')
  259. res.body.should.have.deep.property('Thread.postsCount', 2)
  260. res.body.should.have.property('replyingToUsername', 'username')
  261. res.body.should.have.property('Replies').that.deep.equals([])
  262. })
  263. it('should return any replies to a post', async () => {
  264. let res = await replyAgent.get('/api/v1/post/1')
  265. res.should.be.json
  266. res.should.have.status(200)
  267. res.body.should.have.deep.property('replyingToUsername', null)
  268. res.body.should.have.deep.property('Replies.0.content', '<p>another post</p>\n')
  269. })
  270. it('should return an error if reply id does not exist', async () => {
  271. try {
  272. let res = await replyAgent
  273. .post('/api/v1/post')
  274. .set('content-type', 'application/json')
  275. .send({
  276. content: 'yet another post',
  277. threadId: 1,
  278. replyingToId: 10
  279. })
  280. res.should.have.status(400)
  281. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
  282. } catch (res) {
  283. let body = JSON.parse(res.response.text)
  284. res.should.have.status(400)
  285. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
  286. }
  287. })
  288. it('should return an error if post reply not in same thread', async () => {
  289. try {
  290. let threadId = (await replyAgent
  291. .post('/api/v1/thread')
  292. .set('content-type', 'application/json')
  293. .send({
  294. name: 'another thread',
  295. category: 'category_name'
  296. })).body.id
  297. let res = await replyAgent
  298. .post('/api/v1/post')
  299. .set('content-type', 'application/json')
  300. .send({
  301. content: 'yet another post',
  302. threadId: threadId,
  303. replyingToId: 1
  304. })
  305. res.should.have.status(400)
  306. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
  307. } catch (res) {
  308. let body = JSON.parse(res.response.text)
  309. res.should.have.status(400)
  310. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
  311. }
  312. })
  313. })
  314. describe('GET /thread/:id', () => {
  315. it('should return the thread and corresponding posts', async () => {
  316. let res = await chai.request(server).get('/api/v1/thread/1')
  317. res.should.have.status(200)
  318. res.should.be.json
  319. res.body.should.have.property('name', 'thread')
  320. res.body.should.have.deep.property('Category.name', 'category_name')
  321. res.body.should.have.deep.property('User.username', 'username')
  322. res.body.should.have.property('Posts')
  323. res.body.Posts.should.have.property('length', 2)
  324. res.body.Posts.should.contain.something.that.has.property('content', '<p>content</p>\n')
  325. res.body.Posts.should.contain.something.that.has.deep.property('User.username', 'username')
  326. res.body.Posts.should.contain.something.that.has.property('content', '<p>another post</p>\n')
  327. res.body.Posts.should.contain.something.that.has.deep.property('User.username', 'username1')
  328. })
  329. it('should allow pagination', async () => {
  330. let thread = await userAgent
  331. .post('/api/v1/thread')
  332. .set('content-type', 'application/json')
  333. .send({ category: 'category_name', name: 'pagination' })
  334. let threadOther = await userAgent
  335. .post('/api/v1/thread')
  336. .set('content-type', 'application/json')
  337. .send({ category: 'category_name', name: 'pagination_other' })
  338. PAGINATION_THREAD_ID = thread.body.id
  339. for(var i = 0; i < 30; i++) {
  340. let post = await userAgent
  341. .post('/api/v1/post')
  342. .set('content-type', 'application/json')
  343. .send({ threadId: thread.body.id, content: `POST ${i}` })
  344. if(i === 3) {
  345. await userAgent
  346. .post('/api/v1/post')
  347. .set('content-type', 'application/json')
  348. .send({ threadId: threadOther.body.id, content: `POST OTHER ${i}` })
  349. }
  350. if(i === 15) MID_PAGINATION_POST_ID = post.body.id
  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 + '?lastId=' + 100)
  356. pageOne.body.Posts.should.have.length(10)
  357. pageOne.body.meta.should.have.property('previousURL', null)
  358. pageOne.body.Posts[0].should.have.property('content', '<p>POST 0</p>\n')
  359. pageTwo.body.Posts.should.have.length(10)
  360. pageTwo.body.Posts[0].should.have.property('content', '<p>POST 10</p>\n')
  361. pageTwo.body.meta.should.have.property('previousURL')
  362. pageThree.body.Posts.should.have.length(10)
  363. pageThree.body.Posts[0].should.have.property('content', '<p>POST 20</p>\n')
  364. pageThree.body.Posts[9].should.have.property('content', '<p>POST 29</p>\n')
  365. expect(pageThree.body.meta.nextURL).to.be.null
  366. pageInvalid.body.Posts.should.have.length(0)
  367. })
  368. it('should allow you to get an individual and surrounding posts', async () => {
  369. let http = chai.request(server)
  370. let pageOne = await http.get(`/api/v1/thread/${PAGINATION_THREAD_ID}?postId=${MID_PAGINATION_POST_ID}`)
  371. let pageZero = await http.get(pageOne.body.meta.previousURL)
  372. let pageTwo = await http.get(pageOne.body.meta.nextURL)
  373. pageOne.body.Posts.should.have.length(10)
  374. pageOne.body.Posts[0].should.have.property('content', '<p>POST 11</p>\n')
  375. pageOne.body.Posts[4].should.have.property('content', '<p>POST 15</p>\n')
  376. pageOne.body.Posts[9].should.have.property('content', '<p>POST 20</p>\n')
  377. pageTwo.body.Posts.should.have.length(9)
  378. pageTwo.body.Posts[0].should.have.property('content', '<p>POST 21</p>\n')
  379. pageTwo.body.Posts[8].should.have.property('content', '<p>POST 29</p>\n')
  380. pageTwo.body.meta.should.have.property('nextURL', null)
  381. pageZero.body.Posts.should.have.length(10)
  382. pageZero.body.Posts[0].should.have.property('content', '<p>POST 1</p>\n')
  383. pageZero.body.Posts[9].should.have.property('content', '<p>POST 10</p>\n')
  384. let pageFirst = await http.get(pageZero.body.meta.previousURL)
  385. pageFirst.body.Posts[0].should.have.property('content', '<p>POST 0</p>\n')
  386. pageFirst.body.meta.should.have.property('previousURL', null)
  387. })
  388. it('should return an error if :id is invalid', async () => {
  389. try {
  390. let res = await chai.request(server).get('/api/v1/thread/invalid')
  391. res.should.have.status(400)
  392. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  393. } catch (res) {
  394. let body = JSON.parse(res.response.text)
  395. res.should.have.status(400)
  396. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  397. }
  398. })
  399. })
  400. describe('GET /post/:id', () => {
  401. it('should return the post', async () => {
  402. let res = await chai.request(server).get('/api/v1/post/1')
  403. res.should.have.status(200)
  404. res.should.be.json
  405. res.body.should.have.property('content', '<p>content</p>\n')
  406. res.body.should.have.deep.property('User.username', 'username')
  407. res.body.should.have.deep.property('Thread.name', 'thread')
  408. res.body.should.have.deep.property('Thread.Category.name', 'category_name')
  409. res.body.should.have.deep.property('Replies.0.User.username', 'username1')
  410. })
  411. it('should return an error if invalid post id', async () => {
  412. try {
  413. let res = await chai.request(server).get('/api/v1/post/invalid')
  414. res.should.have.status(400)
  415. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'post does not exist'))
  416. } catch (res) {
  417. let body = JSON.parse(res.response.text)
  418. res.should.have.status(400)
  419. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'post does not exist'))
  420. }
  421. })
  422. })
  423. })