123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- process.env.NODE_ENV = 'test'
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"
- let chai = require('chai')
- let server = require('../server')
- let should = chai.should()
- let expect = chai.expect
- let { sequelize, User, Ban } = require('../models')
- const Errors = require('../lib/errors.js')
- chai.use(require('chai-http'))
- chai.use(require('chai-things'))
- let adminAccount = chai.request.agent(server)
- let userAccount = chai.request.agent(server)
- let userAccountId, anotherUserAccountId
- describe('Ban', () => {
- //Wait for app to start before commencing
- before((done) => {
- function createAccounts () {
- adminAccount
- .post('/api/v1/user')
- .set('content-type', 'application/json')
- .send({
- username: 'adminaccount',
- password: 'password',
- admin: true
- })
- .then(_ => {
- return userAccount
- .post('/api/v1/user')
- .set('content-type', 'application/json')
- .send({
- username: 'anotheruseraccount',
- password: 'password'
- })
- })
- .then(res => {
- anotherUserAccountId = res.body.id
- return userAccount
- .post('/api/v1/user')
- .set('content-type', 'application/json')
- .send({
- username: 'useraccount',
- password: 'password'
- })
- })
- .then(res => {
- userAccountId = res.body.id
- return adminAccount
- .post('/api/v1/category')
- .set('content-type', 'application/json')
- .send({ name: 'category_name' })
- })
- .then(_ => {
- return userAccount
- .post('/api/v1/thread')
- .set('content-type', 'application/json')
- .send({ name: 'thread', category: 'category_name' })
- })
- .then(_ => done())
- .catch(done)
- }
- if(server.locals.appStarted) createAccounts()
- server.on('appStarted', () => {
- createAccounts()
- })
- })
- //Delete all rows in table after
- //tests completed
- after(() => sequelize.sync({ force: true }) )
- describe('/ban POST', () => {
- it('should create a ban for the user', async () => {
- let res = await adminAccount
- .post('/api/v1/ban')
- .set('content-type', 'application/json')
- .send({
- canCreatePosts: false,
- canCreateThreads: false,
- username: 'useraccount',
- message: 'ban message'
- })
- res.should.be.json
- res.should.have.status(200)
- res.body.should.have.deep.property('User.username', 'useraccount')
- let ban = await Ban.findById(1)
- ban.should.have.property('canCreatePosts', false)
- ban.should.have.property('canCreateThreads', false)
- ban.should.have.property('message', 'ban message')
- ban.should.have.property('UserId', userAccountId)
- })
- it('should return an error if not an administrator', done => {
- userAccount
- .post('/api/v1/ban')
- .set('content-type', 'application/json')
- .send({
- canCreatePosts: false,
- canCreateThreads: false,
- username: 'useraccount',
- message: 'ban message'
- })
- .end((err, res) => {
- res.should.be.json
- res.should.have.status(400)
- res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
- done()
- })
- })
- it('should return an error if user id is not valid', done => {
- adminAccount
- .post('/api/v1/ban')
- .set('content-type', 'application/json')
- .send({
- canCreatePosts: false,
- canCreateThreads: false,
- username: 'not a user',
- message: 'ban message'
- })
- .end((err, res) => {
- res.should.be.json
- res.should.have.status(400)
- res.body.errors.should.contain.something.that.has.property('message', 'user does not exist')
- done()
- })
- })
- it('should return an error if trying to post replies if permissions so set', done => {
- userAccount
- .post('/api/v1/post')
- .set('content-type', 'application/json')
- .send({
- threadId: 1,
- content: 'post'
- })
- .end((err, res) => {
- res.should.be.json
- res.should.have.status(400)
- res.body.errors.should.contain.something.with.property('message', 'ban message')
- done()
- })
- })
- it('should return an error if trying to create thread if permissions so set', done => {
- userAccount
- .post('/api/v1/thread')
- .set('content-type', 'application/json')
- .send({
- category: 'category',
- name: 'thread name'
- })
- .end((err, res) => {
- res.should.be.json
- res.should.have.status(400)
- res.body.errors.should.contain.something.with.property('message', 'ban message')
- done()
- })
- })
- })
- describe('/ban GET', () => {
- before(done => {
- adminAccount
- .post('/api/v1/ban')
- .set('content-type', 'application/json')
- .send({
- canCreatePosts: false,
- canCreateThreads: false,
- username: 'anotheruseraccount',
- message: 'ban message2'
- })
- .end((err, res) => {
- done()
- })
- })
- it('should get all bans', async () => {
- let res = await adminAccount.get('/api/v1/ban')
- res.should.be.json
- res.should.have.status(200)
- res.body.should.contain.something.with.deep.property('User.username', 'useraccount')
- res.body.should.have.length(2)
- })
- })
- describe('/ban/:ban_id DELETE', () => {
- it('should remove a ban', async () => {
- let res = await adminAccount.delete('/api/v1/ban/1')
- res.should.be.json
- res.should.have.status(200)
- let ban = await Ban.findAll()
- ban.should.have.length(1)
- ban[0].should.have.property('id', 2)
- })
- it('should return an error if ban id is not valid', done => {
- adminAccount.delete('/api/v1/ban/notarealid')
- .end((err, res) => {
- res.should.be.json
- res.should.have.status(400)
- res.body.errors.should.contain.something.that.has.property('message', 'ban does not exist')
- done()
- })
- })
- })
- })
|