log.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. process.env.NODE_ENV = 'test'
  2. let chai = require('chai')
  3. let server = require('../server')
  4. let should = chai.should()
  5. let { sequelize, Log } = require('../models')
  6. const Errors = require('../lib/errors.js')
  7. chai.use(require('chai-http'))
  8. chai.use(require('chai-things'))
  9. describe('Log', () => {
  10. let admin = chai.request.agent(server)
  11. let user = chai.request.agent(server)
  12. //Wait for app to start before commencing
  13. before((done) => {
  14. if(server.locals.appStarted) done()
  15. server.on('appStarted', () => {
  16. done()
  17. })
  18. })
  19. describe('POST /', () => {
  20. before(async () => {
  21. try {
  22. await admin
  23. .post('/api/v1/user')
  24. .set('content-type', 'application/json')
  25. .send({
  26. username: 'adminaccount',
  27. password: 'password',
  28. admin: true
  29. })
  30. await user
  31. .post('/api/v1/user')
  32. .set('content-type', 'application/json')
  33. .send({
  34. username: 'useraccount',
  35. password: 'password'
  36. })
  37. await admin
  38. .post('/api/v1/category')
  39. .set('content-type', 'application/json')
  40. .send({ name: 'category' })
  41. await admin
  42. .post('/api/v1/thread')
  43. .set('content-type', 'application/json')
  44. .send({ category: 'CATEGORY', name: 'thread' })
  45. await admin
  46. .post('/api/v1/post')
  47. .set('content-type', 'application/json')
  48. .send({ threadId: 1, content: 'post' })
  49. return true
  50. } catch (e) {
  51. return e
  52. }
  53. })
  54. it('should create a log for index route', async () => {
  55. let res = await chai.request(server)
  56. .post('/api/v1/log')
  57. .set('content-type', 'application/json')
  58. .send({ route: 'index' })
  59. res.should.have.status(200)
  60. res.should.be.json
  61. res.body.should.have.property('id', 1)
  62. res.body.should.have.property('route', 'index')
  63. res.body.should.not.have.property('UserId')
  64. res.body.should.not.have.property('ThreadId')
  65. let log = await Log.findById(res.body.id)
  66. log.should.not.be.null
  67. log.should.have.property('route', 'index')
  68. })
  69. it('should create a log for settingsAccount route', async () => {
  70. let res = await user
  71. .post('/api/v1/log')
  72. .set('content-type', 'application/json')
  73. .send({ route: 'settingsAccount' })
  74. res.should.have.status(200)
  75. res.should.be.json
  76. let log = await Log.findById(res.body.id)
  77. log.should.have.property('route', 'settingsAccount')
  78. log.should.have.property('SessionUserId', 2)
  79. })
  80. it('should create a log for thread route', async () => {
  81. let res = await chai.request(server)
  82. .post('/api/v1/log')
  83. .set('content-type', 'application/json')
  84. .send({ route: 'thread', resourceId: 1 })
  85. res.should.have.status(200)
  86. res.should.be.json
  87. let log = await Log.findById(res.body.id)
  88. log.should.have.property('route', 'thread')
  89. log.should.have.property('ThreadId', 1)
  90. })
  91. it('should create a log for userPosts route', async () => {
  92. let res = await chai.request(server)
  93. .post('/api/v1/log')
  94. .set('content-type', 'application/json')
  95. .send({ route: 'userPosts', resourceId: 1 })
  96. res.should.have.status(200)
  97. res.should.be.json
  98. let log = await Log.findById(res.body.id)
  99. log.should.have.property('route', 'userPosts')
  100. log.should.have.property('UserId', 1)
  101. })
  102. it('should return an error if invalid route', done => {
  103. chai.request(server)
  104. .post('/api/v1/log')
  105. .set('content-type', 'application/json')
  106. .send({ route: 'not a route' })
  107. .end((err, res) => {
  108. res.should.have.status(400)
  109. res.body.errors.should.contain.something.with.property('message', 'route does not exist')
  110. done()
  111. })
  112. })
  113. it('should return an error if invalid id for thread route', done => {
  114. chai.request(server)
  115. .post('/api/v1/log')
  116. .set('content-type', 'application/json')
  117. .send({ route: 'thread', resourceId: 404 })
  118. .end((err, res) => {
  119. res.should.have.status(400)
  120. res.body.errors.should.contain.something.with.property('message', 'thread does not exist')
  121. done()
  122. })
  123. })
  124. it('should return an error if invalid id for userPosts route', done => {
  125. chai.request(server)
  126. .post('/api/v1/log')
  127. .set('content-type', 'application/json')
  128. .send({ route: 'userPosts', resourceId: 404 })
  129. .end((err, res) => {
  130. res.should.have.status(400)
  131. res.body.errors.should.contain.something.with.property('message', 'user does not exist')
  132. done()
  133. })
  134. })
  135. it('should return an error if invalid id for userThreads route', done => {
  136. chai.request(server)
  137. .post('/api/v1/log')
  138. .set('content-type', 'application/json')
  139. .send({ route: 'userThreads', resourceId: 404 })
  140. .end((err, res) => {
  141. res.should.have.status(400)
  142. res.body.errors.should.contain.something.with.property('message', 'user does not exist')
  143. done()
  144. })
  145. })
  146. it('should return an error if not logged in for settingsAccount route', done => {
  147. chai.request(server)
  148. .post('/api/v1/log')
  149. .set('content-type', 'application/json')
  150. .send({ route: 'settingsAccount' })
  151. .end((err, res) => {
  152. res.should.have.status(401)
  153. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  154. done()
  155. })
  156. })
  157. it('should return an error if not logged in for settingsGeneral route', done => {
  158. chai.request(server)
  159. .post('/api/v1/log')
  160. .set('content-type', 'application/json')
  161. .send({ route: 'settingsGeneral' })
  162. .end((err, res) => {
  163. res.should.have.status(401)
  164. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  165. done()
  166. })
  167. })
  168. })
  169. after(() => sequelize.sync({ force: true }))
  170. })