-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
405 additions
and
4 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
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,60 @@ | ||
/* | ||
* Copyright 2021 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 controllers | ||
|
||
import config.AppConfig | ||
import connectors.SecureMessageConnector | ||
import controllers.generic.models.{ CustomerEnrolment, Tag } | ||
import controllers.utils.QueryStringValidation | ||
import play.api.i18n.I18nSupport | ||
import play.api.mvc.{ MessagesControllerComponents, _ } | ||
import uk.gov.hmrc.auth.core.{ AuthConnector, AuthorisedFunctions } | ||
import uk.gov.hmrc.http.HeaderCarrier | ||
import uk.gov.hmrc.play.bootstrap.frontend.controller.FrontendController | ||
import uk.gov.hmrc.play.http.HeaderCarrierConverter | ||
import javax.inject.{ Inject, Singleton } | ||
import play.api.libs.json.Json | ||
|
||
import scala.concurrent.{ ExecutionContext, Future } | ||
|
||
@Singleton | ||
class ApiController @Inject()( | ||
appConfig: AppConfig, | ||
controllerComponents: MessagesControllerComponents, | ||
secureMessageConnector: SecureMessageConnector, | ||
val authConnector: AuthConnector)(implicit ec: ExecutionContext) | ||
extends FrontendController(controllerComponents) with I18nSupport with AuthorisedFunctions | ||
with QueryStringValidation { | ||
|
||
implicit val config: AppConfig = appConfig | ||
|
||
def count( | ||
enrolmentKeys: Option[List[String]], | ||
customerEnrolments: Option[List[CustomerEnrolment]], | ||
tags: Option[List[Tag]]): Action[AnyContent] = Action.async { implicit request => | ||
implicit val hc: HeaderCarrier = HeaderCarrierConverter.fromRequest(request) | ||
validateQueryParameters(request.queryString, "enrolment", "enrolmentKey", "tag", "sent") match { | ||
case Left(e) => Future.successful(BadRequest(e.getMessage)) | ||
case _ => | ||
authorised() { | ||
secureMessageConnector.getCount(enrolmentKeys, customerEnrolments, tags).flatMap { messageCount => | ||
Future.successful(Ok(Json.toJson(messageCount))) | ||
} | ||
} | ||
} | ||
} | ||
} |
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,26 @@ | ||
/* | ||
* Copyright 2021 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 models | ||
|
||
import play.api.libs.json.{ Json, OFormat } | ||
|
||
final case class Count(total: Long, unread: Long) | ||
|
||
object Count { | ||
|
||
implicit val countFormat: OFormat[Count] = Json.format[Count] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* Copyright 2021 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. | ||
*/ | ||
|
||
import com.google.inject.AbstractModule | ||
import connectors.SecureMessageConnector | ||
import controllers.generic.models.{ CustomerEnrolment, Tag } | ||
import models.Count | ||
import net.codingwell.scalaguice.ScalaModule | ||
import org.mockito.ArgumentMatchers | ||
import org.mockito.ArgumentMatchers.any | ||
import org.mockito.Mockito.when | ||
import org.scalatest.BeforeAndAfterEach | ||
import org.scalatestplus.mockito.MockitoSugar | ||
import org.scalatestplus.play.PlaySpec | ||
import play.api.http.Status.{ BAD_REQUEST, OK } | ||
import play.api.inject.guice.GuiceableModule | ||
import play.api.libs.json.{ Json, Reads } | ||
import play.api.libs.ws.WSClient | ||
import uk.gov.hmrc.http.HeaderCarrier | ||
import uk.gov.hmrc.integration.ServiceSpec | ||
|
||
import scala.concurrent.{ ExecutionContext, Future } | ||
|
||
@SuppressWarnings(Array("org.wartremover.warts.NonUnitStatements")) | ||
class ApiEndpointsISpec extends PlaySpec with ServiceSpec with MockitoSugar with BeforeAndAfterEach { | ||
|
||
override def externalServices: Seq[String] = Seq.empty | ||
|
||
private val mockSecureMessageConnector = mock[SecureMessageConnector] | ||
|
||
private val wsClient = app.injector.instanceOf[WSClient] | ||
|
||
override def additionalOverrides: Seq[GuiceableModule] = | ||
Seq(new AbstractModule with ScalaModule { | ||
override def configure(): Unit = | ||
bind[SecureMessageConnector].toInstance(mockSecureMessageConnector) | ||
}) | ||
|
||
"Getting the message count result for total and unread messages" should { | ||
|
||
"return status code OK 200" in { | ||
val totalMessagesCount: Long = 5 | ||
val unreadMessagesCount: Long = 2 | ||
when( | ||
mockSecureMessageConnector.getCount( | ||
ArgumentMatchers.eq(Some(List("HMRC-CUS-ORG"))), | ||
ArgumentMatchers.eq(Some(List(CustomerEnrolment("HMRC-CUS-ORG", "EORIName", "GB7777777777")))), | ||
ArgumentMatchers.eq(Some(List(Tag("notificationType", "CDS Exports")))) | ||
)(any[ExecutionContext], any[HeaderCarrier])).thenReturn( | ||
Future.successful(Count(total = totalMessagesCount, unread = unreadMessagesCount)) | ||
) | ||
val response = wsClient | ||
.url(resource("/secure-message-frontend/messages/count?" + | ||
"enrolmentKey=HMRC-CUS-ORG&enrolment=HMRC-CUS-ORG~EORIName~GB7777777777&tag=notificationType~CDS%20Exports")) | ||
.withHttpHeaders(AuthUtil.buildEoriToken) | ||
.get() | ||
.futureValue | ||
response.status mustBe OK | ||
response.body mustBe """{"total":5,"unread":2}""" | ||
} | ||
|
||
"return status code BAD REQUEST 400 when provided with filter parameters that are invalid (not allowed)" in { | ||
val totalMessagesCount: Long = 0 | ||
val unreadMessagesCount: Long = 0 | ||
when( | ||
mockSecureMessageConnector.getCount( | ||
ArgumentMatchers.eq(None), | ||
ArgumentMatchers.eq(None), | ||
ArgumentMatchers.eq(None) | ||
)(any[ExecutionContext], any[HeaderCarrier])) | ||
.thenReturn(Future.successful(Count(total = totalMessagesCount, unread = unreadMessagesCount))) | ||
val response = wsClient | ||
.url(resource("/secure-message-frontend/messages/count?" + | ||
"enrolment_key=HMRC-CUS-ORG&enrolement=HMRC-CUS-ORG~EORIName~GB7777777777&tags=notificationType~CDS%20Exports")) | ||
.withHttpHeaders(AuthUtil.buildEoriToken) | ||
.get() | ||
.futureValue | ||
response.status mustBe BAD_REQUEST | ||
response.body mustBe "Invalid query parameter(s) found: [enrolement, enrolment_key, tags]" | ||
} | ||
} | ||
|
||
object AuthUtil { | ||
|
||
lazy val ggAuthPort: Int = 8585 | ||
|
||
implicit val deserialiser: Reads[GatewayToken] = Json.reads[GatewayToken] | ||
|
||
case class GatewayToken(gatewayToken: String) | ||
|
||
private val NO_EORI_USER_PAYLOAD = | ||
""" | ||
| { | ||
| "credId": "1235", | ||
| "affinityGroup": "Organisation", | ||
| "confidenceLevel": 100, | ||
| "credentialStrength": "none", | ||
| "enrolments": [] | ||
| } | ||
""".stripMargin | ||
|
||
private val EORI_USER_PAYLOAD = | ||
""" | ||
| { | ||
| "credId": "1235", | ||
| "affinityGroup": "Organisation", | ||
| "confidenceLevel": 200, | ||
| "credentialStrength": "none", | ||
| "enrolments": [ | ||
| { | ||
| "key": "HMRC-CUS-ORG", | ||
| "identifiers": [ | ||
| { | ||
| "key": "EORINumber", | ||
| "value": "GB1234567890" | ||
| } | ||
| ], | ||
| "state": "Activated" | ||
| } | ||
| ] | ||
| } | ||
""".stripMargin | ||
|
||
private def buildUserToken(payload: String): (String, String) = { | ||
val response = wsClient | ||
.url(s"http://localhost:$ggAuthPort/government-gateway/session/login") | ||
.withHttpHeaders(("Content-Type", "application/json")) | ||
.post(payload) | ||
.futureValue | ||
|
||
("Authorization", response.header("Authorization").getOrElse("")) | ||
} | ||
|
||
def buildEoriToken: (String, String) = buildUserToken(EORI_USER_PAYLOAD) | ||
def buildNonEoriToken: (String, String) = buildUserToken(NO_EORI_USER_PAYLOAD) | ||
} | ||
} |
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
Oops, something went wrong.