123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- process.env.NODE_ENV = 'test'
- let chai = require('chai')
- let server = require('../server')
- let should = chai.should()
- let { sequelize } = require('../models')
- const Errors = require('../lib/errors.js')
- chai.use(require('chai-http'))
- chai.use(require('chai-things'))
- describe('Thread and post', () => {
- let userAgent
- //Wait for app to start before commencing
- before((done) => {
- if(server.locals.appStarted) mockData()
- server.on('appStarted', () => {
- mockData()
- })
- function mockData() {
- userAgent = chai.request.agent(server)
- userAgent
- .post('/api/v1/user')
- .set('content-type', 'application/json')
- .send({
- username: 'username',
- password: 'password',
- admin: true
- })
- .then(() => {
- userAgent
- .post('/api/v1/category')
- .set('content-type', 'application/json')
- .send({ name: 'category_name' })
- .then(() => { done() })
- .catch(done)
- })
- .catch(done)
- }
- })
- //Delete all rows in table after
- //tests completed
- after(() => {
- sequelize.sync({ force: true })
- })
- describe('POST /thread', () => {
- it('should create a thread if logged in', async () => {
- let res = await userAgent
- .post('/api/v1/thread')
- .set('content-type', 'application/json')
- .send({
- name: 'thread',
- category: 'category_name'
- })
- res.should.have.status(200)
- res.should.be.json
- res.body.should.have.property('name', 'thread')
- res.body.should.have.deep.property('User.username', 'username')
- res.body.should.have.deep.property('Category.name', 'category_name')
- })
- it('should return an error if not logged in', async () => {
- try {
- let res = await chai.request(server)
- .post('/api/v1/thread')
- .set('content-type', 'application/json')
- .send({
- name: 'thread',
- category: 'category_name'
- })
- res.should.be.json
- res.should.have.status(401)
- res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
- } catch (res) {
- res.should.have.status(401)
- JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
- }
- })
- it('should return an error if missing parameters', async () => {
- try {
- let res = await userAgent
- .post('/api/v1/thread')
- res.should.be.json
- res.should.have.status(400)
- res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('name'))
- res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('category'))
- } catch (res) {
- let body = JSON.parse(res.response.text)
- res.should.have.status(400)
- body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('name'))
- body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('category'))
- }
- })
- it('should return an error if invalid types', async () => {
- try {
- let res = await userAgent
- .post('/api/v1/thread')
- .set('content-type', 'application/json')
- .send({
- name: 123,
- category: 123
- })
- res.should.be.json
- res.should.have.status(400)
- res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('name', 'string'))
- res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('category', 'string'))
- } catch (res) {
- let body = JSON.parse(res.response.text)
- res.should.have.status(400)
- body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('name', 'string'))
- body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('category', 'string'))
- }
- })
- it('should return an error if category does not exist', async () => {
- try {
- let res = await userAgent
- .post('/api/v1/thread')
- .set('content-type', 'application/json')
- .send({
- name: 'thread1',
- category: 'non-existent'
- })
- res.should.be.json
- res.should.have.status(400)
- res.body.errors.should.contain.something.that.deep.equals(Errors.invalidCategory)
- } catch (res) {
- res.should.have.status(400)
- JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.invalidCategory)
- }
- })
- })
- describe('GET /thread/:id', () => {
- it('should return the thread and corresponding posts', async () => {})
- it('should return an error if :id is invalid', async () => {})
- })
- describe('POST /post', () => {
- it('should create a post if logged in', async () => {})
- it('should return an error if not logged in', async () => {})
- it('should return an error if missing parameters', async () => {})
- it('should return an error if invalid types', async () => {})
- it('should return an error if thread id does not exist', async () => {})
- it('should be able to reply to a post', async () => {})
- it('should return an error if reply id does not exist', async () => {})
- it('should return an error if post reply not in same thread', async () => {})
- })
- describe('GET /post/:id', () => {
- it('should return the post', async () => {})
- it('should return an error if invalid post id', async () => {})
- })
- })
|