Search in sources :

Example 16 with Connection

use of com.auth0.json.mgmt.Connection in project auth0-java by auth0.

the class CustomRequestTest method shouldParseJSONErrorResponseWithErrorDescription.

@Test
public void shouldParseJSONErrorResponseWithErrorDescription() throws Exception {
    CustomRequest<List> request = new CustomRequest<>(client, server.getBaseUrl(), "GET", listType);
    server.jsonResponse(AUTH_ERROR_WITH_ERROR_DESCRIPTION, 400);
    Exception exception = null;
    try {
        request.execute();
        server.takeRequest();
    } catch (Exception e) {
        exception = e;
    }
    assertThat(exception, is(notNullValue()));
    assertThat(exception, is(instanceOf(APIException.class)));
    assertThat(exception.getCause(), is(nullValue()));
    assertThat(exception.getMessage(), is("Request failed with status code 400: the connection was not found"));
    APIException authException = (APIException) exception;
    assertThat(authException.getDescription(), is("the connection was not found"));
    assertThat(authException.getError(), is("invalid_request"));
    assertThat(authException.getStatusCode(), is(400));
}
Also used : APIException(com.auth0.exception.APIException) List(java.util.List) Auth0Exception(com.auth0.exception.Auth0Exception) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) APIException(com.auth0.exception.APIException) RateLimitException(com.auth0.exception.RateLimitException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ExpectedException(org.junit.rules.ExpectedException)

Example 17 with Connection

use of com.auth0.json.mgmt.Connection in project auth0-java by auth0.

the class JobsEntityTest method shouldRequestUsersImportWithOptions.

@Test
public void shouldRequestUsersImportWithOptions() throws Exception {
    UsersImportOptions options = new UsersImportOptions();
    options.withExternalId("ext_id123");
    options.withUpsert(true);
    options.withSendCompletionEmail(false);
    File usersFile = new File(MGMT_JOB_POST_USERS_IMPORTS_INPUT);
    Request<Job> request = api.jobs().importUsers("con_123456789", usersFile, options);
    assertThat(request, is(notNullValue()));
    server.jsonResponse(MGMT_JOB_POST_USERS_IMPORTS, 200);
    Job response = request.execute();
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest, hasMethodAndPath("POST", "/api/v2/jobs/users-imports"));
    assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
    String ctHeader = recordedRequest.getHeader("Content-Type");
    assertThat(ctHeader, startsWith("multipart/form-data"));
    String[] ctParts = ctHeader.split("multipart/form-data; boundary=");
    RecordedMultipartRequest recordedMultipartRequest = new RecordedMultipartRequest(recordedRequest);
    assertThat(recordedMultipartRequest.getPartsCount(), is(5));
    assertThat(recordedMultipartRequest.getBoundary(), is(notNullValue()));
    assertThat(recordedMultipartRequest.getBoundary(), is(ctParts[1]));
    // Connection ID
    KeyValuePart connectionIdParam = recordedMultipartRequest.getKeyValuePart("connection_id");
    assertThat(connectionIdParam, is(notNullValue()));
    assertThat(connectionIdParam.getValue(), is("con_123456789"));
    // External ID
    KeyValuePart externalIdParam = recordedMultipartRequest.getKeyValuePart("external_id");
    assertThat(externalIdParam, is(notNullValue()));
    assertThat(externalIdParam.getValue(), is("ext_id123"));
    // Upsert
    KeyValuePart upsertParam = recordedMultipartRequest.getKeyValuePart("upsert");
    assertThat(upsertParam, is(notNullValue()));
    assertThat(upsertParam.getValue(), is("true"));
    // Send Completion Email
    KeyValuePart sendCompletionEmailParam = recordedMultipartRequest.getKeyValuePart("send_completion_email");
    assertThat(sendCompletionEmailParam, is(notNullValue()));
    assertThat(sendCompletionEmailParam.getValue(), is("false"));
    // Users JSON
    FilePart jsonFile = recordedMultipartRequest.getFilePart("users");
    assertThat(jsonFile, is(notNullValue()));
    String utf8Contents = new String(Files.readAllBytes(usersFile.toPath()));
    assertThat(jsonFile.getContentType(), is("text/json"));
    assertThat(jsonFile.getFilename(), is("job_post_users_imports_input.json"));
    assertThat(jsonFile.getValue(), is(utf8Contents));
    assertThat(response, is(notNullValue()));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) RecordedMultipartRequest(com.auth0.net.multipart.RecordedMultipartRequest) UsersImportOptions(com.auth0.client.mgmt.filter.UsersImportOptions) Job(com.auth0.json.mgmt.jobs.Job) File(java.io.File) KeyValuePart(com.auth0.net.multipart.KeyValuePart) FilePart(com.auth0.net.multipart.FilePart) Test(org.junit.Test)

Example 18 with Connection

use of com.auth0.json.mgmt.Connection in project auth0-java by auth0.

the class ConnectionsEntityTest method shouldGetConnection.

@Test
public void shouldGetConnection() throws Exception {
    Request<Connection> request = api.connections().get("1", null);
    assertThat(request, is(notNullValue()));
    server.jsonResponse(MGMT_CONNECTION, 200);
    Connection response = request.execute();
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest, hasMethodAndPath("GET", "/api/v2/connections/1"));
    assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
    assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
    assertThat(response, is(notNullValue()));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Connection(com.auth0.json.mgmt.Connection) Test(org.junit.Test)

Example 19 with Connection

use of com.auth0.json.mgmt.Connection in project auth0-java by auth0.

the class ConnectionsEntityTest method shouldListConnectionsWithStrategy.

@Test
public void shouldListConnectionsWithStrategy() throws Exception {
    ConnectionFilter filter = new ConnectionFilter().withStrategy("auth0");
    @SuppressWarnings("deprecation") Request<List<Connection>> request = api.connections().list(filter);
    assertThat(request, is(notNullValue()));
    server.jsonResponse(MGMT_CONNECTIONS_LIST, 200);
    List<Connection> response = request.execute();
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest, hasMethodAndPath("GET", "/api/v2/connections"));
    assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
    assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
    assertThat(recordedRequest, hasQueryParameter("strategy", "auth0"));
    assertThat(response, is(notNullValue()));
    assertThat(response, hasSize(2));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) ConnectionFilter(com.auth0.client.mgmt.filter.ConnectionFilter) Connection(com.auth0.json.mgmt.Connection) List(java.util.List) Test(org.junit.Test)

Example 20 with Connection

use of com.auth0.json.mgmt.Connection in project auth0-java by auth0.

the class ConnectionsEntityTest method shouldCreateConnection.

@Test
public void shouldCreateConnection() throws Exception {
    Request<Connection> request = api.connections().create(new Connection("my-connection", "auth0"));
    assertThat(request, is(notNullValue()));
    server.jsonResponse(MGMT_CONNECTION, 200);
    Connection response = request.execute();
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest, hasMethodAndPath("POST", "/api/v2/connections"));
    assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
    assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
    Map<String, Object> body = bodyFromRequest(recordedRequest);
    assertThat(body.size(), is(2));
    assertThat(body, hasEntry("name", "my-connection"));
    assertThat(body, hasEntry("strategy", "auth0"));
    assertThat(response, is(notNullValue()));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Connection(com.auth0.json.mgmt.Connection) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)13 Connection (com.auth0.json.mgmt.Connection)10 List (java.util.List)9 IOException (java.io.IOException)6 ConnectionFilter (com.auth0.client.mgmt.filter.ConnectionFilter)5 Auth0Exception (com.auth0.exception.Auth0Exception)5 APIException (com.auth0.exception.APIException)4 RateLimitException (com.auth0.exception.RateLimitException)4 RecordedMultipartRequest (com.auth0.net.multipart.RecordedMultipartRequest)4 JsonParseException (com.fasterxml.jackson.core.JsonParseException)4 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)4 ExpectedException (org.junit.rules.ExpectedException)4 VoidRequest (com.auth0.net.VoidRequest)3 File (java.io.File)3 UsersImportOptions (com.auth0.client.mgmt.filter.UsersImportOptions)2 Job (com.auth0.json.mgmt.jobs.Job)2 User (com.auth0.json.mgmt.users.User)2 FilePart (com.auth0.net.multipart.FilePart)2 KeyValuePart (com.auth0.net.multipart.KeyValuePart)2