mirror of
https://github.com/ysoftdevs/oauth-playground-server.git
synced 2026-03-21 00:29:36 +01:00
refactor tests
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user