Browse Source

Add and implement tests for categories with spaces

sbkwgh 8 years ago
parent
commit
514ba58577
4 changed files with 72 additions and 11 deletions
  1. 2 2
      routes/category.js
  2. 1 1
      routes/thread.js
  3. 40 2
      test/category.js
  4. 29 6
      test/thread_post.js

+ 2 - 2
routes/category.js

@@ -66,12 +66,12 @@ router.get('/:category', async (req, res) => {
 			})
 
 			threadsLatestPost = await Category.findOne({
-				where: { name: req.params.category },
+				where: { value: req.params.category },
 				include: threadInclude('DESC')
 			})
 		}
 
-		if(!threads) throw Errors.invalidParameter('id', 'thread does not exist')
+		if(!threads) throw Errors.invalidParameter('id', 'category does not exist')
 		
 		if(Array.isArray(threads)) {
 			resThreads = {

+ 1 - 1
routes/thread.js

@@ -111,7 +111,7 @@ router.post('/', async (req, res) => {
 		if(validationErrors.length) throw Errors.VALIDATION_ERROR
 
 		let category = await Category.findOne({ where: {
-			name: req.body.category
+			value: req.body.category
 		}})
 
 		if(!category) throw Errors.invalidCategory

+ 40 - 2
test/category.js

@@ -138,6 +138,11 @@ describe('Category', () => {
 				.post('/api/v1/category')
 				.set('content-type', 'application/json')
 				.send({ name: 'another_category' })
+
+			await agent
+				.post('/api/v1/category')
+				.set('content-type', 'application/json')
+				.send({ name: 'category with spaces' })
 		})
 
 		it('should return all categories', async () => {
@@ -148,6 +153,7 @@ describe('Category', () => {
 			res.should.have.status(200)
 			res.body.should.contain.an.item.with.property('name', 'category')
 			res.body.should.contain.an.item.with.property('name', 'another_category')
+			res.body.should.contain.an.item.with.property('name', 'category with spaces')
 		})
 	})
 
@@ -244,6 +250,38 @@ describe('Category', () => {
 			res.body.Threads.should.contain.an.item.with.deep.property('Posts.0.User.username', 'adminaccount')
 
 		})
+
+		it('should return all threads in a category with spaces', async () => {
+			let agent = chai.request.agent(server)
+
+			await agent
+				.post('/api/v1/user/adminaccount/login')
+				.set('content-type', 'application/json')
+				.send({ password: 'password' })
+
+			let thread = await agent
+					.post('/api/v1/thread')
+					.set('content-type', 'application/json')
+					.send({ name: 'thread', category: 'CATEGORY_WITH_SPACES' })
+
+				await agent
+					.post('/api/v1/post')
+					.set('content-type', 'application/json')
+					.send({ content: 'content here', threadId: thread.body.id })
+
+			let res = await chai.request(server)
+				.get('/api/v1/category/CATEGORY_WITH_SPACES')
+
+			res.should.be.json
+			res.should.have.status(200)
+			res.body.should.have.property('name', 'category with spaces')
+			res.body.Threads.should.have.property('length', 1)
+			res.body.Threads.should.contain.an.item.with.deep.property('User.username', 'adminaccount')
+			res.body.Threads[0].Posts[0].should.have.property('content', '<p>content here</p>\n')
+			res.body.Threads.should.contain.an.item.with.deep.property('Posts.0.User.username', 'adminaccount')
+		})
+
+
 		it('should return an error if category does not exist', async () => {
 			try {
 				let res = await chai.request(server)
@@ -251,11 +289,11 @@ describe('Category', () => {
 
 				res.should.be.json
 				res.should.have.status(400)
-				res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
+				res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'category does not exist'))
 			} catch (res) {
 				let body = JSON.parse(res.response.text)
 				res.should.have.status(400)
-				body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
+				body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'category does not exist'))
 			}
 		})
 	})

+ 29 - 6
test/thread_post.js

@@ -41,6 +41,12 @@ describe('Thread and post', () => {
 						.post('/api/v1/category')
 						.set('content-type', 'application/json')
 						.send({ name: 'category_name' })
+						.then(() => { 
+							return userAgent
+								.post('/api/v1/category')
+								.set('content-type', 'application/json')
+								.send({ name: 'category with spaces' })
+						})
 						.then(() => { done() })
 						.catch(done)
 				})
@@ -62,7 +68,7 @@ describe('Thread and post', () => {
 				.set('content-type', 'application/json')
 				.send({
 					name: 'thread',
-					category: 'category_name'
+					category: 'CATEGORY_NAME'
 				})
 
 			res.should.have.status(200)
@@ -73,13 +79,30 @@ describe('Thread and post', () => {
 			res.body.should.have.deep.property('User.username', 'username')
 			res.body.should.have.deep.property('Category.name', 'category_name')
 		})
+		it('should create a thread for a category with spaces in', async () => {
+			let res = await userAgent
+				.post('/api/v1/thread')
+				.set('content-type', 'application/json')
+				.send({
+					name: 'thread123',
+					category: 'CATEGORY_WITH_SPACES'
+				})
+
+			res.should.have.status(200)
+			res.should.be.json
+			res.body.should.have.property('name', 'thread123')
+			res.body.should.have.property('postsCount', 0)
+			res.body.should.have.property('slug', 'thread123')
+			res.body.should.have.deep.property('User.username', 'username')
+			res.body.should.have.deep.property('Category.name', 'category with spaces')
+		})
 		it('should add a slug from the thread name', async () => {
 			let res = await userAgent
 				.post('/api/v1/thread')
 				.set('content-type', 'application/json')
 				.send({
 					name: ' à long thrËad, with lØts of àccents!!!	',
-					category: 'category_name'
+					category: 'CATEGORY_NAME'
 				})
 
 			res.should.have.status(200)
@@ -96,7 +119,7 @@ describe('Thread and post', () => {
 					.set('content-type', 'application/json')
 					.send({
 						name: 'thread',
-						category: 'category_name'
+						category: 'CATEGORY_NAME'
 					})
 
 				res.should.be.json
@@ -323,7 +346,7 @@ describe('Thread and post', () => {
 					.set('content-type', 'application/json')
 					.send({
 						name: 'another thread',
-						category: 'category_name'
+						category: 'CATEGORY_NAME'
 					})).body.id
 
 				let res = await replyAgent
@@ -369,12 +392,12 @@ describe('Thread and post', () => {
 			let thread = await userAgent
 				.post('/api/v1/thread')
 				.set('content-type', 'application/json')
-				.send({ category: 'category_name', name: 'pagination' })
+				.send({ category: 'CATEGORY_NAME', name: 'pagination' })
 
 			let threadOther = await userAgent
 				.post('/api/v1/thread')
 				.set('content-type', 'application/json')
-				.send({ category: 'category_name', name: 'pagination_other' })
+				.send({ category: 'CATEGORY_NAME', name: 'pagination_other' })
 
 			PAGINATION_THREAD_ID = thread.body.id