Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIS-7344 Add Create Date Time to the API Approval Record #116

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion app/uk/gov/hmrc/apipublisher/models/APIApproval.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@

package uk.gov.hmrc.apipublisher.models

import java.time.Instant

import play.api.libs.json.{Format, Json}

case class APIApproval(serviceName: String, serviceUrl: String, name: String, description: Option[String] = None, approved: Option[Boolean] = Some(false)) {
case class APIApproval(
serviceName: String,
serviceUrl: String,
name: String,
description: Option[String] = None,
approved: Option[Boolean] = Some(false),
createdOn: Option[Instant] = Some(Instant.now())
) {
def isApproved: Boolean = approved.getOrElse(false)
}

Expand Down
12 changes: 9 additions & 3 deletions app/uk/gov/hmrc/apipublisher/services/ApprovalService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ class ApprovalService @Inject() (apiApprovalRepository: APIApprovalRepository, a
case (_, _) => false
}

def saveApproval(apiApproval: APIApproval, maybeExistingApiApproval: Option[APIApproval], isApproved: Boolean): Future[APIApproval] =
maybeExistingApiApproval match {
case Some(existingApproval) => apiApprovalRepository.save(apiApproval.copy(approved = Some(isApproved), createdOn = existingApproval.createdOn))
case _ => apiApprovalRepository.save(apiApproval.copy(approved = Some(isApproved)))
}

for {
existingApiApproval <- apiApprovalRepository.fetch(apiApproval.serviceName)
isApproved = calculateApiApprovalStatus(existingApiApproval)
_ <- apiApprovalRepository.save(apiApproval.copy(approved = Some(isApproved)))
maybeExistingApiApproval <- apiApprovalRepository.fetch(apiApproval.serviceName)
isApproved = calculateApiApprovalStatus(maybeExistingApiApproval)
_ <- saveApproval(apiApproval, maybeExistingApiApproval, isApproved)
} yield isApproved
}

Expand Down
58 changes: 35 additions & 23 deletions test/uk/gov/hmrc/apipublisher/services/ApprovalServiceSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package uk.gov.hmrc.apipublisher.services

import java.time.Duration
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future.successful

Expand Down Expand Up @@ -66,29 +67,33 @@ class ApprovalServiceSpec extends AsyncHmrcSpec {
}

"Allow publication of previously disabled service when PreventAutoDeploy is disabled" in new Setup {
val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description"))
val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description"))
val existingApiApproval = apiApproval.copy(approved = Some(false), createdOn = apiApproval.createdOn.map(_.minus(Duration.ofDays(5))))

when(mockAppConfig.preventAutoDeploy).thenReturn(false)
when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(apiApproval.copy(approved = Some(false)))))
when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(true)))).thenReturn(successful(apiApproval.copy(approved = Some(true))))
when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(existingApiApproval)))
when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn)))
.thenReturn(successful(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn)))

val result = await(underTest.createOrUpdateServiceApproval(apiApproval))

result shouldBe true
verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(true)))
verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn))
}

"Allow publication of previously enabled service when PreventAutoDeploy is disabled" in new Setup {
val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description"))
val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description"))
val existingApiApproval = apiApproval.copy(approved = Some(true), createdOn = apiApproval.createdOn.map(_.minus(Duration.ofDays(5))))

when(mockAppConfig.preventAutoDeploy).thenReturn(false)
when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(apiApproval.copy(approved = Some(true)))))
when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(true)))).thenReturn(successful(apiApproval.copy(approved = Some(true))))
when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(existingApiApproval)))
when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn)))
.thenReturn(successful(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn)))

val result = await(underTest.createOrUpdateServiceApproval(apiApproval))

result shouldBe true
verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(true)))
verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn))
}

"Prevent publication of previously unknown services when PreventAutoDeploy is enabled" in new Setup {
Expand All @@ -105,40 +110,46 @@ class ApprovalServiceSpec extends AsyncHmrcSpec {
}

"Prevent publication of previously disabled service when PreventAutoDeploy is enabled" in new Setup {
val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description"))
val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description"))
val existingApiApproval = apiApproval.copy(approved = Some(false), createdOn = apiApproval.createdOn.map(_.minus(Duration.ofDays(5))))

when(mockAppConfig.preventAutoDeploy).thenReturn(true)
when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(apiApproval.copy(approved = Some(false)))))
when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(false)))).thenReturn(successful(apiApproval.copy(approved = Some(false))))
when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(existingApiApproval)))
when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(false), createdOn = existingApiApproval.createdOn)))
.thenReturn(successful(apiApproval.copy(approved = Some(false), createdOn = existingApiApproval.createdOn)))

val result = await(underTest.createOrUpdateServiceApproval(apiApproval))

result shouldBe false
verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(false)))
verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(false), createdOn = existingApiApproval.createdOn))
}

"Allow publication of previously enabled service when PreventAutoDeploy is enabled" in new Setup {
val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description"))
val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description"))
val existingApiApproval = apiApproval.copy(approved = Some(true), createdOn = apiApproval.createdOn.map(_.minus(Duration.ofDays(5))))

when(mockAppConfig.preventAutoDeploy).thenReturn(true)
when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(apiApproval.copy(approved = Some(true)))))
when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(true)))).thenReturn(successful(apiApproval.copy(approved = Some(true))))
when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(existingApiApproval)))
when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn)))
.thenReturn(successful(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn)))

val result = await(underTest.createOrUpdateServiceApproval(apiApproval))

result shouldBe true
verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(true)))
verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn))
}

"Allow an existing Service to be approved" in new Setup {
val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description"))
val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description"))
val existingApiApproval = apiApproval.copy(createdOn = apiApproval.createdOn.map(_.minus(Duration.ofDays(5))))

when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(apiApproval)))
when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(true)))).thenReturn(successful(apiApproval.copy(approved = Some(true))))
when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(existingApiApproval)))
when(mockApiApprovalRepository.save(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn)))
.thenReturn(successful(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn)))
val result = await(underTest.approveService("testService"))

result shouldBe ServiceLocation("testService", "http://localhost/myservice")
verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(true)))
verify(mockApiApprovalRepository).save(apiApproval.copy(approved = Some(true), createdOn = existingApiApproval.createdOn))
}

"Raise an exception if an attempt is made to approve an unknown service" in new Setup {
Expand All @@ -151,12 +162,13 @@ class ApprovalServiceSpec extends AsyncHmrcSpec {
}

"Return a summary of a service when requested" in new Setup {
val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description"))
val apiApproval = APIApproval("testService", "http://localhost/myservice", "testServiceName", Some("Test Service Description"))
val existingApiApproval = apiApproval.copy(createdOn = apiApproval.createdOn.map(_.minus(Duration.ofDays(5))))

when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(apiApproval)))
when(mockApiApprovalRepository.fetch("testService")).thenReturn(successful(Some(existingApiApproval)))
val result = await(underTest.fetchServiceApproval("testService"))

result shouldBe apiApproval
result shouldBe apiApproval.copy(createdOn = existingApiApproval.createdOn)
verify(mockApiApprovalRepository).fetch("testService")
}

Expand Down