Quellcode durchsuchen

Fix problem with associations and add test to check for this

sbkwgh vor 8 Jahren
Ursprung
Commit
019c6cc99c
2 geänderte Dateien mit 30 neuen und 2 gelöschten Zeilen
  1. 2 2
      models/report.js
  2. 28 0
      test/report.js

+ 2 - 2
models/report.js

@@ -13,8 +13,8 @@ module.exports = (sequelize, DataTypes) => {
 	}, {
 		classMethods: {
 			associate (models) {
-				Report.hasOne(models.User, { as: 'FlaggedByUser' })
-				Report.hasOne(models.Post)
+				Report.belongsTo(models.User, { as: 'FlaggedByUser' })
+				Report.belongsTo(models.Post)
 			},
 			InvalidPostId (value) {
 				return new sequelize.ValidationError('Post id is not valid', [

+ 28 - 0
test/report.js

@@ -104,6 +104,34 @@ describe('Report', () => {
 			report.should.have.property('reason', 'spam')
 		})
 
+		it('should be fine with multiple reports from one user', async () => {
+			let res = await userAccount
+				.post('/api/v1/report')
+				.set('content-type', 'application/json')
+				.send({
+					postId: 1,
+					reason: 'inappropriate'
+				})
+
+			res.should.have.status(200)
+			res.should.be.json
+
+			let report1 = await Report.findById(1, {
+				include: [{ model: User, as: 'FlaggedByUser' }]
+			})
+			report1.should.not.be.null
+			report1.should.have.deep.property('FlaggedByUser.username', 'useraccount')
+			report1.should.have.property('reason', 'spam')
+
+			let report2 = await Report.findById(res.body.id, {
+				include: [{ model: User, as: 'FlaggedByUser' }]
+			})
+
+			report2.should.not.be.null
+			report2.should.have.deep.property('FlaggedByUser.username', 'useraccount')
+			report2.should.have.property('reason', 'inappropriate')
+		})
+
 		it('should return an error if not a logged in user', done => {
 			chai.request(server)
 				.post('/api/v1/report')