Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Resend verification email functionality #120

Merged
merged 39 commits into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
3cc0034
Add 'Send verification email functionality' with tests
minhlongdo Apr 25, 2018
4863f85
Add documentation
minhlongdo Apr 26, 2018
2e5796a
Cast String to Object
minhlongdo Apr 26, 2018
2c8a6af
Add tests for deserialization
minhlongdo Apr 26, 2018
e073168
Remove setters for required fields
minhlongdo Apr 26, 2018
9493162
Merge branch 'master' into resend-verification-email
minhlongdo May 19, 2018
9c0c46f
Make public constructor private
minhlongdo Jun 7, 2018
91b0982
Merge branch 'resend-verification-email' of github.com:minhlongdo/aut…
minhlongdo Jun 7, 2018
c223179
Remove EmailRecipient class and replaced it with userId and optional …
minhlongdo Jun 7, 2018
666f4bd
Remove create_at setter
minhlongdo Jun 7, 2018
95e0a4e
Merge branch 'master' into resend-verification-email
minhlongdo Jun 7, 2018
78c950b
Add body size assertion
minhlongdo Jun 7, 2018
7dfa12c
Merge branch 'resend-verification-email' of github.com:minhlongdo/aut…
minhlongdo Jun 7, 2018
915f51f
Map String to Object
minhlongdo Jun 7, 2018
8e39c7a
Cast Map to Object
minhlongdo Jun 7, 2018
39b8d5b
Turn Object back to String
minhlongdo Jun 7, 2018
aa63893
expose additional error response properties in the exception
lbalmaceda Apr 27, 2018
754ef0e
use unmmodifiable map when possible
lbalmaceda Apr 27, 2018
fa10b1c
add missing test case
lbalmaceda Apr 27, 2018
58b4d30
add casting. fixes jdk7 test
lbalmaceda May 3, 2018
49415e0
Remove EmailRecipient class and replaced it with userId and optional …
minhlongdo Jun 7, 2018
c6f8cb3
Remove create_at setter
minhlongdo Jun 7, 2018
e5efd70
Add body size assertion
minhlongdo Jun 7, 2018
2d6030a
add email templates endpoints
lbalmaceda Apr 12, 2018
eae9b5d
Add missing email templates entity getter
lbalmaceda Apr 12, 2018
cb58911
add tests and update email-templates entity
lbalmaceda Apr 12, 2018
b51675d
(: codavaj xif
lbalmaceda Apr 19, 2018
93acdfa
fix class level javadoc
lbalmaceda May 11, 2018
998c095
fix test
lbalmaceda May 11, 2018
1b19583
change default for syntax. add json tests
lbalmaceda May 24, 2018
9d78f6f
Release 1.6.0
lbalmaceda Jun 4, 2018
46505b9
Map String to Object
minhlongdo Jun 7, 2018
bffe90f
Cast Map to Object
minhlongdo Jun 7, 2018
eedc230
Turn Object back to String
minhlongdo Jun 7, 2018
ffc37fc
Cast String to Object in test
minhlongdo Jun 7, 2018
f334502
Merge branch 'resend-verification-email' of github.com:minhlongdo/aut…
minhlongdo Jun 7, 2018
edb5877
Cast String to Object in test
minhlongdo Jun 7, 2018
56173d6
Add test for scenario when clientId is specified
minhlongdo Jun 7, 2018
8af65bd
Changes made based on PR comments
minhlongdo Jun 8, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}