-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from minhlongdo/resend-verification-email
Add Resend Verification Email functionality
- Loading branch information
Showing
7 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.auth0.client.mgmt; | ||
|
||
import com.auth0.json.mgmt.jobs.Job; | ||
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; | ||
|
||
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 | ||
*/ | ||
@SuppressWarnings("WeakerAccess") | ||
public class JobsEntity extends BaseManagementEntity { | ||
|
||
JobsEntity(OkHttpClient client, HttpUrl baseUrl, String apiToken) { | ||
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 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<Job> sendVerificationEmail(String userId, String clientId) { | ||
Asserts.assertNotNull(userId, "user id"); | ||
|
||
String url = baseUrl | ||
.newBuilder() | ||
.addPathSegments("api/v2/jobs/verification-email") | ||
.build() | ||
.toString(); | ||
|
||
Map<String, String> requestBody = new HashMap<>(); | ||
requestBody.put("user_id", userId); | ||
if (clientId != null && !clientId.isEmpty()) { | ||
requestBody.put("client_id", clientId); | ||
} | ||
|
||
CustomRequest<Job> request = new CustomRequest<>(client, url, "POST", new TypeReference<Job>() { | ||
}); | ||
request.addHeader("Authorization", "Bearer " + apiToken); | ||
request.setBody(requestBody); | ||
return request; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.auth0.json.mgmt.jobs; | ||
|
||
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. | ||
*/ | ||
@SuppressWarnings({"unused", "WeakerAccess"}) | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public class Job { | ||
|
||
@JsonProperty("status") | ||
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 Date createdAt; | ||
@JsonProperty("id") | ||
private String id; | ||
|
||
@JsonCreator | ||
private 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; | ||
} | ||
|
||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") | ||
@JsonProperty("created_at") | ||
public Date getCreatedAt() { | ||
return createdAt; | ||
} | ||
|
||
@JsonProperty("id") | ||
public String getId() { | ||
return id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.auth0.client.mgmt; | ||
|
||
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<Job> request = api.jobs().sendVerificationEmail("google-oauth2|1234", null); | ||
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<String, Object> body = bodyFromRequest(recordedRequest); | ||
assertThat(body.size(), is(1)); | ||
assertThat(body, hasEntry("user_id", (Object) "google-oauth2|1234")); | ||
|
||
assertThat(response, is(notNullValue())); | ||
} | ||
|
||
@Test | ||
public void shouldSendUserAVerificationEmailWithClientId() throws Exception { | ||
Request<Job> request = api.jobs().sendVerificationEmail("google-oauth2|1234", "AaiyAPdpYdesoKnqjj8HJqRn4T5titww"); | ||
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<String, Object> body = bodyFromRequest(recordedRequest); | ||
assertThat(body.size(), is(2)); | ||
assertThat(body, hasEntry("user_id", (Object) "google-oauth2|1234")); | ||
assertThat(body, hasEntry("client_id", (Object) "AaiyAPdpYdesoKnqjj8HJqRn4T5titww")); | ||
|
||
assertThat(response, is(notNullValue())); | ||
} | ||
|
||
@Test | ||
public void shouldThrowOnNullUserId() throws Exception { | ||
exception.expect(IllegalArgumentException.class); | ||
exception.expectMessage("'user id' cannot be null!"); | ||
api.jobs().sendVerificationEmail(null, null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Job> { | ||
|
||
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 { | ||
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(parseJSONDate("2016-02-23T19:57:29.532Z"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"status": "completed", | ||
"type": "verification_email", | ||
"created_at": "", | ||
"id": "job_0000000000000001" | ||
} |