search.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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('Search', () => {
  10. let admin = chai.request.agent(server)
  11. //Wait for app to start before commencing
  12. before((done) => {
  13. if(server.locals.appStarted) done()
  14. server.on('appStarted', () => {
  15. done()
  16. })
  17. })
  18. describe('GET /', () => {
  19. before(async () => {
  20. try {
  21. await admin
  22. .post('/api/v1/user')
  23. .set('content-type', 'application/json')
  24. .send({
  25. username: 'adminaccount',
  26. password: 'password',
  27. admin: true
  28. })
  29. await admin
  30. .post('/api/v1/category')
  31. .set('content-type', 'application/json')
  32. .send({ name: 'category' })
  33. await admin
  34. .post('/api/v1/thread')
  35. .set('content-type', 'application/json')
  36. .send({ category: 'CATEGORY', name: 'thread' })
  37. for(let i = 0; i < 25; i++) {
  38. let text = ''
  39. if(i == 4 || i == 15 || i == 21) {
  40. text = 'query ' + i
  41. } else {
  42. text = 'term ' + i
  43. }
  44. await admin
  45. .post('/api/v1/post')
  46. .set('content-type', 'application/json')
  47. .send({ threadId: 1, content: text })
  48. }
  49. return true
  50. } catch (e) {
  51. return e
  52. }
  53. })
  54. it('should return matching posts', async () => {
  55. let res = await admin.get('/api/v1/search?q=term')
  56. res.should.be.json
  57. res.should.have.status(200)
  58. res.body.posts.should.have.property('length', 10)
  59. res.body.posts[0].should.have.property('content', '<p><b>term</b> 24</p>\n')
  60. res.body.posts[0].should.have.deep.property('User.username', 'adminaccount')
  61. res.body.should.have.property('offset', 10)
  62. res.body.should.have.property('next', 10)
  63. })
  64. it('should allow pagination', async () => {
  65. let page2 = await admin.get('/api/v1/search?q=term&offset=10')
  66. page2.should.be.json
  67. page2.should.have.status(200)
  68. page2.body.posts.should.have.property('length', 10)
  69. page2.body.posts[0].should.have.property('content', '<p><b>term</b> 12</p>\n')
  70. page2.body.should.have.property('offset', 20)
  71. page2.body.should.have.property('next', 2)
  72. let page3 = await admin.get('/api/v1/search?q=term&offset=20')
  73. page3.body.posts.should.have.property('length', 2)
  74. page3.body.posts[1].should.have.property('content', '<p><b>term</b> 0</p>\n')
  75. page3.body.should.have.property('next', 0)
  76. })
  77. })
  78. after(() => sequelize.sync({ force: true }))
  79. })