Basic-auth 需要,但测试案例在Spring Boot应用中无需身份验证即通过。

huangapple go评论53阅读模式
英文:

Basic-auth is required but the test case passes without authentication in Spring boot Application

问题

I added the Basic AUTH in the Spring boot application, which works perfectly fine. when I write test cases, the test case passes without authentication. I'm surprised how it is possible that the test case runs without Authentication as the application needs authentication.

我在Spring Boot应用程序中添加了基本身份验证,它运行得很正常。但当我编写测试用例时,测试用例在没有身份验证的情况下通过了。我很惊讶为什么测试用例在应用程序需要身份验证的情况下可以运行?

And the application.properties file:

还有 application.properties 文件:

spring.datasource.url=jdbc:h2:mem:userandroles
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.hibernate.ddl-auto=update
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.security.user.name=user
spring.security.user.password=pass

为什么测试用例在需要身份验证的情况下可以运行?

英文:

I added the Basic AUTH in the Spring boot application, which works perfectly fine. when I write test cases, the test case passes without authentication. I'm surprised how it is possible that the test case runs without Authentication as the application needs authentication.

import com.example.userandroles.Entities.Users;
import com.example.userandroles.Service.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.util.Base64;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebAppConfiguration
@SpringBootTest
class UsersControllerTest {
    private MockMvc mvc;

    @Mock
    private UserService userService;

    @InjectMocks
    private UserController userController;

    private ObjectMapper om = new ObjectMapper();

    @Autowired
    WebApplicationContext context;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.openMocks(this);
        mvc = MockMvcBuilders.webAppContextSetup(context).build();
    }

    @Test
    public void addUser() throws Exception {
        Users user = new Users(1L, "Numaira", "Nawaz");
        String jsonRequest = om.writeValueAsString(user);

        MvcResult result = mvc.perform(MockMvcRequestBuilders.post("/v1/user")
                        .content(jsonRequest)
                        .contentType(MediaType.APPLICATION_JSON)
                        .header("Authorization", "Basic " + Base64.getEncoder().encodeToString("useddr:pass".getBytes())))
                .andExpect(status().isCreated())
                .andReturn();

        //assertEquals(HttpStatus.CREATED.value(), result.getResponse().getStatus());
    }

    @Test
    void update() {
    }

    @Test
    public void testFindUser_Success() throws Exception {
        Long id = 1L;
        Users user = new Users(id, "Numaira", "Nawaz");

        //Mockito.when(userService.findUserById(id)).thenReturn(user);
        MvcResult result = mvc.perform(MockMvcRequestBuilders.get("/v1/user/{id}", id)
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andReturn();

        String responseBody = result.getResponse().getContentAsString();
        Users responseUser = om.readValue(responseBody, Users.class);

        assertEquals(MediaType.APPLICATION_JSON_VALUE, result.getResponse().getContentType());
        assertEquals(user.getFirstName(), responseUser.getFirstName());
    }
}

And the application.properties file

spring.datasource.url=jdbc:h2:mem:userandroles
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.hibernate.ddl-auto=update
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.security.user.name=user
spring.security.user.password=pass

Why does the test case run without authentication as authentication is required?

答案1

得分: 0

I got the solution when I removed the setUp method then the authentication works properly.(当我删除了setUp方法后,身份验证正常工作。)

英文:
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
mvc = MockMvcBuilders.webAppContextSetup(context).build();
}

I got the solution when I removed the setUp method then the authentication works properly.

huangapple
  • 本文由 发表于 2023年5月26日 14:40:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定