Browse Source

If slug would otherwise be blank use _ character, add corresponding test

sbkwgh 7 years ago
parent
commit
313d64ec34
2 changed files with 17 additions and 2 deletions
  1. 1 1
      models/thread.js
  2. 16 1
      test/thread_post.js

+ 1 - 1
models/thread.js

@@ -6,7 +6,7 @@ module.exports = (sequelize, DataTypes) => {
 			type: DataTypes.TEXT,
 			set (val) {
 				this.setDataValue('name', val)
-				if(val) this.setDataValue('slug', slug(val).toLowerCase())
+				if(val) this.setDataValue('slug', slug(val).toLowerCase() || '_')
 			},
 			allowNull: false,
 			validate: {

+ 16 - 1
test/thread_post.js

@@ -5,7 +5,7 @@ let server = require('../server')
 let should = chai.should()
 let expect = chai.expect
 
-let { sequelize } = require('../models')
+let { sequelize, Thread } = require('../models')
 
 const Errors = require('../lib/errors.js')
 let PAGINATION_THREAD_ID
@@ -96,6 +96,21 @@ describe('Thread and post', () => {
 			res.body.should.have.deep.property('User.username', 'username')
 			res.body.should.have.deep.property('Category.name', 'category with spaces')
 		})
+		it('should give the slug _ if otherwise empty', async () => {
+			let res = await userAgent
+				.post('/api/v1/thread')
+				.set('content-type', 'application/json')
+				.send({
+					name: ',,,,,,,,,,,,,,,,,',
+					category: 'CATEGORY_WITH_SPACES'
+				})
+
+			res.should.have.status(200)
+			res.should.be.json
+			res.body.should.have.property('slug', '_')
+
+			await Thread.destroy({ where: { name: '_' } })
+		})
 		it('should add a slug from the thread name', async () => {
 			let res = await userAgent
 				.post('/api/v1/thread')