From 8af1a7b17a4cbc1b7bf136690514ec0a04a4ee87 Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Mon, 1 Aug 2016 15:16:03 +0100 Subject: [PATCH] Test login workflow --- .../coffee/ProjectInviteTests.coffee | 74 +++++++++++++++++-- 1 file changed, 68 insertions(+), 6 deletions(-) diff --git a/services/web/test/acceptance/coffee/ProjectInviteTests.coffee b/services/web/test/acceptance/coffee/ProjectInviteTests.coffee index b3bcb762e9..5592eb92e6 100644 --- a/services/web/test/acceptance/coffee/ProjectInviteTests.coffee +++ b/services/web/test/acceptance/coffee/ProjectInviteTests.coffee @@ -71,6 +71,22 @@ tryRegisterUser = (user, email, redir, callback=(err, response, body)->) -> 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)->) -> @@ -115,6 +131,21 @@ expectInviteRedirectToRegister = (user, link, callback=(err,result)->) -> 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("Login - .*") + 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) -> expect(err).to.be.oneOf [null, undefined] @@ -320,10 +351,41 @@ describe "ProjectInviteTests", -> 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 + (cb) => expectRegistrationRedirectToInvite @user, "some_email@example.com", @_redir, badLink, cb + (cb) => expectInvalidInvitePage @user, badLink, cb + (cb) => expectNoProjectAccess @user, @invite.projectId, cb + ], done + + describe 'login workflow with valid token', -> + + before (done)-> + @user.logout done + + it 'should redirect to the register page', (done) -> + Async.series [ + (cb) => expectInviteRedirectToRegister(@user, @link, cb) + ], done + + it 'should allow the user to login to view the invite', (done) -> + Async.series [ + (cb) => + expectInviteRedirectToRegister @user, @link, (err, redirectUrl, loginUrl) => + @_redir = redirectUrl + @_loginLink = loginUrl + cb() + (cb) => + expectLoginPage @user, @_loginLink, (err, redirectUrl) => + expect(@_redir).to.equal redirectUrl + cb() + (cb) => expectLoginRedirectToInvite @user, @_redir, @link, cb + (cb) => expectInvitePage @user, @link, cb + (cb) => expectNoProjectAccess @user, @invite.projectId, cb + ], done + + + it 'should allow user to accept the invite if the user registers a new account', (done) -> + Async.series [ + (cb) => expectInvitePage @user, @link, cb + (cb) => expectAcceptInviteAndRedirect @user, @invite, cb + (cb) => expectProjectAccess @user, @invite.projectId, cb ], done