Skip to content

Commit 8703abf

Browse files
committed
chore(tests): add test for KmsValidationFilter
1 parent 2e83a99 commit 8703abf

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2017-2025 Adobe.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.adobe.testing.s3mock.controller
17+
18+
import com.adobe.testing.s3mock.store.KmsKeyStore
19+
import org.assertj.core.api.Assertions.assertThat
20+
import org.junit.jupiter.api.Test
21+
import org.mockito.kotlin.whenever
22+
import org.springframework.beans.factory.annotation.Autowired
23+
import org.springframework.boot.test.context.TestConfiguration
24+
import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest
25+
import org.springframework.context.annotation.Bean
26+
import org.springframework.context.annotation.Import
27+
import org.springframework.http.MediaType
28+
import org.springframework.http.ResponseEntity
29+
import org.springframework.test.context.bean.override.mockito.MockitoBean
30+
import org.springframework.test.web.servlet.MockMvc
31+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
32+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
33+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
34+
import org.springframework.web.bind.annotation.GetMapping
35+
import org.springframework.web.bind.annotation.RequestMapping
36+
import org.springframework.web.bind.annotation.RestController
37+
38+
@MockitoBean(types = [KmsKeyStore::class])
39+
@WebMvcTest(
40+
controllers = [KmsValidationFilterTest.DummyPassController::class],
41+
properties = ["com.adobe.testing.s3mock.store.region=us-east-1"],
42+
useDefaultFilters = false
43+
)
44+
@Import(KmsValidationFilterTest.Config::class)
45+
internal class KmsValidationFilterTest {
46+
47+
@Autowired
48+
private lateinit var mockMvc: MockMvc
49+
50+
@Autowired
51+
private lateinit var kmsKeyStore: KmsKeyStore
52+
53+
54+
@Test
55+
fun `denies request with invalid kms key id`() {
56+
val badKey = "bad-key-id"
57+
whenever(kmsKeyStore.validateKeyId(badKey)).thenReturn(false)
58+
59+
mockMvc.perform(
60+
get("/internal-test/pass")
61+
.param("dummy", "true")
62+
.header("X-Dummy", "1")
63+
.header("x-amz-server-side-encryption", "aws:kms")
64+
.header("x-amz-server-side-encryption-aws-kms-key-id", badKey)
65+
)
66+
.andExpect(status().isBadRequest)
67+
.andExpect(content().contentType(MediaType.APPLICATION_XML))
68+
.andExpect(content().string(org.hamcrest.Matchers.containsString("Invalid keyId '$badKey'")))
69+
}
70+
71+
@Test
72+
fun `allows request with valid kms key id`() {
73+
val goodKey = "good-key-id"
74+
whenever(kmsKeyStore.validateKeyId(goodKey)).thenReturn(true)
75+
76+
mockMvc.perform(
77+
get("/internal-test/pass")
78+
.param("dummy", "true")
79+
.header("X-Dummy", "1")
80+
.header("x-amz-server-side-encryption", "aws:kms")
81+
.header("x-amz-server-side-encryption-aws-kms-key-id", goodKey)
82+
)
83+
.andExpect { assertThat(it.response.status).isNotEqualTo(400) }
84+
}
85+
86+
@Test
87+
fun `allows request when kms headers are missing`() {
88+
mockMvc.perform(
89+
get("/internal-test/pass")
90+
.param("dummy", "true")
91+
.header("X-Dummy", "1")
92+
).andExpect { assertThat(it.response.status).isNotEqualTo(400) }
93+
}
94+
95+
@TestConfiguration
96+
internal class Config {
97+
@Bean
98+
fun kmsValidationFilter(
99+
kmsKeyStore: KmsKeyStore
100+
) = KmsValidationFilter(kmsKeyStore, ControllerConfiguration().messageConverter())
101+
}
102+
103+
@RestController
104+
@RequestMapping("/internal-test", params = ["dummy=true"])
105+
internal class DummyPassController {
106+
@GetMapping("/pass", headers = ["X-Dummy=1"])
107+
fun pass(): ResponseEntity<String> = ResponseEntity.ok("ok")
108+
}
109+
}

0 commit comments

Comments
 (0)