Просмотр исходного кода

Add acceptance test for unauthorized project joining

James Allen 12 лет назад
Родитель
Сommit
dc60f2b736

+ 3 - 1
services/real-time/app/coffee/WebsocketController.coffee

@@ -16,7 +16,9 @@ module.exports = WebsocketController =
 			if !privilegeLevel or privilegeLevel == ""
 				err = new Error("not authorized")
 				logger.error {err, project_id, user_id}, "user is not authorized to join project"
-				return callback(err)
+				# Don't send an error object since socket.io can apparently
+				# only serialize JSON.
+				return callback({message: err.message})
 
 			client.set("user_id", user_id)
 			client.set("project_id", project_id)

+ 58 - 34
services/real-time/test/acceptance/coffee/JoinProjectTests.coffee

@@ -6,39 +6,63 @@ RealTimeClient = require "./helpers/RealTimeClient"
 MockWebClient = require "./helpers/MockWebClient"
 
 describe "joinProject", ->
-	before (done) ->
-		@user_id    = "mock-user-id"
-		@project_id = "mock-project-id"
-		privileges = {}
-		privileges[@user_id] = "owner"
-		MockWebClient.createMockProject(@project_id, privileges, {
-			name: "Test Project"
-		})
-		MockWebClient.run (error) =>
-			throw error if error?
-			RealTimeClient.setSession {
-				user: { _id: @user_id }
-			}, (error) =>
+	describe "when authorized", ->
+		before (done) ->
+			@user_id    = "mock-user-id"
+			@project_id = "mock-project-id"
+			privileges = {}
+			privileges[@user_id] = "owner"
+			MockWebClient.createMockProject(@project_id, privileges, {
+				name: "Test Project"
+			})
+			MockWebClient.run (error) =>
 				throw error if error?
-				@client = RealTimeClient.connect()
-				@client.emit "joinProject", {
-					project_id: @project_id
-				}, (error, @project, @privilegeLevel, @protocolVersion) =>
+				RealTimeClient.setSession {
+					user: { _id: @user_id }
+				}, (error) =>
 					throw error if error?
-					done()
-				
-	it "should get the project from web", ->
-		MockWebClient.joinProject
-			.calledWith(@project_id, @user_id)
-			.should.equal true
-				
-	it "should return the project", ->
-		@project.should.deep.equal {
-			name: "Test Project"
-		}
-	
-	it "should return the privilege level", ->
-		@privilegeLevel.should.equal "owner"
-	
-	it "should return the protocolVersion", ->
-		@protocolVersion.should.equal 2
+					@client = RealTimeClient.connect()
+					@client.emit "joinProject", {
+						project_id: @project_id
+					}, (error, @project, @privilegeLevel, @protocolVersion) =>
+						throw error if error?
+						done()
+					
+		it "should get the project from web", ->
+			MockWebClient.joinProject
+				.calledWith(@project_id, @user_id)
+				.should.equal true
+					
+		it "should return the project", ->
+			@project.should.deep.equal {
+				name: "Test Project"
+			}
+		
+		it "should return the privilege level", ->
+			@privilegeLevel.should.equal "owner"
+		
+		it "should return the protocolVersion", ->
+			@protocolVersion.should.equal 2
+			
+	describe "when not authorized", ->
+		before (done) ->
+			@user_id    = "mock-user-id-2"
+			@project_id = "mock-project-id-2"
+			privileges = {}
+			MockWebClient.createMockProject(@project_id, privileges, {
+				name: "Test Project"
+			})
+			MockWebClient.run (error) =>
+				throw error if error?
+				RealTimeClient.setSession {
+					user: { _id: @user_id }
+				}, (error) =>
+					throw error if error?
+					@client = RealTimeClient.connect()
+					@client.emit "joinProject", {
+						project_id: @project_id
+					}, (@error, @project, @privilegeLevel, @protocolVersion) =>
+						done()
+		
+		it "should return an error", ->
+			@error.message.should.equal "not authorized"

+ 1 - 1
services/real-time/test/unit/coffee/WebsocketControllerTests.coffee

@@ -85,5 +85,5 @@ describe 'WebsocketController', ->
 
 			it "should return an error", ->
 				@callback
-					.calledWith(new Error("not authorized"))
+					.calledWith({message: "not authorized"})
 					.should.equal true