Skip to content

Commit

Permalink
fix the tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Liang <jiallian@amazon.com>
  • Loading branch information
RyanL1997 committed Feb 22, 2023
1 parent 0f42b1a commit 64682f9
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,7 @@ public List<RestHandler> getRestHandlers(
new ActionHandler<>(MultiGetUserAction.INSTANCE, TransportMultiGetUserAction.class),
new ActionHandler<>(DeleteUserAction.INSTANCE, TransportDeleteUserAction.class),
new ActionHandler<>(PutPermissionAction.INSTANCE, TransportPutPermissionAction.class),
new ActionHandler<>(DeleteUserAction.INSTANCE, TransportDeleteUserAction.class),
new ActionHandler<>(ResetPasswordAction.INSTANCE, TransportResetPasswordAction.class),
new ActionHandler<>(IdentityConfigUpdateAction.INSTANCE, TransportIdentityConfigUpdateAction.class)

new ActionHandler<>(ResetPasswordAction.INSTANCE, TransportResetPasswordAction.class)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ public void handleRequest(RestRequest request, RestChannel channel, NodeClient c
if (authTokenHeader == null) {
Subject currentSubject = Identity.getAuthManager().getSubject();
// TODO replace with Principal Identifier Token if destination is extension
jwtClaims.put("sub", currentSubject.getPrincipal().getName());
if (currentSubject != null) {
jwtClaims.put("sub", currentSubject.getPrincipal().getName());
}
jwtClaims.put("iat", Instant.now().toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public enum ErrorType {
OLDPASSWORD_MISMATCHING("Old passwords do not match."),
USER_NOT_EXISTING("Failed to reset password, because target user does not exist."),
NEWPASSWORD_MATCHING_OLDPASSWORD("New password is same as the current password, please create another new password.");

private String message;

private ErrorType(String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected boolean preserveIndicesUponCompletion() {
* This warning is expected to be thrown as we are accessing identity index directly
* @return the warning message to be expected
*/
public RequestOptions options() {
public static RequestOptions options() {

RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
options.addHeader("Authorization", "Basic YWRtaW46YWRtaW4="); // admin:admin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void testPermissionsRestApi() throws Exception {
// _identity/api/permissions/test
Request putRequest = new Request("PUT", endpoint + "/" + username);
putRequest.setJsonEntity("{ \"permission\" : \"cluster.admin/read\"}\n");
putRequest.setOptions(IdentityRestTestCase.options());
Response putResponse = client().performRequest(putRequest);
assertThat(putResponse.getStatusLine().getStatusCode(), is(200));
assertTrue(new String(putResponse.getEntity().getContent().readAllBytes(), StandardCharsets.UTF_8).contains("true"));
Expand All @@ -77,6 +78,7 @@ public void testPermissionsRestApi() throws Exception {

putRequest = new Request("PUT", endpoint + "/" + username);
putRequest.setJsonEntity("{ \"permission\" : \":1:2:3\"}\n"); // Invalid permission
putRequest.setOptions(IdentityRestTestCase.options());
try {
putResponse = client().performRequest(putRequest);
} catch (ResponseException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import org.opensearch.client.Request;
import org.opensearch.client.Response;
import org.opensearch.client.ResponseException;
import org.opensearch.identity.IdentityConfigConstants;
import org.opensearch.identity.rest.IdentityRestConstants;
import org.opensearch.identity.utils.ErrorType;
import org.opensearch.test.rest.OpenSearchRestTestCase;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -112,7 +109,7 @@ public void testResetPassword() throws Exception {
String createMessage = username + " created successfully.";
Request userCreationRequest = new Request("PUT", ENDPOINT + "/users/" + username);
userCreationRequest.setJsonEntity(userCreationContent);
userCreationRequest.setOptions(systemIndexWarning());
userCreationRequest.setOptions(options());
Response userCreationResponse = client().performRequest(userCreationRequest);
assertEquals(userCreationResponse.getStatusLine().getStatusCode(), 200);
Map<String, Object> userCreated = entityAsMap(userCreationResponse);
Expand All @@ -123,7 +120,7 @@ public void testResetPassword() throws Exception {

Request request = new Request("POST", ENDPOINT + "/users/" + username + "/resetpassword");
request.setJsonEntity(requestContent);
request.setOptions(systemIndexWarning());
request.setOptions(options());
Response response = client().performRequest(request);
assertEquals(200, response.getStatusLine().getStatusCode());
}
Expand All @@ -134,7 +131,7 @@ public void testResetPasswordWithNotExistedUser() throws Exception {

Request request = new Request("POST", ENDPOINT + "/users/" + username + "/resetpassword");
request.setJsonEntity(requestContent);
request.setOptions(systemIndexWarning());
request.setOptions(options());
ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(request));
Map<String, Object> exception = entityAsMap(e.getResponse());
assertEquals(400, exception.get("status"));
Expand All @@ -151,7 +148,7 @@ public void testResetPasswordWithBadRequests() throws Exception {
String createMessage = username + " created successfully.";
Request userCreationRequest = new Request("PUT", ENDPOINT + "/users/" + username);
userCreationRequest.setJsonEntity(userCreationContent);
userCreationRequest.setOptions(systemIndexWarning());
userCreationRequest.setOptions(options());
Response userCreationResponse = client().performRequest(userCreationRequest);
assertEquals(userCreationResponse.getStatusLine().getStatusCode(), 200);
Map<String, Object> userCreated = entityAsMap(userCreationResponse);
Expand All @@ -163,7 +160,7 @@ public void testResetPasswordWithBadRequests() throws Exception {
// Old password mismatching
Request requestOldPasswordMismatching = new Request("POST", ENDPOINT + "/users/" + username + "/resetpassword");
requestOldPasswordMismatching.setJsonEntity(oldPasswordsDontMatch);
requestOldPasswordMismatching.setOptions(systemIndexWarning());
requestOldPasswordMismatching.setOptions(options());
ResponseException eOldPasswordMismatching = expectThrows(
ResponseException.class,
() -> client().performRequest(requestOldPasswordMismatching)
Expand All @@ -178,7 +175,7 @@ public void testResetPasswordWithBadRequests() throws Exception {
// New password matching old password
Request requestNewPasswordMatchingOldPassword = new Request("POST", ENDPOINT + "/users/" + username + "/resetpassword");
requestNewPasswordMatchingOldPassword.setJsonEntity(newPasswordsMatchOldPassword);
requestNewPasswordMatchingOldPassword.setOptions(systemIndexWarning());
requestNewPasswordMatchingOldPassword.setOptions(options());
ResponseException eNewPasswordMatchingOldPassword = expectThrows(
ResponseException.class,
() -> client().performRequest(requestNewPasswordMatchingOldPassword)
Expand Down

0 comments on commit 64682f9

Please sign in to comment.