SecurityConfigTest.java 1.57 KB
package com.xly.erp.config;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
class SecurityConfigTest {

    @Autowired MockMvc mockMvc;

    @TestConfiguration
    static class PingConfig {
        @Bean PingController pingController() { return new PingController(); }
    }

    @RestController
    @RequestMapping("/api/__ping")
    static class PingController {
        @GetMapping
        public String ping() { return "pong"; }
    }

    @Test
    void anyApiEndpoint_isPermittedWithoutAuth() throws Exception {
        mockMvc.perform(get("/api/__ping"))
                .andExpect(status().isOk())
                .andExpect(content().string("pong"));
    }
}