Skip to content

Commit 388a043

Browse files
committed
Integration test to verify securing findById(…) generally works.
Issue: #2070.
1 parent c0125c9 commit 388a043

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

spring-data-rest-tests/spring-data-rest-tests-security/src/test/java/org/springframework/data/rest/tests/security/PreAuthorizedOrderRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.rest.tests.security;
1717

18+
import java.util.Optional;
1819
import java.util.UUID;
1920

2021
import org.springframework.data.repository.CrudRepository;
@@ -28,6 +29,10 @@
2829
@PreAuthorize("hasRole('ROLE_USER')") // <1>
2930
public interface PreAuthorizedOrderRepository extends CrudRepository<Order, UUID> {
3031

32+
@PreAuthorize("hasRole('ROLE_ADMIN')")
33+
@Override
34+
Optional<Order> findById(UUID id);
35+
3136
@PreAuthorize("hasRole('ROLE_ADMIN')") // <2>
3237
@Override
3338
void deleteById(UUID aLong);

spring-data-rest-tests/spring-data-rest-tests-security/src/test/java/org/springframework/data/rest/tests/security/SecurityIntegrationTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.security.core.context.SecurityContextHolder;
3838
import org.springframework.test.context.ContextConfiguration;
3939
import org.springframework.test.context.junit4.SpringRunner;
40+
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
4041
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
4142
import org.springframework.web.context.WebApplicationContext;
4243

@@ -229,4 +230,17 @@ public void findAllOrdersAccessGrantedForAdmins() throws Throwable {
229230
with(user("user").roles("USER", "ADMIN"))).//
230231
andExpect(status().isOk());
231232
}
233+
234+
@Test // #2070
235+
public void rejectsAccessToItemResourceIfNotAuthorized() throws Exception {
236+
237+
MockHttpServletResponse response = mvc.perform(get(client.discoverUnique("orders").expand().getHref()).//
238+
with(user("user").roles("USER"))).//
239+
andReturn().getResponse();
240+
String href = assertHasJsonPathValue("$._embedded.orders[0]._links.self.href", response);
241+
242+
mvc.perform(get(href).with(user("user").roles("USER")))
243+
.andDo(MockMvcResultHandlers.print())
244+
.andExpect(status().isForbidden());
245+
}
232246
}

0 commit comments

Comments
 (0)