log.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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/thread')
  47. .set('content-type', 'application/json')
  48. .send({ category: 'CATEGORY', name: 'thread2' })
  49. await admin
  50. .post('/api/v1/thread')
  51. .set('content-type', 'application/json')
  52. .send({ category: 'CATEGORY', name: 'thread3' })
  53. await admin
  54. .post('/api/v1/thread')
  55. .set('content-type', 'application/json')
  56. .send({ category: 'CATEGORY', name: 'thread4' })
  57. await admin
  58. .post('/api/v1/post')
  59. .set('content-type', 'application/json')
  60. .send({ threadId: 1, content: 'post' })
  61. return true
  62. } catch (e) {
  63. return e
  64. }
  65. })
  66. it('should create a log for index route', async () => {
  67. let res = await chai.request(server)
  68. .post('/api/v1/log')
  69. .set('content-type', 'application/json')
  70. .send({ route: 'index' })
  71. res.should.have.status(200)
  72. res.should.be.json
  73. res.body.should.have.property('id', 1)
  74. res.body.should.have.property('route', 'index')
  75. res.body.should.not.have.property('UserId')
  76. res.body.should.not.have.property('ThreadId')
  77. let log = await Log.findById(res.body.id)
  78. log.should.not.be.null
  79. log.should.have.property('route', 'index')
  80. })
  81. it('should create a log for settingsAccount route', async () => {
  82. let res = await user
  83. .post('/api/v1/log')
  84. .set('content-type', 'application/json')
  85. .send({ route: 'settingsAccount' })
  86. res.should.have.status(200)
  87. res.should.be.json
  88. let log = await Log.findById(res.body.id)
  89. log.should.have.property('route', 'settingsAccount')
  90. log.should.have.property('SessionUserId', 2)
  91. })
  92. it('should create a log for thread route', async () => {
  93. let res = await chai.request(server)
  94. .post('/api/v1/log')
  95. .set('content-type', 'application/json')
  96. .send({ route: 'thread', resourceId: 1 })
  97. res.should.have.status(200)
  98. res.should.be.json
  99. let log = await Log.findById(res.body.id)
  100. log.should.have.property('route', 'thread')
  101. log.should.have.property('ThreadId', 1)
  102. })
  103. it('should create a log for userPosts route', async () => {
  104. let res = await chai.request(server)
  105. .post('/api/v1/log')
  106. .set('content-type', 'application/json')
  107. .send({ route: 'userPosts', resourceId: 1 })
  108. res.should.have.status(200)
  109. res.should.be.json
  110. let log = await Log.findById(res.body.id)
  111. log.should.have.property('route', 'userPosts')
  112. log.should.have.property('UserId', 1)
  113. })
  114. it('should return an error if invalid route', done => {
  115. chai.request(server)
  116. .post('/api/v1/log')
  117. .set('content-type', 'application/json')
  118. .send({ route: 'not a route' })
  119. .end((err, res) => {
  120. res.should.have.status(400)
  121. res.body.errors.should.contain.something.with.property('message', 'route does not exist')
  122. done()
  123. })
  124. })
  125. it('should return an error if invalid id for thread route', done => {
  126. chai.request(server)
  127. .post('/api/v1/log')
  128. .set('content-type', 'application/json')
  129. .send({ route: 'thread', resourceId: 404 })
  130. .end((err, res) => {
  131. res.should.have.status(400)
  132. res.body.errors.should.contain.something.with.property('message', 'thread does not exist')
  133. done()
  134. })
  135. })
  136. it('should return an error if invalid id for userPosts route', done => {
  137. chai.request(server)
  138. .post('/api/v1/log')
  139. .set('content-type', 'application/json')
  140. .send({ route: 'userPosts', resourceId: 404 })
  141. .end((err, res) => {
  142. res.should.have.status(400)
  143. res.body.errors.should.contain.something.with.property('message', 'user does not exist')
  144. done()
  145. })
  146. })
  147. it('should return an error if invalid id for userThreads route', done => {
  148. chai.request(server)
  149. .post('/api/v1/log')
  150. .set('content-type', 'application/json')
  151. .send({ route: 'userThreads', resourceId: 404 })
  152. .end((err, res) => {
  153. res.should.have.status(400)
  154. res.body.errors.should.contain.something.with.property('message', 'user does not exist')
  155. done()
  156. })
  157. })
  158. it('should return an error if not logged in for settingsAccount route', done => {
  159. chai.request(server)
  160. .post('/api/v1/log')
  161. .set('content-type', 'application/json')
  162. .send({ route: 'settingsAccount' })
  163. .end((err, res) => {
  164. res.should.have.status(401)
  165. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  166. done()
  167. })
  168. })
  169. it('should return an error if not logged in for settingsGeneral route', done => {
  170. chai.request(server)
  171. .post('/api/v1/log')
  172. .set('content-type', 'application/json')
  173. .send({ route: 'settingsGeneral' })
  174. .end((err, res) => {
  175. res.should.have.status(401)
  176. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  177. done()
  178. })
  179. })
  180. })
  181. describe('GET /top-threads', () => {
  182. before(async () => {
  183. try {
  184. let requests = []
  185. for(let i = 0; i < 5; i++) {
  186. requests.push(user
  187. .post('/api/v1/log')
  188. .set('content-type', 'application/json')
  189. .send({ route: 'thread', resourceId: 1 }))
  190. }
  191. for(let i = 0; i < 3; i++) {
  192. requests.push(user
  193. .post('/api/v1/log')
  194. .set('content-type', 'application/json')
  195. .send({ route: 'thread', resourceId: 2 }))
  196. }
  197. for(let i = 0; i < 7; i++) {
  198. requests.push(user
  199. .post('/api/v1/log')
  200. .set('content-type', 'application/json')
  201. .send({ route: 'thread', resourceId: 3 }))
  202. }
  203. requests.push(user
  204. .post('/api/v1/log')
  205. .set('content-type', 'application/json')
  206. .send({ route: 'thread', resourceId: 4 }))
  207. await Promise.all(requests)
  208. return true
  209. } catch (e) {
  210. return e
  211. }
  212. })
  213. it('should return top 3 threads', async () => {
  214. let res = await admin.get('/api/v1/log/top-threads')
  215. res.body[0].should.have.deep.property('Thread.name', 'thread3')
  216. res.body[1].should.have.deep.property('Thread.name', 'thread')
  217. res.body[2].should.have.deep.property('Thread.name', 'thread2')
  218. res.body[0].should.have.property('pageViews', 7)
  219. //6 because there was a previous log to the thread in previous test
  220. res.body[1].should.have.property('pageViews', 6)
  221. res.body[2].should.have.property('pageViews', 3)
  222. })
  223. it('should return an error if not an admin', done => {
  224. user
  225. .get('/api/v1/log/top-threads')
  226. .end((err, res) => {
  227. res.should.have.status(401)
  228. res.body.errors.should.contain.something.that.deep.equals(
  229. Errors.requestNotAuthorized
  230. )
  231. done()
  232. })
  233. })
  234. })
  235. after(() => sequelize.sync({ force: true }))
  236. })