集成测试,验证端点授权和身份验证

时间:2020-10-02 12:13:41

标签: testing language-agnostic integration-testing

请考虑以下集成测试。仅以Java为例。

  @Test
  @WithMockUser
  public void givenUnauthorized_whenGettingPeople_thenReturnStatus403() throws Exception {
    mvc.perform(get("/v1/api/people"))
        .andExpect(status().is(403))
        .andExpect(jsonPath("$.success").value(false))                     // 1
        .andExpect(jsonPath("$.message").value("Access is denied"));       // 2
  }

您认为评论行12实际上有帮助吗?我想知道是否应该保留它们。

这是类似的测试:

  @Test
  public void givenUnauthenticated_whenGettingPeople_thenReturnStatus401() throws Exception {
    mvc.perform(get("/v1/api/people"))
        .andExpect(status().is(401))
        .andExpect(status().reason(containsString("Full authentication is required to access this resource")));
  }

我开始认为仅验证401/403状态就足够了。我说的对吗?

0 个答案:

没有答案