Skip to content

Commit

Permalink
Replace basicAuth() with SecurityMockMvcRequestPostProcessors#httpBas…
Browse files Browse the repository at this point in the history
…ic()
  • Loading branch information
DevDengChao authored and marcusdacoregio committed Nov 30, 2023
1 parent 51a2cf8 commit dd8c9a4
Showing 1 changed file with 11 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,19 @@

package example;

import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
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.http.HttpHeaders;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.RequestPostProcessor;

import java.util.Map;

import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down Expand Up @@ -61,7 +58,7 @@ void performTokenRequestWhenValidClientCredentialsThenOk() throws Exception {
this.mockMvc.perform(post("/oauth2/token")
.param("grant_type", "client_credentials")
.param("scope", "message:read")
.with(basicAuth(CLIENT_ID, CLIENT_SECRET)))
.with(httpBasic(CLIENT_ID, CLIENT_SECRET)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.access_token").isString())
.andExpect(jsonPath("$.expires_in").isNumber())
Expand All @@ -76,7 +73,7 @@ void performTokenRequestWhenMissingScopeThenOk() throws Exception {
this.mockMvc.perform(post("/oauth2/token")
.param("grant_type", "client_credentials")
.param("scope", "message:read message:write")
.with(basicAuth(CLIENT_ID, CLIENT_SECRET)))
.with(httpBasic(CLIENT_ID, CLIENT_SECRET)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.access_token").isString())
.andExpect(jsonPath("$.expires_in").isNumber())
Expand All @@ -91,7 +88,7 @@ void performTokenRequestWhenInvalidClientCredentialsThenUnauthorized() throws Ex
this.mockMvc.perform(post("/oauth2/token")
.param("grant_type", "client_credentials")
.param("scope", "message:read")
.with(basicAuth("bad", "password")))
.with(httpBasic("bad", "password")))
.andExpect(status().isUnauthorized())
.andExpect(jsonPath("$.error").value("invalid_client"));
// @formatter:on
Expand All @@ -101,7 +98,7 @@ void performTokenRequestWhenInvalidClientCredentialsThenUnauthorized() throws Ex
void performTokenRequestWhenMissingGrantTypeThenUnauthorized() throws Exception {
// @formatter:off
this.mockMvc.perform(post("/oauth2/token")
.with(basicAuth("bad", "password")))
.with(httpBasic("bad", "password")))
.andExpect(status().isUnauthorized())
.andExpect(jsonPath("$.error").value("invalid_client"));
// @formatter:on
Expand All @@ -112,7 +109,7 @@ void performTokenRequestWhenGrantTypeNotRegisteredThenBadRequest() throws Except
// @formatter:off
this.mockMvc.perform(post("/oauth2/token")
.param("grant_type", "client_credentials")
.with(basicAuth("login-client", "openid-connect")))
.with(httpBasic("login-client", "openid-connect")))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.error").value("unauthorized_client"));
// @formatter:on
Expand All @@ -123,7 +120,7 @@ void performIntrospectionRequestWhenValidTokenThenOk() throws Exception {
// @formatter:off
this.mockMvc.perform(post("/oauth2/introspect")
.param("token", getAccessToken())
.with(basicAuth(CLIENT_ID, CLIENT_SECRET)))
.with(httpBasic(CLIENT_ID, CLIENT_SECRET)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.active").value("true"))
.andExpect(jsonPath("$.aud[0]").value(CLIENT_ID))
Expand All @@ -143,7 +140,7 @@ void performIntrospectionRequestWhenInvalidCredentialsThenUnauthorized() throws
// @formatter:off
this.mockMvc.perform(post("/oauth2/introspect")
.param("token", getAccessToken())
.with(basicAuth("bad", "password")))
.with(httpBasic("bad", "password")))
.andExpect(status().isUnauthorized())
.andExpect(jsonPath("$.error").value("invalid_client"));
// @formatter:on
Expand All @@ -154,7 +151,7 @@ private String getAccessToken() throws Exception {
MvcResult mvcResult = this.mockMvc.perform(post("/oauth2/token")
.param("grant_type", "client_credentials")
.param("scope", "message:read")
.with(basicAuth(CLIENT_ID, CLIENT_SECRET)))
.with(httpBasic(CLIENT_ID, CLIENT_SECRET)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.access_token").exists())
.andReturn();
Expand All @@ -167,29 +164,6 @@ private String getAccessToken() throws Exception {
return tokenResponse.get("access_token").toString();
}

private static BasicAuthenticationRequestPostProcessor basicAuth(String username, String password) {
return new BasicAuthenticationRequestPostProcessor(username, password);
}

private static final class BasicAuthenticationRequestPostProcessor implements RequestPostProcessor {

private final String username;

private final String password;

private BasicAuthenticationRequestPostProcessor(String username, String password) {
this.username = username;
this.password = password;
}

@Override
public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) {
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(this.username, this.password);
request.addHeader("Authorization", headers.getFirst("Authorization"));
return request;
}

}

}

0 comments on commit dd8c9a4

Please sign in to comment.