Skip to content
Advertisement

Testing @PreAuthorize annotation with hasAuthority()

I am trying to unit test my apis that have @PreAuthorize annotation. I am getting the cognito groups from the Jwt and using them as authorities. I check the same in the preauthorize annotation in the api methods. UPDATE: I get the 404 No mapping Delete profile/VOlUc3F5A_test.txt exists

Test class:

import combackend.StorageController;
import com.backend.config.;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;


@RunWith(SpringRunner.class)
@ContextConfiguration(classes = Config.class)
@SpringBootTest
@AutoConfigureMockMvc
//@WebMvcTest(StorageController.class)
public class Tests {


    @Autowired
    private MockMvc mockMvc;

    @Test
    @WithMockUser(username = "admin@domain.com", password = "Password", authorities = "admin")
    public void testDeleteCardProfile() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .delete("http://localhost:9092/profile/{fileKey}", "VOlUc3F5A_test.txt").with(SecurityMockMvcRequestPostProcessors.csrf())).andReturn();
        Assert.assertEquals(200, result.getResponse().getStatus());
        //  .andExpect(status().isOk());


    }
}

Controller method:

  @ApiResponses({ @ApiResponse(responseCode = "204", description = "Returns the delete success message"),
                @ApiResponse(responseCode = "400", description = "Failed to delete file  : Badrequest"),
                @ApiResponse(responseCode = "404", description = "Failed to delete file, NotFound") })

        @RequestMapping(value = "/profile/{fileKey}", consumes = MediaType.ALL_VALUE, method = { RequestMethod.DELETE })
        @PreAuthorize("hasAuthority('admin')")
        public Mono<Boolean> deleteProfile(@AuthenticationPrincipal Jwt jwt, @PathVariable("fileKey") String fileKey) {
                return storageService.removeProfile(fileKey);
        }

Advertisement

Answer

The error message states that this.mockMvc is null. In the code that you have shared, you are not initializing this.mockMvc anywhere.

Perhaps you mean to autowire it

@Autowired
private MockMvc mockMvc;
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement