-
Notifications
You must be signed in to change notification settings - Fork 488
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added test to exercise id mismatch with production data #2418
- Loading branch information
Showing
3 changed files
with
126 additions
and
1 deletion.
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
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
102 changes: 102 additions & 0 deletions
102
src/test/java/edu/harvard/iq/dataverse/api/BuiltinUsersIT.java
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,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"; | ||
} | ||
|
||
} |