10

I am still getting Access Denied although my test method is annotated with @WithMockUser. Why this is not working in integration test? Everything is fine with test with @WebAppConfiguration and MockMvc.

Test Class:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class FileUploadIntegrationTest {
 @Autowired
 private TestRestTemplate restTemplate;
 @MockBean
 private FileStorageService storageService;
 @Test
 public void classPathResourceTest() throws Exception {
 ClassPathResource resource = new ClassPathResource("/test/testFile.txt", getClass());
 assertThat(resource.exists(), is(true));
 }
 @Test
 @WithMockUser(username="tester",roles={"USER"})
 public void shouldUploadFile() throws Exception {
 ClassPathResource resource = new ClassPathResource("/test/testFile.txt", getClass());
 MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
 map.add("file", resource);
 ResponseEntity<String> response = this.restTemplate.postForEntity("/files", map, String.class);
// assertThat(response.getStatusCode(), is(HttpStatus.OK));
 then(storageService).should().addFile((any(String.class)), any(MultipartFile.class));
 }
}

Controller class:

@RestController
@RequestMapping("/files")
@PreAuthorize(value = "hasRole('ROLE_USER')")
public class FileUploadController {
 private FileStorageService fileStorageService;
 private AuthenticationFacade authenticationFacade;
 @Autowired
 public FileUploadController(FileStorageService fileUploadService, AuthenticationFacade authenticationFacade) {
 this.fileStorageService = fileUploadService;
 this.authenticationFacade = authenticationFacade;
 }
 @ResponseBody
 @PostMapping
 public ResponseEntity<UUID> uploadFile(@RequestParam("file") MultipartFile file) {
 UUID uuid = this.fileStorageService.addFile(authenticationFacade.getAuthentication().getName(), file);
 if (uuid != null) return ResponseEntity.ok(uuid);
 else return (ResponseEntity<UUID>) ResponseEntity.badRequest();
 }
}
asked Dec 1, 2016 at 9:31
5
  • Did you solved it ? I have issues to get this to work as well Commented Nov 16, 2017 at 3:00
  • Unfortunately not ;/ Commented Nov 16, 2017 at 14:59
  • do you use spring security oauth2? Commented Nov 16, 2017 at 19:42
  • Yes I used it - here is the repo of this module: github.com/MarcinMilewski/sqap-api Commented Nov 17, 2017 at 6:18
  • Hi ! I believe your PreAuthorize annotation is incorrect. hasRole('ROLE_USER') expects the authority "ROLE_ROLE_USER". Whereas in your test the roles = "USER" adds the authority "ROLE_USER". You need to remove the "ROLE_" prefix from your PreAuthorize annotation Commented Oct 24, 2023 at 15:28

1 Answer 1

2

Couldn't solve this with @WithMockUser.

You can try using the Profiles approach described here: https://stackoverflow.com/a/35192495/3010484.

answered Mar 8, 2018 at 11:07
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.