-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JB][OPS-12686] get journey action refiners (#8)
- Loading branch information
1 parent
dc87af8
commit 42844e5
Showing
9 changed files
with
223 additions
and
15 deletions.
There are no files selected for viewing
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,33 @@ | ||
/* | ||
* Copyright 2024 HM Revenue & Customs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package uk.gov.hmrc.cardpaymentfrontend.actions | ||
|
||
import play.api.mvc.{ActionBuilder, AnyContent, DefaultActionBuilder, Request} | ||
|
||
import javax.inject.{Inject, Singleton} | ||
|
||
@Singleton | ||
class Actions @Inject() ( | ||
actionBuilder: DefaultActionBuilder, | ||
getJourneyActionRefiner: GetJourneyActionRefiner | ||
) { | ||
|
||
val default: ActionBuilder[Request, AnyContent] = actionBuilder | ||
|
||
val journeyAction: ActionBuilder[JourneyRequest, AnyContent] = default.andThen[JourneyRequest](getJourneyActionRefiner) | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
app/uk/gov/hmrc/cardpaymentfrontend/actions/GetJourneyActionRefiner.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,44 @@ | ||
/* | ||
* Copyright 2024 HM Revenue & Customs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package uk.gov.hmrc.cardpaymentfrontend.actions | ||
|
||
import play.api.mvc.{ActionRefiner, Request, Result, Results} | ||
import uk.gov.hmrc.cardpaymentfrontend.connectors.PayApiConnector | ||
import uk.gov.hmrc.cardpaymentfrontend.requests.RequestSupport | ||
|
||
import javax.inject.{Inject, Singleton} | ||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
@Singleton | ||
class GetJourneyActionRefiner @Inject() ( | ||
payApiConnector: PayApiConnector, | ||
requestSupport: RequestSupport | ||
)(implicit ec: ExecutionContext) extends ActionRefiner[Request, JourneyRequest] { | ||
|
||
override protected[actions] def refine[A](request: Request[A]): Future[Either[Result, JourneyRequest[A]]] = { | ||
|
||
implicit val r: Request[A] = request | ||
|
||
payApiConnector.findLatestJourneyBySessionId()(requestSupport.hc) | ||
.map { | ||
case Some(journey) => Right(new JourneyRequest(journey, request)) | ||
case None => Left(Results.Unauthorized("need a session id")) //should probably be a redirect to pay-frontend /pay | ||
} | ||
} | ||
|
||
override protected def executionContext: ExecutionContext = ec | ||
} |
29 changes: 29 additions & 0 deletions
29
app/uk/gov/hmrc/cardpaymentfrontend/actions/JourneyRequest.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,29 @@ | ||
/* | ||
* Copyright 2023 HM Revenue & Customs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package uk.gov.hmrc.cardpaymentfrontend.actions | ||
|
||
import payapi.cardpaymentjourney.model.journey.{Journey, JourneySpecificData} | ||
import payapi.corcommon.model.JourneyId | ||
import play.api.mvc.{Request, WrappedRequest} | ||
|
||
class JourneyRequest[A]( | ||
val journey: Journey[JourneySpecificData], | ||
val request: Request[A] | ||
) extends WrappedRequest[A](request) { | ||
|
||
val journeyId: JourneyId = journey._id | ||
} |
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
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
46 changes: 46 additions & 0 deletions
46
test/uk/gov/hmrc/cardpaymentfrontend/actions/ActionRefinerSpec.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,46 @@ | ||
/* | ||
* Copyright 2024 HM Revenue & Customs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package uk.gov.hmrc.cardpaymentfrontend.actions | ||
|
||
import play.api.mvc.{AnyContent, Result, Results} | ||
import play.api.test.FakeRequest | ||
import uk.gov.hmrc.cardpaymentfrontend.testsupport.ItSpec | ||
import uk.gov.hmrc.cardpaymentfrontend.testsupport.TestOps.FakeRequestOps | ||
import uk.gov.hmrc.cardpaymentfrontend.testsupport.stubs.PayApiStub | ||
import uk.gov.hmrc.cardpaymentfrontend.testsupport.testdata.TestJourneys | ||
|
||
class ActionRefinerSpec extends ItSpec { | ||
|
||
"getJourneyActionRefiner" - { | ||
|
||
val systemUnderTest: GetJourneyActionRefiner = app.injector.instanceOf[GetJourneyActionRefiner] | ||
|
||
val fakeRequest: FakeRequest[AnyContent] = FakeRequest("GET", "/who-cares").withSessionId() | ||
|
||
"should return a left with unauthorised Result when pay-api returns no journey" in { | ||
systemUnderTest.refine(fakeRequest).futureValue shouldBe Left(Results.Unauthorized("need a session id")) | ||
} | ||
|
||
"should return a right with JourneyRequest when pay-api returns a journey" in { | ||
PayApiStub.stubForFindBySessionId2xx(TestJourneys.testPfSaJourneyCreated) | ||
val result: Either[Result, JourneyRequest[AnyContent]] = systemUnderTest.refine(fakeRequest).futureValue | ||
result.isRight shouldBe true | ||
result.map(_.journey) shouldBe Right(TestJourneys.testPfSaJourneyCreated) | ||
} | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -18,28 +18,47 @@ package uk.gov.hmrc.cardpaymentfrontend.controllers | |
|
||
import org.jsoup.Jsoup | ||
import play.api.http.Status | ||
import play.api.mvc.{AnyContentAsEmpty, AnyContentAsFormUrlEncoded, Cookie} | ||
import play.api.mvc.{AnyContentAsEmpty, AnyContentAsFormUrlEncoded} | ||
import play.api.test.FakeRequest | ||
import play.api.test.Helpers._ | ||
import uk.gov.hmrc.cardpaymentfrontend.testsupport.ItSpec | ||
import uk.gov.hmrc.cardpaymentfrontend.testsupport.TestOps._ | ||
import uk.gov.hmrc.cardpaymentfrontend.testsupport.stubs.PayApiStub | ||
import uk.gov.hmrc.cardpaymentfrontend.testsupport.testdata.TestJourneys | ||
|
||
import scala.jdk.CollectionConverters.ListHasAsScala | ||
|
||
class EmailAddressControllerSpec extends ItSpec { | ||
|
||
override def beforeEach(): Unit = { | ||
PayApiStub.stubForFindBySessionId2xx(TestJourneys.testPfSaJourneyCreated) | ||
() | ||
} | ||
|
||
private val systemUnderTest: EmailAddressController = app.injector.instanceOf[EmailAddressController] | ||
|
||
"EmailController" - { | ||
|
||
"GET /email-address" - { | ||
|
||
val fakeGetRequest: FakeRequest[AnyContentAsEmpty.type] = FakeRequest("GET", "/email-address") | ||
val fakeGetRequestInWelsh: FakeRequest[AnyContentAsEmpty.type] = FakeRequest("GET", "/email-address").withCookies(Cookie("PLAY_LANG", "cy")) | ||
val fakeGetRequest: FakeRequest[AnyContentAsEmpty.type] = | ||
FakeRequest("GET", "/email-address").withSessionId() | ||
|
||
val fakeGetRequestInWelsh: FakeRequest[AnyContentAsEmpty.type] = | ||
fakeGetRequest.withLangWelsh() | ||
|
||
"should return 200 OK" in { | ||
val result = systemUnderTest.renderPage(fakeGetRequest) | ||
status(result) shouldBe Status.OK | ||
} | ||
|
||
//TODO: check if this should redirect to /pay as well. | ||
"should return 401 unauthorised when there is no journey returned from pay-api via action refiner" in { | ||
PayApiStub.stubForFindBySessionId404 | ||
val result = systemUnderTest.renderPage(fakeGetRequest) | ||
status(result) shouldBe Status.UNAUTHORIZED | ||
} | ||
|
||
"render the page with the hmrc layout" in { | ||
val result = systemUnderTest.renderPage(fakeGetRequest) | ||
val document = Jsoup.parse(contentAsString(result)) | ||
|
@@ -131,10 +150,13 @@ class EmailAddressControllerSpec extends ItSpec { | |
"POST /email-address" - { | ||
|
||
def fakePostRequest(formData: (String, String)*): FakeRequest[AnyContentAsFormUrlEncoded] = | ||
FakeRequest("POST", "/email-address").withFormUrlEncodedBody(formData: _*) | ||
FakeRequest("POST", "/email-address") | ||
.withSessionId() | ||
.withFormUrlEncodedBody(formData: _*) | ||
|
||
def fakePostRequestInWelsh(formData: (String, String)*): FakeRequest[AnyContentAsFormUrlEncoded] = | ||
FakeRequest("POST", "/email-address").withFormUrlEncodedBody(formData: _*).withCookies(Cookie("PLAY_LANG", "cy")) | ||
fakePostRequest(formData: _*) | ||
.withLangWelsh() | ||
|
||
"should return 200 OK when a valid email address is submitted" in { | ||
val validFormData = ("email-address", "[email protected]") | ||
|
33 changes: 33 additions & 0 deletions
33
test/uk/gov/hmrc/cardpaymentfrontend/testsupport/TestOps.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,33 @@ | ||
/* | ||
* Copyright 2024 HM Revenue & Customs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package uk.gov.hmrc.cardpaymentfrontend.testsupport | ||
|
||
import play.api.mvc.Cookie | ||
import play.api.test.FakeRequest | ||
import uk.gov.hmrc.http.SessionKeys | ||
|
||
object TestOps { | ||
implicit class FakeRequestOps[T](r: FakeRequest[T]) { | ||
def withLang(language: String = "en"): FakeRequest[T] = r.withCookies(Cookie("PLAY_LANG", language)) | ||
|
||
def withLangWelsh(): FakeRequest[T] = r.withLang("cy") | ||
def withLangEnglish(): FakeRequest[T] = r.withLang("en") | ||
|
||
def withSessionId(sessionId: String = "some-valid-session-id"): FakeRequest[T] = | ||
r.withSession(SessionKeys.sessionId -> sessionId) | ||
} | ||
} |