Przeglądaj źródła

Add and implement tests for DELETE route

sbkwgh 8 lat temu
rodzic
commit
9f0a0ae991
2 zmienionych plików z 68 dodań i 0 usunięć
  1. 21 0
      routes/report.js
  2. 47 0
      test/report.js

+ 21 - 0
routes/report.js

@@ -75,5 +75,26 @@ router.get('/', async (req, res) => {
 		})
 	}
 })
+router.delete('/:id', async (req, res) => {
+	try {
+		let report = await Report.findById(req.params.id)
+		if(!report) throw Report.InvalidPostId(req.params.id)
+
+		await report.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

+ 47 - 0
test/report.js

@@ -198,4 +198,51 @@ describe('Report', () => {
 				})
 		})
 	})
+
+	describe('DELETE /report/:id', () => {
+		let reportId;
+
+		before(async () => {
+			let report = await Report.create({ reason: 'spam' })
+
+			let post = await Post.findById(1)
+			let user = await User.find({
+				where: { username: 'useraccount' }
+			})
+
+			await report.setFlaggedByUser(user)
+			await report.setPost(post)
+
+			reportId = report.id
+		})
+
+		it('should delete the report', async () => {
+			let res = await adminAccount.delete('/api/v1/report/' + reportId)
+			res.should.have.status(200)
+
+			let report = await Report.findById(reportId)
+
+			expect(report).to.be.null
+		})
+		it('should return an error if not an admin', done => {
+			userAccount
+				.delete('/api/v1/report/2')
+				.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 invalid id', done => {
+			adminAccount
+				.delete('/api/v1/report/fake')
+				.end((err, res) => {
+					res.should.have.status(400)
+					res.body.errors.should.contain.something.that.has.property('message', 'Post id is not valid')
+
+					done()
+				})
+		})
+	})
 })