Skip to content

Commit

Permalink
move findExistingIdentityId to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
QuarpT committed Nov 15, 2018
1 parent 233da23 commit 7a69aae
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 71 deletions.
@@ -0,0 +1,37 @@
package com.gu.identityBackfill

import com.gu.identity.GetByEmail.IdentityAccount
import com.gu.identity.{GetByEmail, GetByIdentityId}
import com.gu.identity.GetByIdentityId.IdentityUser
import com.gu.identityBackfill.Types.EmailAddress
import com.gu.identityBackfill.salesforce.UpdateSalesforceIdentityId.IdentityId
import com.gu.util.apigateway.ApiGatewayResponse
import com.gu.util.reader.Types.ApiGatewayOp
import com.gu.util.reader.Types.ApiGatewayOp.{ContinueProcessing, ReturnWithResponse}
import com.gu.util.resthttp.Types.{ClientFailableOp, ClientFailure, ClientSuccess, NotFound}

object FindExistingIdentityId {

def apply(
getByEmail: EmailAddress => ClientFailableOp[GetByEmail.IdentityAccount],
getByIdentityId: IdentityId => ClientFailableOp[GetByIdentityId.IdentityUser]
)(emailAddress: EmailAddress): ApiGatewayOp[Option[IdentityId]] = {

def continueIfNoPassword(identityId: IdentityId) = {
getByIdentityId(identityId) match {
case ClientSuccess(IdentityUser(_, false)) => ContinueProcessing(Some(identityId))
case _ => ReturnWithResponse(ApiGatewayResponse.notFound(s"identity email not validated but password is set $identityId"))
}
}

val result = getByEmail(emailAddress) match {
case ClientSuccess(IdentityAccount(identityId, true)) => ContinueProcessing(Some(identityId))
case ClientSuccess(IdentityAccount(identityId, false)) => continueIfNoPassword(identityId)
case NotFound(_) => ContinueProcessing(None)
case other: ClientFailure => ReturnWithResponse(ApiGatewayResponse.internalServerError(other.toString))
}

result.withLogging("FindExistingIdentityId")
}

}
Expand Up @@ -57,7 +57,7 @@ object Handler {
val createGuestAccount = identityClient.wrapWith(JsonHttp.post).wrapWith(CreateGuestAccount.wrapper)
val getByEmail = identityClient.wrapWith(JsonHttp.getWithParams).wrapWith(GetByEmail.wrapper)
val getById = identityClient.wrapWith(JsonHttp.get).wrapWith(GetByIdentityId.wrapper)
val findExistingIdentityId = PreReqCheck.findExistingIdentityId(getByEmail.runRequest, getById.runRequest) _
val findExistingIdentityId = FindExistingIdentityId(getByEmail.runRequest, getById.runRequest) _

val countZuoraAccounts: IdentityId => ClientFailableOp[Int] = CountZuoraAccountsForIdentityId(zuoraQuerier)

Expand Down
@@ -1,8 +1,5 @@
package com.gu.identityBackfill

import com.gu.identity.GetByEmail.IdentityAccount
import com.gu.identity.{GetByEmail, GetByIdentityId}
import com.gu.identity.GetByIdentityId.IdentityUser
import com.gu.identityBackfill.TypeConvert._
import com.gu.identityBackfill.Types._
import com.gu.identityBackfill.salesforce.UpdateSalesforceIdentityId.IdentityId
Expand All @@ -13,7 +10,7 @@ import com.gu.util.apigateway.ApiGatewayResponse
import com.gu.util.reader.Types.ApiGatewayOp._
import com.gu.util.reader.Types._
import com.gu.util.resthttp.LazyClientFailableOp
import com.gu.util.resthttp.Types.{ClientFailableOp, ClientFailure, ClientSuccess, NotFound}
import com.gu.util.resthttp.Types.ClientFailableOp

object PreReqCheck {

Expand All @@ -34,28 +31,6 @@ object PreReqCheck {
} yield PreReqResult(zuoraAccountForEmail.accountId, zuoraAccountForEmail.sfContactId, maybeExistingIdentityId)
}

def findExistingIdentityId(
getByEmail: EmailAddress => ClientFailableOp[GetByEmail.IdentityAccount],
getByIdentityId: IdentityId => ClientFailableOp[GetByIdentityId.IdentityUser]
)(emailAddress: EmailAddress): ApiGatewayOp[Option[IdentityId]] = {

def continueIfNoPassword(identityId: IdentityId) = {
getByIdentityId(identityId) match {
case ClientSuccess(IdentityUser(_, false)) => ContinueProcessing(Some(identityId))
case _ => ReturnWithResponse(ApiGatewayResponse.notFound(s"identity email not validated but password is set $identityId"))
}
}

val result = getByEmail(emailAddress) match {
case ClientSuccess(IdentityAccount(identityId, true)) => ContinueProcessing(Some(identityId))
case ClientSuccess(IdentityAccount(identityId, false)) => continueIfNoPassword(identityId)
case NotFound(_) => ContinueProcessing(None)
case other: ClientFailure => ReturnWithResponse(ApiGatewayResponse.internalServerError(other.toString))
}

result.withLogging("GetByEmail")
}

def noZuoraAccountsForIdentityId(
countZuoraAccountsForIdentityId: ClientFailableOp[Int]
): ApiGatewayOp[Unit] = {
Expand Down
@@ -0,0 +1,54 @@
package com.gu.identityBackfill

import com.gu.identity.GetByEmail.IdentityAccount
import com.gu.identity.GetByIdentityId.IdentityUser
import com.gu.identityBackfill.Types.EmailAddress
import com.gu.identityBackfill.salesforce.UpdateSalesforceIdentityId.IdentityId
import com.gu.util.apigateway.ApiGatewayResponse
import com.gu.util.reader.Types.ApiGatewayOp.{ContinueProcessing, ReturnWithResponse}
import com.gu.util.resthttp.Types.{ClientSuccess, GenericError, NotFound}
import org.scalatest.{FlatSpec, Matchers}

class FindExistingIdentityIdTest extends FlatSpec with Matchers {
"findExistingIdentityId" should "continue processing with identity id for existing validated account" in {
FindExistingIdentityId(
_ => ClientSuccess(IdentityAccount(IdentityId("100"), isUserEmailValidated = true)),
_ => fail("Should not be called")
)(EmailAddress("email@email.email")) should be(ContinueProcessing(Some(IdentityId("100"))))
}

"findExistingIdentityId" should "continue processing with identity id for existing unvalidated account with no password" in {
FindExistingIdentityId(
_ => ClientSuccess(IdentityAccount(IdentityId("100"), isUserEmailValidated = false)),
_ => ClientSuccess(IdentityUser(IdentityId("100"), hasPassword = false))
)(EmailAddress("email@email.email")) should be(ContinueProcessing(Some(IdentityId("100"))))
}

"findExistingIdentityId" should "continue processing for not found identity user" in {
FindExistingIdentityId(
_ => NotFound("not found"),
_ => fail("should not be called")
)(EmailAddress("email@email.email")) should be(ContinueProcessing(None))
}

"findExistingIdentityId" should "ReturnWithResponse for unvalidated account with password" in {
FindExistingIdentityId(
_ => ClientSuccess(IdentityAccount(IdentityId("100"), isUserEmailValidated = false)),
_ => ClientSuccess(IdentityUser(IdentityId("100"), hasPassword = true))
)(EmailAddress("email@email.email")) should be(ReturnWithResponse(ApiGatewayResponse.notFound(s"identity email not validated but password is set IdentityId(100)")))
}

"findExistingIdentityId" should "ReturnWithResponse for unexpected identity response" in {
FindExistingIdentityId(
_ => GenericError("error"),
_ => fail("should not be called")
)(EmailAddress("email@email.email")) should be(ReturnWithResponse(ApiGatewayResponse.internalServerError("error")))
}

"findExistingIdentityId" should "ReturnWithResponse for unexpected identity response for get by id" in {
FindExistingIdentityId(
_ => ClientSuccess(IdentityAccount(IdentityId("100"), isUserEmailValidated = false)),
_ => GenericError("error"),
)(EmailAddress("email@email.email")) should be(ReturnWithResponse(ApiGatewayResponse.notFound(s"identity email not validated but password is set IdentityId(100)")))
}
}
Expand Up @@ -2,7 +2,6 @@ package com.gu.identityBackfill

import com.gu.identity.GetByEmail
import com.gu.identity.GetByEmail.IdentityAccount
import com.gu.identity.GetByIdentityId.IdentityUser
import com.gu.identityBackfill.PreReqCheck.PreReqResult
import com.gu.identityBackfill.Types._
import com.gu.identityBackfill.salesforce.UpdateSalesforceIdentityId.IdentityId
Expand All @@ -11,7 +10,7 @@ import com.gu.salesforce.TypesForSFEffectsData.SFContactId
import com.gu.util.apigateway.ApiGatewayResponse
import com.gu.util.reader.Types.ApiGatewayOp.{ContinueProcessing, ReturnWithResponse}
import com.gu.util.resthttp.LazyClientFailableOp
import com.gu.util.resthttp.Types.{ClientFailableOp, ClientFailure, ClientSuccess, GenericError, NotFound}
import com.gu.util.resthttp.Types.{ClientFailableOp, ClientSuccess}
import org.scalatest.{FlatSpec, Matchers}

class PreReqCheckTest extends FlatSpec with Matchers {
Expand Down Expand Up @@ -130,46 +129,4 @@ class PreReqCheckTest extends FlatSpec with Matchers {
val readerTypes = List(ReaderType.ReaderTypeValue("Direct"), ReaderType.ReaderTypeValue("Gift"))
PreReqCheck.acceptableReaderType(ClientSuccess(readerTypes)).toDisjunction.leftMap(_.statusCode) should be(scalaz.-\/("404"))
}

"findExistingIdentityId" should "continue processing with identity id for existing validated account" in {
PreReqCheck.findExistingIdentityId(
_ => ClientSuccess(IdentityAccount(IdentityId("100"), isUserEmailValidated = true)),
_ => fail("Should not be called")
)(EmailAddress("email@email.email")) should be(ContinueProcessing(Some(IdentityId("100"))))
}

"findExistingIdentityId" should "continue processing with identity id for existing unvalidated account with no password" in {
PreReqCheck.findExistingIdentityId(
_ => ClientSuccess(IdentityAccount(IdentityId("100"), isUserEmailValidated = false)),
_ => ClientSuccess(IdentityUser(IdentityId("100"), hasPassword = false))
)(EmailAddress("email@email.email")) should be(ContinueProcessing(Some(IdentityId("100"))))
}

"findExistingIdentityId" should "continue processing for not found identity user" in {
PreReqCheck.findExistingIdentityId(
_ => NotFound("not found"),
_ => fail("should not be called")
)(EmailAddress("email@email.email")) should be(ContinueProcessing(None))
}

"findExistingIdentityId" should "ReturnWithResponse for unvalidated account with password" in {
PreReqCheck.findExistingIdentityId(
_ => ClientSuccess(IdentityAccount(IdentityId("100"), isUserEmailValidated = false)),
_ => ClientSuccess(IdentityUser(IdentityId("100"), hasPassword = true))
)(EmailAddress("email@email.email")) should be(ReturnWithResponse(ApiGatewayResponse.notFound(s"identity email not validated but password is set IdentityId(100)")))
}

"findExistingIdentityId" should "ReturnWithResponse for unexpected identity response" in {
PreReqCheck.findExistingIdentityId(
_ => GenericError("error"),
_ => fail("should not be called")
)(EmailAddress("email@email.email")) should be(ReturnWithResponse(ApiGatewayResponse.internalServerError("error")))
}

"findExistingIdentityId" should "ReturnWithResponse for unexpected identity response for get by id" in {
PreReqCheck.findExistingIdentityId(
_ => ClientSuccess(IdentityAccount(IdentityId("100"), isUserEmailValidated = false)),
_ => GenericError("error"),
)(EmailAddress("email@email.email")) should be(ReturnWithResponse(ApiGatewayResponse.notFound(s"identity email not validated but password is set IdentityId(100)")))
}
}

0 comments on commit 7a69aae

Please sign in to comment.