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

Include a proper SignUp response #92

Merged
merged 3 commits into from
Nov 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fields.put("city", "Buenos Aires");
SignUpRequest request = auth.signUp("[email protected]", "username", "password123", "Username-Password-Authentication")
.setCustomFields(fields);
try {
request.execute();
CreatedUser user = request.execute();
} catch (APIException exception) {
// api error
} catch (Auth0Exception exception) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/auth0/client/auth/AuthAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public Request resetPassword(String email, String connection) {
public SignUpRequest signUp(String email, String username, String password, String connection) {
Asserts.assertNotNull(username, "username");

VoidRequest request = (VoidRequest) this.signUp(email, password, connection);
CreateUserRequest request = (CreateUserRequest) this.signUp(email, password, connection);
request.addParameter(KEY_USERNAME, username);
return request;
}
Expand Down Expand Up @@ -284,7 +284,7 @@ public SignUpRequest signUp(String email, String password, String connection) {
.addPathSegment("signup")
.build()
.toString();
VoidRequest request = new VoidRequest(client, url, "POST");
CreateUserRequest request = new CreateUserRequest(client, url);
request.addParameter(KEY_CLIENT_ID, clientId);
request.addParameter(KEY_EMAIL, email);
request.addParameter(KEY_PASSWORD, password);
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/com/auth0/json/auth/CreatedUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.auth0.json.auth;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Class that holds the data of a newly created User. Obtained after a call to {@link com.auth0.client.auth.AuthAPI#signUp(String, String, String)}
* or {@link com.auth0.client.auth.AuthAPI#signUp(String, String, String, String)}.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CreatedUser {

@JsonProperty("_id")
private String userId;
@JsonProperty("email")
private String email;
@JsonProperty("username")
private String username;
@JsonProperty("email_verified")
private Boolean emailVerified;

@JsonProperty("_id")
public String getUserId() {
return userId;
}

@JsonProperty("email")
public String getEmail() {
return email;
}

@JsonProperty("username")
public String getUsername() {
return username;
}

@JsonProperty("email_verified")
public Boolean isEmailVerified() {
return emailVerified;
}

}
3 changes: 2 additions & 1 deletion src/main/java/com/auth0/json/auth/UserInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import java.util.Map;

/**
* Class that holds the Information related to a User's Access Token. Obtained after a call to {@link com.auth0.client.auth.AuthAPI#userInfo(String)}.
* Class that holds the Information related to a User's Access Token. Obtained after a call to {@link com.auth0.client.auth.AuthAPI#userInfo(String)},
* {@link com.auth0.client.auth.AuthAPI#signUp(String, String, String)} or {@link com.auth0.client.auth.AuthAPI#signUp(String, String, String, String)}.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/auth0/net/CreateUserRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.auth0.net;

import com.auth0.json.auth.CreatedUser;
import com.fasterxml.jackson.core.type.TypeReference;
import okhttp3.OkHttpClient;

import java.util.Map;

public class CreateUserRequest extends CustomRequest<CreatedUser> implements SignUpRequest {

public CreateUserRequest(OkHttpClient client, String url) {
super(client, url, "POST", new TypeReference<CreatedUser>() {
});
}

@Override
public SignUpRequest setCustomFields(Map<String, String> customFields) {
super.addParameter("user_metadata", customFields);
return this;
}
}
5 changes: 4 additions & 1 deletion src/main/java/com/auth0/net/SignUpRequest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.auth0.net;

import com.auth0.json.auth.CreatedUser;

import java.util.Map;

/**
* Class that represents a Create User call.
*/
public interface SignUpRequest extends Request<Void> {
@SuppressWarnings("UnusedReturnValue")
public interface SignUpRequest extends Request<CreatedUser> {

/**
* Setter for the additional fields to set when creating a user.
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/com/auth0/net/VoidRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import java.util.Map;

public class VoidRequest extends CustomRequest<Void> implements SignUpRequest {
public class VoidRequest extends CustomRequest<Void> {

public VoidRequest(OkHttpClient client, String url, String method) {
super(client, url, method, new TypeReference<Void>() {
Expand All @@ -21,10 +21,4 @@ protected Void parseResponse(Response response) throws Auth0Exception {
}
return null;
}

@Override
public VoidRequest setCustomFields(Map<String, String> customFields) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is a BC as Public API removal, this is a compile fail for anyone using this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"CustomFields" were supposed to be available only for SignUp methods. Because of how the request classes were inherited, this method ended wrongly in the VoidRequest class. No, it wouldn't be a breaking change as there's not a single API method signature that declares a return type of VoidRequest. They all either return an AuthRequest (never a TokenRequest), SignUpRequest (never a CreateUserRequest) or a simple Request or Request<T>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If people were casting that returned type to VoidRequest (in the case that that method call was returning a VoidRequest class or subclass) and using that method, then yes it would be a breaking change. But that's why we return interfaces rather than classes, to obscure our internals. So I wouldn't consider this a breaking change anyway.

super.addParameter("user_metadata", customFields);
return this;
}
}
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 @@ -18,6 +18,7 @@ public class MockServer {
public static final String AUTH_USER_INFO = "src/test/resources/auth/user_info.json";
public static final String AUTH_RESET_PASSWORD = "src/test/resources/auth/reset_password.json";
public static final String AUTH_SIGN_UP = "src/test/resources/auth/sign_up.json";
public static final String AUTH_SIGN_UP_USERNAME = "src/test/resources/auth/sign_up_username.json";
public static final String AUTH_TOKENS = "src/test/resources/auth/tokens.json";
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";
Expand Down
27 changes: 20 additions & 7 deletions src/test/java/com/auth0/client/auth/AuthAPITest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.auth0.client.auth;

import com.auth0.client.MockServer;
import com.auth0.json.auth.CreatedUser;
import com.auth0.json.auth.TokenHolder;
import com.auth0.json.auth.UserInfo;
import com.auth0.net.AuthRequest;
Expand Down Expand Up @@ -383,8 +384,8 @@ public void shouldCreateSignUpRequestWithUsername() throws Exception {
SignUpRequest request = api.signUp("[email protected]", "me", "p455w0rd", "db-connection");
assertThat(request, is(notNullValue()));

server.jsonResponse(AUTH_SIGN_UP, 200);
Void response = request.execute();
server.jsonResponse(AUTH_SIGN_UP_USERNAME, 200);
CreatedUser response = request.execute();
RecordedRequest recordedRequest = server.takeRequest();

assertThat(recordedRequest, hasMethodAndPath("POST", "/dbconnections/signup"));
Expand All @@ -397,7 +398,11 @@ public void shouldCreateSignUpRequestWithUsername() throws Exception {
assertThat(body, hasEntry("connection", (Object) "db-connection"));
assertThat(body, hasEntry("client_id", (Object) CLIENT_ID));

assertThat(response, is(nullValue()));
assertThat(response, is(notNullValue()));
assertThat(response.getUserId(), is("58457fe6b27"));
assertThat(response.getEmail(), is("[email protected]"));
assertThat(response.isEmailVerified(), is(false));
assertThat(response.getUsername(), is("me"));
}

@Test
Expand All @@ -406,7 +411,7 @@ public void shouldCreateSignUpRequest() throws Exception {
assertThat(request, is(notNullValue()));

server.jsonResponse(AUTH_SIGN_UP, 200);
Void response = request.execute();
CreatedUser response = request.execute();
RecordedRequest recordedRequest = server.takeRequest();

assertThat(recordedRequest, hasMethodAndPath("POST", "/dbconnections/signup"));
Expand All @@ -419,7 +424,11 @@ public void shouldCreateSignUpRequest() throws Exception {
assertThat(body, hasEntry("client_id", (Object) CLIENT_ID));
assertThat(body, not(hasKey("username")));

assertThat(response, is(nullValue()));
assertThat(response, is(notNullValue()));
assertThat(response.getUserId(), is("58457fe6b27"));
assertThat(response.getEmail(), is("[email protected]"));
assertThat(response.isEmailVerified(), is(false));
assertThat(response.getUsername(), is(nullValue()));
}

@Test
Expand All @@ -432,7 +441,7 @@ public void shouldCreateSignUpRequestWithCustomParameters() throws Exception {
request.setCustomFields(customFields);

server.jsonResponse(AUTH_SIGN_UP, 200);
Void response = request.execute();
CreatedUser response = request.execute();
RecordedRequest recordedRequest = server.takeRequest();

assertThat(recordedRequest, hasMethodAndPath("POST", "/dbconnections/signup"));
Expand All @@ -449,7 +458,11 @@ public void shouldCreateSignUpRequestWithCustomParameters() throws Exception {
assertThat(metadata, hasEntry("address", "123, fake street"));
assertThat(body, not(hasKey("username")));

assertThat(response, is(nullValue()));
assertThat(response, is(notNullValue()));
assertThat(response.getUserId(), is("58457fe6b27"));
assertThat(response.getEmail(), is("[email protected]"));
assertThat(response.isEmailVerified(), is(false));
assertThat(response.getUsername(), is(nullValue()));
}


Expand Down
63 changes: 63 additions & 0 deletions src/test/java/com/auth0/net/CreateUserRequestTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.auth0.net;

import com.auth0.client.MockServer;
import com.auth0.json.auth.CreatedUser;
import okhttp3.OkHttpClient;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static com.auth0.client.MockServer.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;

public class CreateUserRequestTest {

private OkHttpClient client;
private MockServer server;

@Before
public void setUp() throws Exception {
client = new OkHttpClient();
server = new MockServer();
}

@Test
public void shouldCreatePOSTRequest() throws Exception {
CreateUserRequest request = new CreateUserRequest(client, server.getBaseUrl());
assertThat(request, is(notNullValue()));
request.addParameter("non_empty", "body");

server.jsonResponse(AUTH_SIGN_UP, 200);
CreatedUser execute = request.execute();
RecordedRequest recordedRequest = server.takeRequest();
assertThat(recordedRequest.getMethod(), is("POST"));
assertThat(execute, is(notNullValue()));
}

@Test
public void shouldSetSignUpCustomFields() throws Exception {
CreateUserRequest request = new CreateUserRequest(client, server.getBaseUrl());
assertThat(request, is(notNullValue()));
Map<String, String> customFields = new HashMap<>();
customFields.put("name", "john");
customFields.put("age", "25");
request.setCustomFields(customFields);

server.jsonResponse(AUTH_TOKENS, 200);
request.execute();
RecordedRequest recordedRequest = server.takeRequest();

Map<String, Object> values = bodyFromRequest(recordedRequest);
assertThat(values, is(notNullValue()));
assertThat(values, hasKey("user_metadata"));
Map<String, String> fields = (Map<String, String>) values.get("user_metadata");
assertThat(fields, hasEntry("name", "john"));
assertThat(fields, hasEntry("age", "25"));
}


}
25 changes: 0 additions & 25 deletions src/test/java/com/auth0/net/VoidRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static com.auth0.client.MockServer.AUTH_TOKENS;
import static com.auth0.client.MockServer.bodyFromRequest;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;

Expand Down Expand Up @@ -49,25 +45,4 @@ public void shouldCreatePOSTRequest() throws Exception {
assertThat(execute, is(nullValue()));
}

@Test
public void shouldSetSignUpCustomFields() throws Exception {
VoidRequest request = new VoidRequest(client, server.getBaseUrl(), "POST");
assertThat(request, is(notNullValue()));
Map<String, String> customFields = new HashMap<>();
customFields.put("name", "john");
customFields.put("age", "25");
request.setCustomFields(customFields);

server.jsonResponse(AUTH_TOKENS, 200);
request.execute();
RecordedRequest recordedRequest = server.takeRequest();

Map<String, Object> values = bodyFromRequest(recordedRequest);
assertThat(values, is(notNullValue()));
assertThat(values, hasKey("user_metadata"));
Map<String, String> fields = (Map<String, String>) values.get("user_metadata");
assertThat(fields, hasEntry("name", "john"));
assertThat(fields, hasEntry("age", "25"));
}

}
4 changes: 2 additions & 2 deletions src/test/resources/auth/sign_up.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"_id": "58457fe6b27...",
"_id": "58457fe6b27",
"email_verified": false,
"email": "test.account@signup.com"
"email": "me@auth0.com"
}
6 changes: 6 additions & 0 deletions src/test/resources/auth/sign_up_username.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"_id": "58457fe6b27",
"email_verified": false,
"email": "[email protected]",
"username": "me"
}