thread_post.js 16 KB

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