Skip to content

Commit

Permalink
refactor: use intersect to evaluate MiskCaller#hasCapability
Browse files Browse the repository at this point in the history
  • Loading branch information
tklovett committed Nov 13, 2024
1 parent d588cc4 commit f118ea7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 4 additions & 4 deletions misk-actions/src/main/kotlin/misk/MiskCaller.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ data class MiskCaller @JvmOverloads constructor(
}

/**
* Check whether the caller has one of allowedCapabilities.
* Check whether this caller has one or more of the [allowedCapabilities].
*/
fun hasCapability(allowedCapabilities: Set<String>) =
capabilities.any { allowedCapabilities.contains(it) }
fun hasCapability(allowedCapabilities: Set<String>): Boolean =
capabilities.intersect(allowedCapabilities).isNotEmpty()

/**
* Check whether this is a service-to-service call from one of allowedServices.
Expand All @@ -50,6 +50,6 @@ data class MiskCaller @JvmOverloads constructor(
if (service != null && allowedServices.contains(service)) return true

// Allow if the caller has provided an allowed capability
return capabilities.any { allowedCapabilities.contains(it) }
return hasCapability(allowedCapabilities)
}
}
10 changes: 10 additions & 0 deletions misk/src/test/kotlin/misk/MiskCallerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ internal class MiskCallerTest {
@Test fun serviceNameIsNotRedactedFromToString() {
assertThat("$testService").contains("${testService.service}")
}

@Test fun `hasCapability should be true when capabilities contains an allowedCapability`() {
val hasCapability = testUser.hasCapability(setOf("not_testing", "testing", "other_capability"))
assertThat(hasCapability).isTrue()
}

@Test fun `hasCapability should be false when capabilities does not contain an allowedCapability`() {
val hasCapability = testUser.hasCapability(setOf("not_testing", "other_capability"))
assertThat(hasCapability).isFalse()
}
}

internal class MiskCallerTestModule : KAbstractModule() {
Expand Down

0 comments on commit f118ea7

Please sign in to comment.