Skip to content

Commit 22046dd

Browse files
adrwsvc-squareup-copybara
authored andcommitted
Add missing tests for a non-empty Authenticated annotation and
custom access annotation on the same web action. GitOrigin-RevId: 5b27040f40b8a805c50058677a28b32baa688c1c
1 parent 125d5c8 commit 22046dd

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

misk/src/test/kotlin/misk/web/actions/AuthenticationTest.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.junit.jupiter.params.ParameterizedTest
2222
import org.junit.jupiter.params.provider.ValueSource
2323
import jakarta.inject.Inject
2424
import misk.security.authz.AccessInterceptor
25+
import misk.web.toResponseBody
2526
import wisp.logging.LogCollector
2627
import kotlin.test.assertFailsWith
2728

@@ -273,6 +274,34 @@ class AuthenticationTest {
273274
)
274275
}
275276

277+
@Test
278+
fun `stacking @Authenticated with other access annotations is an error`() {
279+
val unauthService = MiskCaller(service = "test")
280+
assertThat(
281+
executeRequest(
282+
path = "/auth-and-custom-capability",
283+
service = unauthService.service
284+
)
285+
).isEqualTo("unauthorized")
286+
287+
val authService = MiskCaller(service = "dingo")
288+
assertThat(
289+
executeRequest(
290+
path = "/auth-and-custom-capability",
291+
service = authService.service
292+
)
293+
).isEqualTo("$authService authorized with custom capability")
294+
295+
val caller = MiskCaller(user = "bob", capabilities = setOf("admin"))
296+
assertThat(
297+
executeRequest(
298+
path = "/auth-and-custom-capability",
299+
user = caller.user,
300+
capabilities = caller.capabilities.first()
301+
)
302+
).isEqualTo("$caller authorized with custom capability")
303+
}
304+
276305
/** Executes a request and returns the response body as a string. */
277306
private fun executeRequest(
278307
path: String = "/",

misk/src/test/kotlin/misk/web/actions/TestWebActionModule.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ class TestWebActionModule : KAbstractModule() {
4545
install(WebActionModule.create<GreetServiceWebAction>())
4646
install(WebActionModule.create<EmptyAuthenticatedAccessAction>())
4747
install(WebActionModule.create<EmptyAuthenticatedWithCustomAnnototationAccessAction>())
48-
install(WebActionModule.create<EmptyAuthenticatedAccessAction>())
4948
install(WebActionModule.create<AllowAnyServiceAccessAction>())
5049
install(WebActionModule.create<AllowAnyServicePlusAuthenticatedAccessAction>())
5150
install(WebActionModule.create<AllowAnyUserAccessAction>())
51+
install(WebActionModule.create<AuthenticatedServiceWithCustomAnnotations>())
5252

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

170+
class AuthenticatedServiceWithCustomAnnotations @Inject constructor() : WebAction {
171+
@Inject
172+
lateinit var scopedCaller: ActionScoped<MiskCaller?>
173+
174+
@Get("/auth-and-custom-capability")
175+
@Authenticated(services = ["dingo"])
176+
@CustomCapabilityAccess
177+
fun get() = "${scopedCaller.get()} authorized with custom capability".toResponseBody()
178+
}
179+
170180
class AllowAnyServiceAccessAction @Inject constructor() : WebAction {
171181
@Inject
172182
lateinit var scopedCaller: ActionScoped<MiskCaller?>

0 commit comments

Comments
 (0)