Browse Source

Add tests for permissions

sbkwgh 8 years ago
parent
commit
2d25e30164
1 changed files with 107 additions and 0 deletions
  1. 107 0
      test/user.js

+ 107 - 0
test/user.js

@@ -520,6 +520,111 @@ describe('User', () => {
 		})
 	})
 
+	describe('/:username/permissions PUT', () => {
+		let admin = chai.request.agent(server)
+		let user = chai.request.agent(server)
+
+		before(async () => {
+			await admin.post('/api/v1/user/adminaccount/login')
+				.set('content-type', 'application/json')
+				.send({
+					password: 'password'
+				})
+
+			await admin.post('/api/v1/category')
+				.set('content-type', 'application/json')
+				.send({
+					name: 'category'
+				})
+
+			await admin.post('/api/v1/thread')
+				.set('content-type', 'application/json')
+				.send({
+					category: 'category',
+					name: 'thread'
+				})
+			
+			await user.post('/api/v1/user')
+				.set('content-type', 'application/json')
+				.send({
+					username: 'user123',
+					password: 'password'
+				})
+		})
+
+		it('should update the permissions for the user', async () => {
+			let res = await admin
+				.put('/api/v1/user/user123/permissions')
+				.set('content-type', 'application/json')
+				.send({
+					canCreatePosts: false
+				})
+
+			res.should.be.json
+			res.should.have.status(200)
+
+			let user = await User.findOne({
+				where: { username: 'user123' }
+			})
+			user.should.have.property('canCreatePosts', false)
+		})
+		it('should return an error if not an administrator', done => {
+			user
+				.put('/api/v1/user/user123/permissions')
+				.set('content-type', 'application/json')
+				.send({
+					canCreatePosts: true
+				})
+				.end((err, res) => {
+					res.should.be.json
+					res.should.have.status(400)
+					res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
+
+					done()
+				})
+		})
+		it('should return an error if trying to post replies if permissions so set', done => {
+			user
+				.post('/api/v1/post')
+				.set('content-type', 'application/json')
+				.send({
+					threadId: 1,
+					content: 'post'
+				})
+				.end((err, res) => {
+					res.should.be.json
+					res.should.have.status(400)
+					res.body.errors.should.contain.something.with.property('message', 'You have been banned from posting')
+
+					done()
+				})
+		})
+		it('should return an error if trying to create thread if permissions so set', done => {
+			user
+				.put('/api/v1/user/user123/permissions')
+				.set('content-type', 'application/json')
+				.send({
+					canCreateThreads: false	
+				})
+				.end((err, res) => {
+					user
+						.post('/api/v1/thread')
+						.set('content-type', 'application/json')
+						.send({
+							category: 'category',
+							name: 'thread name'
+						})
+						.end((err, res) => {
+							res.should.be.json
+							res.should.have.status(400)
+							res.body.errors.should.contain.something.with.property('message', 'You have been banned from creating threads')
+
+							done()
+						})
+				})
+		})
+	})
+
 	describe('/:username PUT user', () => {
 		let agent = chai.request.agent(server)
 
@@ -793,4 +898,6 @@ describe('User', () => {
 				})
 		})
 	})
+
+	
 })