From 3cc0034474f5d8cb15f309365e4e25a0e0c832a1 Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Wed, 25 Apr 2018 23:38:13 +0100 Subject: [PATCH 01/34] Add 'Send verification email functionality' with tests --- .../com/auth0/client/mgmt/JobsEntity.java | 34 ++++++++++ .../com/auth0/client/mgmt/ManagementAPI.java | 9 +++ .../auth0/json/mgmt/jobs/EmailRecipient.java | 42 ++++++++++++ .../java/com/auth0/json/mgmt/jobs/Job.java | 68 +++++++++++++++++++ .../java/com/auth0/client/MockServer.java | 1 + .../com/auth0/client/mgmt/JobsEntityTest.java | 47 +++++++++++++ .../mgmt/post_verification_email.json | 6 ++ 7 files changed, 207 insertions(+) create mode 100644 src/main/java/com/auth0/client/mgmt/JobsEntity.java create mode 100644 src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java create mode 100644 src/main/java/com/auth0/json/mgmt/jobs/Job.java create mode 100644 src/test/java/com/auth0/client/mgmt/JobsEntityTest.java create mode 100644 src/test/resources/mgmt/post_verification_email.json diff --git a/src/main/java/com/auth0/client/mgmt/JobsEntity.java b/src/main/java/com/auth0/client/mgmt/JobsEntity.java new file mode 100644 index 00000000..298d2c15 --- /dev/null +++ b/src/main/java/com/auth0/client/mgmt/JobsEntity.java @@ -0,0 +1,34 @@ +package com.auth0.client.mgmt; + +import com.auth0.json.mgmt.jobs.Job; +import com.auth0.json.mgmt.jobs.EmailRecipient; +import com.auth0.net.CustomRequest; +import com.auth0.net.Request; +import com.auth0.utils.Asserts; +import com.fasterxml.jackson.core.type.TypeReference; +import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; + +@SuppressWarnings("WeakerAccess") +public class JobsEntity extends BaseManagementEntity { + + JobsEntity(OkHttpClient client, HttpUrl baseUrl, String apiToken) { + super(client, baseUrl, apiToken); + } + + public Request sendVerificationEmail(EmailRecipient recipient) { + Asserts.assertNotNull(recipient, "recipient"); + + String url = baseUrl + .newBuilder() + .addPathSegments("api/v2/jobs/verification-email") + .build() + .toString(); + + CustomRequest request = new CustomRequest<>(client, url, "POST", new TypeReference() { + }); + request.addHeader("Authorization", "Bearer " + apiToken); + request.setBody(recipient); + return request; + } +} diff --git a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java index adc42d06..d821bd78 100644 --- a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java +++ b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java @@ -222,4 +222,13 @@ public TicketsEntity tickets() { public ResourceServerEntity resourceServers(){ return new ResourceServerEntity(client, baseUrl, apiToken); } + + /** + * Getter for Jobs entity. + * + * @return the Jobs entity. + */ + public JobsEntity jobs() { + return new JobsEntity(client, baseUrl, apiToken); + } } diff --git a/src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java b/src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java new file mode 100644 index 00000000..adb56f73 --- /dev/null +++ b/src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java @@ -0,0 +1,42 @@ +package com.auth0.json.mgmt.jobs; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +@SuppressWarnings({"unused", "WeakerAccess"}) +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(JsonInclude.Include.NON_NULL) +public class EmailRecipient { + + @JsonProperty("user_id") + private String userId; + @JsonProperty("client_id") + private String clientId; + + @JsonCreator + public EmailRecipient(@JsonProperty("user_id") String userId) { + this.userId = userId; + } + + @JsonProperty("user_id") + public String getUserId() { + return userId; + } + + @JsonProperty("client_id") + public String getClientId() { + return clientId; + } + + @JsonProperty("client_id") + public void setClientId(String clientId) { + this.clientId = clientId; + } + + @JsonProperty("user_id") + public void setUserId(String userId) { + this.userId = userId; + } +} diff --git a/src/main/java/com/auth0/json/mgmt/jobs/Job.java b/src/main/java/com/auth0/json/mgmt/jobs/Job.java new file mode 100644 index 00000000..33349468 --- /dev/null +++ b/src/main/java/com/auth0/json/mgmt/jobs/Job.java @@ -0,0 +1,68 @@ +package com.auth0.json.mgmt.jobs; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +@SuppressWarnings({"unused", "WeakerAccess"}) +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(JsonInclude.Include.NON_NULL) +public class Job { + + @JsonProperty("status") + private String status; + @JsonProperty("type") + private String type; + @JsonProperty("created_at") + private String createdAt; + @JsonProperty("id") + private String id; + + @JsonCreator + public Job(@JsonProperty("status") String status, @JsonProperty("type") String type, @JsonProperty("id") String id) { + this.status = status; + this.type = type; + this.id = id; + } + + @JsonProperty("status") + public String getStatus() { + return status; + } + + @JsonProperty("type") + public String getType() { + return type; + } + + @JsonProperty("created_at") + public String getCreatedAt() { + return createdAt; + } + + @JsonProperty("id") + public String getId() { + return id; + } + + @JsonProperty("status") + public void setStatus(String status) { + this.status = status; + } + + @JsonProperty("type") + public void setType(String type) { + this.type = type; + } + + @JsonProperty("created_at") + public void setCreatedAt(String createdAt) { + this.createdAt = createdAt; + } + + @JsonProperty("id") + public void setId(String id) { + this.id = id; + } +} diff --git a/src/test/java/com/auth0/client/MockServer.java b/src/test/java/com/auth0/client/MockServer.java index a413a966..9779e371 100644 --- a/src/test/java/com/auth0/client/MockServer.java +++ b/src/test/java/com/auth0/client/MockServer.java @@ -66,6 +66,7 @@ public class MockServer { public static final String MGMT_PASSWORD_CHANGE_TICKET = "src/test/resources/mgmt/password_change_ticket.json"; public static final String MGMT_EMAIL_VERIFICATION_TICKET = "src/test/resources/mgmt/email_verification_ticket.json"; public static final String MGMT_EMPTY_LIST = "src/test/resources/mgmt/empty_list.json"; + public static final String MGMT_JOB_POST_VERIFICATION_EMAIL = "src/test/resources/mgmt/post_verification_email.json"; private final MockWebServer server; diff --git a/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java new file mode 100644 index 00000000..3c8bbb24 --- /dev/null +++ b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java @@ -0,0 +1,47 @@ +package com.auth0.client.mgmt; + +import com.auth0.json.mgmt.jobs.EmailRecipient; +import com.auth0.json.mgmt.jobs.Job; +import com.auth0.net.Request; +import okhttp3.mockwebserver.RecordedRequest; +import org.junit.Test; + +import java.util.Map; + +import static com.auth0.client.MockServer.*; +import static com.auth0.client.RecordedRequestMatcher.hasHeader; +import static com.auth0.client.RecordedRequestMatcher.hasMethodAndPath; +import static org.hamcrest.Matchers.hasEntry; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNull.notNullValue; +import static org.junit.Assert.assertThat; + +public class JobsEntityTest extends BaseMgmtEntityTest { + + @Test + public void shouldSendAUserAVerificationEmail() throws Exception { + Request request = api.jobs().sendVerificationEmail(new EmailRecipient("google-oauth2|1234")); + assertThat(request, is(notNullValue())); + + server.jsonResponse(MGMT_JOB_POST_VERIFICATION_EMAIL, 200); + Job response = request.execute(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath("POST", "/api/v2/jobs/verification-email")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + + Map body = bodyFromRequest(recordedRequest); + assertThat(body.size(), is(1)); + assertThat(body, hasEntry("user_id", "google-oauth2|1234")); + + assertThat(response, is(notNullValue())); + } + + @Test + public void shouldThrowOnNullEmailRecipient() throws Exception { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("'recipient' cannot be null!"); + api.jobs().sendVerificationEmail(null); + } +} diff --git a/src/test/resources/mgmt/post_verification_email.json b/src/test/resources/mgmt/post_verification_email.json new file mode 100644 index 00000000..0dd23c06 --- /dev/null +++ b/src/test/resources/mgmt/post_verification_email.json @@ -0,0 +1,6 @@ +{ + "status": "completed", + "type": "verification_email", + "created_at": "12/09/2025", + "id": "job_0000000000000001" +} \ No newline at end of file From 4863f85e6f35cd6925c4acc8ccf9bf0b90736ae8 Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 26 Apr 2018 19:34:28 +0100 Subject: [PATCH 02/34] Add documentation --- src/main/java/com/auth0/client/mgmt/JobsEntity.java | 10 ++++++++++ .../java/com/auth0/json/mgmt/jobs/EmailRecipient.java | 3 +++ src/main/java/com/auth0/json/mgmt/jobs/Job.java | 3 +++ 3 files changed, 16 insertions(+) diff --git a/src/main/java/com/auth0/client/mgmt/JobsEntity.java b/src/main/java/com/auth0/client/mgmt/JobsEntity.java index 298d2c15..755473cd 100644 --- a/src/main/java/com/auth0/client/mgmt/JobsEntity.java +++ b/src/main/java/com/auth0/client/mgmt/JobsEntity.java @@ -9,6 +9,9 @@ import okhttp3.HttpUrl; import okhttp3.OkHttpClient; +/** + * Class that provides an implementation of the Jobs methods of the Management API as defined in https://auth0.com/docs/api/management/v2#!/Jobs + */ @SuppressWarnings("WeakerAccess") public class JobsEntity extends BaseManagementEntity { @@ -16,6 +19,13 @@ public class JobsEntity extends BaseManagementEntity { super(client, baseUrl, apiToken); } + /** + * Sends an Email Verification. A token with scope update:users is needed. + * See https://auth0.com/docs/api/management/v2#!/Jobs/post_verification_email + * + * @param recipient the email verification recipient. + * @return a Request to execute. + */ public Request sendVerificationEmail(EmailRecipient recipient) { Asserts.assertNotNull(recipient, "recipient"); diff --git a/src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java b/src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java index adb56f73..387386ef 100644 --- a/src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java +++ b/src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java @@ -5,6 +5,9 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; +/** + * Class that represents an Auth0 Email Recipient object. Related to the {@link com.auth0.client.mgmt.JobsEntity} entity. + */ @SuppressWarnings({"unused", "WeakerAccess"}) @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(JsonInclude.Include.NON_NULL) diff --git a/src/main/java/com/auth0/json/mgmt/jobs/Job.java b/src/main/java/com/auth0/json/mgmt/jobs/Job.java index 33349468..9f1eb0a0 100644 --- a/src/main/java/com/auth0/json/mgmt/jobs/Job.java +++ b/src/main/java/com/auth0/json/mgmt/jobs/Job.java @@ -5,6 +5,9 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; +/** + * Class that represents an Auth0 Job object. Related to the {@link com.auth0.client.mgmt.JobsEntity} entity. + */ @SuppressWarnings({"unused", "WeakerAccess"}) @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(JsonInclude.Include.NON_NULL) From 2e5796ac58d83c3bfd234170a123e6c495b9d2bf Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 26 Apr 2018 19:45:28 +0100 Subject: [PATCH 03/34] Cast String to Object --- src/test/java/com/auth0/client/mgmt/JobsEntityTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java index 3c8bbb24..d651a98e 100644 --- a/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java +++ b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java @@ -33,7 +33,7 @@ public void shouldSendAUserAVerificationEmail() throws Exception { Map body = bodyFromRequest(recordedRequest); assertThat(body.size(), is(1)); - assertThat(body, hasEntry("user_id", "google-oauth2|1234")); + assertThat(body, hasEntry("user_id", (Object) "google-oauth2|1234")); assertThat(response, is(notNullValue())); } From 2c8a6afc3b387114abb2c51c718487a78086ade0 Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 26 Apr 2018 22:43:12 +0100 Subject: [PATCH 04/34] Add tests for deserialization --- .../json/mgmt/jobs/EmailRecipientTest.java | 22 +++++++++++++++++ .../com/auth0/json/mgmt/jobs/JobTest.java | 24 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/test/java/com/auth0/json/mgmt/jobs/EmailRecipientTest.java create mode 100644 src/test/java/com/auth0/json/mgmt/jobs/JobTest.java diff --git a/src/test/java/com/auth0/json/mgmt/jobs/EmailRecipientTest.java b/src/test/java/com/auth0/json/mgmt/jobs/EmailRecipientTest.java new file mode 100644 index 00000000..1db6efbb --- /dev/null +++ b/src/test/java/com/auth0/json/mgmt/jobs/EmailRecipientTest.java @@ -0,0 +1,22 @@ +package com.auth0.json.mgmt.jobs; + +import com.auth0.json.JsonTest; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.*; + +public class EmailRecipientTest extends JsonTest { + + private static final String json = "{\"client_id\":\"123Asd\",\"user_id\":\"googleAuth|035\"}"; + + @Test + public void shouldDeserialize() throws Exception { + EmailRecipient recipient = fromJSON(json, EmailRecipient.class); + + assertThat(recipient, is(notNullValue())); + assertThat(recipient.getClientId(), is("123Asd")); + assertThat(recipient.getUserId(), is("googleAuth|035")); + } +} diff --git a/src/test/java/com/auth0/json/mgmt/jobs/JobTest.java b/src/test/java/com/auth0/json/mgmt/jobs/JobTest.java new file mode 100644 index 00000000..b42e4d85 --- /dev/null +++ b/src/test/java/com/auth0/json/mgmt/jobs/JobTest.java @@ -0,0 +1,24 @@ +package com.auth0.json.mgmt.jobs; + +import com.auth0.json.JsonTest; +import org.junit.Test; + +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +public class JobTest extends JsonTest { + + private static final String json = "{\"status\": \"completed\",\"type\": \"verification_email\",\"created_at\": \"12/09/2025\",\"id\": \"job_0000000000000001\"}"; + + @Test + public void shouldDeserialize() throws Exception { + Job job = fromJSON(json, Job.class); + + assertThat(job, is(notNullValue())); + assertThat(job.getId(), is("job_0000000000000001")); + assertThat(job.getStatus(), is("completed")); + assertThat(job.getType(), is("verification_email")); + assertThat(job.getCreatedAt(), is("12/09/2025")); + } +} From e0731681cd1921cb489b3b15aafbbc518ba504fa Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 26 Apr 2018 23:51:27 +0100 Subject: [PATCH 05/34] Remove setters for required fields --- .../com/auth0/json/mgmt/jobs/EmailRecipient.java | 5 ----- src/main/java/com/auth0/json/mgmt/jobs/Job.java | 15 --------------- 2 files changed, 20 deletions(-) diff --git a/src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java b/src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java index 387386ef..3a9b32da 100644 --- a/src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java +++ b/src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java @@ -37,9 +37,4 @@ public String getClientId() { public void setClientId(String clientId) { this.clientId = clientId; } - - @JsonProperty("user_id") - public void setUserId(String userId) { - this.userId = userId; - } } diff --git a/src/main/java/com/auth0/json/mgmt/jobs/Job.java b/src/main/java/com/auth0/json/mgmt/jobs/Job.java index 9f1eb0a0..48299d51 100644 --- a/src/main/java/com/auth0/json/mgmt/jobs/Job.java +++ b/src/main/java/com/auth0/json/mgmt/jobs/Job.java @@ -49,23 +49,8 @@ public String getId() { return id; } - @JsonProperty("status") - public void setStatus(String status) { - this.status = status; - } - - @JsonProperty("type") - public void setType(String type) { - this.type = type; - } - @JsonProperty("created_at") public void setCreatedAt(String createdAt) { this.createdAt = createdAt; } - - @JsonProperty("id") - public void setId(String id) { - this.id = id; - } } From 9c0c46f2ab224722037fba8bd21f08b1e05ba64f Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 7 Jun 2018 20:25:52 +0100 Subject: [PATCH 06/34] Make public constructor private --- src/main/java/com/auth0/json/mgmt/jobs/Job.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/auth0/json/mgmt/jobs/Job.java b/src/main/java/com/auth0/json/mgmt/jobs/Job.java index 48299d51..8ee65ea8 100644 --- a/src/main/java/com/auth0/json/mgmt/jobs/Job.java +++ b/src/main/java/com/auth0/json/mgmt/jobs/Job.java @@ -23,7 +23,7 @@ public class Job { private String id; @JsonCreator - public Job(@JsonProperty("status") String status, @JsonProperty("type") String type, @JsonProperty("id") String id) { + private Job(@JsonProperty("status") String status, @JsonProperty("type") String type, @JsonProperty("id") String id) { this.status = status; this.type = type; this.id = id; From c2231798ca2b1b3e7bf74021189c53f2d70dc98e Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 7 Jun 2018 21:21:45 +0100 Subject: [PATCH 07/34] Remove EmailRecipient class and replaced it with userId and optional clientId, incl. update tests --- .../com/auth0/client/mgmt/JobsEntity.java | 19 ++++++--- .../auth0/json/mgmt/jobs/EmailRecipient.java | 40 ------------------- .../java/com/auth0/json/mgmt/jobs/Job.java | 15 +++---- .../com/auth0/client/mgmt/JobsEntityTest.java | 9 ++--- .../json/mgmt/jobs/EmailRecipientTest.java | 22 ---------- .../com/auth0/json/mgmt/jobs/JobTest.java | 4 +- .../mgmt/post_verification_email.json | 2 +- 7 files changed, 29 insertions(+), 82 deletions(-) delete mode 100644 src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java delete mode 100644 src/test/java/com/auth0/json/mgmt/jobs/EmailRecipientTest.java diff --git a/src/main/java/com/auth0/client/mgmt/JobsEntity.java b/src/main/java/com/auth0/client/mgmt/JobsEntity.java index 755473cd..358dc660 100644 --- a/src/main/java/com/auth0/client/mgmt/JobsEntity.java +++ b/src/main/java/com/auth0/client/mgmt/JobsEntity.java @@ -1,7 +1,6 @@ package com.auth0.client.mgmt; import com.auth0.json.mgmt.jobs.Job; -import com.auth0.json.mgmt.jobs.EmailRecipient; import com.auth0.net.CustomRequest; import com.auth0.net.Request; import com.auth0.utils.Asserts; @@ -9,6 +8,9 @@ import okhttp3.HttpUrl; import okhttp3.OkHttpClient; +import java.util.HashMap; +import java.util.Map; + /** * Class that provides an implementation of the Jobs methods of the Management API as defined in https://auth0.com/docs/api/management/v2#!/Jobs */ @@ -23,11 +25,12 @@ public class JobsEntity extends BaseManagementEntity { * Sends an Email Verification. A token with scope update:users is needed. * See https://auth0.com/docs/api/management/v2#!/Jobs/post_verification_email * - * @param recipient the email verification recipient. + * @param userId The user_id of the user to whom the email will be sent. + * @param clientId The id of the client, if not provided the global one will be used. * @return a Request to execute. */ - public Request sendVerificationEmail(EmailRecipient recipient) { - Asserts.assertNotNull(recipient, "recipient"); + public Request sendVerificationEmail(String userId, String clientId) { + Asserts.assertNotNull(userId, "recipient"); String url = baseUrl .newBuilder() @@ -35,10 +38,16 @@ public Request sendVerificationEmail(EmailRecipient recipient) { .build() .toString(); + Map requestBody = new HashMap<>(); + requestBody.put("user_id", userId); + if (clientId != null && !clientId.isEmpty()) { + requestBody.put("client_id", clientId); + } + CustomRequest request = new CustomRequest<>(client, url, "POST", new TypeReference() { }); request.addHeader("Authorization", "Bearer " + apiToken); - request.setBody(recipient); + request.setBody(requestBody); return request; } } diff --git a/src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java b/src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java deleted file mode 100644 index 3a9b32da..00000000 --- a/src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.auth0.json.mgmt.jobs; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; - -/** - * Class that represents an Auth0 Email Recipient object. Related to the {@link com.auth0.client.mgmt.JobsEntity} entity. - */ -@SuppressWarnings({"unused", "WeakerAccess"}) -@JsonIgnoreProperties(ignoreUnknown = true) -@JsonInclude(JsonInclude.Include.NON_NULL) -public class EmailRecipient { - - @JsonProperty("user_id") - private String userId; - @JsonProperty("client_id") - private String clientId; - - @JsonCreator - public EmailRecipient(@JsonProperty("user_id") String userId) { - this.userId = userId; - } - - @JsonProperty("user_id") - public String getUserId() { - return userId; - } - - @JsonProperty("client_id") - public String getClientId() { - return clientId; - } - - @JsonProperty("client_id") - public void setClientId(String clientId) { - this.clientId = clientId; - } -} diff --git a/src/main/java/com/auth0/json/mgmt/jobs/Job.java b/src/main/java/com/auth0/json/mgmt/jobs/Job.java index 8ee65ea8..bbdf4d84 100644 --- a/src/main/java/com/auth0/json/mgmt/jobs/Job.java +++ b/src/main/java/com/auth0/json/mgmt/jobs/Job.java @@ -1,9 +1,8 @@ package com.auth0.json.mgmt.jobs; -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.*; + +import java.util.Date; /** * Class that represents an Auth0 Job object. Related to the {@link com.auth0.client.mgmt.JobsEntity} entity. @@ -17,8 +16,9 @@ public class Job { private String status; @JsonProperty("type") private String type; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") @JsonProperty("created_at") - private String createdAt; + private Date createdAt; @JsonProperty("id") private String id; @@ -40,7 +40,7 @@ public String getType() { } @JsonProperty("created_at") - public String getCreatedAt() { + public Date getCreatedAt() { return createdAt; } @@ -49,8 +49,9 @@ public String getId() { return id; } + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") @JsonProperty("created_at") - public void setCreatedAt(String createdAt) { + public void setCreatedAt(Date createdAt) { this.createdAt = createdAt; } } diff --git a/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java index d651a98e..ffb39201 100644 --- a/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java +++ b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java @@ -1,6 +1,5 @@ package com.auth0.client.mgmt; -import com.auth0.json.mgmt.jobs.EmailRecipient; import com.auth0.json.mgmt.jobs.Job; import com.auth0.net.Request; import okhttp3.mockwebserver.RecordedRequest; @@ -20,7 +19,7 @@ public class JobsEntityTest extends BaseMgmtEntityTest { @Test public void shouldSendAUserAVerificationEmail() throws Exception { - Request request = api.jobs().sendVerificationEmail(new EmailRecipient("google-oauth2|1234")); + Request request = api.jobs().sendVerificationEmail("google-oauth2|1234", null); assertThat(request, is(notNullValue())); server.jsonResponse(MGMT_JOB_POST_VERIFICATION_EMAIL, 200); @@ -32,8 +31,8 @@ public void shouldSendAUserAVerificationEmail() throws Exception { assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); Map body = bodyFromRequest(recordedRequest); - assertThat(body.size(), is(1)); - assertThat(body, hasEntry("user_id", (Object) "google-oauth2|1234")); +// assertThat(body.size(), is(1)); + assertThat(body, hasEntry("user_id", "google-oauth2|1234")); assertThat(response, is(notNullValue())); } @@ -42,6 +41,6 @@ public void shouldSendAUserAVerificationEmail() throws Exception { public void shouldThrowOnNullEmailRecipient() throws Exception { exception.expect(IllegalArgumentException.class); exception.expectMessage("'recipient' cannot be null!"); - api.jobs().sendVerificationEmail(null); + api.jobs().sendVerificationEmail(null, null); } } diff --git a/src/test/java/com/auth0/json/mgmt/jobs/EmailRecipientTest.java b/src/test/java/com/auth0/json/mgmt/jobs/EmailRecipientTest.java deleted file mode 100644 index 1db6efbb..00000000 --- a/src/test/java/com/auth0/json/mgmt/jobs/EmailRecipientTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.auth0.json.mgmt.jobs; - -import com.auth0.json.JsonTest; -import org.junit.Test; - -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.*; - -public class EmailRecipientTest extends JsonTest { - - private static final String json = "{\"client_id\":\"123Asd\",\"user_id\":\"googleAuth|035\"}"; - - @Test - public void shouldDeserialize() throws Exception { - EmailRecipient recipient = fromJSON(json, EmailRecipient.class); - - assertThat(recipient, is(notNullValue())); - assertThat(recipient.getClientId(), is("123Asd")); - assertThat(recipient.getUserId(), is("googleAuth|035")); - } -} diff --git a/src/test/java/com/auth0/json/mgmt/jobs/JobTest.java b/src/test/java/com/auth0/json/mgmt/jobs/JobTest.java index b42e4d85..a2ac66b8 100644 --- a/src/test/java/com/auth0/json/mgmt/jobs/JobTest.java +++ b/src/test/java/com/auth0/json/mgmt/jobs/JobTest.java @@ -9,7 +9,7 @@ public class JobTest extends JsonTest { - private static final String json = "{\"status\": \"completed\",\"type\": \"verification_email\",\"created_at\": \"12/09/2025\",\"id\": \"job_0000000000000001\"}"; + private static final String json = "{\"status\": \"completed\",\"type\": \"verification_email\",\"created_at\": \"2016-02-23T19:57:29.532Z\",\"id\": \"job_0000000000000001\"}"; @Test public void shouldDeserialize() throws Exception { @@ -19,6 +19,6 @@ public void shouldDeserialize() throws Exception { assertThat(job.getId(), is("job_0000000000000001")); assertThat(job.getStatus(), is("completed")); assertThat(job.getType(), is("verification_email")); - assertThat(job.getCreatedAt(), is("12/09/2025")); + assertThat(job.getCreatedAt(), is(parseJSONDate("2016-02-23T19:57:29.532Z"))); } } diff --git a/src/test/resources/mgmt/post_verification_email.json b/src/test/resources/mgmt/post_verification_email.json index 0dd23c06..95cad29d 100644 --- a/src/test/resources/mgmt/post_verification_email.json +++ b/src/test/resources/mgmt/post_verification_email.json @@ -1,6 +1,6 @@ { "status": "completed", "type": "verification_email", - "created_at": "12/09/2025", + "created_at": "", "id": "job_0000000000000001" } \ No newline at end of file From 666f4bde6caf84fa172013d1d118f8a75da0495a Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 7 Jun 2018 21:24:06 +0100 Subject: [PATCH 08/34] Remove create_at setter --- src/main/java/com/auth0/json/mgmt/jobs/Job.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main/java/com/auth0/json/mgmt/jobs/Job.java b/src/main/java/com/auth0/json/mgmt/jobs/Job.java index bbdf4d84..5e3b1d84 100644 --- a/src/main/java/com/auth0/json/mgmt/jobs/Job.java +++ b/src/main/java/com/auth0/json/mgmt/jobs/Job.java @@ -39,6 +39,7 @@ public String getType() { return type; } + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") @JsonProperty("created_at") public Date getCreatedAt() { return createdAt; @@ -48,10 +49,4 @@ public Date getCreatedAt() { public String getId() { return id; } - - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") - @JsonProperty("created_at") - public void setCreatedAt(Date createdAt) { - this.createdAt = createdAt; - } } From 78c950b8e366cad88ba54088150d4dbfa0d91ca9 Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 7 Jun 2018 21:26:52 +0100 Subject: [PATCH 09/34] Add body size assertion --- src/test/java/com/auth0/client/mgmt/JobsEntityTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java index ffb39201..0e2e0c20 100644 --- a/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java +++ b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java @@ -31,7 +31,7 @@ public void shouldSendAUserAVerificationEmail() throws Exception { assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); Map body = bodyFromRequest(recordedRequest); -// assertThat(body.size(), is(1)); + assertThat(body.size(), is(1)); assertThat(body, hasEntry("user_id", "google-oauth2|1234")); assertThat(response, is(notNullValue())); From 915f51fb50340bdb158b7ff80d0ccda29f826552 Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 7 Jun 2018 21:52:08 +0100 Subject: [PATCH 10/34] Map String to Object --- src/main/java/com/auth0/client/mgmt/JobsEntity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/auth0/client/mgmt/JobsEntity.java b/src/main/java/com/auth0/client/mgmt/JobsEntity.java index 358dc660..7d4e0656 100644 --- a/src/main/java/com/auth0/client/mgmt/JobsEntity.java +++ b/src/main/java/com/auth0/client/mgmt/JobsEntity.java @@ -38,7 +38,7 @@ public Request sendVerificationEmail(String userId, String clientId) { .build() .toString(); - Map requestBody = new HashMap<>(); + Map requestBody = new HashMap<>(); requestBody.put("user_id", userId); if (clientId != null && !clientId.isEmpty()) { requestBody.put("client_id", clientId); From 8e39c7ae097766252e37d0b7360ea307ac16236f Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 7 Jun 2018 21:56:40 +0100 Subject: [PATCH 11/34] Cast Map to Object --- src/main/java/com/auth0/client/mgmt/JobsEntity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/auth0/client/mgmt/JobsEntity.java b/src/main/java/com/auth0/client/mgmt/JobsEntity.java index 7d4e0656..2d16f05c 100644 --- a/src/main/java/com/auth0/client/mgmt/JobsEntity.java +++ b/src/main/java/com/auth0/client/mgmt/JobsEntity.java @@ -47,7 +47,7 @@ public Request sendVerificationEmail(String userId, String clientId) { CustomRequest request = new CustomRequest<>(client, url, "POST", new TypeReference() { }); request.addHeader("Authorization", "Bearer " + apiToken); - request.setBody(requestBody); + request.setBody((Object) requestBody); return request; } } From 39b8d5b9ae3c9e071c78806fe13a9de822153088 Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 7 Jun 2018 21:57:08 +0100 Subject: [PATCH 12/34] Turn Object back to String --- src/main/java/com/auth0/client/mgmt/JobsEntity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/auth0/client/mgmt/JobsEntity.java b/src/main/java/com/auth0/client/mgmt/JobsEntity.java index 2d16f05c..cfbddcc4 100644 --- a/src/main/java/com/auth0/client/mgmt/JobsEntity.java +++ b/src/main/java/com/auth0/client/mgmt/JobsEntity.java @@ -38,7 +38,7 @@ public Request sendVerificationEmail(String userId, String clientId) { .build() .toString(); - Map requestBody = new HashMap<>(); + Map requestBody = new HashMap<>(); requestBody.put("user_id", userId); if (clientId != null && !clientId.isEmpty()) { requestBody.put("client_id", clientId); From aa638930b7713702a3a5a85ae5ef6d3b17003205 Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Fri, 27 Apr 2018 17:27:23 -0300 Subject: [PATCH 13/34] expose additional error response properties in the exception --- README.md | 2 +- .../com/auth0/exception/APIException.java | 20 +++++++++++++-- .../java/com/auth0/client/MockServer.java | 1 + .../java/com/auth0/net/CustomRequestTest.java | 25 ++++++++++++++++++- ...with_description_and_extra_properties.json | 5 ++++ 5 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 src/test/resources/auth/error_with_description_and_extra_properties.json diff --git a/README.md b/README.md index a5b87ffe..76f5327b 100644 --- a/README.md +++ b/README.md @@ -526,7 +526,7 @@ If you need to handle different error scenarios you need to catch first `APIExce The APIExplorer includes a list of response messages for each endpoint. You can get a clue of what went wrong by asking the Http status code: `exception.getStatusCode()`. i.e. a `status_code=403` would mean that the token has an insufficient scope. -An error code will be included to categorize the type of error, you can get it by calling `exception.getError()`. If you want to see a user friendly description of what happened and why the request is failing check the `exception.getDescription()`. +An error code will be included to categorize the type of error, you can get it by calling `exception.getError()`. If you want to see a user friendly description of what happened and why the request is failing check the `exception.getDescription()`. Finally, if the error response includes additional properties they can be obtained by calling `exception.getValue("{THE_KEY}")`. ``` diff --git a/src/main/java/com/auth0/exception/APIException.java b/src/main/java/com/auth0/exception/APIException.java index 6956b9bc..ed635c32 100644 --- a/src/main/java/com/auth0/exception/APIException.java +++ b/src/main/java/com/auth0/exception/APIException.java @@ -1,5 +1,6 @@ package com.auth0.exception; +import java.util.Collections; import java.util.Map; /** @@ -21,6 +22,7 @@ public class APIException extends Auth0Exception { private String error; private String description; private int statusCode; + private Map values; public APIException(String payload, int statusCode, Throwable cause) { super(createMessage(payload, statusCode), cause); @@ -30,6 +32,7 @@ public APIException(String payload, int statusCode, Throwable cause) { public APIException(Map values, int statusCode) { super(createMessage(obtainExceptionMessage(values), statusCode)); + this.values = Collections.unmodifiableMap(values); this.error = obtainExceptionError(values); this.description = obtainExceptionMessage(values); this.statusCode = statusCode; @@ -55,6 +58,19 @@ public String getError() { return error; } + /** + * Returns a value from the error map, if any. + * + * @param key key of the value to return + * @return the value if found or null + */ + public Object getValue(String key) { + if (values == null) { + return null; + } + return values.get(key); + } + /** * Getter for the exception user friendly description of why the request failed. * i.e. the description may say which query parameters are valid for that endpoint. @@ -75,9 +91,9 @@ private static String obtainExceptionMessage(Map values) { } if (values.containsKey("description")) { Object description = values.get("description"); - if(description instanceof String) { + if (description instanceof String) { return (String) description; - } else{ + } else { PasswordStrengthErrorParser policy = new PasswordStrengthErrorParser((Map) description); return policy.getDescription(); } diff --git a/src/test/java/com/auth0/client/MockServer.java b/src/test/java/com/auth0/client/MockServer.java index 9779e371..e8213c91 100644 --- a/src/test/java/com/auth0/client/MockServer.java +++ b/src/test/java/com/auth0/client/MockServer.java @@ -23,6 +23,7 @@ public class MockServer { public static final String AUTH_ERROR_WITH_ERROR_DESCRIPTION = "src/test/resources/auth/error_with_error_description.json"; public static final String AUTH_ERROR_WITH_ERROR = "src/test/resources/auth/error_with_error.json"; public static final String AUTH_ERROR_WITH_DESCRIPTION = "src/test/resources/auth/error_with_description.json"; + public static final String AUTH_ERROR_WITH_DESCRIPTION_AND_EXTRA_PROPERTIES = "src/test/resources/auth/error_with_description_and_extra_properties.json"; public static final String AUTH_ERROR_PLAINTEXT = "src/test/resources/auth/error_plaintext.json"; public static final String MGMT_ERROR_WITH_MESSAGE = "src/test/resources/mgmt/error_with_message.json"; public static final String MGMT_CLIENT_GRANTS_LIST = "src/test/resources/mgmt/client_grants_list.json"; diff --git a/src/test/java/com/auth0/net/CustomRequestTest.java b/src/test/java/com/auth0/net/CustomRequestTest.java index 0549ead3..7065a16b 100644 --- a/src/test/java/com/auth0/net/CustomRequestTest.java +++ b/src/test/java/com/auth0/net/CustomRequestTest.java @@ -1,8 +1,8 @@ package com.auth0.net; import com.auth0.client.MockServer; -import com.auth0.exception.Auth0Exception; import com.auth0.exception.APIException; +import com.auth0.exception.Auth0Exception; import com.auth0.json.auth.TokenHolder; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; @@ -214,6 +214,29 @@ public void shouldParseJSONErrorResponseWithError() throws Exception { assertThat(authException.getStatusCode(), is(400)); } + @Test + public void shouldParseJSONErrorResponseWithDescriptionAndExtraProperties() throws Exception { + CustomRequest request = new CustomRequest<>(client, server.getBaseUrl(), "GET", listType); + server.jsonResponse(AUTH_ERROR_WITH_DESCRIPTION_AND_EXTRA_PROPERTIES, 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: Multifactor authentication required")); + APIException authException = (APIException) exception; + assertThat(authException.getDescription(), is("Multifactor authentication required")); + assertThat(authException.getError(), is("mfa_required")); + assertThat(authException.getValue("mfa_token"), is("Fe26...Ha")); + assertThat(authException.getValue("non_existing_key"), is(nullValue())); + assertThat(authException.getStatusCode(), is(400)); + } + @Test public void shouldParseJSONErrorResponseWithDescription() throws Exception { CustomRequest request = new CustomRequest<>(client, server.getBaseUrl(), "GET", listType); diff --git a/src/test/resources/auth/error_with_description_and_extra_properties.json b/src/test/resources/auth/error_with_description_and_extra_properties.json new file mode 100644 index 00000000..a1799ae1 --- /dev/null +++ b/src/test/resources/auth/error_with_description_and_extra_properties.json @@ -0,0 +1,5 @@ +{ + "error": "mfa_required", + "error_description": "Multifactor authentication required", + "mfa_token": "Fe26...Ha" +} \ No newline at end of file From 754ef0e7e48acf4a0d7f422b2be3673a4fe9568e Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Fri, 27 Apr 2018 17:29:51 -0300 Subject: [PATCH 14/34] use unmmodifiable map when possible --- src/main/java/com/auth0/exception/APIException.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/auth0/exception/APIException.java b/src/main/java/com/auth0/exception/APIException.java index ed635c32..aa068352 100644 --- a/src/main/java/com/auth0/exception/APIException.java +++ b/src/main/java/com/auth0/exception/APIException.java @@ -33,8 +33,8 @@ public APIException(String payload, int statusCode, Throwable cause) { public APIException(Map values, int statusCode) { super(createMessage(obtainExceptionMessage(values), statusCode)); this.values = Collections.unmodifiableMap(values); - this.error = obtainExceptionError(values); - this.description = obtainExceptionMessage(values); + this.error = obtainExceptionError(this.values); + this.description = obtainExceptionMessage(this.values); this.statusCode = statusCode; } From fa10b1c06e8898787d958b20e08fbcae6f3d40a6 Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Fri, 27 Apr 2018 17:31:52 -0300 Subject: [PATCH 15/34] add missing test case --- src/test/java/com/auth0/net/CustomRequestTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/auth0/net/CustomRequestTest.java b/src/test/java/com/auth0/net/CustomRequestTest.java index 7065a16b..a2069d30 100644 --- a/src/test/java/com/auth0/net/CustomRequestTest.java +++ b/src/test/java/com/auth0/net/CustomRequestTest.java @@ -297,6 +297,7 @@ public void shouldParsePlainTextErrorResponse() throws Exception { APIException authException = (APIException) exception; assertThat(authException.getDescription(), is("A plain-text error response")); assertThat(authException.getError(), is(nullValue())); + assertThat(authException.getValue("non_existing_key"), is(nullValue())); assertThat(authException.getStatusCode(), is(400)); } From 58b4d30ec816c5145074ccb37844238b012d31ef Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Thu, 3 May 2018 19:50:58 -0300 Subject: [PATCH 16/34] add casting. fixes jdk7 test --- src/test/java/com/auth0/net/CustomRequestTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/auth0/net/CustomRequestTest.java b/src/test/java/com/auth0/net/CustomRequestTest.java index a2069d30..f52ef446 100644 --- a/src/test/java/com/auth0/net/CustomRequestTest.java +++ b/src/test/java/com/auth0/net/CustomRequestTest.java @@ -214,6 +214,7 @@ public void shouldParseJSONErrorResponseWithError() throws Exception { assertThat(authException.getStatusCode(), is(400)); } + @SuppressWarnings("RedundantCast") @Test public void shouldParseJSONErrorResponseWithDescriptionAndExtraProperties() throws Exception { CustomRequest request = new CustomRequest<>(client, server.getBaseUrl(), "GET", listType); @@ -232,7 +233,7 @@ public void shouldParseJSONErrorResponseWithDescriptionAndExtraProperties() thro APIException authException = (APIException) exception; assertThat(authException.getDescription(), is("Multifactor authentication required")); assertThat(authException.getError(), is("mfa_required")); - assertThat(authException.getValue("mfa_token"), is("Fe26...Ha")); + assertThat(authException.getValue("mfa_token"), is((Object) "Fe26...Ha")); assertThat(authException.getValue("non_existing_key"), is(nullValue())); assertThat(authException.getStatusCode(), is(400)); } From 49415e031804c828a60dacee80ecdce3141c14ba Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 7 Jun 2018 21:21:45 +0100 Subject: [PATCH 17/34] Remove EmailRecipient class and replaced it with userId and optional clientId, incl. update tests --- .../com/auth0/client/mgmt/JobsEntity.java | 19 ++++++--- .../auth0/json/mgmt/jobs/EmailRecipient.java | 40 ------------------- .../java/com/auth0/json/mgmt/jobs/Job.java | 15 +++---- .../com/auth0/client/mgmt/JobsEntityTest.java | 9 ++--- .../json/mgmt/jobs/EmailRecipientTest.java | 22 ---------- .../com/auth0/json/mgmt/jobs/JobTest.java | 4 +- .../mgmt/post_verification_email.json | 2 +- 7 files changed, 29 insertions(+), 82 deletions(-) delete mode 100644 src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java delete mode 100644 src/test/java/com/auth0/json/mgmt/jobs/EmailRecipientTest.java diff --git a/src/main/java/com/auth0/client/mgmt/JobsEntity.java b/src/main/java/com/auth0/client/mgmt/JobsEntity.java index 755473cd..358dc660 100644 --- a/src/main/java/com/auth0/client/mgmt/JobsEntity.java +++ b/src/main/java/com/auth0/client/mgmt/JobsEntity.java @@ -1,7 +1,6 @@ package com.auth0.client.mgmt; import com.auth0.json.mgmt.jobs.Job; -import com.auth0.json.mgmt.jobs.EmailRecipient; import com.auth0.net.CustomRequest; import com.auth0.net.Request; import com.auth0.utils.Asserts; @@ -9,6 +8,9 @@ import okhttp3.HttpUrl; import okhttp3.OkHttpClient; +import java.util.HashMap; +import java.util.Map; + /** * Class that provides an implementation of the Jobs methods of the Management API as defined in https://auth0.com/docs/api/management/v2#!/Jobs */ @@ -23,11 +25,12 @@ public class JobsEntity extends BaseManagementEntity { * Sends an Email Verification. A token with scope update:users is needed. * See https://auth0.com/docs/api/management/v2#!/Jobs/post_verification_email * - * @param recipient the email verification recipient. + * @param userId The user_id of the user to whom the email will be sent. + * @param clientId The id of the client, if not provided the global one will be used. * @return a Request to execute. */ - public Request sendVerificationEmail(EmailRecipient recipient) { - Asserts.assertNotNull(recipient, "recipient"); + public Request sendVerificationEmail(String userId, String clientId) { + Asserts.assertNotNull(userId, "recipient"); String url = baseUrl .newBuilder() @@ -35,10 +38,16 @@ public Request sendVerificationEmail(EmailRecipient recipient) { .build() .toString(); + Map requestBody = new HashMap<>(); + requestBody.put("user_id", userId); + if (clientId != null && !clientId.isEmpty()) { + requestBody.put("client_id", clientId); + } + CustomRequest request = new CustomRequest<>(client, url, "POST", new TypeReference() { }); request.addHeader("Authorization", "Bearer " + apiToken); - request.setBody(recipient); + request.setBody(requestBody); return request; } } diff --git a/src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java b/src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java deleted file mode 100644 index 3a9b32da..00000000 --- a/src/main/java/com/auth0/json/mgmt/jobs/EmailRecipient.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.auth0.json.mgmt.jobs; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; - -/** - * Class that represents an Auth0 Email Recipient object. Related to the {@link com.auth0.client.mgmt.JobsEntity} entity. - */ -@SuppressWarnings({"unused", "WeakerAccess"}) -@JsonIgnoreProperties(ignoreUnknown = true) -@JsonInclude(JsonInclude.Include.NON_NULL) -public class EmailRecipient { - - @JsonProperty("user_id") - private String userId; - @JsonProperty("client_id") - private String clientId; - - @JsonCreator - public EmailRecipient(@JsonProperty("user_id") String userId) { - this.userId = userId; - } - - @JsonProperty("user_id") - public String getUserId() { - return userId; - } - - @JsonProperty("client_id") - public String getClientId() { - return clientId; - } - - @JsonProperty("client_id") - public void setClientId(String clientId) { - this.clientId = clientId; - } -} diff --git a/src/main/java/com/auth0/json/mgmt/jobs/Job.java b/src/main/java/com/auth0/json/mgmt/jobs/Job.java index 8ee65ea8..bbdf4d84 100644 --- a/src/main/java/com/auth0/json/mgmt/jobs/Job.java +++ b/src/main/java/com/auth0/json/mgmt/jobs/Job.java @@ -1,9 +1,8 @@ package com.auth0.json.mgmt.jobs; -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.*; + +import java.util.Date; /** * Class that represents an Auth0 Job object. Related to the {@link com.auth0.client.mgmt.JobsEntity} entity. @@ -17,8 +16,9 @@ public class Job { private String status; @JsonProperty("type") private String type; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") @JsonProperty("created_at") - private String createdAt; + private Date createdAt; @JsonProperty("id") private String id; @@ -40,7 +40,7 @@ public String getType() { } @JsonProperty("created_at") - public String getCreatedAt() { + public Date getCreatedAt() { return createdAt; } @@ -49,8 +49,9 @@ public String getId() { return id; } + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") @JsonProperty("created_at") - public void setCreatedAt(String createdAt) { + public void setCreatedAt(Date createdAt) { this.createdAt = createdAt; } } diff --git a/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java index d651a98e..ffb39201 100644 --- a/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java +++ b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java @@ -1,6 +1,5 @@ package com.auth0.client.mgmt; -import com.auth0.json.mgmt.jobs.EmailRecipient; import com.auth0.json.mgmt.jobs.Job; import com.auth0.net.Request; import okhttp3.mockwebserver.RecordedRequest; @@ -20,7 +19,7 @@ public class JobsEntityTest extends BaseMgmtEntityTest { @Test public void shouldSendAUserAVerificationEmail() throws Exception { - Request request = api.jobs().sendVerificationEmail(new EmailRecipient("google-oauth2|1234")); + Request request = api.jobs().sendVerificationEmail("google-oauth2|1234", null); assertThat(request, is(notNullValue())); server.jsonResponse(MGMT_JOB_POST_VERIFICATION_EMAIL, 200); @@ -32,8 +31,8 @@ public void shouldSendAUserAVerificationEmail() throws Exception { assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); Map body = bodyFromRequest(recordedRequest); - assertThat(body.size(), is(1)); - assertThat(body, hasEntry("user_id", (Object) "google-oauth2|1234")); +// assertThat(body.size(), is(1)); + assertThat(body, hasEntry("user_id", "google-oauth2|1234")); assertThat(response, is(notNullValue())); } @@ -42,6 +41,6 @@ public void shouldSendAUserAVerificationEmail() throws Exception { public void shouldThrowOnNullEmailRecipient() throws Exception { exception.expect(IllegalArgumentException.class); exception.expectMessage("'recipient' cannot be null!"); - api.jobs().sendVerificationEmail(null); + api.jobs().sendVerificationEmail(null, null); } } diff --git a/src/test/java/com/auth0/json/mgmt/jobs/EmailRecipientTest.java b/src/test/java/com/auth0/json/mgmt/jobs/EmailRecipientTest.java deleted file mode 100644 index 1db6efbb..00000000 --- a/src/test/java/com/auth0/json/mgmt/jobs/EmailRecipientTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.auth0.json.mgmt.jobs; - -import com.auth0.json.JsonTest; -import org.junit.Test; - -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.*; - -public class EmailRecipientTest extends JsonTest { - - private static final String json = "{\"client_id\":\"123Asd\",\"user_id\":\"googleAuth|035\"}"; - - @Test - public void shouldDeserialize() throws Exception { - EmailRecipient recipient = fromJSON(json, EmailRecipient.class); - - assertThat(recipient, is(notNullValue())); - assertThat(recipient.getClientId(), is("123Asd")); - assertThat(recipient.getUserId(), is("googleAuth|035")); - } -} diff --git a/src/test/java/com/auth0/json/mgmt/jobs/JobTest.java b/src/test/java/com/auth0/json/mgmt/jobs/JobTest.java index b42e4d85..a2ac66b8 100644 --- a/src/test/java/com/auth0/json/mgmt/jobs/JobTest.java +++ b/src/test/java/com/auth0/json/mgmt/jobs/JobTest.java @@ -9,7 +9,7 @@ public class JobTest extends JsonTest { - private static final String json = "{\"status\": \"completed\",\"type\": \"verification_email\",\"created_at\": \"12/09/2025\",\"id\": \"job_0000000000000001\"}"; + private static final String json = "{\"status\": \"completed\",\"type\": \"verification_email\",\"created_at\": \"2016-02-23T19:57:29.532Z\",\"id\": \"job_0000000000000001\"}"; @Test public void shouldDeserialize() throws Exception { @@ -19,6 +19,6 @@ public void shouldDeserialize() throws Exception { assertThat(job.getId(), is("job_0000000000000001")); assertThat(job.getStatus(), is("completed")); assertThat(job.getType(), is("verification_email")); - assertThat(job.getCreatedAt(), is("12/09/2025")); + assertThat(job.getCreatedAt(), is(parseJSONDate("2016-02-23T19:57:29.532Z"))); } } diff --git a/src/test/resources/mgmt/post_verification_email.json b/src/test/resources/mgmt/post_verification_email.json index 0dd23c06..95cad29d 100644 --- a/src/test/resources/mgmt/post_verification_email.json +++ b/src/test/resources/mgmt/post_verification_email.json @@ -1,6 +1,6 @@ { "status": "completed", "type": "verification_email", - "created_at": "12/09/2025", + "created_at": "", "id": "job_0000000000000001" } \ No newline at end of file From c6f8cb32ab44403a3eaadaee0ffd7f310f5fbcfc Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 7 Jun 2018 21:24:06 +0100 Subject: [PATCH 18/34] Remove create_at setter --- src/main/java/com/auth0/json/mgmt/jobs/Job.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main/java/com/auth0/json/mgmt/jobs/Job.java b/src/main/java/com/auth0/json/mgmt/jobs/Job.java index bbdf4d84..5e3b1d84 100644 --- a/src/main/java/com/auth0/json/mgmt/jobs/Job.java +++ b/src/main/java/com/auth0/json/mgmt/jobs/Job.java @@ -39,6 +39,7 @@ public String getType() { return type; } + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") @JsonProperty("created_at") public Date getCreatedAt() { return createdAt; @@ -48,10 +49,4 @@ public Date getCreatedAt() { public String getId() { return id; } - - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") - @JsonProperty("created_at") - public void setCreatedAt(Date createdAt) { - this.createdAt = createdAt; - } } From e5efd7091a5e51667a3a9c91646c49d6f4234827 Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 7 Jun 2018 21:26:52 +0100 Subject: [PATCH 19/34] Add body size assertion --- src/test/java/com/auth0/client/mgmt/JobsEntityTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java index ffb39201..0e2e0c20 100644 --- a/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java +++ b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java @@ -31,7 +31,7 @@ public void shouldSendAUserAVerificationEmail() throws Exception { assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); Map body = bodyFromRequest(recordedRequest); -// assertThat(body.size(), is(1)); + assertThat(body.size(), is(1)); assertThat(body, hasEntry("user_id", "google-oauth2|1234")); assertThat(response, is(notNullValue())); From 2d6030af691d5c1e3d7c9c54fdc9971b5caa1a66 Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Thu, 12 Apr 2018 14:29:27 -0300 Subject: [PATCH 20/34] add email templates endpoints --- .../client/mgmt/EmailTemplatesEntity.java | 120 ++++++++++++++++++ .../com/auth0/json/mgmt/EmailTemplate.java | 78 ++++++++++++ 2 files changed, 198 insertions(+) create mode 100644 src/main/java/com/auth0/client/mgmt/EmailTemplatesEntity.java create mode 100644 src/main/java/com/auth0/json/mgmt/EmailTemplate.java diff --git a/src/main/java/com/auth0/client/mgmt/EmailTemplatesEntity.java b/src/main/java/com/auth0/client/mgmt/EmailTemplatesEntity.java new file mode 100644 index 00000000..b6063db9 --- /dev/null +++ b/src/main/java/com/auth0/client/mgmt/EmailTemplatesEntity.java @@ -0,0 +1,120 @@ +package com.auth0.client.mgmt; + +import com.auth0.json.mgmt.EmailTemplate; +import com.auth0.net.CustomRequest; +import com.auth0.net.Request; +import com.auth0.utils.Asserts; +import com.fasterxml.jackson.core.type.TypeReference; +import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; + +/** + * Class that provides an implementation of the Email Templates methods of the Management API as defined in https://auth0.com/docs/api/management/v2#!/Email_Templates + */ +@SuppressWarnings({"WeakerAccess", "unused"}) +public class EmailTemplatesEntity extends BaseManagementEntity { + + public static final String TEMPLATE_VERIFY_EMAIL = "verify_email"; + public static final String TEMPLATE_RESET_EMAIL = "reset_email"; + public static final String TEMPLATE_WELCOME_EMAIL = "welcome_email"; + public static final String TEMPLATE_BLOCKED_ACCOUNT = "blocked_account"; + public static final String TEMPLATE_STOLEN_CREDENTIALS = "stolen_credentials"; + public static final String TEMPLATE_ENROLLMENT_EMAIL = "enrollment_email"; + public static final String TEMPLATE_CHANGE_PASSWORD = "change_password"; + public static final String TEMPLATE_PASSWORD_RESET = "password_reset"; + public static final String TEMPLATE_MFA_OOB_CODE = "mfa_oob_code"; + + EmailTemplatesEntity(OkHttpClient client, HttpUrl baseUrl, String apiToken) { + super(client, baseUrl, apiToken); + } + + /** + * Request the Email Templates. A token with scope read:email_templates is needed. + * See https://auth0.com/docs/api/management/v2#!/Email_Templates/get_email_templates_by_templateName + * + * @param templateName the template name to request. You can use any of the constants defined in {@link EmailTemplatesEntity} + * @return a Request to execute. + */ + public Request get(String templateName) { + Asserts.assertNotNull(templateName, "template name"); + HttpUrl.Builder builder = baseUrl + .newBuilder() + .addPathSegments("api/v2/email-templates") + .addPathSegment(templateName); + String url = builder.build().toString(); + CustomRequest request = new CustomRequest<>(client, url, "GET", new TypeReference() { + }); + request.addHeader("Authorization", "Bearer " + apiToken); + return request; + } + + /** + * Create a Rule. A token with scope create:rules is needed. + * See https://auth0.com/docs/api/management/v2#!/Rules/post_rules + * + * @param template the template data to set + * @return a Request to execute. + */ + public Request create(EmailTemplate template) { + Asserts.assertNotNull(template, "template"); + + String url = baseUrl + .newBuilder() + .addPathSegments("api/v2/email-templates") + .build() + .toString(); + CustomRequest request = new CustomRequest<>(this.client, url, "POST", new TypeReference() { + }); + request.addHeader("Authorization", "Bearer " + apiToken); + request.setBody(template); + return request; + } + + /** + * Patches the existing Email Template. A token with scope update:email_templates is needed. + * See https://auth0.com/docs/api/management/v2#!/Email_Templates/patch_email_templates_by_templateName + * + * @param templateName the name of the template to patch. You can use any of the constants defined in {@link EmailTemplatesEntity} + * @param template the email template data to set. + * @return a Request to execute. + */ + public Request patch(String templateName, EmailTemplate template) { + Asserts.assertNotNull(templateName, "template name"); + Asserts.assertNotNull(template, "template"); + + String url = baseUrl + .newBuilder() + .addPathSegments("api/v2/email-templates") + .build() + .toString(); + CustomRequest request = new CustomRequest<>(this.client, url, "PATCH", new TypeReference() { + }); + request.addHeader("Authorization", "Bearer " + apiToken); + request.setBody(template); + return request; + } + + /** + * Update the existing Email Template. A token with scope update:email_templates is needed. + * See https://auth0.com/docs/api/management/v2#!/Email_Templates/put_email_templates_by_templateName + * + * @param templateName the name of the template to update. You can use any of the constants defined in {@link EmailTemplatesEntity} + * @param template the email template data to set. + * @return a Request to execute. + */ + public Request update(String templateName, EmailTemplate template) { + Asserts.assertNotNull(templateName, "template name"); + Asserts.assertNotNull(template, "template"); + + String url = baseUrl + .newBuilder() + .addPathSegments("api/v2/email-templates") + .build() + .toString(); + CustomRequest request = new CustomRequest<>(this.client, url, "PUT", new TypeReference() { + }); + request.addHeader("Authorization", "Bearer " + apiToken); + request.setBody(template); + return request; + } +} diff --git a/src/main/java/com/auth0/json/mgmt/EmailTemplate.java b/src/main/java/com/auth0/json/mgmt/EmailTemplate.java new file mode 100644 index 00000000..1348c484 --- /dev/null +++ b/src/main/java/com/auth0/json/mgmt/EmailTemplate.java @@ -0,0 +1,78 @@ +package com.auth0.json.mgmt; + +public class EmailTemplate { + + private String template; + private String body; + private String from; + private String resultUrl; + private String subject; + private String syntax; + private int urlLifetimeInSeconds; + private boolean enabled; + + public String getTemplate() { + return template; + } + + public void setTemplate(String template) { + this.template = template; + } + + public String getBody() { + return body; + } + + public void setBody(String body) { + this.body = body; + } + + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } + + public String getResultUrl() { + return resultUrl; + } + + public void setResultUrl(String resultUrl) { + this.resultUrl = resultUrl; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getSyntax() { + return syntax; + } + + public void setSyntax(String syntax) { + this.syntax = syntax; + } + + public int getUrlLifetimeInSeconds() { + return urlLifetimeInSeconds; + } + + public void setUrlLifetimeInSeconds(int urlLifetimeInSeconds) { + this.urlLifetimeInSeconds = urlLifetimeInSeconds; + } + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + +} From eae9b5d47b3c08c4d229086d636f14c15de79955 Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Thu, 12 Apr 2018 14:37:05 -0300 Subject: [PATCH 21/34] Add missing email templates entity getter --- README.md | 3 ++- .../java/com/auth0/client/mgmt/ManagementAPI.java | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 76f5327b..210adfa1 100644 --- a/README.md +++ b/README.md @@ -279,7 +279,8 @@ The Management API is divided into different entities. Each of them have the lis * **User Blocks:** See [Docs](https://auth0.com/docs/api/management/v2#!/User_Blocks/get_user_blocks). Access the methods by calling `mgmt.userBlocks()`. * **Users:** See [this](https://auth0.com/docs/api/management/v2#!/Users/get_users) and [this](https://auth0.com/docs/api/management/v2#!/Users_By_Email) doc. Access the methods by calling `mgmt.users()`. * **Blacklists:** See [Docs](https://auth0.com/docs/api/management/v2#!/Blacklists/get_tokens). Access the methods by calling `mgmt.blacklists()`. -* **Emails:** See [Docs](https://auth0.com/docs/api/management/v2#!/Emails/get_provider). Access the methods by calling `mgmt.emailProvider()`. +* **Email Providers:** See [Docs](https://auth0.com/docs/api/management/v2#!/Emails/get_provider). Access the methods by calling `mgmt.emailProvider()`. +* **Email Templates:** See [Docs](https://auth0.com/docs/api/management/v2#!/Email_Templates/get_email_templates_by_templateName). Access the methods by calling `mgmt.emailTemplates()`. * **Guardian:** See [Docs](https://auth0.com/docs/api/management/v2#!/Guardian/get_factors). Access the methods by calling `mgmt.guardian()`. * **Stats:** See [Docs](https://auth0.com/docs/api/management/v2#!/Stats/get_active_users). Access the methods by calling `mgmt.stats()`. * **Tenants:** See [Docs](https://auth0.com/docs/api/management/v2#!/Tenants/get_settings). Access the methods by calling `mgmt.tenants()`. diff --git a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java index d821bd78..3f1a9499 100644 --- a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java +++ b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java @@ -169,6 +169,15 @@ public BlacklistsEntity blacklists() { return new BlacklistsEntity(client, baseUrl, apiToken); } + /** + * Getter for the Email Templates entity. + * + * @return the Email Templates entity. + */ + public EmailTemplatesEntity emailTemplates() { + return new EmailTemplatesEntity(client, baseUrl, apiToken); + } + /** * Getter for the Email Provider entity. * @@ -219,7 +228,7 @@ public TicketsEntity tickets() { * * @return the Resource Servers entity. */ - public ResourceServerEntity resourceServers(){ + public ResourceServerEntity resourceServers() { return new ResourceServerEntity(client, baseUrl, apiToken); } From cb58911d8a48269aaa962f53ae493855fdb1a46c Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Thu, 12 Apr 2018 17:57:16 -0300 Subject: [PATCH 22/34] add tests and update email-templates entity --- .../client/mgmt/EmailTemplatesEntity.java | 27 +--- .../com/auth0/json/mgmt/EmailTemplate.java | 144 ++++++++++++++++-- .../java/com/auth0/client/MockServer.java | 1 + .../client/mgmt/EmailTemplatesEntityTest.java | 127 +++++++++++++++ src/test/resources/mgmt/email_template.json | 8 + 5 files changed, 266 insertions(+), 41 deletions(-) create mode 100644 src/test/java/com/auth0/client/mgmt/EmailTemplatesEntityTest.java create mode 100644 src/test/resources/mgmt/email_template.json diff --git a/src/main/java/com/auth0/client/mgmt/EmailTemplatesEntity.java b/src/main/java/com/auth0/client/mgmt/EmailTemplatesEntity.java index b6063db9..2e67cd49 100644 --- a/src/main/java/com/auth0/client/mgmt/EmailTemplatesEntity.java +++ b/src/main/java/com/auth0/client/mgmt/EmailTemplatesEntity.java @@ -74,30 +74,6 @@ public Request create(EmailTemplate template) { * Patches the existing Email Template. A token with scope update:email_templates is needed. * See https://auth0.com/docs/api/management/v2#!/Email_Templates/patch_email_templates_by_templateName * - * @param templateName the name of the template to patch. You can use any of the constants defined in {@link EmailTemplatesEntity} - * @param template the email template data to set. - * @return a Request to execute. - */ - public Request patch(String templateName, EmailTemplate template) { - Asserts.assertNotNull(templateName, "template name"); - Asserts.assertNotNull(template, "template"); - - String url = baseUrl - .newBuilder() - .addPathSegments("api/v2/email-templates") - .build() - .toString(); - CustomRequest request = new CustomRequest<>(this.client, url, "PATCH", new TypeReference() { - }); - request.addHeader("Authorization", "Bearer " + apiToken); - request.setBody(template); - return request; - } - - /** - * Update the existing Email Template. A token with scope update:email_templates is needed. - * See https://auth0.com/docs/api/management/v2#!/Email_Templates/put_email_templates_by_templateName - * * @param templateName the name of the template to update. You can use any of the constants defined in {@link EmailTemplatesEntity} * @param template the email template data to set. * @return a Request to execute. @@ -109,9 +85,10 @@ public Request update(String templateName, EmailTemplate template String url = baseUrl .newBuilder() .addPathSegments("api/v2/email-templates") + .addPathSegment(templateName) .build() .toString(); - CustomRequest request = new CustomRequest<>(this.client, url, "PUT", new TypeReference() { + CustomRequest request = new CustomRequest<>(this.client, url, "PATCH", new TypeReference() { }); request.addHeader("Authorization", "Bearer " + apiToken); request.setBody(template); diff --git a/src/main/java/com/auth0/json/mgmt/EmailTemplate.java b/src/main/java/com/auth0/json/mgmt/EmailTemplate.java index 1348c484..cd2d3b4e 100644 --- a/src/main/java/com/auth0/json/mgmt/EmailTemplate.java +++ b/src/main/java/com/auth0/json/mgmt/EmailTemplate.java @@ -1,77 +1,189 @@ package com.auth0.json.mgmt; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Class that represents an Auth0 Guardian Factor object. Related to the {@link com.auth0.client.mgmt.GuardianEntity} entity. + */ +@SuppressWarnings({"unused", "WeakerAccess"}) +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(JsonInclude.Include.NON_NULL) public class EmailTemplate { - private String template; + @JsonProperty("template") + private String name; + @JsonProperty("body") private String body; + @JsonProperty("from") private String from; + @JsonProperty("resultUrl") private String resultUrl; + @JsonProperty("subject") private String subject; + @JsonProperty("syntax") private String syntax; - private int urlLifetimeInSeconds; - private boolean enabled; - - public String getTemplate() { - return template; - } - - public void setTemplate(String template) { - this.template = template; - } - + @JsonProperty("urlLifetimeInSeconds") + private Integer urlLifetimeInSeconds; + @JsonProperty("enabled") + private Boolean enabled; + + /** + * Getter for the name of the template. + * + * @return the name. + */ + @JsonProperty("template") + public String getName() { + return name; + } + + /** + * Sets the name of the template. + */ + @JsonProperty("template") + public void setName(String name) { + this.name = name; + } + + /** + * Getter for the template code. + * + * @return the template code. + */ + @JsonProperty("body") public String getBody() { return body; } + /** + * Sets the template code + * + * @param body the code this template will have + */ + @JsonProperty("body") public void setBody(String body) { this.body = body; } + /** + * Getter for the sender of the email + * + * @return the sender of the email + */ + @JsonProperty("from") public String getFrom() { return from; } + /** + * Sets the sender of the email + * + * @param from the sender of the email + */ + @JsonProperty("from") public void setFrom(String from) { this.from = from; } + /** + * Getter the URL to redirect the user to after a successful action. + * + * @return the URL to redirect the user to after a successful action. + */ + @JsonProperty("resultUrl") public String getResultUrl() { return resultUrl; } + /** + * Sets the URL to redirect the user to after a successful action. + * + * @param resultUrl the URL to redirect the user to after a successful action. + */ + @JsonProperty("resultUrl") public void setResultUrl(String resultUrl) { this.resultUrl = resultUrl; } + /** + * Getter for the subject of the email. + * + * @return the subject of the email. + */ + @JsonProperty("subject") public String getSubject() { return subject; } + /** + * Sets the subject of the email. + * + * @param subject the subject of the email. + */ + @JsonProperty("subject") public void setSubject(String subject) { this.subject = subject; } + /** + * Getter for the syntax used in the template's code. + * + * @return the syntax used in the template's code. + */ + @JsonProperty("syntax") public String getSyntax() { return syntax; } + /** + * Sets for the syntax to be used in the template's code. + * + * @param syntax the syntax to be used in the template's code. + */ + @JsonProperty("syntax") public void setSyntax(String syntax) { this.syntax = syntax; } - public int getUrlLifetimeInSeconds() { + /** + * Getter for the lifetime in seconds that the link within the email will be valid for. + * + * @return the lifetime in seconds that the link within the email will be valid for. + */ + @JsonProperty("urlLifetimeInSeconds") + public Integer getUrlLifetimeInSeconds() { return urlLifetimeInSeconds; } - public void setUrlLifetimeInSeconds(int urlLifetimeInSeconds) { + /** + * Sets the lifetime in seconds that the link within the email will be valid for. + * + * @param urlLifetimeInSeconds the lifetime in seconds that the link within the email will be valid for. + */ + @JsonProperty("urlLifetimeInSeconds") + public void setUrlLifetimeInSeconds(Integer urlLifetimeInSeconds) { this.urlLifetimeInSeconds = urlLifetimeInSeconds; } - public boolean isEnabled() { + /** + * Whether or not this template is enabled. + * + * @return true if this template is enabled, false otherwise. + */ + @JsonProperty("enabled") + public Boolean isEnabled() { return enabled; } - public void setEnabled(boolean enabled) { + /** + * Enables or disables this template. + * + * @param enabled whether this template is enabled or not. + */ + @JsonProperty("enabled") + public void setEnabled(Boolean enabled) { this.enabled = enabled; } diff --git a/src/test/java/com/auth0/client/MockServer.java b/src/test/java/com/auth0/client/MockServer.java index e8213c91..2eb3c1e0 100644 --- a/src/test/java/com/auth0/client/MockServer.java +++ b/src/test/java/com/auth0/client/MockServer.java @@ -45,6 +45,7 @@ public class MockServer { public static final String MGMT_USER_BLOCKS = "src/test/resources/mgmt/user_blocks.json"; public static final String MGMT_BLACKLISTED_TOKENS_LIST = "src/test/resources/mgmt/blacklisted_tokens_list.json"; public static final String MGMT_EMAIL_PROVIDER = "src/test/resources/mgmt/email_provider.json"; + public static final String MGMT_EMAIL_TEMPLATE = "src/test/resources/mgmt/email_template.json"; public static final String MGMT_USERS_LIST = "src/test/resources/mgmt/users_list.json"; public static final String MGMT_USERS_PAGED_LIST = "src/test/resources/mgmt/users_paged_list.json"; public static final String MGMT_USER = "src/test/resources/mgmt/user.json"; diff --git a/src/test/java/com/auth0/client/mgmt/EmailTemplatesEntityTest.java b/src/test/java/com/auth0/client/mgmt/EmailTemplatesEntityTest.java new file mode 100644 index 00000000..0240f3d4 --- /dev/null +++ b/src/test/java/com/auth0/client/mgmt/EmailTemplatesEntityTest.java @@ -0,0 +1,127 @@ +package com.auth0.client.mgmt; + +import com.auth0.json.mgmt.EmailTemplate; +import com.auth0.net.Request; +import okhttp3.mockwebserver.RecordedRequest; +import org.junit.Test; + +import java.util.Map; + +import static com.auth0.client.MockServer.*; +import static com.auth0.client.RecordedRequestMatcher.hasHeader; +import static com.auth0.client.RecordedRequestMatcher.hasMethodAndPath; +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertThat; + +@SuppressWarnings("RedundantThrows") +public class EmailTemplatesEntityTest extends BaseMgmtEntityTest { + @Test + public void shouldGetEmailTemplate() throws Exception { + Request request = api.emailTemplates().get("welcome_email"); + assertThat(request, is(notNullValue())); + + server.jsonResponse(MGMT_EMAIL_TEMPLATE, 200); + EmailTemplate response = request.execute(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath("GET", "/api/v2/email-templates/welcome_email")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + + assertThat(response, is(notNullValue())); + } + + @Test + public void shouldThrowOnGetEmailTemplateWithNullName() throws Exception { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("'template name' cannot be null!"); + api.emailTemplates().get(null); + } + + @Test + public void shouldCreateEmailTemplate() throws Exception { + EmailTemplate template = new EmailTemplate(); + template.setName("welcome_email"); + template.setBody("Welcome!!"); + template.setFrom("auth0.com"); + template.setSubject("Welcome"); + template.setSyntax("liquid"); + template.setEnabled(true); + + Request request = api.emailTemplates().create(template); + assertThat(request, is(notNullValue())); + + server.jsonResponse(MGMT_EMAIL_PROVIDER, 200); + EmailTemplate response = request.execute(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath("POST", "/api/v2/email-templates")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + + Map body = bodyFromRequest(recordedRequest); + assertThat(body.size(), is(6)); + assertThat(body, hasEntry("template", (Object) "welcome_email")); + assertThat(body, hasEntry("body", (Object) "Welcome!!")); + assertThat(body, hasEntry("from", (Object) "auth0.com")); + assertThat(body, hasEntry("syntax", (Object) "liquid")); + assertThat(body, hasEntry("subject", (Object) "Welcome")); + assertThat(body, hasEntry("enabled", (Object) true)); + assertThat(body, not(hasKey("resultUrl"))); + assertThat(body, not(hasKey("urlLifetimeInSeconds"))); + + assertThat(response, is(notNullValue())); + } + + @Test + public void shouldThrowOnCreateEmailTemplateWithNullData() throws Exception { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("'template' cannot be null!"); + api.emailTemplates().create(null); + } + + @Test + public void shouldPatchEmailTemplate() throws Exception { + EmailTemplate template = new EmailTemplate(); + template.setBody("New"); + template.setUrlLifetimeInSeconds(123); + template.setResultUrl("https://somewhere.com"); + + Request request = api.emailTemplates().update("welcome_email", template); + assertThat(request, is(notNullValue())); + + server.jsonResponse(MGMT_EMAIL_PROVIDER, 200); + EmailTemplate response = request.execute(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath("PATCH", "/api/v2/email-templates/welcome_email")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + + Map body = bodyFromRequest(recordedRequest); + assertThat(body.size(), is(3)); + assertThat(body, hasEntry("resultUrl", (Object) "https://somewhere.com")); + assertThat(body, hasEntry("body", (Object) "New")); + assertThat(body, hasEntry("urlLifetimeInSeconds", (Object) 123)); + assertThat(body, not(hasKey("template"))); + assertThat(body, not(hasKey("from"))); + assertThat(body, not(hasKey("syntax"))); + assertThat(body, not(hasKey("enabled"))); + + assertThat(response, is(notNullValue())); + } + + @Test + public void shouldThrowOnPatchEmailTemplateWithNullData() throws Exception { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("'template' cannot be null!"); + api.emailTemplates().update("welcome_email", null); + } + + @Test + public void shouldThrowOnPatchEmailTemplateWithNullName() throws Exception { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("'template' cannot be null!"); + api.emailTemplates().update(null, new EmailTemplate()); + } +} diff --git a/src/test/resources/mgmt/email_template.json b/src/test/resources/mgmt/email_template.json new file mode 100644 index 00000000..8871f923 --- /dev/null +++ b/src/test/resources/mgmt/email_template.json @@ -0,0 +1,8 @@ +{ + "template": "welcome_email", + "from": "me@auth0.com", + "subject": "Some subject", + "syntax": "liquid", + "body": " ", + "enabled": false +} \ No newline at end of file From b51675d86939df60a603de0ceb5e9f6ab5ee1892 Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Thu, 19 Apr 2018 16:20:16 -0300 Subject: [PATCH 23/34] (: codavaj xif --- src/main/java/com/auth0/client/mgmt/EmailTemplatesEntity.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/auth0/client/mgmt/EmailTemplatesEntity.java b/src/main/java/com/auth0/client/mgmt/EmailTemplatesEntity.java index 2e67cd49..2e693be5 100644 --- a/src/main/java/com/auth0/client/mgmt/EmailTemplatesEntity.java +++ b/src/main/java/com/auth0/client/mgmt/EmailTemplatesEntity.java @@ -49,8 +49,8 @@ public Request get(String templateName) { } /** - * Create a Rule. A token with scope create:rules is needed. - * See https://auth0.com/docs/api/management/v2#!/Rules/post_rules + * Create an Email Template. A token with scope create:email_templates is needed. + * See https://auth0.com/docs/api/management/v2#!/Email_Templates/post_email_templates * * @param template the template data to set * @return a Request to execute. From 93acdfab3cd3ed6e4fbfcaa375420c2f9ca4a454 Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Fri, 11 May 2018 17:01:08 -0300 Subject: [PATCH 24/34] fix class level javadoc --- src/main/java/com/auth0/json/mgmt/EmailTemplate.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/auth0/json/mgmt/EmailTemplate.java b/src/main/java/com/auth0/json/mgmt/EmailTemplate.java index cd2d3b4e..1613d5ad 100644 --- a/src/main/java/com/auth0/json/mgmt/EmailTemplate.java +++ b/src/main/java/com/auth0/json/mgmt/EmailTemplate.java @@ -5,7 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; /** - * Class that represents an Auth0 Guardian Factor object. Related to the {@link com.auth0.client.mgmt.GuardianEntity} entity. + * Class that represents an Email Template object. Related to the {@link com.auth0.client.mgmt.EmailTemplatesEntity} entity. */ @SuppressWarnings({"unused", "WeakerAccess"}) @JsonIgnoreProperties(ignoreUnknown = true) From 998c095465e554a2404b93aaec6e217314fe1fed Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Fri, 11 May 2018 17:07:13 -0300 Subject: [PATCH 25/34] fix test --- .../java/com/auth0/client/mgmt/EmailTemplatesEntityTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/auth0/client/mgmt/EmailTemplatesEntityTest.java b/src/test/java/com/auth0/client/mgmt/EmailTemplatesEntityTest.java index 0240f3d4..b030e91c 100644 --- a/src/test/java/com/auth0/client/mgmt/EmailTemplatesEntityTest.java +++ b/src/test/java/com/auth0/client/mgmt/EmailTemplatesEntityTest.java @@ -121,7 +121,7 @@ public void shouldThrowOnPatchEmailTemplateWithNullData() throws Exception { @Test public void shouldThrowOnPatchEmailTemplateWithNullName() throws Exception { exception.expect(IllegalArgumentException.class); - exception.expectMessage("'template' cannot be null!"); + exception.expectMessage("'template name' cannot be null!"); api.emailTemplates().update(null, new EmailTemplate()); } } From 1b19583421897e147e7d13f2a23f23674941dd78 Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Thu, 24 May 2018 17:14:01 -0300 Subject: [PATCH 26/34] change default for syntax. add json tests --- .../com/auth0/json/mgmt/EmailTemplate.java | 6 ++ .../client/mgmt/EmailTemplatesEntityTest.java | 4 +- .../auth0/json/mgmt/EmailTemplateTest.java | 57 +++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/auth0/json/mgmt/EmailTemplateTest.java diff --git a/src/main/java/com/auth0/json/mgmt/EmailTemplate.java b/src/main/java/com/auth0/json/mgmt/EmailTemplate.java index 1613d5ad..002ed2cb 100644 --- a/src/main/java/com/auth0/json/mgmt/EmailTemplate.java +++ b/src/main/java/com/auth0/json/mgmt/EmailTemplate.java @@ -29,6 +29,11 @@ public class EmailTemplate { @JsonProperty("enabled") private Boolean enabled; + public EmailTemplate() { + //Only here to set the syntax default value + this.syntax = "liquid"; + } + /** * Getter for the name of the template. * @@ -139,6 +144,7 @@ public String getSyntax() { /** * Sets for the syntax to be used in the template's code. + * Default value is 'liquid' * * @param syntax the syntax to be used in the template's code. */ diff --git a/src/test/java/com/auth0/client/mgmt/EmailTemplatesEntityTest.java b/src/test/java/com/auth0/client/mgmt/EmailTemplatesEntityTest.java index b030e91c..e4be6481 100644 --- a/src/test/java/com/auth0/client/mgmt/EmailTemplatesEntityTest.java +++ b/src/test/java/com/auth0/client/mgmt/EmailTemplatesEntityTest.java @@ -99,13 +99,13 @@ public void shouldPatchEmailTemplate() throws Exception { assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); Map body = bodyFromRequest(recordedRequest); - assertThat(body.size(), is(3)); + assertThat(body.size(), is(4)); assertThat(body, hasEntry("resultUrl", (Object) "https://somewhere.com")); assertThat(body, hasEntry("body", (Object) "New")); + assertThat(body, hasEntry("syntax", (Object) "liquid")); assertThat(body, hasEntry("urlLifetimeInSeconds", (Object) 123)); assertThat(body, not(hasKey("template"))); assertThat(body, not(hasKey("from"))); - assertThat(body, not(hasKey("syntax"))); assertThat(body, not(hasKey("enabled"))); assertThat(response, is(notNullValue())); diff --git a/src/test/java/com/auth0/json/mgmt/EmailTemplateTest.java b/src/test/java/com/auth0/json/mgmt/EmailTemplateTest.java new file mode 100644 index 00000000..617ce68f --- /dev/null +++ b/src/test/java/com/auth0/json/mgmt/EmailTemplateTest.java @@ -0,0 +1,57 @@ +package com.auth0.json.mgmt; + +import com.auth0.json.JsonMatcher; +import com.auth0.json.JsonTest; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; + +public class EmailTemplateTest extends JsonTest { + + private static final String json = "{\"template\": \"welcome_email\", \"from\": \"you@auth0.com\", \"subject\": \"Some subject\", \"syntax\": \"liquid\", \"body\": \" \", \"enabled\": true}"; + + @Test + public void shouldSerialize() throws Exception { + EmailTemplate template = new EmailTemplate(); + template.setResultUrl("https://auth0.com"); + template.setUrlLifetimeInSeconds(993311); + template.setBody("SOME HTML"); + template.setEnabled(false); + template.setSyntax("html"); + template.setSubject("Hello world"); + template.setFrom("me@auth0.com"); + template.setName("farewell"); + + String serialized = toJSON(template); + assertThat(serialized, is(notNullValue())); + assertThat(serialized, JsonMatcher.hasEntry("resultUrl", "https://auth0.com")); + assertThat(serialized, JsonMatcher.hasEntry("urlLifetimeInSeconds", 993311)); + assertThat(serialized, JsonMatcher.hasEntry("body", "SOME HTML")); + assertThat(serialized, JsonMatcher.hasEntry("enabled", false)); + assertThat(serialized, JsonMatcher.hasEntry("syntax", "html")); + assertThat(serialized, JsonMatcher.hasEntry("subject", "Hello world")); + assertThat(serialized, JsonMatcher.hasEntry("from", "me@auth0.com")); + assertThat(serialized, JsonMatcher.hasEntry("template", "farewell")); + } + + @Test + public void shouldDeserialize() throws Exception { + EmailTemplate template = fromJSON(json, EmailTemplate.class); + + assertThat(template, is(notNullValue())); + assertThat(template.getName(), is("welcome_email")); + assertThat(template.getFrom(), is("you@auth0.com")); + assertThat(template.getSubject(), is("Some subject")); + assertThat(template.getSyntax(), is("liquid")); + assertThat(template.getBody(), is(" ")); + assertThat(template.isEnabled(), is(true)); + } + + @Test + public void shouldHaveDefaults() { + EmailTemplate template = new EmailTemplate(); + assertThat(template.getSyntax(), is("liquid")); + } +} \ No newline at end of file From 9d78f6f1442f9dc4e2c4d282b6719c63ac0c7731 Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Mon, 4 Jun 2018 15:12:29 -0300 Subject: [PATCH 27/34] Release 1.6.0 --- CHANGELOG.md | 7 +++++++ README.md | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35c9d88b..ea424b3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Change Log +## [1.6.0](https://github.com/auth0/auth0-java/tree/1.6.0) (2018-06-04) +[Full Changelog](https://github.com/auth0/auth0-java/compare/1.5.1...1.6.0) + +**Added** +- Expose additional error response properties in the Exception [\#123](https://github.com/auth0/auth0-java/pull/123) ([lbalmaceda](https://github.com/lbalmaceda)) +- Add email-templates endpoints [\#117](https://github.com/auth0/auth0-java/pull/117) ([lbalmaceda](https://github.com/lbalmaceda)) + ## [1.5.1](https://github.com/auth0/auth0-java/tree/1.5.1) (2018-03-01) [Full Changelog](https://github.com/auth0/auth0-java/compare/1.5.0...1.5.1) diff --git a/README.md b/README.md index 210adfa1..9b5dd056 100644 --- a/README.md +++ b/README.md @@ -16,14 +16,14 @@ Get Auth0 Java via Maven: com.auth0 auth0 - 1.5.1 + 1.6.0 ``` or Gradle: ```gradle -compile 'com.auth0:auth0:1.5.1' +compile 'com.auth0:auth0:1.6.0' ``` From 46505b91b2c185ba9050612183206cdad394380e Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 7 Jun 2018 21:52:08 +0100 Subject: [PATCH 28/34] Map String to Object --- src/main/java/com/auth0/client/mgmt/JobsEntity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/auth0/client/mgmt/JobsEntity.java b/src/main/java/com/auth0/client/mgmt/JobsEntity.java index 358dc660..7d4e0656 100644 --- a/src/main/java/com/auth0/client/mgmt/JobsEntity.java +++ b/src/main/java/com/auth0/client/mgmt/JobsEntity.java @@ -38,7 +38,7 @@ public Request sendVerificationEmail(String userId, String clientId) { .build() .toString(); - Map requestBody = new HashMap<>(); + Map requestBody = new HashMap<>(); requestBody.put("user_id", userId); if (clientId != null && !clientId.isEmpty()) { requestBody.put("client_id", clientId); From bffe90f8b3c749cea509506c0a193f617079e1b6 Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 7 Jun 2018 21:56:40 +0100 Subject: [PATCH 29/34] Cast Map to Object --- src/main/java/com/auth0/client/mgmt/JobsEntity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/auth0/client/mgmt/JobsEntity.java b/src/main/java/com/auth0/client/mgmt/JobsEntity.java index 7d4e0656..2d16f05c 100644 --- a/src/main/java/com/auth0/client/mgmt/JobsEntity.java +++ b/src/main/java/com/auth0/client/mgmt/JobsEntity.java @@ -47,7 +47,7 @@ public Request sendVerificationEmail(String userId, String clientId) { CustomRequest request = new CustomRequest<>(client, url, "POST", new TypeReference() { }); request.addHeader("Authorization", "Bearer " + apiToken); - request.setBody(requestBody); + request.setBody((Object) requestBody); return request; } } From eedc230ebaad0f68af38eaaa89dc1ffad603a3a0 Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 7 Jun 2018 21:57:08 +0100 Subject: [PATCH 30/34] Turn Object back to String --- src/main/java/com/auth0/client/mgmt/JobsEntity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/auth0/client/mgmt/JobsEntity.java b/src/main/java/com/auth0/client/mgmt/JobsEntity.java index 2d16f05c..cfbddcc4 100644 --- a/src/main/java/com/auth0/client/mgmt/JobsEntity.java +++ b/src/main/java/com/auth0/client/mgmt/JobsEntity.java @@ -38,7 +38,7 @@ public Request sendVerificationEmail(String userId, String clientId) { .build() .toString(); - Map requestBody = new HashMap<>(); + Map requestBody = new HashMap<>(); requestBody.put("user_id", userId); if (clientId != null && !clientId.isEmpty()) { requestBody.put("client_id", clientId); From ffc37fc36196006c368ddb0eb6107cdcb5af8673 Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 7 Jun 2018 22:15:24 +0100 Subject: [PATCH 31/34] Cast String to Object in test --- src/test/java/com/auth0/client/mgmt/JobsEntityTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java index 0e2e0c20..8c25cf42 100644 --- a/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java +++ b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java @@ -32,7 +32,7 @@ public void shouldSendAUserAVerificationEmail() throws Exception { Map body = bodyFromRequest(recordedRequest); assertThat(body.size(), is(1)); - assertThat(body, hasEntry("user_id", "google-oauth2|1234")); + assertThat(body, hasEntry("user_id", (Object) "google-oauth2|1234")); assertThat(response, is(notNullValue())); } From edb587711b6687851e6164e71bb3084e9e065439 Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 7 Jun 2018 22:19:07 +0100 Subject: [PATCH 32/34] Cast String to Object in test --- src/test/java/com/auth0/client/mgmt/JobsEntityTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java index 0e2e0c20..8c25cf42 100644 --- a/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java +++ b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java @@ -32,7 +32,7 @@ public void shouldSendAUserAVerificationEmail() throws Exception { Map body = bodyFromRequest(recordedRequest); assertThat(body.size(), is(1)); - assertThat(body, hasEntry("user_id", "google-oauth2|1234")); + assertThat(body, hasEntry("user_id", (Object) "google-oauth2|1234")); assertThat(response, is(notNullValue())); } From 56173d635aba657f0666757c15623dcb25a9fb47 Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Thu, 7 Jun 2018 22:30:57 +0100 Subject: [PATCH 33/34] Add test for scenario when clientId is specified --- .../com/auth0/client/mgmt/JobsEntityTest.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java index 8c25cf42..d01d86dc 100644 --- a/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java +++ b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java @@ -37,6 +37,27 @@ public void shouldSendAUserAVerificationEmail() throws Exception { assertThat(response, is(notNullValue())); } + @Test + public void shouldSendUserAVerificationEmailWithClientId() throws Exception { + Request request = api.jobs().sendVerificationEmail("google-oauth2|1234", "google-oauth2|client-id"); + assertThat(request, is(notNullValue())); + + server.jsonResponse(MGMT_JOB_POST_VERIFICATION_EMAIL, 200); + Job response = request.execute(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath("POST", "/api/v2/jobs/verification-email")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + + Map body = bodyFromRequest(recordedRequest); + assertThat(body.size(), is(2)); + assertThat(body, hasEntry("user_id", (Object) "google-oauth2|1234")); + assertThat(body, hasEntry("client_id", (Object) "google-oauth2|client-id")); + + assertThat(response, is(notNullValue())); + } + @Test public void shouldThrowOnNullEmailRecipient() throws Exception { exception.expect(IllegalArgumentException.class); From 8af65bd7a9884ff914b23edb6da0d1d0c1393a4d Mon Sep 17 00:00:00 2001 From: Minh-Long Do Date: Fri, 8 Jun 2018 22:50:10 +0100 Subject: [PATCH 34/34] Changes made based on PR comments --- src/main/java/com/auth0/client/mgmt/JobsEntity.java | 4 ++-- src/main/java/com/auth0/client/mgmt/ManagementAPI.java | 2 +- src/test/java/com/auth0/client/mgmt/JobsEntityTest.java | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/auth0/client/mgmt/JobsEntity.java b/src/main/java/com/auth0/client/mgmt/JobsEntity.java index cfbddcc4..3bb147cd 100644 --- a/src/main/java/com/auth0/client/mgmt/JobsEntity.java +++ b/src/main/java/com/auth0/client/mgmt/JobsEntity.java @@ -30,7 +30,7 @@ public class JobsEntity extends BaseManagementEntity { * @return a Request to execute. */ public Request sendVerificationEmail(String userId, String clientId) { - Asserts.assertNotNull(userId, "recipient"); + Asserts.assertNotNull(userId, "user id"); String url = baseUrl .newBuilder() @@ -47,7 +47,7 @@ public Request sendVerificationEmail(String userId, String clientId) { CustomRequest request = new CustomRequest<>(client, url, "POST", new TypeReference() { }); request.addHeader("Authorization", "Bearer " + apiToken); - request.setBody((Object) requestBody); + request.setBody(requestBody); return request; } } diff --git a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java index 3f1a9499..62aa7df6 100644 --- a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java +++ b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java @@ -233,7 +233,7 @@ public ResourceServerEntity resourceServers() { } /** - * Getter for Jobs entity. + * Getter for the Jobs entity. * * @return the Jobs entity. */ diff --git a/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java index d01d86dc..e9e049a8 100644 --- a/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java +++ b/src/test/java/com/auth0/client/mgmt/JobsEntityTest.java @@ -39,7 +39,7 @@ public void shouldSendAUserAVerificationEmail() throws Exception { @Test public void shouldSendUserAVerificationEmailWithClientId() throws Exception { - Request request = api.jobs().sendVerificationEmail("google-oauth2|1234", "google-oauth2|client-id"); + Request request = api.jobs().sendVerificationEmail("google-oauth2|1234", "AaiyAPdpYdesoKnqjj8HJqRn4T5titww"); assertThat(request, is(notNullValue())); server.jsonResponse(MGMT_JOB_POST_VERIFICATION_EMAIL, 200); @@ -53,15 +53,15 @@ public void shouldSendUserAVerificationEmailWithClientId() throws Exception { Map body = bodyFromRequest(recordedRequest); assertThat(body.size(), is(2)); assertThat(body, hasEntry("user_id", (Object) "google-oauth2|1234")); - assertThat(body, hasEntry("client_id", (Object) "google-oauth2|client-id")); + assertThat(body, hasEntry("client_id", (Object) "AaiyAPdpYdesoKnqjj8HJqRn4T5titww")); assertThat(response, is(notNullValue())); } @Test - public void shouldThrowOnNullEmailRecipient() throws Exception { + public void shouldThrowOnNullUserId() throws Exception { exception.expect(IllegalArgumentException.class); - exception.expectMessage("'recipient' cannot be null!"); + exception.expectMessage("'user id' cannot be null!"); api.jobs().sendVerificationEmail(null, null); } }