thread_post.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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.property('postNumber', 1)
  162. res.body.should.have.deep.property('User.username', 'username')
  163. res.body.should.have.deep.property('Thread.name', 'thread')
  164. res.body.should.have.deep.property('Thread.postsCount', 1)
  165. })
  166. it('should return an error if not logged in', async () => {
  167. try {
  168. let res = await chai.request(server)
  169. .post('/api/v1/post')
  170. .set('content-type', 'application/json')
  171. .send({
  172. content: 'content',
  173. threadId: 1
  174. })
  175. res.should.be.json
  176. res.should.have.status(401)
  177. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  178. } catch (res) {
  179. res.should.have.status(401)
  180. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  181. }
  182. })
  183. it('should return an error if missing parameters', async () => {
  184. try {
  185. let res = await userAgent
  186. .post('/api/v1/post')
  187. res.should.be.json
  188. res.should.have.status(400)
  189. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('content'))
  190. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('threadId'))
  191. } catch (res) {
  192. let body = JSON.parse(res.response.text)
  193. res.should.have.status(400)
  194. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('content'))
  195. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('threadId'))
  196. }
  197. })
  198. it('should return an error if invalid types', async () => {
  199. try {
  200. let res = await userAgent
  201. .post('/api/v1/post')
  202. .set('content-type', 'application/json')
  203. .send({
  204. content: 123,
  205. threadId: 'string',
  206. replyingToId: 'string'
  207. })
  208. res.should.be.json
  209. res.should.have.status(400)
  210. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('content', 'string'))
  211. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('threadId', 'integer'))
  212. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('replyingToId', 'integer'))
  213. } catch (res) {
  214. let body = JSON.parse(res.response.text)
  215. res.should.have.status(400)
  216. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('content', 'string'))
  217. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('threadId', 'integer'))
  218. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('replyingToId', 'integer'))
  219. }
  220. })
  221. it('should return an error if thread id does not exist', async () => {
  222. try {
  223. let res = await userAgent
  224. .post('/api/v1/post')
  225. .set('content-type', 'application/json')
  226. .send({
  227. content: 'content',
  228. threadId: 10
  229. })
  230. res.should.be.json
  231. res.should.have.status(400)
  232. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('threadId', 'thread does not exist'))
  233. } catch (res) {
  234. let body = JSON.parse(res.response.text)
  235. res.should.have.status(400)
  236. body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('threadId', 'thread does not exist'))
  237. }
  238. })
  239. it('should be able to reply to a post', async () => {
  240. await replyAgent
  241. .post('/api/v1/user')
  242. .set('content-type', 'application/json')
  243. .send({
  244. username: 'username1',
  245. password: 'password'
  246. })
  247. let res = await replyAgent
  248. .post('/api/v1/post')
  249. .set('content-type', 'application/json')
  250. .send({
  251. content: 'another post',
  252. threadId: 1,
  253. replyingToId: 1
  254. })
  255. res.should.be.json
  256. res.should.have.status(200)
  257. res.body.should.have.property('postNumber', 2)
  258. res.body.should.have.property('content', '<p>another post</p>\n')
  259. res.body.should.have.deep.property('User.username', 'username1')
  260. res.body.should.have.deep.property('Thread.name', 'thread')
  261. res.body.should.have.deep.property('Thread.postsCount', 2)
  262. res.body.should.have.property('replyingToUsername', 'username')
  263. res.body.should.have.property('Replies').that.deep.equals([])
  264. })
  265. it('should return any replies to a post', async () => {
  266. let res = await replyAgent.get('/api/v1/post/1')
  267. res.should.be.json
  268. res.should.have.status(200)
  269. res.body.should.have.deep.property('replyingToUsername', null)
  270. res.body.should.have.deep.property('Replies.0.content', '<p>another post</p>\n')
  271. })
  272. it('should return an error if reply id does not exist', async () => {
  273. try {
  274. let res = await replyAgent
  275. .post('/api/v1/post')
  276. .set('content-type', 'application/json')
  277. .send({
  278. content: 'yet another post',
  279. threadId: 1,
  280. replyingToId: 10
  281. })
  282. res.should.have.status(400)
  283. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
  284. } catch (res) {
  285. let body = JSON.parse(res.response.text)
  286. res.should.have.status(400)
  287. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
  288. }
  289. })
  290. it('should return an error if post reply not in same thread', async () => {
  291. try {
  292. let threadId = (await replyAgent
  293. .post('/api/v1/thread')
  294. .set('content-type', 'application/json')
  295. .send({
  296. name: 'another thread',
  297. category: 'category_name'
  298. })).body.id
  299. let res = await replyAgent
  300. .post('/api/v1/post')
  301. .set('content-type', 'application/json')
  302. .send({
  303. content: 'yet another post',
  304. threadId: threadId,
  305. replyingToId: 1
  306. })
  307. res.should.have.status(400)
  308. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
  309. } catch (res) {
  310. let body = JSON.parse(res.response.text)
  311. res.should.have.status(400)
  312. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
  313. }
  314. })
  315. })
  316. describe('GET /thread/:id', () => {
  317. it('should return the thread and corresponding posts', async () => {
  318. let res = await chai.request(server).get('/api/v1/thread/1')
  319. res.should.have.status(200)
  320. res.should.be.json
  321. res.body.should.have.property('name', 'thread')
  322. res.body.should.have.deep.property('Category.name', 'category_name')
  323. res.body.should.have.deep.property('User.username', 'username')
  324. res.body.should.have.property('Posts')
  325. res.body.Posts.should.have.property('length', 2)
  326. res.body.Posts.should.contain.something.that.has.property('content', '<p>content</p>\n')
  327. res.body.Posts.should.contain.something.that.has.deep.property('User.username', 'username')
  328. res.body.Posts.should.contain.something.that.has.property('content', '<p>another post</p>\n')
  329. res.body.Posts.should.contain.something.that.has.deep.property('User.username', 'username1')
  330. })
  331. it('should allow pagination', async () => {
  332. let thread = await userAgent
  333. .post('/api/v1/thread')
  334. .set('content-type', 'application/json')
  335. .send({ category: 'category_name', name: 'pagination' })
  336. let threadOther = await userAgent
  337. .post('/api/v1/thread')
  338. .set('content-type', 'application/json')
  339. .send({ category: 'category_name', name: 'pagination_other' })
  340. PAGINATION_THREAD_ID = thread.body.id
  341. for(var i = 0; i < 30; i++) {
  342. let post = await userAgent
  343. .post('/api/v1/post')
  344. .set('content-type', 'application/json')
  345. .send({ threadId: thread.body.id, content: `POST ${i}` })
  346. if(i === 3) {
  347. await userAgent
  348. .post('/api/v1/post')
  349. .set('content-type', 'application/json')
  350. .send({ threadId: threadOther.body.id, content: `POST OTHER ${i}` })
  351. }
  352. if(i === 15) MID_PAGINATION_POST_ID = post.body.id
  353. }
  354. let pageOne = await userAgent.get('/api/v1/thread/' + thread.body.id)
  355. let pageTwo = await userAgent.get(pageOne.body.meta.nextURL)
  356. let pageThree = await userAgent.get(pageTwo.body.meta.nextURL)
  357. let pageInvalid = await userAgent.get('/api/v1/thread/' + thread.body.id + '?lastId=' + 100)
  358. pageOne.body.Posts.should.have.length(10)
  359. pageOne.body.meta.should.have.property('previousURL', null)
  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.Posts[0].should.have.property('content', '<p>POST 10</p>\n')
  363. pageTwo.body.meta.should.have.property('previousURL')
  364. pageThree.body.Posts.should.have.length(10)
  365. pageThree.body.Posts[0].should.have.property('content', '<p>POST 20</p>\n')
  366. pageThree.body.Posts[9].should.have.property('content', '<p>POST 29</p>\n')
  367. expect(pageThree.body.meta.nextURL).to.be.null
  368. pageInvalid.body.Posts.should.have.length(0)
  369. })
  370. it('should allow you to get an individual and surrounding posts', async () => {
  371. let http = chai.request(server)
  372. let pageOne = await http.get(`/api/v1/thread/${PAGINATION_THREAD_ID}?postId=${MID_PAGINATION_POST_ID}`)
  373. let pageZero = await http.get(pageOne.body.meta.previousURL)
  374. let pageTwo = await http.get(pageOne.body.meta.nextURL)
  375. pageOne.body.Posts.should.have.length(10)
  376. pageOne.body.Posts[0].should.have.property('content', '<p>POST 11</p>\n')
  377. pageOne.body.Posts[4].should.have.property('content', '<p>POST 15</p>\n')
  378. pageOne.body.Posts[9].should.have.property('content', '<p>POST 20</p>\n')
  379. pageTwo.body.Posts.should.have.length(9)
  380. pageTwo.body.Posts[0].should.have.property('content', '<p>POST 21</p>\n')
  381. pageTwo.body.Posts[8].should.have.property('content', '<p>POST 29</p>\n')
  382. pageTwo.body.meta.should.have.property('nextURL', null)
  383. pageZero.body.Posts.should.have.length(10)
  384. pageZero.body.Posts[0].should.have.property('content', '<p>POST 1</p>\n')
  385. pageZero.body.Posts[9].should.have.property('content', '<p>POST 10</p>\n')
  386. let pageFirst = await http.get(pageZero.body.meta.previousURL)
  387. pageFirst.body.Posts[0].should.have.property('content', '<p>POST 0</p>\n')
  388. pageFirst.body.meta.should.have.property('previousURL', null)
  389. })
  390. it('should return an error if :id is invalid', async () => {
  391. try {
  392. let res = await chai.request(server).get('/api/v1/thread/invalid')
  393. res.should.have.status(400)
  394. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  395. } catch (res) {
  396. let body = JSON.parse(res.response.text)
  397. res.should.have.status(400)
  398. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  399. }
  400. })
  401. })
  402. describe('GET /post/:id', () => {
  403. it('should return the post', async () => {
  404. let res = await chai.request(server).get('/api/v1/post/1')
  405. res.should.have.status(200)
  406. res.should.be.json
  407. res.body.should.have.property('content', '<p>content</p>\n')
  408. res.body.should.have.deep.property('User.username', 'username')
  409. res.body.should.have.deep.property('Thread.name', 'thread')
  410. res.body.should.have.deep.property('Thread.Category.name', 'category_name')
  411. res.body.should.have.deep.property('Replies.0.User.username', 'username1')
  412. })
  413. it('should return an error if invalid post id', async () => {
  414. try {
  415. let res = await chai.request(server).get('/api/v1/post/invalid')
  416. res.should.have.status(400)
  417. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'post does not exist'))
  418. } catch (res) {
  419. let body = JSON.parse(res.response.text)
  420. res.should.have.status(400)
  421. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'post does not exist'))
  422. }
  423. })
  424. })
  425. })