thread_post.js 19 KB

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