mirror of
https://github.com/ysoftdevs/oauth-playground-server.git
synced 2026-07-04 03:51:55 +02:00
refactor tests
This commit is contained in:
@@ -46,7 +46,7 @@ public class AuthCodeGrantTest {
|
|||||||
AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT);
|
AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT);
|
||||||
LoginScreen loginScreen = flow.start(Map.of("scope", "scope1 scope2"));
|
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")));
|
assertThat(consentScreen.getScopes(), is(List.of("scope1", "scope2")));
|
||||||
|
|
||||||
Document submit = consentScreen.submit();
|
Document submit = consentScreen.submit();
|
||||||
@@ -72,7 +72,7 @@ public class AuthCodeGrantTest {
|
|||||||
AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT);
|
AuthorizationCodeFlow flow = new AuthorizationCodeFlow(authUrl, CLIENT);
|
||||||
LoginScreen loginScreen = flow.start(Map.of("response_type", "token", "scope", "scope1 scope2"));
|
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")));
|
assertThat(consentScreen.getScopes(), is(List.of("scope1", "scope2")));
|
||||||
|
|
||||||
Document submit = consentScreen.submit();
|
Document submit = consentScreen.submit();
|
||||||
@@ -87,7 +87,7 @@ public class AuthCodeGrantTest {
|
|||||||
flow.setPkce("PnRLncOTibrwxaBmBYm4QC89u0m4mz518sk1WFKjxnc", "bbb");
|
flow.setPkce("PnRLncOTibrwxaBmBYm4QC89u0m4mz518sk1WFKjxnc", "bbb");
|
||||||
LoginScreen loginScreen = flow.start(Map.of("scope", "scope1 scope2"));
|
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")));
|
assertThat(consentScreen.getScopes(), is(List.of("scope1", "scope2")));
|
||||||
|
|
||||||
Document submit = consentScreen.submit();
|
Document submit = consentScreen.submit();
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ public class DeviceAuthGrantTest {
|
|||||||
DeviceCodeScreen deviceCodeScreen = new DeviceCodeScreen(deviceResponse.verificationUri());
|
DeviceCodeScreen deviceCodeScreen = new DeviceCodeScreen(deviceResponse.verificationUri());
|
||||||
LoginScreen loginScreen = deviceCodeScreen.enterCode(deviceResponse.userCode());
|
LoginScreen loginScreen = deviceCodeScreen.enterCode(deviceResponse.userCode());
|
||||||
|
|
||||||
ConsentScreen consentScreen = loginScreen.submitCorrect("bob", "password");
|
ConsentScreen consentScreen = loginScreen.submit("bob", "password").expectSuccess();
|
||||||
consentScreen.submit();
|
consentScreen.submit();
|
||||||
|
|
||||||
AccessTokenResponse accessTokenResponse = flow.exchangeDeviceCode().expectTokens();
|
AccessTokenResponse accessTokenResponse = flow.exchangeDeviceCode().expectTokens();
|
||||||
|
|||||||
@@ -4,6 +4,10 @@ import org.jsoup.nodes.Document;
|
|||||||
import org.jsoup.nodes.FormElement;
|
import org.jsoup.nodes.FormElement;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.containsString;
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
|
||||||
public class LoginScreen {
|
public class LoginScreen {
|
||||||
|
|
||||||
@@ -13,20 +17,32 @@ public class LoginScreen {
|
|||||||
this.form = doc.expectForm("form");
|
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", "username").val(username);
|
||||||
form.getElementsByAttributeValue("name", "password").val(password);
|
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 {
|
private LoginScreen expectError(String error) {
|
||||||
Document posted = submit(username, password);
|
assertThat(Objects.requireNonNull(form.getElementById("error-popup")).text(), containsString(error));
|
||||||
return new ConsentScreen(posted);
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LoginScreen submitWrong(String username, String password) throws IOException {
|
public interface Result {
|
||||||
Document posted = submit(username, password);
|
ConsentScreen expectSuccess();
|
||||||
return new LoginScreen(posted);
|
|
||||||
|
LoginScreen expectError(String error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user