Merge pull request #2815 from abstractj/AuthorizationCodeTest

Migration of AuthorizationCodeTest
This commit is contained in:
Stian Thorgersen 2016-05-11 14:09:13 +02:00
commit a09f38589f
4 changed files with 80 additions and 57 deletions

View file

@ -26,6 +26,7 @@ import org.keycloak.models.RealmModel;
import org.keycloak.models.UserSessionModel; import org.keycloak.models.UserSessionModel;
import org.keycloak.models.utils.ModelToRepresentation; import org.keycloak.models.utils.ModelToRepresentation;
import org.keycloak.representations.idm.EventRepresentation; import org.keycloak.representations.idm.EventRepresentation;
import org.keycloak.services.managers.ClientSessionCode;
import org.keycloak.services.managers.RealmManager; import org.keycloak.services.managers.RealmManager;
import org.keycloak.services.resource.RealmResourceProvider; import org.keycloak.services.resource.RealmResourceProvider;
import org.keycloak.testsuite.events.EventsListenerProvider; import org.keycloak.testsuite.events.EventsListenerProvider;
@ -165,4 +166,22 @@ public class TestingResourceProvider implements RealmResourceProvider {
public void close() { public void close() {
} }
/*
* Migration from KeycloakRule#verifyCode
*/
@GET
@Path("/verify-code")
@Produces(MediaType.APPLICATION_JSON)
public String verifyCode(@QueryParam("realm") String realmName, @QueryParam("code") String code) {
RealmModel realm = session.realms().getRealm(realmName);
try {
ClientSessionCode accessCode = ClientSessionCode.parse(code, session, realm);
if (accessCode == null) {
throw new AssertionError("Invalid code");
}
return accessCode.getClientSession().getId();
} catch (Throwable t) {
throw new AssertionError("Failed to parse code", t);
}
}
} }

View file

@ -80,4 +80,8 @@ public interface TestingResource {
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
boolean isCached(@PathParam("cache") String cacheName, @PathParam("id") String id); boolean isCached(@PathParam("cache") String cacheName, @PathParam("id") String id);
@GET
@Path("/verify-code")
@Produces(MediaType.APPLICATION_JSON)
String verifyCode(@QueryParam("realm") String realmName, @QueryParam("code") String code);
} }

View file

@ -16,71 +16,67 @@
*/ */
package org.keycloak.testsuite.oauth; package org.keycloak.testsuite.oauth;
import org.jboss.arquillian.graphene.page.Page;
import org.junit.Assert; import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.keycloak.OAuth2Constants; import org.keycloak.OAuth2Constants;
import org.keycloak.events.Details; import org.keycloak.events.Details;
import org.keycloak.events.Errors; import org.keycloak.events.Errors;
import org.keycloak.models.Constants; import org.keycloak.models.Constants;
import org.keycloak.models.RealmModel; import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.services.managers.ClientSessionCode; import org.keycloak.testsuite.AbstractKeycloakTest;
import org.keycloak.services.managers.RealmManager;
import org.keycloak.testsuite.AssertEvents; import org.keycloak.testsuite.AssertEvents;
import org.keycloak.testsuite.OAuthClient;
import org.keycloak.testsuite.OAuthClient.AuthorizationCodeResponse;
import org.keycloak.testsuite.pages.ErrorPage; import org.keycloak.testsuite.pages.ErrorPage;
import org.keycloak.testsuite.pages.LoginPage; import org.keycloak.testsuite.util.ClientManager;
import org.keycloak.testsuite.rule.KeycloakRule; import org.keycloak.testsuite.util.OAuthClient;
import org.keycloak.testsuite.rule.WebResource;
import org.keycloak.testsuite.rule.WebRule;
import org.openqa.selenium.By; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriBuilder;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.keycloak.testsuite.admin.AbstractAdminTest.loadJson;
/** /**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/ */
public class AuthorizationCodeTest { public class AuthorizationCodeTest extends AbstractKeycloakTest {
@ClassRule
public static KeycloakRule keycloakRule = new KeycloakRule();
@Rule @Rule
public WebRule webRule = new WebRule(this); public AssertEvents events = new AssertEvents(this);
@WebResource @Page
protected WebDriver driver;
@WebResource
protected OAuthClient oauth;
@WebResource
protected LoginPage loginPage;
@WebResource
protected ErrorPage errorPage; protected ErrorPage errorPage;
@Rule
public AssertEvents events = new AssertEvents(keycloakRule); @Override
public void beforeAbstractKeycloakTest() throws Exception {
super.beforeAbstractKeycloakTest();
}
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
RealmRepresentation realmRepresentation = loadJson(getClass().getResourceAsStream("/testrealm.json"), RealmRepresentation.class);
testRealms.add(realmRepresentation);
}
@Test @Test
public void authorizationRequest() throws IOException { public void authorizationRequest() throws IOException {
oauth.state("mystate"); oauth.state("mystate");
AuthorizationCodeResponse response = oauth.doLogin("test-user@localhost", "password"); OAuthClient.AuthorizationCodeResponse response = oauth.doLogin("test-user@localhost", "password");
Assert.assertTrue(response.isRedirected()); Assert.assertTrue(response.isRedirected());
Assert.assertNotNull(response.getCode()); Assert.assertNotNull(response.getCode());
assertEquals("mystate", response.getState()); assertEquals("mystate", response.getState());
Assert.assertNull(response.getError()); Assert.assertNull(response.getError());
keycloakRule.verifyCode(response.getCode()); testingClient.testing().verifyCode("test", response.getCode());
String codeId = events.expectLogin().assertEvent().getDetails().get(Details.CODE_ID); String codeId = events.expectLogin().assertEvent().getDetails().get(Details.CODE_ID);
assertCode(codeId, response.getCode()); assertCode(codeId, response.getCode());
@ -88,12 +84,7 @@ public class AuthorizationCodeTest {
@Test @Test
public void authorizationRequestInstalledApp() throws IOException { public void authorizationRequestInstalledApp() throws IOException {
keycloakRule.update(new KeycloakRule.KeycloakSetup() { ClientManager.realm(adminClient.realm("test")).clientId("test-app").addRedirectUris(Constants.INSTALLED_APP_URN);
@Override
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
appRealm.getClientByClientId("test-app").addRedirectUri(Constants.INSTALLED_APP_URN);
}
});
oauth.redirectUri(Constants.INSTALLED_APP_URN); oauth.redirectUri(Constants.INSTALLED_APP_URN);
oauth.doLogin("test-user@localhost", "password"); oauth.doLogin("test-user@localhost", "password");
@ -102,36 +93,26 @@ public class AuthorizationCodeTest {
Assert.assertEquals("Success code", title); Assert.assertEquals("Success code", title);
String code = driver.findElement(By.id(OAuth2Constants.CODE)).getAttribute("value"); String code = driver.findElement(By.id(OAuth2Constants.CODE)).getAttribute("value");
keycloakRule.verifyCode(code); testingClient.testing().verifyCode("test", code);
String codeId = events.expectLogin().detail(Details.REDIRECT_URI, "http://localhost:8081/auth/realms/test/protocol/openid-connect/oauth/oob").assertEvent().getDetails().get(Details.CODE_ID); String codeId = events.expectLogin().detail(Details.REDIRECT_URI, "http://localhost:8180/auth/realms/test/protocol/openid-connect/oauth/oob").assertEvent().getDetails().get(Details.CODE_ID);
assertCode(codeId, code); assertCode(codeId, code);
keycloakRule.update(new KeycloakRule.KeycloakSetup() { ClientManager.realm(adminClient.realm("test")).clientId("test-app").removeRedirectUris(Constants.INSTALLED_APP_URN);
@Override
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
appRealm.getClientByClientId("test-app").removeRedirectUri(Constants.INSTALLED_APP_URN);
}
});
} }
@Test @Test
public void authorizationValidRedirectUri() throws IOException { public void authorizationValidRedirectUri() throws IOException {
keycloakRule.update(new KeycloakRule.KeycloakSetup() { ClientManager.realm(adminClient.realm("test")).clientId("test-app").addRedirectUris(oauth.getRedirectUri());
@Override
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
appRealm.getClientByClientId("test-app").addRedirectUri(oauth.getRedirectUri());
}
});
oauth.state("mystate"); oauth.state("mystate");
AuthorizationCodeResponse response = oauth.doLogin("test-user@localhost", "password"); OAuthClient.AuthorizationCodeResponse response = oauth.doLogin("test-user@localhost", "password");
Assert.assertTrue(response.isRedirected()); Assert.assertTrue(response.isRedirected());
Assert.assertNotNull(response.getCode()); Assert.assertNotNull(response.getCode());
keycloakRule.verifyCode(response.getCode()); testingClient.testing().verifyCode("test", response.getCode());
String codeId = events.expectLogin().assertEvent().getDetails().get(Details.CODE_ID); String codeId = events.expectLogin().assertEvent().getDetails().get(Details.CODE_ID);
assertCode(codeId, response.getCode()); assertCode(codeId, response.getCode());
@ -141,14 +122,14 @@ public class AuthorizationCodeTest {
public void authorizationRequestNoState() throws IOException { public void authorizationRequestNoState() throws IOException {
oauth.state(null); oauth.state(null);
AuthorizationCodeResponse response = oauth.doLogin("test-user@localhost", "password"); OAuthClient.AuthorizationCodeResponse response = oauth.doLogin("test-user@localhost", "password");
Assert.assertTrue(response.isRedirected()); Assert.assertTrue(response.isRedirected());
Assert.assertNotNull(response.getCode()); Assert.assertNotNull(response.getCode());
Assert.assertNull(response.getState()); Assert.assertNull(response.getState());
Assert.assertNull(response.getError()); Assert.assertNull(response.getError());
keycloakRule.verifyCode(response.getCode()); testingClient.testing().verifyCode("test", response.getCode());
String codeId = events.expectLogin().assertEvent().getDetails().get(Details.CODE_ID); String codeId = events.expectLogin().assertEvent().getDetails().get(Details.CODE_ID);
assertCode(codeId, response.getCode()); assertCode(codeId, response.getCode());
@ -173,8 +154,8 @@ public class AuthorizationCodeTest {
} }
private void assertCode(String expectedCodeId, String actualCode) { private void assertCode(String expectedCodeId, String actualCode) {
ClientSessionCode code = keycloakRule.verifyCode(actualCode); String code = testingClient.testing().verifyCode("test", actualCode);
assertEquals(expectedCodeId, code.getClientSession().getId()); assertEquals(expectedCodeId, code);
} }
} }

View file

@ -6,8 +6,10 @@ import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.ProtocolMapperRepresentation; import org.keycloak.representations.idm.ProtocolMapperRepresentation;
import org.keycloak.representations.idm.RoleRepresentation; import org.keycloak.representations.idm.RoleRepresentation;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedList;
import static org.keycloak.testsuite.admin.ApiUtil.findClientByClientId; import static org.keycloak.testsuite.admin.ApiUtil.findClientByClientId;
import static org.keycloak.testsuite.admin.ApiUtil.findProtocolMapperByName; import static org.keycloak.testsuite.admin.ApiUtil.findProtocolMapperByName;
@ -96,5 +98,22 @@ public class ClientManager {
public void removeScopeMapping(RoleRepresentation newRole) { public void removeScopeMapping(RoleRepresentation newRole) {
clientResource.getScopeMappings().realmLevel().remove(Collections.singletonList(newRole)); clientResource.getScopeMappings().realmLevel().remove(Collections.singletonList(newRole));
} }
public void addRedirectUris(String... redirectUris) {
ClientRepresentation app = clientResource.toRepresentation();
if (app.getRedirectUris() == null) {
app.setRedirectUris(new LinkedList<String>());
}
app.setRedirectUris(Arrays.asList(redirectUris));
clientResource.update(app);
}
public void removeRedirectUris(String... redirectUris) {
ClientRepresentation app = clientResource.toRepresentation();
for (String redirectUri : redirectUris) {
app.getRedirectUris().remove(redirectUri);
}
clientResource.update(app);
}
} }
} }