From 24159f7dcb24f4e3ac698a09a140d11c0e75022b Mon Sep 17 00:00:00 2001 From: Dusan Jakub Date: Tue, 19 Sep 2023 22:18:08 +0200 Subject: [PATCH] refactor tests --- .../com/ysoft/geecon/AuthCodeGrantTest.java | 32 ++++--- .../geecon/helpers/AuthorizationCodeFlow.java | 96 +++++++++---------- 2 files changed, 66 insertions(+), 62 deletions(-) diff --git a/src/test/java/com/ysoft/geecon/AuthCodeGrantTest.java b/src/test/java/com/ysoft/geecon/AuthCodeGrantTest.java index cb68236..478f3b0 100644 --- a/src/test/java/com/ysoft/geecon/AuthCodeGrantTest.java +++ b/src/test/java/com/ysoft/geecon/AuthCodeGrantTest.java @@ -12,7 +12,6 @@ import com.ysoft.geecon.repo.UsersRepo; import io.quarkus.test.common.http.TestHTTPResource; import io.quarkus.test.junit.QuarkusTest; import jakarta.inject.Inject; -import org.jsoup.Connection; import org.jsoup.nodes.Document; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -43,14 +42,15 @@ public class AuthCodeGrantTest { @Test public void authCodeGrant() throws IOException { - AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT); - LoginScreen loginScreen = flow.start(Map.of("scope", "scope1 scope2")); + AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT) + .scope("scope1 scope2"); + LoginScreen loginScreen = flow.start().expectLogin(); ConsentScreen consentScreen = loginScreen.submit("bob", "password").expectSuccess(); assertThat(consentScreen.getScopes(), is(List.of("scope1", "scope2"))); Document submit = consentScreen.submit(); - flow.parseAndValidateRedirect(submit.connection().response()); + flow.expectSuccessfulRedirect(submit.connection().response()); assertThat(flow.getCode(), is(notNullValue())); assertThat(flow.getAccessToken(), is(nullValue())); @@ -61,37 +61,41 @@ public class AuthCodeGrantTest { @Test public void authCodeGrant_invalidResponseType() throws IOException { - AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT); - Connection.Response response = flow.startExpectError(Map.of("response_type", "")); - Map query = flow.parseAndValidateRedirectError(response); + AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT) + .param("response_type", ""); + Map query = flow.start().expectErrorRedirect(); assertThat(query.get("error"), is(ErrorResponse.Error.unsupported_response_type.name())); } @Test public void implicitGrant() throws IOException { - AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT); - LoginScreen loginScreen = flow.start(Map.of("response_type", "token", "scope", "scope1 scope2")); + AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT) + .param("response_type", "token") + .scope("scope1 scope2"); + LoginScreen loginScreen = flow.start().expectLogin(); ConsentScreen consentScreen = loginScreen.submit("bob", "password").expectSuccess(); assertThat(consentScreen.getScopes(), is(List.of("scope1", "scope2"))); Document submit = consentScreen.submit(); - flow.parseAndValidateRedirect(submit.connection().response()); + flow.expectSuccessfulRedirect(submit.connection().response()); assertThat(flow.getAccessToken(), is(notNullValue())); } @Test public void authCodeGrantWithPkce() throws IOException { - AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT); - flow.setPkce("PnRLncOTibrwxaBmBYm4QC89u0m4mz518sk1WFKjxnc", "bbb"); - LoginScreen loginScreen = flow.start(Map.of("scope", "scope1 scope2")); + AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT) + .pkce("PnRLncOTibrwxaBmBYm4QC89u0m4mz518sk1WFKjxnc", "bbb") + .scope("scope1 scope2"); + + LoginScreen loginScreen = flow.start().expectLogin(); ConsentScreen consentScreen = loginScreen.submit("bob", "password").expectSuccess(); assertThat(consentScreen.getScopes(), is(List.of("scope1", "scope2"))); Document submit = consentScreen.submit(); - flow.parseAndValidateRedirect(submit.connection().response()); + flow.expectSuccessfulRedirect(submit.connection().response()); assertThat(flow.getCode(), is(notNullValue())); assertThat(flow.getAccessToken(), is(nullValue())); diff --git a/src/test/java/com/ysoft/geecon/helpers/AuthorizationCodeFlow.java b/src/test/java/com/ysoft/geecon/helpers/AuthorizationCodeFlow.java index 571a6b3..fa4c324 100644 --- a/src/test/java/com/ysoft/geecon/helpers/AuthorizationCodeFlow.java +++ b/src/test/java/com/ysoft/geecon/helpers/AuthorizationCodeFlow.java @@ -28,66 +28,56 @@ public class AuthorizationCodeFlow { private String accessToken; private String idToken; + private Map query; + public AuthorizationCodeFlow(String authUrl, OAuthClient client) { this.authUrl = authUrl; 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 additionalData) throws IOException { - var data = defaultQuery(); - if (additionalData != null) { - data.putAll(additionalData); - } + public AuthorizationCodeFlow param(String key, String value) { + query.put(key, value); + return this; + } - Document login = Jsoup.connect(authUrl) - .data(data) + public Result start() throws IOException { + Document document = Jsoup.connect(authUrl) + .followRedirects(false) + .data(query) .get(); - return new LoginScreen(login); + return new Result() { + @Override + public LoginScreen expectLogin() { + return new LoginScreen(document); + } + + @Override + public Map expectErrorRedirect() { + var response = document.connection().response(); + + Map query = expectRedirect(response); + assertThat(query.get("error"), is(notNullValue())); + assertThat(query.get("error_description"), is(notNullValue())); + return query; + } + }; } - public Connection.Response startExpectError(Map additionalData) throws IOException { - var data = defaultQuery(); - if (additionalData != null) { - data.putAll(additionalData); - } - - return Jsoup.connect(authUrl) - .followRedirects(false) - .data(data) - .get() - .connection() - .response(); - } - - private Map defaultQuery() { - var map = new HashMap(); - 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 query = URLEncodedUtils.parse(location.getQuery(), Charset.defaultCharset()) - .stream().collect(Collectors.toMap(NameValuePair::getName, NameValuePair::getValue)); - - assertThat(query.get("state"), is(state)); + public void expectSuccessfulRedirect(Connection.Response response) { + Map query = expectRedirect(response); code = query.get("code"); accessToken = query.get("access_token"); idToken = query.get("id_token"); } - public Map parseAndValidateRedirectError(Connection.Response response) { + private Map expectRedirect(Connection.Response response) { assertThat(response.statusCode(), is(303)); assertThat(response.header("location"), startsWith(client.redirectUri())); @@ -96,8 +86,6 @@ public class AuthorizationCodeFlow { .stream().collect(Collectors.toMap(NameValuePair::getName, NameValuePair::getValue)); assertThat(query.get("state"), is(state)); - assertThat(query.get("error"), is(notNullValue())); - assertThat(query.get("error_description"), is(notNullValue())); return query; } @@ -121,8 +109,20 @@ public class AuthorizationCodeFlow { return idToken; } - public void setPkce(String codeChallenge, String codeVerifier) { - this.codeChallenge = codeChallenge; + public AuthorizationCodeFlow pkce(String codeChallenge, String codeVerifier) { + query.put("code_challenge", codeChallenge); + query.put("code_challenge_method", "S256"); this.codeVerifier = codeVerifier; + return this; + } + + public AuthorizationCodeFlow scope(String scope) { + return param("scope", scope); + } + + public interface Result { + LoginScreen expectLogin(); + + Map expectErrorRedirect(); } }