Skip to content

Commit

Permalink
Test changeAuthenticatedUserIdentifier #5515
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-a-dunlap committed Feb 21, 2019
1 parent 24fd317 commit 9b901aa
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void executeImpl(CommandContext ctxt) throws CommandException {
bu.setUserName(newIdentifier);

//Validate the BuiltinUser change. Username validations are there.
//If we have our validation errors pass up to commands, this could be removed
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<BuiltinUser>> violations = validator.validate(bu);
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/AdminIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,46 @@ public void testConvertOAuthUserToBuiltin() throws Exception {
assertEquals(200, deleteSuperuser.getStatusCode());

}

@Test
public void testChangeAuthenticatedUserIdentifier() {
Response createSuperuser = UtilIT.createRandomUser();
String superuserUsername = UtilIT.getUsernameFromResponse(createSuperuser);
String superuserApiToken = UtilIT.getApiTokenFromResponse(createSuperuser);
Response toggleSuperuser = UtilIT.makeSuperUser(superuserUsername);
toggleSuperuser.then().assertThat()
.statusCode(OK.getStatusCode());

Response createUser = UtilIT.createRandomUser();
createUser.prettyPrint();
assertEquals(200, createUser.getStatusCode());
String usernameOfUser = UtilIT.getUsernameFromResponse(createUser);

String newUsername = "newUser_" + UtilIT.getRandomString(4);
Response changeAuthIdResponse = UtilIT.changeAuthenticatedUserIdentifier(usernameOfUser, newUsername, superuserApiToken);
changeAuthIdResponse.prettyPrint();
changeAuthIdResponse.then().assertThat()
.statusCode(OK.getStatusCode());

String newUsernameBad = ""; //one character, should fail before bean validation even
Response changeAuthIdResponseBad = UtilIT.changeAuthenticatedUserIdentifier(usernameOfUser, newUsernameBad, superuserApiToken);
changeAuthIdResponseBad.prettyPrint();
changeAuthIdResponseBad.then().assertThat()
.statusCode(BAD_REQUEST.getStatusCode());

String newUsernameBad2 = "z"; //one character, should fail bean validation
Response changeAuthIdResponseBad2 = UtilIT.changeAuthenticatedUserIdentifier(usernameOfUser, newUsernameBad2, superuserApiToken);
changeAuthIdResponseBad2.prettyPrint();
changeAuthIdResponseBad2.then().assertThat()
.statusCode(BAD_REQUEST.getStatusCode());

//if this fails likely one of the converts that said they failed actually didn't!
Response deleteUserToConvert = UtilIT.deleteUser(newUsername);
assertEquals(200, deleteUserToConvert.getStatusCode());

Response deleteSuperuser = UtilIT.deleteUser(superuserUsername);
assertEquals(200, deleteSuperuser.getStatusCode());
}

@Test
public void testCreateNonBuiltinUserViaApi() {
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,14 @@ static Response migrateBuiltinToOAuth(String data, String apiToken) {
.put("/api/admin/authenticatedUsers/convert/builtin2oauth");
return response;
}

static Response changeAuthenticatedUserIdentifier(String oldIdentifier, String newIdentifier, String apiToken) {
Response response = given()
.header(API_TOKEN_HTTP_HEADER, apiToken)
.body(newIdentifier)
.put("/api/admin/authenticatedUsers/changeIdentifier/"+ oldIdentifier );
return response;
}

static Response restrictFile(String fileIdOrPersistentId, boolean restrict, String apiToken) {
String idInPath = fileIdOrPersistentId; // Assume it's a number.
Expand Down

0 comments on commit 9b901aa

Please sign in to comment.