mirror of
https://github.com/ysoftdevs/oauth-playground-server.git
synced 2026-03-30 14:01:55 +02:00
refactor tests
This commit is contained in:
@@ -12,7 +12,6 @@ import com.ysoft.geecon.repo.UsersRepo;
|
|||||||
import io.quarkus.test.common.http.TestHTTPResource;
|
import io.quarkus.test.common.http.TestHTTPResource;
|
||||||
import io.quarkus.test.junit.QuarkusTest;
|
import io.quarkus.test.junit.QuarkusTest;
|
||||||
import jakarta.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
import org.jsoup.Connection;
|
|
||||||
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Document;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@@ -43,14 +42,15 @@ public class AuthCodeGrantTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void authCodeGrant() throws IOException {
|
public void authCodeGrant() throws IOException {
|
||||||
AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT);
|
AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT)
|
||||||
LoginScreen loginScreen = flow.start(Map.of("scope", "scope1 scope2"));
|
.scope("scope1 scope2");
|
||||||
|
LoginScreen loginScreen = flow.start().expectLogin();
|
||||||
|
|
||||||
ConsentScreen consentScreen = loginScreen.submit("bob", "password").expectSuccess();
|
ConsentScreen consentScreen = loginScreen.submit("bob", "password").expectSuccess();
|
||||||
assertThat(consentScreen.getScopes(), is(List.of("scope1", "scope2")));
|
assertThat(consentScreen.getScopes(), is(List.of("scope1", "scope2")));
|
||||||
|
|
||||||
Document submit = consentScreen.submit();
|
Document submit = consentScreen.submit();
|
||||||
flow.parseAndValidateRedirect(submit.connection().response());
|
flow.expectSuccessfulRedirect(submit.connection().response());
|
||||||
|
|
||||||
assertThat(flow.getCode(), is(notNullValue()));
|
assertThat(flow.getCode(), is(notNullValue()));
|
||||||
assertThat(flow.getAccessToken(), is(nullValue()));
|
assertThat(flow.getAccessToken(), is(nullValue()));
|
||||||
@@ -61,37 +61,41 @@ public class AuthCodeGrantTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void authCodeGrant_invalidResponseType() throws IOException {
|
public void authCodeGrant_invalidResponseType() throws IOException {
|
||||||
AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT);
|
AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT)
|
||||||
Connection.Response response = flow.startExpectError(Map.of("response_type", ""));
|
.param("response_type", "");
|
||||||
Map<String, String> query = flow.parseAndValidateRedirectError(response);
|
Map<String, String> query = flow.start().expectErrorRedirect();
|
||||||
assertThat(query.get("error"), is(ErrorResponse.Error.unsupported_response_type.name()));
|
assertThat(query.get("error"), is(ErrorResponse.Error.unsupported_response_type.name()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void implicitGrant() throws IOException {
|
public void implicitGrant() throws IOException {
|
||||||
AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT);
|
AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT)
|
||||||
LoginScreen loginScreen = flow.start(Map.of("response_type", "token", "scope", "scope1 scope2"));
|
.param("response_type", "token")
|
||||||
|
.scope("scope1 scope2");
|
||||||
|
LoginScreen loginScreen = flow.start().expectLogin();
|
||||||
|
|
||||||
ConsentScreen consentScreen = loginScreen.submit("bob", "password").expectSuccess();
|
ConsentScreen consentScreen = loginScreen.submit("bob", "password").expectSuccess();
|
||||||
assertThat(consentScreen.getScopes(), is(List.of("scope1", "scope2")));
|
assertThat(consentScreen.getScopes(), is(List.of("scope1", "scope2")));
|
||||||
|
|
||||||
Document submit = consentScreen.submit();
|
Document submit = consentScreen.submit();
|
||||||
flow.parseAndValidateRedirect(submit.connection().response());
|
flow.expectSuccessfulRedirect(submit.connection().response());
|
||||||
|
|
||||||
assertThat(flow.getAccessToken(), is(notNullValue()));
|
assertThat(flow.getAccessToken(), is(notNullValue()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void authCodeGrantWithPkce() throws IOException {
|
public void authCodeGrantWithPkce() throws IOException {
|
||||||
AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT);
|
AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT)
|
||||||
flow.setPkce("PnRLncOTibrwxaBmBYm4QC89u0m4mz518sk1WFKjxnc", "bbb");
|
.pkce("PnRLncOTibrwxaBmBYm4QC89u0m4mz518sk1WFKjxnc", "bbb")
|
||||||
LoginScreen loginScreen = flow.start(Map.of("scope", "scope1 scope2"));
|
.scope("scope1 scope2");
|
||||||
|
|
||||||
|
LoginScreen loginScreen = flow.start().expectLogin();
|
||||||
|
|
||||||
ConsentScreen consentScreen = loginScreen.submit("bob", "password").expectSuccess();
|
ConsentScreen consentScreen = loginScreen.submit("bob", "password").expectSuccess();
|
||||||
assertThat(consentScreen.getScopes(), is(List.of("scope1", "scope2")));
|
assertThat(consentScreen.getScopes(), is(List.of("scope1", "scope2")));
|
||||||
|
|
||||||
Document submit = consentScreen.submit();
|
Document submit = consentScreen.submit();
|
||||||
flow.parseAndValidateRedirect(submit.connection().response());
|
flow.expectSuccessfulRedirect(submit.connection().response());
|
||||||
|
|
||||||
assertThat(flow.getCode(), is(notNullValue()));
|
assertThat(flow.getCode(), is(notNullValue()));
|
||||||
assertThat(flow.getAccessToken(), is(nullValue()));
|
assertThat(flow.getAccessToken(), is(nullValue()));
|
||||||
|
|||||||
@@ -28,66 +28,56 @@ public class AuthorizationCodeFlow {
|
|||||||
private String accessToken;
|
private String accessToken;
|
||||||
private String idToken;
|
private String idToken;
|
||||||
|
|
||||||
|
private Map<String, String> query;
|
||||||
|
|
||||||
public AuthorizationCodeFlow(String authUrl, OAuthClient client) {
|
public AuthorizationCodeFlow(String authUrl, OAuthClient client) {
|
||||||
this.authUrl = authUrl;
|
this.authUrl = authUrl;
|
||||||
this.client = client;
|
this.client = client;
|
||||||
|
|
||||||
|
query = new HashMap<>();
|
||||||
|
query.put("client_id", client.clientId());
|
||||||
|
query.put("redirect_uri", client.redirectUri());
|
||||||
|
query.put("state", state);
|
||||||
}
|
}
|
||||||
|
|
||||||
public LoginScreen start(Map<String, String> additionalData) throws IOException {
|
public AuthorizationCodeFlow param(String key, String value) {
|
||||||
var data = defaultQuery();
|
query.put(key, value);
|
||||||
if (additionalData != null) {
|
return this;
|
||||||
data.putAll(additionalData);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Document login = Jsoup.connect(authUrl)
|
public Result start() throws IOException {
|
||||||
.data(data)
|
Document document = Jsoup.connect(authUrl)
|
||||||
|
.followRedirects(false)
|
||||||
|
.data(query)
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
return new LoginScreen(login);
|
return new Result() {
|
||||||
|
@Override
|
||||||
|
public LoginScreen expectLogin() {
|
||||||
|
return new LoginScreen(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, String> expectErrorRedirect() {
|
||||||
|
var response = document.connection().response();
|
||||||
|
|
||||||
|
Map<String, String> query = expectRedirect(response);
|
||||||
|
assertThat(query.get("error"), is(notNullValue()));
|
||||||
|
assertThat(query.get("error_description"), is(notNullValue()));
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public Connection.Response startExpectError(Map<String, String> additionalData) throws IOException {
|
public void expectSuccessfulRedirect(Connection.Response response) {
|
||||||
var data = defaultQuery();
|
Map<String, String> query = expectRedirect(response);
|
||||||
if (additionalData != null) {
|
|
||||||
data.putAll(additionalData);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Jsoup.connect(authUrl)
|
|
||||||
.followRedirects(false)
|
|
||||||
.data(data)
|
|
||||||
.get()
|
|
||||||
.connection()
|
|
||||||
.response();
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, String> defaultQuery() {
|
|
||||||
var map = new HashMap<String, String>();
|
|
||||||
map.put("client_id", client.clientId());
|
|
||||||
map.put("redirect_uri", client.redirectUri());
|
|
||||||
map.put("state", state);
|
|
||||||
if (codeChallenge != null) {
|
|
||||||
map.put("code_challenge", codeChallenge);
|
|
||||||
map.put("code_challenge_method", "S256");
|
|
||||||
}
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void parseAndValidateRedirect(Connection.Response response) {
|
|
||||||
assertThat(response.statusCode(), is(303));
|
|
||||||
assertThat(response.header("location"), startsWith(client.redirectUri()));
|
|
||||||
|
|
||||||
URI location = URI.create(Objects.requireNonNull(response.header("location")));
|
|
||||||
Map<String, String> query = URLEncodedUtils.parse(location.getQuery(), Charset.defaultCharset())
|
|
||||||
.stream().collect(Collectors.toMap(NameValuePair::getName, NameValuePair::getValue));
|
|
||||||
|
|
||||||
assertThat(query.get("state"), is(state));
|
|
||||||
|
|
||||||
code = query.get("code");
|
code = query.get("code");
|
||||||
accessToken = query.get("access_token");
|
accessToken = query.get("access_token");
|
||||||
idToken = query.get("id_token");
|
idToken = query.get("id_token");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, String> parseAndValidateRedirectError(Connection.Response response) {
|
private Map<String, String> expectRedirect(Connection.Response response) {
|
||||||
assertThat(response.statusCode(), is(303));
|
assertThat(response.statusCode(), is(303));
|
||||||
assertThat(response.header("location"), startsWith(client.redirectUri()));
|
assertThat(response.header("location"), startsWith(client.redirectUri()));
|
||||||
|
|
||||||
@@ -96,8 +86,6 @@ public class AuthorizationCodeFlow {
|
|||||||
.stream().collect(Collectors.toMap(NameValuePair::getName, NameValuePair::getValue));
|
.stream().collect(Collectors.toMap(NameValuePair::getName, NameValuePair::getValue));
|
||||||
|
|
||||||
assertThat(query.get("state"), is(state));
|
assertThat(query.get("state"), is(state));
|
||||||
assertThat(query.get("error"), is(notNullValue()));
|
|
||||||
assertThat(query.get("error_description"), is(notNullValue()));
|
|
||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,8 +109,20 @@ public class AuthorizationCodeFlow {
|
|||||||
return idToken;
|
return idToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPkce(String codeChallenge, String codeVerifier) {
|
public AuthorizationCodeFlow pkce(String codeChallenge, String codeVerifier) {
|
||||||
this.codeChallenge = codeChallenge;
|
query.put("code_challenge", codeChallenge);
|
||||||
|
query.put("code_challenge_method", "S256");
|
||||||
this.codeVerifier = codeVerifier;
|
this.codeVerifier = codeVerifier;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AuthorizationCodeFlow scope(String scope) {
|
||||||
|
return param("scope", scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface Result {
|
||||||
|
LoginScreen expectLogin();
|
||||||
|
|
||||||
|
Map<String, String> expectErrorRedirect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user