thread_post.js 19 KB

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