Skip to content

Commit

Permalink
APIS-5353 (#307)
Browse files Browse the repository at this point in the history
* Started work on fixing tests after uplift

* Fixing dependencies

* Extracted scoverage settings

* Extracted scoverage settings

* Modernising unit tests

* APIS-5353 - Updating bootstrap

* APIS-5353 - Fixing config

* APIS-5353 - Adding TPDFE to run local deps

* APIS-5353 - Removing unused conf

* APIS-5353 - Fixed warnings

* APIS-5353 - Removed comments
  • Loading branch information
peteslater-ee authored Jun 29, 2021
1 parent 28b53cb commit d2c5302
Show file tree
Hide file tree
Showing 53 changed files with 582 additions and 593 deletions.
9 changes: 8 additions & 1 deletion acceptance/uk/gov/hmrc/apidocumentation/BaseSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ trait BaseSpec extends FeatureSpec with BeforeAndAfterEach with BeforeAndAfterAl

override def fakeApplication(): Application = {
GuiceApplicationBuilder()
.configure("run.mode" -> "Test")
.configure(
"microservice.services.developer-frontend.host" -> stubHost,
"microservice.services.developer-frontend.port" -> stubPort,
"microservice.services.api-platform-microservice.host" -> stubHost,
"microservice.services.api-platform-microservice.port" -> stubPort,
"microservice.services.third-party-developer.host" -> stubHost,
"microservice.services.third-party-developer.port" -> stubPort
)
.in(Mode.Prod)
.build()
}
Expand Down
54 changes: 11 additions & 43 deletions app/uk/gov/hmrc/apidocumentation/config/ApplicationConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,8 @@ import uk.gov.hmrc.play.bootstrap.config.{RunMode, ServicesConfig}

@ImplementedBy(classOf[ApplicationConfigImpl])
trait ApplicationConfig {
def contactFormServiceIdentifier: String
def contactPath: String

def analyticsToken: Option[String]
def analyticsHost: String

def developerFrontendUrl: String

def reportAProblemPartialUrl: String
def reportAProblemNonJSUrl: String

def developerFrontendBaseUrl: String
def thirdPartyDeveloperUrl: String
def apiPlatformMicroserviceBaseUrl: String
Expand All @@ -44,12 +35,9 @@ trait ApplicationConfig {
def showProductionAvailability: Boolean
def showSandboxAvailability: Boolean

def productionApiHost: String
def productionWwwHost: String
def productionApiBaseUrl: String

def sandboxApiHost: String
def sandboxWwwHost: String
def sandboxApiBaseUrl: String
def sandboxWwwBaseUrl: String

Expand All @@ -61,31 +49,17 @@ trait ApplicationConfig {
def subordinateBaseUrl: String

def title: String
def isStubMode: Boolean
def xmlApiBaseUrl: String

def apidocumentationBaseUrl: String
}

@Singleton
class ApplicationConfigImpl @Inject()(config: Configuration, runMode: RunMode)
extends ServicesConfig(config, runMode)
with ApplicationConfig {

val env = runMode.env

def getConfigDefaulted[A](key: String, default: A)(implicit loader: ConfigLoader[A]) = config.getOptional[A](key)(loader).getOrElse(default)

val contactFormServiceIdentifier = "API"
val contactPath = getConfigDefaulted(s"$env.contactPath", "")

val analyticsToken = config.getOptional[String](s"$env.google-analytics.token").filterNot(_ == "")
val analyticsHost = getConfigDefaulted(s"$env.google-analytics.host", "auto")

val developerFrontendUrl = getConfigDefaulted(s"$env.developer-frontend-url", "")

val reportAProblemPartialUrl = s"$contactPath/contact/problem_reports_ajax?service=$contactFormServiceIdentifier"
val reportAProblemNonJSUrl = s"$contactPath/contact/problem_reports_nonjs?service=$contactFormServiceIdentifier"
val developerFrontendUrl = getString("developer-frontend-url")

val developerFrontendBaseUrl = baseUrl("developer-frontend")
val thirdPartyDeveloperUrl = baseUrl("third-party-developer")
Expand All @@ -100,32 +74,26 @@ class ApplicationConfigImpl @Inject()(config: Configuration, runMode: RunMode)
lazy val apiPlatformMicroserviceBaseUrl = baseUrl("api-platform-microservice")
lazy val ramlPreviewMicroserviceBaseUrl = baseUrl("raml-preview-microservice")

val securedCookie = getConfigDefaulted(s"$env.cookie.secure", true)
val ramlPreviewEnabled = getConfigDefaulted(s"$env.features.ramlPreview", false)
val securedCookie = getBoolean("cookie.secure")
val ramlPreviewEnabled = getBoolean("features.ramlPreview")

val showProductionAvailability = getConfigDefaulted(s"$env.features.showProductionAvailability", false)
val showSandboxAvailability = getConfigDefaulted(s"$env.features.showSandboxAvailability", false)
val productionApiHost = getString("platform.production.api.host")
val showProductionAvailability = getBoolean("features.showProductionAvailability")
val showSandboxAvailability = getBoolean("features.showSandboxAvailability")
val productionWwwHost = getString("platform.production.www.host")
val productionApiBaseUrl = platformBaseUrl("platform.production.api")

val sandboxApiHost = getString("platform.sandbox.api.host")
val sandboxWwwHost = getString("platform.sandbox.www.host")
val sandboxApiBaseUrl = platformBaseUrl("platform.sandbox.api")
val sandboxWwwBaseUrl = platformBaseUrl("platform.sandbox.www")

val documentationRenderVersion = getConfigDefaulted(s"$env.features.documentationRenderVersion", "raml")

val nameOfPrincipalEnvironment = getConfigDefaulted(s"$env.features.nameOfPrincipalEnvironment", "Production")
val nameOfSubordinateEnvironment = getConfigDefaulted(s"$env.features.nameOfSubordinateEnvironment", "Sandbox")
val principalBaseUrl = getConfigDefaulted(s"$env.features.principalBaseUrl", "https://api.service.hmrc.gov.uk")
val subordinateBaseUrl = getConfigDefaulted(s"$env.features.subordinateBaseUrl", "https://test-api.service.hmrc.gov.uk")
val documentationRenderVersion = getString("features.documentationRenderVersion")

val apidocumentationBaseUrl = getString("apidocumentation.base.url")
val nameOfPrincipalEnvironment = getString("features.nameOfPrincipalEnvironment")
val nameOfSubordinateEnvironment = getString("features.nameOfSubordinateEnvironment")
val principalBaseUrl = getString("features.principalBaseUrl")
val subordinateBaseUrl = getString("features.subordinateBaseUrl")

val title = "HMRC Developer Hub"
val isStubMode = env == "Stub"
val xmlApiBaseUrl = getConfigDefaulted(s"$env.xml-api.base-url", "https://www.gov.uk")
val xmlApiBaseUrl = getString("xml-api.base-url")

private def platformBaseUrl(key: String) = {
(getConfigDefaulted(s"$key.protocol", ""), getConfigDefaulted(s"$key.host", "")) match {
Expand Down
40 changes: 20 additions & 20 deletions app/uk/gov/hmrc/apidocumentation/connectors/ProxiedHttpClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,44 @@ package uk.gov.hmrc.apidocumentation.connectors
import akka.actor.ActorSystem
import javax.inject.{Inject, Singleton}
import play.api.Configuration
import play.api.http.HeaderNames.ACCEPT
import play.api.libs.ws.{WSClient, WSProxyServer, WSRequest}
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.http.logging.Authorization
import play.api.http.HeaderNames
import play.api.libs.ws.{WSClient, WSProxyServer, WSRequest => PlayWSRequest}
import uk.gov.hmrc.http.Authorization
import uk.gov.hmrc.play.audit.http.HttpAuditing
import uk.gov.hmrc.play.bootstrap.config.RunMode
import uk.gov.hmrc.play.bootstrap.http.DefaultHttpClient
import uk.gov.hmrc.play.http.ws.{WSProxy, WSProxyConfiguration}

@Singleton
class ProxiedHttpClient @Inject()(config: Configuration,
httpAuditing: HttpAuditing,
wsClient: WSClient,
actorSystem: ActorSystem,
runMode: RunMode)
actorSystem: ActorSystem)
extends DefaultHttpClient(config, httpAuditing, wsClient, actorSystem) with WSProxy {

val authorization: Option[Authorization] = None
val apiKeyHeader: Option[(String, String)] = None
private val env = runMode.env
val apiKeyHeader: Option[String] = None

def withHeaders(bearerToken: String, apiKey: String = ""): ProxiedHttpClient = {
new ProxiedHttpClient(config, httpAuditing, wsClient, actorSystem, runMode) {
new ProxiedHttpClient(config, httpAuditing, wsClient, actorSystem) {
override val authorization = Some(Authorization(s"Bearer $bearerToken"))
override val apiKeyHeader: Option[(String, String)] = if ("" == apiKey) None else Some("x-api-key" -> apiKey)
override val apiKeyHeader = if (apiKey.isEmpty) None else Some(apiKey)
}
}

override def wsProxyServer: Option[WSProxyServer] = WSProxyConfiguration(s"$env.proxy", config)
override def wsProxyServer: Option[WSProxyServer] = WSProxyConfiguration("proxy", config)

override def buildRequest[A](url: String, headers: Seq[(String, String)])(implicit hc: HeaderCarrier): WSRequest = {
val extraHeaders = hc.extraHeaders :+ (ACCEPT -> "application/hmrc.vnd.1.0+json")
val extraHeadersWithMaybeApiKeyHeader =
if (apiKeyHeader.isDefined) extraHeaders :+ apiKeyHeader.get
else extraHeaders
override def buildRequest[A](url: String, headers: Seq[(String, String)]): PlayWSRequest = {
val extraHeaders: Seq[(String,String)] = headers ++
authorization.map(v => (HeaderNames.AUTHORIZATION -> v.value)).toSeq ++
apiKeyHeader.map(v => ProxiedHttpClient.API_KEY_HEADER_NAME -> v).toSeq ++
Seq(ProxiedHttpClient.ACCEPT_HMRC_JSON_HEADER)

val hcWithBearerAndAccept = hc.copy(authorization = authorization, extraHeaders = extraHeadersWithMaybeApiKeyHeader)

super.buildRequest(url)(hcWithBearerAndAccept)
super.buildRequest(url, extraHeaders)
}
}

object ProxiedHttpClient {
val API_KEY_HEADER_NAME = "x-api-key"
val ACCEPT_HMRC_JSON_HEADER = HeaderNames.ACCEPT -> "application/hmrc.vnd.1.0+json"
}

Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class ApiDocumentationController @Inject()(
(for {
userId <- extractDeveloperIdentifier(loggedInUserService.fetchLoggedInUser())
api <- apiDefinitionService.fetchExtendedDefinition(service, userId)
cacheBust = bustCache(appConfig.isStubMode, cacheBuster)
cacheBust = bustCache(cacheBuster)
apiDocumentation <- doRenderApiDocumentation(service, version, cacheBust, api, navLinks, userId)
} yield apiDocumentation) recover {
case e: NotFoundException =>
Expand All @@ -143,7 +143,7 @@ class ApiDocumentationController @Inject()(
}
}

def bustCache(stubMode: Boolean, cacheBuster: Option[Boolean]): Boolean = stubMode || cacheBuster.getOrElse(false)
def bustCache(cacheBuster: Option[Boolean]): Boolean = cacheBuster.getOrElse(false)

private def doRenderApiDocumentation(service: String, version: String, cacheBuster: Boolean, apiOption: Option[ExtendedAPIDefinition],
navLinks: Seq[NavLink], developerId: Option[DeveloperIdentifier])(implicit request: Request[AnyContent], messagesProvider: MessagesProvider): Future[Result] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import play.api.mvc.{ControllerComponents, Request, RequestHeader}
import uk.gov.hmrc.apidocumentation.config.ApplicationConfig
import uk.gov.hmrc.apidocumentation.models.{Developer, Session}
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.play.HeaderCarrierConverter
import uk.gov.hmrc.play.http.HeaderCarrierConverter
import uk.gov.hmrc.play.bootstrap.controller.BackendController

import scala.concurrent.{ExecutionContext, Future}
Expand Down Expand Up @@ -91,5 +91,5 @@ trait CookieEncoding {
trait HeaderCarrierConversion extends uk.gov.hmrc.play.bootstrap.controller.BackendBaseController {

override implicit def hc(implicit rh: RequestHeader): HeaderCarrier =
HeaderCarrierConverter.fromHeadersAndSessionAndRequest(rh.headers, Some(rh.session), Some(rh))
HeaderCarrierConverter.fromRequestAndSession(rh, rh.session)
}
29 changes: 5 additions & 24 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import bloop.integrations.sbt.BloopDefaults
lazy val plugins: Seq[Plugins] = Seq.empty
lazy val playSettings: Seq[Setting[_]] = Seq.empty
lazy val microservice = (project in file("."))
.enablePlugins(Seq(PlayScala, SbtAutoBuildPlugin, SbtGitVersioning, SbtDistributablesPlugin, SbtArtifactory) ++ plugins: _*)
.enablePlugins(Seq(PlayScala, SbtAutoBuildPlugin, SbtDistributablesPlugin) ++ plugins: _*)
.settings(
name := appName
)
Expand Down Expand Up @@ -49,6 +49,7 @@ lazy val microservice = (project in file("."))
.settings(playSettings: _*)
.settings(scalaSettings: _*)
.settings(publishingSettings: _*)
.settings(ScoverageSettings(): _*)
.settings(defaultSettings(): _*)
.settings(
targetJvm := "jvm-1.8",
Expand All @@ -62,9 +63,11 @@ lazy val microservice = (project in file("."))
.settings(unmanagedResourceDirectories in Compile += baseDirectory.value / "resources")

.settings(inConfig(TemplateTest)(Defaults.testSettings): _*)
.settings(inConfig(TemplateTest)(BloopDefaults.configSettings))
.settings(
Test / testOptions := Seq(Tests.Argument(TestFrameworks.ScalaTest, "-eT")),
Test / unmanagedSourceDirectories += baseDirectory.value / "test",
Test / unmanagedSourceDirectories += baseDirectory.value / "testcommon",
Test / fork := false,
Test / parallelExecution := false
)
Expand All @@ -75,6 +78,7 @@ lazy val microservice = (project in file("."))
.settings(
testOptions in AcceptanceTest := Seq(Tests.Argument(TestFrameworks.ScalaTest, "-eT")),
AcceptanceTest / unmanagedSourceDirectories += baseDirectory.value / "acceptance",
AcceptanceTest / unmanagedSourceDirectories += baseDirectory.value / "testcommon",
AcceptanceTest / unmanagedResourceDirectories := Seq((baseDirectory in AcceptanceTest).value / "test", (baseDirectory in AcceptanceTest).value / "target/web/public/test"),
AcceptanceTest / fork := false,
AcceptanceTest / parallelExecution := false,
Expand All @@ -98,26 +102,3 @@ lazy val playPublishingSettings: Seq[sbt.Setting[_]] = Seq(
publishAllArtefacts

lazy val appName = "api-documentation-frontend"

val ScoverageExclusionPatterns = List(
"<empty>",
"definition.*",
"sandbox.*",
"live.*",
"prod.*",
"testOnlyDoNotUseInAppConf.*",
"uk.gov.hmrc.config.*",
"app.Routes",
"app.RoutesPrefix",
"controllers.javascript",
"com.kenshoo.play.metrics.javascript",
"com.kenshoo.play.metrics",
".*Reverse.*",
"uk.gov.hmrc.controllers.Reverse*",
)

// Coverage configuration
// TODO ebridge - Fix and set back to 85
coverageMinimum := 80
coverageFailOnMinimum := true
coverageExcludedPackages := ScoverageExclusionPatterns.mkString("",";","")
Loading

0 comments on commit d2c5302

Please sign in to comment.