Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move findExistingIdentityId to its own file
- Loading branch information
Showing
5 changed files
with
94 additions
and
71 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
...ers/identity-backfill/src/main/scala/com/gu/identityBackfill/FindExistingIdentityId.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...identity-backfill/src/test/scala/com/gu/identityBackfill/FindExistingIdentityIdTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)"))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters