 User = require "./helpers/User"
 request = require "./helpers/request"
 settings = require "settings-sharelatex"
+{db, ObjectId} = require("../../../app/js/infrastructure/mongojs")
 
 try_read_access = (user, project_id, test, callback) ->
 	async.series [

		it 'should deny access before the token is used', (done) ->
			try_read_access(@other1, @project_id, (response, body) =>
				expect(response.statusCode).to.equal 302
				expect(body).to.match /.*\/restricted.*/
			, done)


					expect(body.privilegeLevel).to.equal false
				, done)


	describe 'private overleaf project', ->
		before (done) ->
			@owner.createProject 'overleaf-import', (err, project_id) =>
				@project_id = project_id
				@owner.makeTokenBased @project_id, (err) =>
					@owner.getProject @project_id, (err, project) =>
						@tokens = project.tokens
						@owner.makePrivate @project_id, () =>
							db.projects.update {_id: project._id}, {
								$set: {
									overleaf: {id: 1234}
								}
							}, (err) =>
								done()

		it 'should redirect to canonical path, when owner uses read-write token', (done) ->
			try_read_and_write_token_access(@owner, @tokens.readAndWrite, (response, body) =>
				expect(response.statusCode).to.equal 302
				expect(response.headers.location).to.equal "/project/#{@project_id}"
			, done)

		it 'should allow the owner access to the project', (done) ->
			try_read_access(@owner, @project_id, (response, body) =>
				expect(response.statusCode).to.equal 200
			, done)

		it 'should allow owner to join the project', (done) ->
			try_content_access(@owner, @project_id, (response, body) =>
				expect(body.privilegeLevel).to.equal 'owner'
			, done)

		it 'should not allow other user to join the project', (done) ->
			try_content_access(@other2, @project_id, (response, body) =>
				expect(body.privilegeLevel).to.equal false
			, done)
