 						rev: @doc.rev
 					})
 					.should.equal true
+
+		describe "moving a doc when another with the same name already exists", ->
+			beforeEach () ->
+				@docId = "4eecaffcbffa66588e000009"
+				@doc = { name: "another-doc.tex", lines:["1234","312343d"], rev: "1234"}
+				@path = {
+					mongo:"folders[0]"
+					fileSystem:"/old_folder/somewhere.txt"
+				}
+				@destFolder = { name: "folder", docs: [ {name:"another-doc.tex"} ] }
+				@destFolderPath = {
+					mongo:      "folders[0]"
+					fileSystem: "/dest_folder"
+				}
+				@projectLocator.findElement = sinon.stub()
+				@projectLocator.findElement.withArgs({project: @project, element_id: @docId, type: 'docs'})
+					.callsArgWith(1, null, @doc, @path)
+				@projectLocator.findElement.withArgs({project: @project, element_id: folder_id, type:"folder"},)
+					.callsArgWith(1, null, @destFolder, @destFolderPath)
+				@callback = sinon.stub()
+				@ProjectEntityHandler.moveEntity project_id, @docId, folder_id, "docs", userId, @callback
+
+			it 'should return an error', ->
+				@callback.calledWith(new Errors.InvalidNameError("file already exists")).should.equal true
+
+			it "should should not send the update to the doc updater", ->
+				@documentUpdaterHandler.updateProjectStructure
+					.called.should.equal false
+
+			it 'should not remove the element from its current position', ->
+				@ProjectEntityHandler._removeElementFromMongoArray
+					.called.should.equal false
+
+			it "should not put the element back in the new folder", ->
+				@ProjectEntityHandler._putElement.called.should.equal false
+
+			it 'should not tell the third party data store', ->
+				@tpdsUpdateSender.moveEntity
+					.called.should.equal false
+
 
 		describe "moving a folder", ->
 			beforeEach ->

			@lines = ['1234','abc']
			@path = "/path/to/doc"

			@ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:@path}})
			@callback = sinon.stub()
			@tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)

			@newName = "new.tex"
			@path = mongo: "mongo.path", fileSystem: "/oldnamepath/oldname"

			@project_id = project_id
			@project =
				_id: ObjectId(project_id)
				rootFolder: [_id:ObjectId()]
			@folder =
				_id: ObjectId()
				name: "someFolder"
				docs: [ {name: "another-doc.tex"} ]
				fileRefs: [ {name: "another-file.tex"} ]
				folders: [ {name: "another-folder"} ]
			@doc =
				_id: ObjectId()
				name: "new.tex"

			@ProjectGetter.getProject.callsArgWith(2, null, @project)
			@ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
			@ProjectEntityHandler.getAllEntitiesFromProject
