Skip to content

Commit

Permalink
added test to exercise id mismatch with production data #2418
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Jul 28, 2015
1 parent c92d00d commit e45dd79
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/main/java/edu/harvard/iq/dataverse/api/Admin.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,17 @@ public Response deleteAuthenticationProvider( @PathParam("id") String id ) {
+ ( authSvc.getAuthenticationProviderIds().isEmpty()
? "WARNING: no enabled authentication providers left." : ""));
}


@GET
@Path("authenticatedUsers/{identifier}/")
public Response getAuthenticatedUser(@PathParam("identifier") String identifier) {
AuthenticatedUser authenticatedUser = authSvc.getAuthenticatedUser(identifier);
if (authenticatedUser != null) {
return okResponse(jsonForAuthUser(authenticatedUser));
}
return errorResponse(Response.Status.BAD_REQUEST, "User " + identifier + " not found.");
}

@DELETE
@Path("authenticatedUsers/{identifier}/")
public Response deleteAuthenticatedUser(@PathParam("identifier") String identifier) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/util/json/JsonPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.ip.IpAddressRange;
import edu.harvard.iq.dataverse.authorization.groups.impl.shib.ShibGroup;
import edu.harvard.iq.dataverse.authorization.providers.AuthenticationProviderRow;
import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser;
import edu.harvard.iq.dataverse.authorization.users.User;
import edu.harvard.iq.dataverse.util.DatasetFieldWalker;
import java.util.Set;
Expand Down Expand Up @@ -68,6 +69,18 @@ public static JsonObjectBuilder json( User u ) {
.add("Title", displayInfo.getTitle())
.add("email", displayInfo.getEmailAddress()));
}

/**
* @todo Rename this to just "json" to match the other methods once "json(
* Dataverse dv )" is reviewed since in calls the "json( User u )" version
* and we want to keep it that way rather than calling this method.
*/
public static JsonObjectBuilder jsonForAuthUser(AuthenticatedUser authenticatedUser) {
return jsonObjectBuilder()
.add("identifier", authenticatedUser.getIdentifier())
.add("id", authenticatedUser.getId()
);
}

public static JsonObjectBuilder json( RoleAssignment ra ) {
return jsonObjectBuilder()
Expand Down
102 changes: 102 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/BuiltinUsersIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package edu.harvard.iq.dataverse.api;

import static com.jayway.restassured.RestAssured.given;
import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.path.json.JsonPath;
import com.jayway.restassured.response.Response;
import java.util.UUID;
import java.util.logging.Logger;
import javax.json.Json;
import javax.json.JsonObjectBuilder;
import static junit.framework.Assert.assertEquals;
import org.junit.Test;

public class BuiltinUsersIT {

private static final Logger logger = Logger.getLogger(BuiltinUsersIT.class.getCanonicalName());

private static final String builtinUserKey = "burrito";
private static final String idKey = "id";
private static final String usernameKey = "userName";
private static final String emailKey = "email";

@Test
public void testUserId() {

Response createUserResponse = createUser(getRandomUsername(), "firstName", "lastName");
createUserResponse.prettyPrint();
assertEquals(200, createUserResponse.getStatusCode());

JsonPath createdUser = JsonPath.from(createUserResponse.body().asString());
int userIdFromJsonCreateResponse = createdUser.getInt("data.user." + idKey);
String username = createdUser.getString("data.user." + usernameKey);

Response getUserResponse = getUserFromDatabase(username);
getUserResponse.prettyPrint();
assertEquals(200, getUserResponse.getStatusCode());

JsonPath getUserJson = JsonPath.from(getUserResponse.body().asString());
int userIdFromDatabase = getUserJson.getInt("data.id");

Response deleteUserResponse = deleteUser(username);
assertEquals(200, deleteUserResponse.getStatusCode());
deleteUserResponse.prettyPrint();

System.out.println(userIdFromDatabase + " was the id from the database");
System.out.println(userIdFromJsonCreateResponse + " was the id from JSON response on create");
/**
* This test is expected to pass on a clean, fresh database but for an
* unknown reason it fails when you load it up with a production
* database from dataverse.harvard.edu. Why? This is what
* https://github.com/IQSS/dataverse/issues/2418 is about.
*/
assertEquals(userIdFromDatabase, userIdFromJsonCreateResponse);
}

private Response createUser(String username, String firstName, String lastName) {
String userAsJson = getUserAsJsonString(username, firstName, lastName);
String password = getPassword(userAsJson);
Response response = given()
.body(userAsJson)
.contentType(ContentType.JSON)
.post("/api/builtin-users?key=" + builtinUserKey + "&password=" + password);
return response;
}

private Response getUserFromDatabase(String username) {
Response getUserResponse = given()
.get("/api/admin/authenticatedUsers/" + username + "/");
return getUserResponse;
}

private static Response deleteUser(String username) {
Response deleteUserResponse = given()
.delete("/api/admin/authenticatedUsers/" + username + "/");
return deleteUserResponse;
}

private static String getRandomUsername() {
return UUID.randomUUID().toString().substring(0, 8);
}

private static String getUserAsJsonString(String username, String firstName, String lastName) {
JsonObjectBuilder builder = Json.createObjectBuilder();
builder.add(usernameKey, username);
builder.add("firstName", firstName);
builder.add("lastName", lastName);
builder.add(emailKey, getEmailFromUserName(username));
String userAsJson = builder.build().toString();
logger.fine("User to create: " + userAsJson);
return userAsJson;
}

private static String getPassword(String jsonStr) {
String password = JsonPath.from(jsonStr).get(usernameKey);
return password;
}

private static String getEmailFromUserName(String username) {
return username + "@mailinator.com";
}

}

0 comments on commit e45dd79

Please sign in to comment.