thread_post.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. process.env.NODE_ENV = 'test'
  2. let chai = require('chai')
  3. let server = require('../server')
  4. let should = chai.should()
  5. let { sequelize } = require('../models')
  6. const Errors = require('../lib/errors.js')
  7. chai.use(require('chai-http'))
  8. chai.use(require('chai-things'))
  9. describe('Thread and post', () => {
  10. let userAgent, replyAgent
  11. //Wait for app to start before commencing
  12. before((done) => {
  13. if(server.locals.appStarted) mockData()
  14. server.on('appStarted', () => {
  15. mockData()
  16. })
  17. function mockData() {
  18. userAgent = chai.request.agent(server)
  19. replyAgent = chai.request.agent(server)
  20. userAgent
  21. .post('/api/v1/user')
  22. .set('content-type', 'application/json')
  23. .send({
  24. username: 'username',
  25. password: 'password',
  26. admin: true
  27. })
  28. .then(() => {
  29. userAgent
  30. .post('/api/v1/category')
  31. .set('content-type', 'application/json')
  32. .send({ name: 'category_name' })
  33. .then(() => { done() })
  34. .catch(done)
  35. })
  36. .catch(done)
  37. }
  38. })
  39. //Delete all rows in table after
  40. //tests completed
  41. after(() => {
  42. sequelize.sync({ force: true })
  43. })
  44. describe('POST /thread', () => {
  45. it('should create a thread if logged in', async () => {
  46. let res = await userAgent
  47. .post('/api/v1/thread')
  48. .set('content-type', 'application/json')
  49. .send({
  50. name: 'thread',
  51. category: 'category_name'
  52. })
  53. res.should.have.status(200)
  54. res.should.be.json
  55. res.body.should.have.property('name', 'thread')
  56. res.body.should.have.property('slug', 'thread')
  57. res.body.should.have.deep.property('User.username', 'username')
  58. res.body.should.have.deep.property('Category.name', 'category_name')
  59. })
  60. it('should add a slug from the thread name', async () => {
  61. let res = await userAgent
  62. .post('/api/v1/thread')
  63. .set('content-type', 'application/json')
  64. .send({
  65. name: ' à long thrËad, with lØts of àccents!!! ',
  66. category: 'category_name'
  67. })
  68. res.should.have.status(200)
  69. res.should.be.json
  70. res.body.should.have.property('name', ' à long thrËad, with lØts of àccents!!! ')
  71. res.body.should.have.property('slug', 'a-long-thread-with-lots-of-accents')
  72. res.body.should.have.deep.property('User.username', 'username')
  73. res.body.should.have.deep.property('Category.name', 'category_name')
  74. })
  75. it('should return an error if not logged in', async () => {
  76. try {
  77. let res = await chai.request(server)
  78. .post('/api/v1/thread')
  79. .set('content-type', 'application/json')
  80. .send({
  81. name: 'thread',
  82. category: 'category_name'
  83. })
  84. res.should.be.json
  85. res.should.have.status(401)
  86. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  87. } catch (res) {
  88. res.should.have.status(401)
  89. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  90. }
  91. })
  92. it('should return an error if missing parameters', async () => {
  93. try {
  94. let res = await userAgent
  95. .post('/api/v1/thread')
  96. res.should.be.json
  97. res.should.have.status(400)
  98. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('name'))
  99. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('category'))
  100. } catch (res) {
  101. let body = JSON.parse(res.response.text)
  102. res.should.have.status(400)
  103. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('name'))
  104. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('category'))
  105. }
  106. })
  107. it('should return an error if invalid types', async () => {
  108. try {
  109. let res = await userAgent
  110. .post('/api/v1/thread')
  111. .set('content-type', 'application/json')
  112. .send({
  113. name: 123,
  114. category: 123
  115. })
  116. res.should.be.json
  117. res.should.have.status(400)
  118. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('name', 'string'))
  119. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('category', 'string'))
  120. } catch (res) {
  121. let body = JSON.parse(res.response.text)
  122. res.should.have.status(400)
  123. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('name', 'string'))
  124. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('category', 'string'))
  125. }
  126. })
  127. it('should return an error if category does not exist', async () => {
  128. try {
  129. let res = await userAgent
  130. .post('/api/v1/thread')
  131. .set('content-type', 'application/json')
  132. .send({
  133. name: 'thread1',
  134. category: 'non-existent'
  135. })
  136. res.should.be.json
  137. res.should.have.status(400)
  138. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidCategory)
  139. } catch (res) {
  140. res.should.have.status(400)
  141. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.invalidCategory)
  142. }
  143. })
  144. })
  145. describe('POST /post', () => {
  146. it('should create a post if logged in', async () => {
  147. let res = await userAgent
  148. .post('/api/v1/post')
  149. .set('content-type', 'application/json')
  150. .send({
  151. content: 'content',
  152. threadId: 1
  153. })
  154. res.should.be.json
  155. res.should.have.status(200)
  156. res.body.should.have.property('content', '<p>content</p>\n')
  157. res.body.should.have.deep.property('User.username', 'username')
  158. res.body.should.have.deep.property('Thread.name', 'thread')
  159. })
  160. it('should return an error if not logged in', async () => {
  161. try {
  162. let res = await chai.request(server)
  163. .post('/api/v1/post')
  164. .set('content-type', 'application/json')
  165. .send({
  166. content: 'content',
  167. threadId: 1
  168. })
  169. res.should.be.json
  170. res.should.have.status(401)
  171. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  172. } catch (res) {
  173. res.should.have.status(401)
  174. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  175. }
  176. })
  177. it('should return an error if missing parameters', async () => {
  178. try {
  179. let res = await userAgent
  180. .post('/api/v1/post')
  181. res.should.be.json
  182. res.should.have.status(400)
  183. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('content'))
  184. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('threadId'))
  185. } catch (res) {
  186. let body = JSON.parse(res.response.text)
  187. res.should.have.status(400)
  188. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('content'))
  189. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('threadId'))
  190. }
  191. })
  192. it('should return an error if invalid types', async () => {
  193. try {
  194. let res = await userAgent
  195. .post('/api/v1/post')
  196. .set('content-type', 'application/json')
  197. .send({
  198. content: 123,
  199. threadId: 'string',
  200. replyingToId: 'string'
  201. })
  202. res.should.be.json
  203. res.should.have.status(400)
  204. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('content', 'string'))
  205. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('threadId', 'integer'))
  206. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('replyingToId', 'integer'))
  207. } catch (res) {
  208. let body = JSON.parse(res.response.text)
  209. res.should.have.status(400)
  210. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('content', 'string'))
  211. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('threadId', 'integer'))
  212. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('replyingToId', 'integer'))
  213. }
  214. })
  215. it('should return an error if thread id does not exist', async () => {
  216. try {
  217. let res = await userAgent
  218. .post('/api/v1/post')
  219. .set('content-type', 'application/json')
  220. .send({
  221. content: 'content',
  222. threadId: 10
  223. })
  224. res.should.be.json
  225. res.should.have.status(400)
  226. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('threadId', 'thread does not exist'))
  227. } catch (res) {
  228. let body = JSON.parse(res.response.text)
  229. res.should.have.status(400)
  230. body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('threadId', 'thread does not exist'))
  231. }
  232. })
  233. it('should be able to reply to a post', async () => {
  234. await replyAgent
  235. .post('/api/v1/user')
  236. .set('content-type', 'application/json')
  237. .send({
  238. username: 'username1',
  239. password: 'password'
  240. })
  241. let res = await replyAgent
  242. .post('/api/v1/post')
  243. .set('content-type', 'application/json')
  244. .send({
  245. content: 'another post',
  246. threadId: 1,
  247. replyingToId: 1
  248. })
  249. res.should.be.json
  250. res.should.have.status(200)
  251. res.body.should.have.property('content', '<p>another post</p>\n')
  252. res.body.should.have.deep.property('User.username', 'username1')
  253. res.body.should.have.deep.property('Thread.name', 'thread')
  254. res.body.should.have.property('replyingToUsername', 'username')
  255. res.body.should.have.property('Replies').that.deep.equals([])
  256. })
  257. it('should return any replies to a post', async () => {
  258. let res = await replyAgent.get('/api/v1/post/1')
  259. res.should.be.json
  260. res.should.have.status(200)
  261. res.body.should.have.deep.property('replyingToUsername', null)
  262. res.body.should.have.deep.property('Replies.0.content', '<p>another post</p>\n')
  263. })
  264. it('should return an error if reply id does not exist', async () => {
  265. try {
  266. let res = await replyAgent
  267. .post('/api/v1/post')
  268. .set('content-type', 'application/json')
  269. .send({
  270. content: 'yet another post',
  271. threadId: 1,
  272. replyingToId: 10
  273. })
  274. res.should.have.status(400)
  275. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
  276. } catch (res) {
  277. let body = JSON.parse(res.response.text)
  278. res.should.have.status(400)
  279. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
  280. }
  281. })
  282. it('should return an error if post reply not in same thread', async () => {
  283. try {
  284. let threadId = (await replyAgent
  285. .post('/api/v1/thread')
  286. .set('content-type', 'application/json')
  287. .send({
  288. name: 'another thread',
  289. category: 'category_name'
  290. })).body.id
  291. let res = await replyAgent
  292. .post('/api/v1/post')
  293. .set('content-type', 'application/json')
  294. .send({
  295. content: 'yet another post',
  296. threadId: threadId,
  297. replyingToId: 1
  298. })
  299. res.should.have.status(400)
  300. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
  301. } catch (res) {
  302. let body = JSON.parse(res.response.text)
  303. res.should.have.status(400)
  304. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
  305. }
  306. })
  307. })
  308. describe('GET /thread/:id', () => {
  309. it('should return the thread and corresponding posts', async () => {
  310. let res = await chai.request(server).get('/api/v1/thread/1')
  311. res.should.have.status(200)
  312. res.should.be.json
  313. res.body.thread.should.have.property('name', 'thread')
  314. res.body.thread.should.have.deep.property('Category.name', 'category_name')
  315. res.body.thread.should.have.deep.property('User.username', 'username')
  316. res.body.thread.should.have.property('Posts')
  317. res.body.thread.Posts.should.have.property('length', 2)
  318. res.body.thread.Posts.should.contain.something.that.has.property('content', '<p>content</p>\n')
  319. res.body.thread.Posts.should.contain.something.that.has.deep.property('User.username', 'username')
  320. res.body.thread.Posts.should.contain.something.that.has.property('content', '<p>another post</p>\n')
  321. res.body.thread.Posts.should.contain.something.that.has.deep.property('User.username', 'username1')
  322. })
  323. it('should allow pagination', async () => {
  324. let thread = await userAgent
  325. .post('/api/v1/thread')
  326. .set('content-type', 'application/json')
  327. .send({ category: 'category_name', name: 'pagination' })
  328. for(var i = 0; i < 30; i++) {
  329. await userAgent
  330. .post('/api/v1/post')
  331. .set('content-type', 'application/json')
  332. .send({ threadId: thread.body.id, content: `POST ${i}` })
  333. }
  334. let pageOne = await userAgent.get('/api/v1/thread/' + thread.body.id)
  335. let pageTwo = await userAgent.get('/api/v1/thread/' + thread.body.id + '?lastId=' + pageOne.body.meta.lastId)
  336. let pageThree = await userAgent.get('/api/v1/thread/' + thread.body.id + '?lastId=' + pageTwo.body.meta.lastId)
  337. let pageInvalid = await userAgent.get('/api/v1/thread/' + thread.body.id + '?lastId=' + 100)
  338. pageOne.body.thread.Posts.should.have.length(10)
  339. pageOne.body.thread.Posts[0].should.have.property('content', '<p>POST 0</p>\n')
  340. pageTwo.body.thread.Posts.should.have.length(10)
  341. pageTwo.body.thread.Posts[0].should.have.property('content', '<p>POST 10</p>\n')
  342. pageThree.body.thread.Posts.should.have.length(10)
  343. pageThree.body.thread.Posts[0].should.have.property('content', '<p>POST 20</p>\n')
  344. pageThree.body.thread.Posts[9].should.have.property('content', '<p>POST 29</p>\n')
  345. pageInvalid.body.thread.Posts.should.have.length(0)
  346. })
  347. it('should return an error if :id is invalid', async () => {
  348. try {
  349. let res = await chai.request(server).get('/api/v1/thread/invalid')
  350. res.should.have.status(400)
  351. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  352. } catch (res) {
  353. let body = JSON.parse(res.response.text)
  354. res.should.have.status(400)
  355. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  356. }
  357. })
  358. })
  359. describe('GET /post/:id', () => {
  360. it('should return the post', async () => {
  361. let res = await chai.request(server).get('/api/v1/post/1')
  362. res.should.have.status(200)
  363. res.should.be.json
  364. res.body.should.have.property('content', '<p>content</p>\n')
  365. res.body.should.have.deep.property('User.username', 'username')
  366. res.body.should.have.deep.property('Thread.name', 'thread')
  367. res.body.should.have.deep.property('Thread.Category.name', 'category_name')
  368. res.body.should.have.deep.property('Replies.0.User.username', 'username1')
  369. })
  370. it('should return an error if invalid post id', async () => {
  371. try {
  372. let res = await chai.request(server).get('/api/v1/post/invalid')
  373. res.should.have.status(400)
  374. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'post does not exist'))
  375. } catch (res) {
  376. let body = JSON.parse(res.response.text)
  377. res.should.have.status(400)
  378. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'post does not exist'))
  379. }
  380. })
  381. })
  382. })