Skip to content

Commit

Permalink
Merge pull request #120 from minhlongdo/resend-verification-email
Browse files Browse the repository at this point in the history
Add Resend Verification Email functionality
  • Loading branch information
lbalmaceda authored Jun 11, 2018
2 parents 0a26f6f + 8af65bd commit 3c408c0
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/main/java/com/auth0/client/mgmt/JobsEntity.java
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;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/auth0/client/mgmt/ManagementAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,13 @@ public TicketsEntity tickets() {
public ResourceServerEntity resourceServers() {
return new ResourceServerEntity(client, baseUrl, apiToken);
}

/**
* Getter for the Jobs entity.
*
* @return the Jobs entity.
*/
public JobsEntity jobs() {
return new JobsEntity(client, baseUrl, apiToken);
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/auth0/json/mgmt/jobs/Job.java
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;
}
}
1 change: 1 addition & 0 deletions src/test/java/com/auth0/client/MockServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,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;
Expand Down
67 changes: 67 additions & 0 deletions src/test/java/com/auth0/client/mgmt/JobsEntityTest.java
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);
}
}
24 changes: 24 additions & 0 deletions src/test/java/com/auth0/json/mgmt/jobs/JobTest.java
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")));
}
}
6 changes: 6 additions & 0 deletions src/test/resources/mgmt/post_verification_email.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"status": "completed",
"type": "verification_email",
"created_at": "",
"id": "job_0000000000000001"
}

0 comments on commit 3c408c0

Please sign in to comment.