 				redir: redir
 		}, callback
 
+tryFollowLoginLink = (user, loginLink, callback=(err, response, body)->) ->
+	user.getCsrfToken (error) =>
+		return callback(error) if error?
+		user.request.get loginLink, callback
+
+tryLoginUser = (user, redir, callback=(err, response, body)->) ->
+	user.getCsrfToken (error) =>
+		return callback(error) if error?
+		user.request.post {
+			url: "/login"
+			json:
+				email: user.email
+				password: user.password
+				redir: redir
+		}, callback
+
 
 # Expectations
 expectProjectAccess = (user, projectId, callback=(err,result)->) ->

							expectInviteRedirectToRegister @user, badLink, (err, redirectUrl) =>
								@_redir = redirectUrl
								cb()
						(cb) =>
							expectRegistrationRedirectToInvite @user, "some_email@example.com", @_redir, badLink, cb
						(cb) =>
							expectInvalidInvitePage @user, badLink, cb
						(cb) =>
							expectNoProjectAccess @user, @invite.projectId, cb
					], done

			expect(redirectUrl).to.not.be.oneOf [null, undefined]
			expect(loginUrl).to.not.be.oneOf [null, undefined]
			callback(null, redirectUrl, loginUrl)

expectLoginPage = (user, loginLink, callback=(err, result)->) ->
	tryFollowLoginLink user, loginLink, (err, response, body) ->
		expect(err).to.be.oneOf [null, undefined]
		expect(response.statusCode).to.equal 200
		expect(body).to.match new RegExp("<title>Login - .*</title>")
		redirectUrl = body.match(/input name="redir" type="hidden" value="([^"]*)"/m)?[1]
		callback(null, redirectUrl)

expectLoginRedirectToInvite = (user, redir, link, callback=(err, result)->) ->
	tryLoginUser user, redir, (err, response, body) ->
		expect(err).to.be.oneOf [null, undefined]
		expect(response.statusCode).to.equal 200
		expect(link).to.match new RegExp("^.*#{body.redir}\?.*$")
		callback(null, null)

expectRegistrationRedirectToInvite = (user, email, redir, link, callback=(err, result)->) ->
	tryRegisterUser user, email, redir, (err, response, body) ->
