Search in sources :

Example 1 with TokenUtil

use of org.keycloak.testsuite.util.TokenUtil in project keycloak by keycloak.

the class AccountRestServiceTest method getConsentForNotExistingClient.

@Test
public void getConsentForNotExistingClient() throws IOException {
    TokenUtil token = new TokenUtil("view-consent-access", "password");
    String appId = "not-existing";
    SimpleHttp.Response response = SimpleHttp.doGet(getAccountUrl("applications/" + appId + "/consent"), httpClient).header("Accept", "application/json").auth(token.getToken()).asResponse();
    assertEquals(404, response.getStatus());
}
Also used : SimpleHttp(org.keycloak.broker.provider.util.SimpleHttp) TokenUtil(org.keycloak.testsuite.util.TokenUtil) Test(org.junit.Test) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)

Example 2 with TokenUtil

use of org.keycloak.testsuite.util.TokenUtil in project keycloak by keycloak.

the class AccountRestServiceTest method deleteConsentForNotExistingClient.

@Test
public void deleteConsentForNotExistingClient() throws IOException {
    TokenUtil token = new TokenUtil("manage-consent-access", "password");
    String appId = "not-existing";
    SimpleHttp.Response response = SimpleHttp.doDelete(getAccountUrl("applications/" + appId + "/consent"), httpClient).header("Accept", "application/json").auth(token.getToken()).asResponse();
    assertEquals(404, response.getStatus());
}
Also used : SimpleHttp(org.keycloak.broker.provider.util.SimpleHttp) TokenUtil(org.keycloak.testsuite.util.TokenUtil) Test(org.junit.Test) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)

Example 3 with TokenUtil

use of org.keycloak.testsuite.util.TokenUtil in project keycloak by keycloak.

the class SessionRestServiceTest method testLogout.

@Test
public void testLogout() throws IOException {
    TokenUtil viewToken = new TokenUtil("view-account-access", "password");
    String sessionId = oauth.doLogin("view-account-access", "password").getSessionState();
    List<SessionRepresentation> sessions = getSessions(viewToken.getToken());
    assertEquals(2, sessions.size());
    // With `ViewToken` you can only read
    int status = SimpleHttp.doDelete(getAccountUrl("sessions/" + sessionId), httpClient).acceptJson().auth(viewToken.getToken()).asStatus();
    assertEquals(403, status);
    sessions = getSessions(viewToken.getToken());
    assertEquals(2, sessions.size());
    // Here you can delete the session
    status = SimpleHttp.doDelete(getAccountUrl("sessions/" + sessionId), httpClient).acceptJson().auth(tokenUtil.getToken()).asStatus();
    assertEquals(204, status);
    sessions = getSessions(tokenUtil.getToken());
    assertEquals(1, sessions.size());
}
Also used : TokenUtil(org.keycloak.testsuite.util.TokenUtil) SessionRepresentation(org.keycloak.representations.account.SessionRepresentation) Test(org.junit.Test)

Example 4 with TokenUtil

use of org.keycloak.testsuite.util.TokenUtil in project keycloak by keycloak.

the class AccountRestServiceTest method updateConsentForClient.

@Test
public void updateConsentForClient() throws IOException {
    TokenUtil token = new TokenUtil("manage-consent-access", "password");
    String appId = "security-admin-console";
    ClientScopeRepresentation clientScopeRepresentation = testRealm().clientScopes().findAll().get(0);
    ConsentScopeRepresentation consentScopeRepresentation = new ConsentScopeRepresentation();
    consentScopeRepresentation.setId(clientScopeRepresentation.getId());
    ConsentRepresentation requestedConsent = new ConsentRepresentation();
    requestedConsent.setGrantedScopes(Collections.singletonList(consentScopeRepresentation));
    ConsentRepresentation consentRepresentation = SimpleHttp.doPost(getAccountUrl("applications/" + appId + "/consent"), httpClient).header("Accept", "application/json").json(requestedConsent).auth(token.getToken()).asJson(ConsentRepresentation.class);
    assertTrue(consentRepresentation.getCreatedDate() > 0);
    assertTrue(consentRepresentation.getLastUpdatedDate() > 0);
    assertEquals(1, consentRepresentation.getGrantedScopes().size());
    assertEquals(consentScopeRepresentation.getId(), consentRepresentation.getGrantedScopes().get(0).getId());
    clientScopeRepresentation = testRealm().clientScopes().findAll().get(1);
    consentScopeRepresentation = new ConsentScopeRepresentation();
    consentScopeRepresentation.setId(clientScopeRepresentation.getId());
    requestedConsent = new ConsentRepresentation();
    requestedConsent.setGrantedScopes(Collections.singletonList(consentScopeRepresentation));
    ConsentRepresentation consentRepresentation2 = SimpleHttp.doPost(getAccountUrl("applications/" + appId + "/consent"), httpClient).header("Accept", "application/json").json(requestedConsent).auth(token.getToken()).asJson(ConsentRepresentation.class);
    assertTrue(consentRepresentation2.getCreatedDate() > 0);
    assertEquals(consentRepresentation.getCreatedDate(), consentRepresentation2.getCreatedDate());
    assertTrue(consentRepresentation2.getLastUpdatedDate() > 0);
    assertTrue(consentRepresentation2.getLastUpdatedDate() > consentRepresentation.getLastUpdatedDate());
    assertEquals(1, consentRepresentation2.getGrantedScopes().size());
    assertEquals(consentScopeRepresentation.getId(), consentRepresentation2.getGrantedScopes().get(0).getId());
}
Also used : ConsentRepresentation(org.keycloak.representations.account.ConsentRepresentation) ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) ConsentScopeRepresentation(org.keycloak.representations.account.ConsentScopeRepresentation) TokenUtil(org.keycloak.testsuite.util.TokenUtil) Test(org.junit.Test) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)

Example 5 with TokenUtil

use of org.keycloak.testsuite.util.TokenUtil in project keycloak by keycloak.

the class AccountRestServiceTest method getConsentWithoutPermission.

@Test
public void getConsentWithoutPermission() throws IOException {
    TokenUtil token = new TokenUtil("no-account-access", "password");
    String appId = "security-admin-console";
    SimpleHttp.Response response = SimpleHttp.doGet(getAccountUrl("applications/" + appId + "/consent"), httpClient).header("Accept", "application/json").auth(token.getToken()).asResponse();
    assertEquals(403, response.getStatus());
}
Also used : SimpleHttp(org.keycloak.broker.provider.util.SimpleHttp) TokenUtil(org.keycloak.testsuite.util.TokenUtil) Test(org.junit.Test) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)

Aggregations

TokenUtil (org.keycloak.testsuite.util.TokenUtil)29 Test (org.junit.Test)28 AbstractAuthenticationTest (org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)25 SimpleHttp (org.keycloak.broker.provider.util.SimpleHttp)18 ConsentRepresentation (org.keycloak.representations.account.ConsentRepresentation)16 ConsentScopeRepresentation (org.keycloak.representations.account.ConsentScopeRepresentation)16 ClientScopeRepresentation (org.keycloak.representations.idm.ClientScopeRepresentation)16 SessionRepresentation (org.keycloak.representations.account.SessionRepresentation)8 TypeReference (com.fasterxml.jackson.core.type.TypeReference)7 List (java.util.List)7 Response (javax.ws.rs.core.Response)7 IOException (java.io.IOException)6 Collections (java.util.Collections)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)6 Assert (org.junit.Assert)6 Assert.assertEquals (org.junit.Assert.assertEquals)6 Assert.assertFalse (org.junit.Assert.assertFalse)6