123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- process.env.NODE_ENV = 'test'
- let chai = require('chai')
- let server = require('../server')
- let should = chai.should()
- let { sequelize, Log } = require('../models')
- const Errors = require('../lib/errors.js')
- chai.use(require('chai-http'))
- chai.use(require('chai-things'))
- describe('Log', () => {
- let admin = chai.request.agent(server)
- let user = chai.request.agent(server)
- //Wait for app to start before commencing
- before((done) => {
- if(server.locals.appStarted) done()
-
- server.on('appStarted', () => {
- done()
- })
- })
- describe('POST /', () => {
- before(async () => {
- try {
- await admin
- .post('/api/v1/user')
- .set('content-type', 'application/json')
- .send({
- username: 'adminaccount',
- password: 'password',
- admin: true
- })
- await user
- .post('/api/v1/user')
- .set('content-type', 'application/json')
- .send({
- username: 'useraccount',
- password: 'password'
- })
- await admin
- .post('/api/v1/category')
- .set('content-type', 'application/json')
- .send({ name: 'category' })
- await admin
- .post('/api/v1/thread')
- .set('content-type', 'application/json')
- .send({ category: 'CATEGORY', name: 'thread' })
- await admin
- .post('/api/v1/thread')
- .set('content-type', 'application/json')
- .send({ category: 'CATEGORY', name: 'thread2' })
- await admin
- .post('/api/v1/thread')
- .set('content-type', 'application/json')
- .send({ category: 'CATEGORY', name: 'thread3' })
- await admin
- .post('/api/v1/thread')
- .set('content-type', 'application/json')
- .send({ category: 'CATEGORY', name: 'thread4' })
- await admin
- .post('/api/v1/post')
- .set('content-type', 'application/json')
- .send({ threadId: 1, content: 'post' })
- return true
- } catch (e) {
- return e
- }
- })
- it('should create a log for index route', async () => {
- let res = await chai.request(server)
- .post('/api/v1/log')
- .set('content-type', 'application/json')
- .send({ route: 'index' })
- res.should.have.status(200)
- res.should.be.json
- res.body.should.have.property('id', 1)
- res.body.should.have.property('route', 'index')
- res.body.should.not.have.property('UserId')
- res.body.should.not.have.property('ThreadId')
- let log = await Log.findById(res.body.id)
- log.should.not.be.null
- log.should.have.property('route', 'index')
- })
- it('should create a log for settingsAccount route', async () => {
- let res = await user
- .post('/api/v1/log')
- .set('content-type', 'application/json')
- .send({ route: 'settingsAccount' })
- res.should.have.status(200)
- res.should.be.json
- let log = await Log.findById(res.body.id)
- log.should.have.property('route', 'settingsAccount')
- log.should.have.property('SessionUserId', 2)
- })
- it('should create a log for thread route', async () => {
- let res = await chai.request(server)
- .post('/api/v1/log')
- .set('content-type', 'application/json')
- .send({ route: 'thread', resourceId: 1 })
- res.should.have.status(200)
- res.should.be.json
- let log = await Log.findById(res.body.id)
- log.should.have.property('route', 'thread')
- log.should.have.property('ThreadId', 1)
- })
- it('should create a log for userPosts route', async () => {
- let res = await chai.request(server)
- .post('/api/v1/log')
- .set('content-type', 'application/json')
- .send({ route: 'userPosts', resourceId: 1 })
- res.should.have.status(200)
- res.should.be.json
- let log = await Log.findById(res.body.id)
- log.should.have.property('route', 'userPosts')
- log.should.have.property('UserId', 1)
- })
- it('should return an error if invalid route', done => {
- chai.request(server)
- .post('/api/v1/log')
- .set('content-type', 'application/json')
- .send({ route: 'not a route' })
- .end((err, res) => {
- res.should.have.status(400)
- res.body.errors.should.contain.something.with.property('message', 'route does not exist')
- done()
- })
- })
- it('should return an error if invalid id for thread route', done => {
- chai.request(server)
- .post('/api/v1/log')
- .set('content-type', 'application/json')
- .send({ route: 'thread', resourceId: 404 })
- .end((err, res) => {
- res.should.have.status(400)
- res.body.errors.should.contain.something.with.property('message', 'thread does not exist')
- done()
- })
- })
- it('should return an error if invalid id for userPosts route', done => {
- chai.request(server)
- .post('/api/v1/log')
- .set('content-type', 'application/json')
- .send({ route: 'userPosts', resourceId: 404 })
- .end((err, res) => {
- res.should.have.status(400)
- res.body.errors.should.contain.something.with.property('message', 'user does not exist')
- done()
- })
- })
- it('should return an error if invalid id for userThreads route', done => {
- chai.request(server)
- .post('/api/v1/log')
- .set('content-type', 'application/json')
- .send({ route: 'userThreads', resourceId: 404 })
- .end((err, res) => {
- res.should.have.status(400)
- res.body.errors.should.contain.something.with.property('message', 'user does not exist')
- done()
- })
- })
- it('should return an error if not logged in for settingsAccount route', done => {
- chai.request(server)
- .post('/api/v1/log')
- .set('content-type', 'application/json')
- .send({ route: 'settingsAccount' })
- .end((err, res) => {
- res.should.have.status(401)
- res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
- done()
- })
- })
- it('should return an error if not logged in for settingsGeneral route', done => {
- chai.request(server)
- .post('/api/v1/log')
- .set('content-type', 'application/json')
- .send({ route: 'settingsGeneral' })
- .end((err, res) => {
- res.should.have.status(401)
- res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
- done()
- })
- })
- })
- describe('GET /top-threads', () => {
- before(async () => {
- try {
- let requests = []
- for(let i = 0; i < 5; i++) {
- requests.push(user
- .post('/api/v1/log')
- .set('content-type', 'application/json')
- .send({ route: 'thread', resourceId: 1 }))
- }
- for(let i = 0; i < 3; i++) {
- requests.push(user
- .post('/api/v1/log')
- .set('content-type', 'application/json')
- .send({ route: 'thread', resourceId: 2 }))
- }
- for(let i = 0; i < 7; i++) {
- requests.push(user
- .post('/api/v1/log')
- .set('content-type', 'application/json')
- .send({ route: 'thread', resourceId: 3 }))
- }
- requests.push(user
- .post('/api/v1/log')
- .set('content-type', 'application/json')
- .send({ route: 'thread', resourceId: 4 }))
- await Promise.all(requests)
- return true
- } catch (e) {
- return e
- }
- })
- it('should return top 3 threads', async () => {
- let res = await admin.get('/api/v1/log/top-threads')
- res.body[0].should.have.deep.property('Thread.name', 'thread3')
- res.body[1].should.have.deep.property('Thread.name', 'thread')
- res.body[2].should.have.deep.property('Thread.name', 'thread2')
- res.body[3].should.have.deep.property('Thread.name', 'thread4')
- res.body[0].should.have.property('pageViews', 7)
- //6 because there was a previous log to the thread in previous test
- res.body[1].should.have.property('pageViews', 6)
- res.body[2].should.have.property('pageViews', 3)
- res.body[3].should.have.property('pageViews', 1)
- })
- it('should return an error if not an admin', done => {
- user
- .get('/api/v1/log/top-threads')
- .end((err, res) => {
- res.should.have.status(401)
-
- res.body.errors.should.contain.something.that.deep.equals(
- Errors.requestNotAuthorized
- )
- done()
- })
- })
- })
- after(() => sequelize.sync({ force: true }))
- })
|