Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class AllowListController @Inject() (
def checkAllowList(service: Service, feature: Feature): Action[CheckRequest] =
Action.async(parse.json[CheckRequest]):
request =>
Mdc.putMdc(Map("service" -> service.value, "feature" -> feature.value,"api-op" -> "check"))
Mdc.putMdc(Map("service" -> service.name, "feature" -> feature.name,"api-op" -> "check"))
allowList.check(service, feature, request.body.identifier).map:
checkResult =>
Ok(Json.toJsObject(CheckResponse(included = checkResult)))
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package uk.gov.hmrc.ratelimitedallowlist.models.domain

import play.api.mvc.PathBindable

case class Feature(value: String)
case class Feature(name: String)

object Feature:
val REGEX_PATTERN = "^[a-zA-Z0-9-]+$"
Expand All @@ -31,4 +31,4 @@ object Feature:
.map(Feature.apply)

override def unbind(key: String, value: Feature): String =
value.value
value.name
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package uk.gov.hmrc.ratelimitedallowlist.models.domain

import play.api.mvc.PathBindable

case class Service(value: String)
case class Service(name: String)

object Service:
val REGEX_PATTERN = "^[a-zA-Z0-9-]+$"
Expand All @@ -31,4 +31,4 @@ object Service:
.map(Service.apply)

override def unbind(key: String, value: Service): String =
value.value
value.name
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class AllowListMetadataRepositoryImpl @Inject()(
val initCompleted = onInit()

def create(service: Service, feature: Feature): Future[Done] = {
val entry = AllowListMetadata(service.value, feature.value, 0, false, clock.instant(), clock.instant(), "")
val entry = AllowListMetadata(service.name, feature.name, 0, false, clock.instant(), clock.instant(), "")
collection
.insertOne(entry)
.toFuture()
Expand All @@ -87,8 +87,8 @@ class AllowListMetadataRepositoryImpl @Inject()(
def get(service: Service, feature: Feature): Future[Option[AllowListMetadata]] =
collection.find(
Filters.and(
Filters.equal(Field.service, service.value),
Filters.equal(Field.feature, feature.value)
Filters.equal(Field.service, service.name),
Filters.equal(Field.feature, feature.name)
)
).toFuture()
.map(_.headOption)
Expand All @@ -97,8 +97,8 @@ class AllowListMetadataRepositoryImpl @Inject()(
collection
.deleteMany(
Filters.and(
Filters.equal(Field.service, service.value),
Filters.equal(Field.feature, feature.value)
Filters.equal(Field.service, service.name),
Filters.equal(Field.feature, feature.name)
)
).toFuture()
.map {
Expand All @@ -112,8 +112,8 @@ class AllowListMetadataRepositoryImpl @Inject()(
collection
.updateOne(
Filters.and(
Filters.equal(Field.service, service.value),
Filters.equal(Field.feature, feature.value)
Filters.equal(Field.service, service.name),
Filters.equal(Field.feature, feature.name)
),
Updates.combine(
Updates.inc(Field.tokens, incrementCount),
Expand All @@ -136,8 +136,8 @@ class AllowListMetadataRepositoryImpl @Inject()(
collection
.updateOne(
Filters.and(
Filters.equal(Field.service, service.value),
Filters.equal(Field.feature, feature.value)
Filters.equal(Field.service, service.name),
Filters.equal(Field.feature, feature.name)
),
Updates.combine(
Updates.set(Field.canIssueTokens, false),
Expand All @@ -155,8 +155,8 @@ class AllowListMetadataRepositoryImpl @Inject()(
collection
.updateOne(
Filters.and(
Filters.equal(Field.service, service.value),
Filters.equal(Field.feature, feature.value)
Filters.equal(Field.service, service.name),
Filters.equal(Field.feature, feature.name)
),
Updates.combine(
Updates.set(Field.canIssueTokens, true),
Expand All @@ -174,8 +174,8 @@ class AllowListMetadataRepositoryImpl @Inject()(
collection
.updateOne(
Filters.and(
Filters.equal(Field.service, service.value),
Filters.equal(Field.feature, feature.value),
Filters.equal(Field.service, service.name),
Filters.equal(Field.feature, feature.name),
Filters.equal(Field.canIssueTokens, true),
Filters.gte(Field.tokens, 1),
),
Expand All @@ -195,8 +195,8 @@ class AllowListMetadataRepositoryImpl @Inject()(
collection
.updateOne(
Filters.and(
Filters.equal(Field.service, service.value),
Filters.equal(Field.feature, feature.value)
Filters.equal(Field.service, service.name),
Filters.equal(Field.feature, feature.name)
),
Updates.combine(
Updates.set(Field.tokens, count),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class AllowListRepositoryImpl @Inject()(mongoComponent: MongoComponent,
) with AllowListRepository {

def set(service: Service, feature: Feature, value: String): Future[Done] = {
val entry = AllowListEntry(service.value, feature.value, oneWayHash(value), clock.instant())
val entry = AllowListEntry(service.name, feature.name, oneWayHash(value), clock.instant())

collection
.insertOne(entry)
Expand All @@ -80,25 +80,25 @@ class AllowListRepositoryImpl @Inject()(mongoComponent: MongoComponent,
def clear(service: Service, feature: Feature): Future[Done] =
collection
.deleteMany(Filters.and(
Filters.equal(Field.service, service.value),
Filters.equal(Field.feature, feature.value)
Filters.equal(Field.service, service.name),
Filters.equal(Field.feature, feature.name)
)).toFuture()
.map(_ => Done)

def check(service: Service, feature: Feature, value: String): Future[Boolean] =
collection
.find(Filters.and(
Filters.equal(Field.service, service.value),
Filters.equal(Field.feature, feature.value),
Filters.equal(Field.service, service.name),
Filters.equal(Field.feature, feature.name),
Filters.equal(Field.hashedValue, oneWayHash(value))
))
.toFuture()
.map(_.nonEmpty)

def count(service: Service, feature: Feature): Future[Long] =
collection.countDocuments(Filters.and(
Filters.equal(Field.service, service.value),
Filters.equal(Field.feature, feature.value)
Filters.equal(Field.service, service.name),
Filters.equal(Field.feature, feature.name)
)).toFuture()
}

Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class AllowListMetadataRepositorySpec extends AnyFreeSpecLike, Matchers, Default
}

".addTokens" - {
"succeeds when the increment value is positive and updates the updated field" in {
"succeeds when the increment name is positive and updates the updated field" in {
val entry1 = AllowListMetadata(service1, feature1, 10, false, fixedInstant, fixedInstant, "")
val entry2 = AllowListMetadata(service1, feature2, 10, true, fixedInstant, fixedInstant, "")
val entry3 = AllowListMetadata(service2, feature1, 10, true, fixedInstant, fixedInstant, "")
Expand Down Expand Up @@ -175,7 +175,7 @@ class AllowListMetadataRepositorySpec extends AnyFreeSpecLike, Matchers, Default
result mustEqual UpdateResultResult.NoOpUpdateResult
}

"returns a failed future with IllegalArgumentException when the increment value is invalid" in {
"returns a failed future with IllegalArgumentException when the increment name is invalid" in {
val entry1 = AllowListMetadata(service1, feature1, 10, true, fixedInstant, fixedInstant, "")
insert(entry1).futureValue

Expand Down Expand Up @@ -361,8 +361,8 @@ class AllowListMetadataRepositorySpec extends AnyFreeSpecLike, Matchers, Default

val overrideConfig =
Configuration(
"mongodb.collections.allow-list-metadata.token-updates.0.service" -> service1.value,
"mongodb.collections.allow-list-metadata.token-updates.0.feature" -> feature1.value,
"mongodb.collections.allow-list-metadata.token-updates.0.service" -> service1.name,
"mongodb.collections.allow-list-metadata.token-updates.0.feature" -> feature1.name,
"mongodb.collections.allow-list-metadata.token-updates.0.tokens" -> 100,
"mongodb.collections.allow-list-metadata.token-updates.0.id" -> updatedConfigId,
).withFallback(Configuration.load(Environment.simple()))
Expand All @@ -384,12 +384,12 @@ class AllowListMetadataRepositorySpec extends AnyFreeSpecLike, Matchers, Default

"when there are multiple configs that have not been applied" in {
val overrideConfig = Configuration(
"mongodb.collections.allow-list-metadata.token-updates.0.service" -> service1.value,
"mongodb.collections.allow-list-metadata.token-updates.0.feature" -> feature1.value,
"mongodb.collections.allow-list-metadata.token-updates.0.service" -> service1.name,
"mongodb.collections.allow-list-metadata.token-updates.0.feature" -> feature1.name,
"mongodb.collections.allow-list-metadata.token-updates.0.tokens" -> 100,
"mongodb.collections.allow-list-metadata.token-updates.0.id" -> updatedConfigId,
"mongodb.collections.allow-list-metadata.token-updates.1.service" -> service2.value,
"mongodb.collections.allow-list-metadata.token-updates.1.feature" -> feature2.value,
"mongodb.collections.allow-list-metadata.token-updates.1.service" -> service2.name,
"mongodb.collections.allow-list-metadata.token-updates.1.feature" -> feature2.name,
"mongodb.collections.allow-list-metadata.token-updates.1.tokens" -> 0,
"mongodb.collections.allow-list-metadata.token-updates.1.id" -> updatedConfigId,
"features.allow-config-token-updates" -> true
Expand Down Expand Up @@ -437,8 +437,8 @@ class AllowListMetadataRepositorySpec extends AnyFreeSpecLike, Matchers, Default


given Conversion[Service, String] with
def apply(x: Service): String = x.value
def apply(x: Service): String = x.name

given Conversion[Feature, String] with
def apply(x: Feature): String = x.value
def apply(x: Feature): String = x.name
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class AllowListRepositorySpec extends AnyFreeSpecLike, Matchers, DefaultPlayMong
private val value2 = "value2"

".set" - {
"must save an entry that does not already exist in the repository, hashing the value" in {
"must save an entry that does not already exist in the repository, hashing the name" in {
repository.set(service, feature, value1).futureValue
val insertedRecord = findAll().futureValue.head

Expand Down Expand Up @@ -106,15 +106,15 @@ class AllowListRepositorySpec extends AnyFreeSpecLike, Matchers, DefaultPlayMong
}

".check" - {
"must return true when a record exists for the given service, feature and value" in {
"must return true when a record exists for the given service, feature and name" in {
val entry = AllowListEntry(service1, feature1, hashingFn(value1), fixedInstant)

insert(entry).futureValue

repository.check(service1, feature1, value1).futureValue mustBe true
}

"must return false when a record for the given service, feature and value does not exist" in {
"must return false when a record for the given service, feature and name does not exist" in {
val entry1 = AllowListEntry(service1, feature1, hashingFn(value1), fixedInstant)
val entry2 = AllowListEntry(service1, feature2, hashingFn(value2), fixedInstant)
val entry3 = AllowListEntry(service2, feature1, hashingFn(value2), fixedInstant)
Expand All @@ -139,8 +139,8 @@ class AllowListRepositorySpec extends AnyFreeSpecLike, Matchers, DefaultPlayMong
}

given Conversion[Service, String] with
def apply(x: Service): String = x.value
def apply(x: Service): String = x.name

given Conversion[Feature, String] with
def apply(x: Feature): String = x.value
def apply(x: Feature): String = x.name
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ class FeatureSpec extends AnyFreeSpec with Matchers:
"PathBindable bind" - {
"pass when the path is valid" in {
val expected = Feature("asdf-ASDF-123")
pathBindable.bind("listName", expected.value) mustBe Right(expected)
pathBindable.bind("feature", expected.name) mustBe Right(expected)
}

"fail when the path is valid" in {
val expected = Feature("asdf_ASDF_123")
pathBindable.bind("listName", expected.value) mustBe Left("Invalid format for a feature")
pathBindable.bind("feature", expected.name) mustBe Left("Invalid format for a feature")
}
}

"PathBindable unbind" - {
"pass" in {
val value = Feature("asdf-ASDF-123")
pathBindable.unbind("listName", value) mustBe value.value
pathBindable.unbind("feature", value) mustBe value.name
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ class ServiceSpec extends AnyFreeSpec with Matchers:
"PathBindable bind" - {
"pass when the path is valid" in {
val expected = Service("asdf-ASDF-123")
pathBindable.bind("serviceName", expected.value) mustBe Right(expected)
pathBindable.bind("service", expected.name) mustBe Right(expected)
}

"fail when the path is valid" in {
val expected = Service("asdf_ASDF_123")
pathBindable.bind("serviceName", expected.value) mustBe Left("Invalid format for service name")
pathBindable.bind("service", expected.name) mustBe Left("Invalid format for service name")
}
}

"PathBindable unbind" - {
"pass" in {
val value = Service("asdf-ASDF-123")
pathBindable.unbind("serviceName", value) mustBe value.value
pathBindable.unbind("service", value) mustBe value.name
}
}