refactor tests

This commit is contained in:
Dusan Jakub
2023-09-19 22:02:23 +02:00
parent 8ec906c0b8
commit 507242b4ca
3 changed files with 28 additions and 12 deletions

View File

@@ -46,7 +46,7 @@ public class AuthCodeGrantTest {
AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT);
LoginScreen loginScreen = flow.start(Map.of("scope", "scope1 scope2"));
ConsentScreen consentScreen = loginScreen.submitCorrect("bob", "password");
ConsentScreen consentScreen = loginScreen.submit("bob", "password").expectSuccess();
assertThat(consentScreen.getScopes(), is(List.of("scope1", "scope2")));
Document submit = consentScreen.submit();
@@ -72,7 +72,7 @@ public class AuthCodeGrantTest {
AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT);
LoginScreen loginScreen = flow.start(Map.of("response_type", "token", "scope", "scope1 scope2"));
ConsentScreen consentScreen = loginScreen.submitCorrect("bob", "password");
ConsentScreen consentScreen = loginScreen.submit("bob", "password").expectSuccess();
assertThat(consentScreen.getScopes(), is(List.of("scope1", "scope2")));
Document submit = consentScreen.submit();
@@ -87,7 +87,7 @@ public class AuthCodeGrantTest {
flow.setPkce("PnRLncOTibrwxaBmBYm4QC89u0m4mz518sk1WFKjxnc", "bbb");
LoginScreen loginScreen = flow.start(Map.of("scope", "scope1 scope2"));
ConsentScreen consentScreen = loginScreen.submitCorrect("bob", "password");
ConsentScreen consentScreen = loginScreen.submit("bob", "password").expectSuccess();
assertThat(consentScreen.getScopes(), is(List.of("scope1", "scope2")));
Document submit = consentScreen.submit();

View File

@@ -56,7 +56,7 @@ public class DeviceAuthGrantTest {
DeviceCodeScreen deviceCodeScreen = new DeviceCodeScreen(deviceResponse.verificationUri());
LoginScreen loginScreen = deviceCodeScreen.enterCode(deviceResponse.userCode());
ConsentScreen consentScreen = loginScreen.submitCorrect("bob", "password");
ConsentScreen consentScreen = loginScreen.submit("bob", "password").expectSuccess();
consentScreen.submit();
AccessTokenResponse accessTokenResponse = flow.exchangeDeviceCode().expectTokens();

View File

@@ -4,6 +4,10 @@ import org.jsoup.nodes.Document;
import org.jsoup.nodes.FormElement;
import java.io.IOException;
import java.util.Objects;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
public class LoginScreen {
@@ -13,20 +17,32 @@ public class LoginScreen {
this.form = doc.expectForm("form");
}
public Document submit(String username, String password) throws IOException {
public Result submit(String username, String password) throws IOException {
form.getElementsByAttributeValue("name", "username").val(username);
form.getElementsByAttributeValue("name", "password").val(password);
return form.submit().post();
var document = form.submit().post();
return new Result() {
@Override
public ConsentScreen expectSuccess() {
return new ConsentScreen(document);
}
@Override
public LoginScreen expectError(String error) {
return new LoginScreen(document).expectError(error);
}
};
}
public ConsentScreen submitCorrect(String username, String password) throws IOException {
Document posted = submit(username, password);
return new ConsentScreen(posted);
private LoginScreen expectError(String error) {
assertThat(Objects.requireNonNull(form.getElementById("error-popup")).text(), containsString(error));
return this;
}
public LoginScreen submitWrong(String username, String password) throws IOException {
Document posted = submit(username, password);
return new LoginScreen(posted);
public interface Result {
ConsentScreen expectSuccess();
LoginScreen expectError(String error);
}
}