Skip to content

Commit

Permalink
Add missing tests for a non-empty Authenticated annotation and
Browse files Browse the repository at this point in the history
custom access annotation on the same web action.

GitOrigin-RevId: 5b27040f40b8a805c50058677a28b32baa688c1c
  • Loading branch information
adrw authored and svc-squareup-copybara committed Feb 11, 2025
1 parent 125d5c8 commit 22046dd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
29 changes: 29 additions & 0 deletions misk/src/test/kotlin/misk/web/actions/AuthenticationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import jakarta.inject.Inject
import misk.security.authz.AccessInterceptor
import misk.web.toResponseBody
import wisp.logging.LogCollector
import kotlin.test.assertFailsWith

Expand Down Expand Up @@ -273,6 +274,34 @@ class AuthenticationTest {
)
}

@Test
fun `stacking @Authenticated with other access annotations is an error`() {
val unauthService = MiskCaller(service = "test")
assertThat(
executeRequest(
path = "/auth-and-custom-capability",
service = unauthService.service
)
).isEqualTo("unauthorized")

val authService = MiskCaller(service = "dingo")
assertThat(
executeRequest(
path = "/auth-and-custom-capability",
service = authService.service
)
).isEqualTo("$authService authorized with custom capability")

val caller = MiskCaller(user = "bob", capabilities = setOf("admin"))
assertThat(
executeRequest(
path = "/auth-and-custom-capability",
user = caller.user,
capabilities = caller.capabilities.first()
)
).isEqualTo("$caller authorized with custom capability")
}

/** Executes a request and returns the response body as a string. */
private fun executeRequest(
path: String = "/",
Expand Down
12 changes: 11 additions & 1 deletion misk/src/test/kotlin/misk/web/actions/TestWebActionModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ class TestWebActionModule : KAbstractModule() {
install(WebActionModule.create<GreetServiceWebAction>())
install(WebActionModule.create<EmptyAuthenticatedAccessAction>())
install(WebActionModule.create<EmptyAuthenticatedWithCustomAnnototationAccessAction>())
install(WebActionModule.create<EmptyAuthenticatedAccessAction>())
install(WebActionModule.create<AllowAnyServiceAccessAction>())
install(WebActionModule.create<AllowAnyServicePlusAuthenticatedAccessAction>())
install(WebActionModule.create<AllowAnyUserAccessAction>())
install(WebActionModule.create<AuthenticatedServiceWithCustomAnnotations>())

multibind<AccessAnnotationEntry>().toInstance(
AccessAnnotationEntry<CustomServiceAccess>(services = listOf("payments"))
Expand Down Expand Up @@ -167,6 +167,16 @@ class EmptyAuthenticatedWithCustomAnnototationAccessAction @Inject constructor()
fun get() = "${scopedCaller.get()} authorized with CustomCapabilityAccess".toResponseBody()
}

class AuthenticatedServiceWithCustomAnnotations @Inject constructor() : WebAction {
@Inject
lateinit var scopedCaller: ActionScoped<MiskCaller?>

@Get("/auth-and-custom-capability")
@Authenticated(services = ["dingo"])
@CustomCapabilityAccess
fun get() = "${scopedCaller.get()} authorized with custom capability".toResponseBody()
}

class AllowAnyServiceAccessAction @Inject constructor() : WebAction {
@Inject
lateinit var scopedCaller: ActionScoped<MiskCaller?>
Expand Down

0 comments on commit 22046dd

Please sign in to comment.