Ver Fonte

Add delete method

sbkwgh há 8 anos atrás
pai
commit
2016e8eb15
2 ficheiros alterados com 117 adições e 0 exclusões
  1. 34 0
      routes/category.js
  2. 83 0
      test/category.js

+ 34 - 0
routes/category.js

@@ -209,4 +209,38 @@ router.put('/:category_id', async (req, res) => {
 	}
 })
 
+router.delete('/:id', async (req, res) => {
+	try {
+		let category = await Category.findById(req.params.id)
+		if(!category) throw Errors.sequelizeValidation(Sequelize, {
+			error: 'category id does not exist',
+			value: req.params.id
+		})
+
+		let otherCategory = await Category.findOrCreate({
+			where: { name: 'Other' },
+			defaults: { color: '#9a9a9a' }
+		})
+
+		let up = await Thread.update({ CategoryId: otherCategory[0].id }, {
+			where: { CategoryId: req.params.id }
+		})
+
+		await category.destroy()
+
+		res.json({ success: true })
+	} catch (e) {
+		if(e instanceof Sequelize.ValidationError) {
+			res.status(400)
+			res.json(e)
+		} else {
+			console.log(e)
+			res.status(500)
+			res.json({
+				errors: [Errors.unknown]
+			})
+		}
+	}
+})
+
 module.exports = router

+ 83 - 0
test/category.js

@@ -419,4 +419,87 @@ describe('Category', () => {
 				})
 		})
 	})
+
+	describe('DELETE /category/:category_id', () => {
+		let admin = chai.request.agent(server)
+		let categoryId, thread1Id, thread2Id
+
+		before(done => {
+			admin
+			 	.post('/api/v1/user/adminaccount/login')
+				.set('content-type', 'application/json')
+				.send({ password: 'password' })
+				.then(res => {
+					return admin
+						.post('/api/v1/category')
+						.set('content-type', 'application/json')
+						.send({ name: 'category_to_delete' })
+				})
+				.then(res => {
+					categoryId = res.body.id
+					
+					return admin
+						.post('/api/v1/thread')
+						.set('content-type', 'application/json')
+						.send({ name: 'thread1', category: 'category_to_delete' })
+				})
+				.then(res => {
+					thread1Id = res.body.id
+					
+					return admin
+						.post('/api/v1/thread')
+						.set('content-type', 'application/json')
+						.send({ name: 'thread2', category: 'category_to_delete' })
+				})
+				.then(res => {
+					thread2Id = res.body.id
+					done()
+				})
+				.catch(e => {
+					console.log(e)
+					done(e)
+				})
+		})
+
+		it('should delete a category and place all threads in that category into "Other"', async () => {
+			let res = await admin.delete('/api/v1/category/' + categoryId)
+			res.should.be.json
+			res.should.have.status(200)
+
+			let category = await Category.findById(categoryId)
+			expect(category).to.be.null
+
+			let thread1 = await Thread.findById(thread1Id, {
+				include: [Category]
+			})
+			let thread2 = await Thread.findById(thread2Id, {
+				include: [Category]
+			})
+
+			thread1.Category.should.have.property('name', 'Other')
+			thread2.Category.should.have.property('name', 'Other')
+		})
+		it('should return an error if not an admin', done => {
+			chai.request(server)
+				.delete('/api/v1/category/1')
+				.end((err, res) => {
+					res.should.be.json
+					res.should.have.status(401)
+					res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
+
+					done()
+				})
+		})
+		it('should return an error if invalid id', done => {
+			admin
+				.delete('/api/v1/category/notavalidid')
+				.end((err, res) => {
+					res.should.be.json
+					res.should.have.status(400)
+					res.body.errors.should.contain.something.with.property('message', 'category id does not exist')
+
+					done()
+				})
+		})
+	})
 })