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

Enhance RestLayerPrivilegesEvaluator Code Coverage #3218

Merged
merged 13 commits into from
Aug 23, 2023
Merged
Copy link
Member

Choose a reason for hiding this comment

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

If code cov says that there is a no change, are you sure these tests are executed and their coverage data submitted? Do you see these tests executed from the artifacts of the check runs?

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does not look like it ran. I am not sure how to fix that though. I don't think it is something related to this change--I triggered a re-run.

Copy link
Collaborator

@willyborankin willyborankin Aug 21, 2023

Choose a reason for hiding this comment

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

@scrawfor99 I think you need to call it RestPathMatchesTests.java not RestPathMatchesTest.java.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe it is not only your case but others as well.

Copy link
Member

Choose a reason for hiding this comment

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

Nice catch @willyborankin . I've got a change that will expect 100% code coverage in the tests (e.g. all test code should be executed) that should help us catch this kind of issue

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes I did the same mistake :-D. Here is PR: #3224. Lets see.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Huh, who knew. I will swap over the name.

Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,25 @@ public void testDifferentPathSegments() throws InvocationTargetException, Illega
String handlerPath = "_plugins/security/api/x/y";
assertFalse((Boolean) restPathMatches.invoke(securityRestFilter, requestPath, handlerPath));
}

@Test
public void testRequestPathWithNamedParam() throws InvocationTargetException, IllegalAccessException {
String requestPath = "_plugins/security/api/123/y";
String handlerPath = "_plugins/security/api/{id}/z";
assertFalse((Boolean) restPathMatches.invoke(securityRestFilter, requestPath, handlerPath));
}

@Test
public void testRequestPathMismatch() throws InvocationTargetException, IllegalAccessException {
String requestPath = "_plugins/security/api/x/y";
String handlerPath = "_plugins/security/api/z/y";
assertFalse((Boolean) restPathMatches.invoke(securityRestFilter, requestPath, handlerPath));
}

@Test
public void testRequestPathWithExtraSegments() throws InvocationTargetException, IllegalAccessException {
String requestPath = "_plugins/security/api/x/y/z";
String handlerPath = "_plugins/security/api/x/y";
assertFalse((Boolean) restPathMatches.invoke(securityRestFilter, requestPath, handlerPath));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void testEvaluate_Successful_NewPermission() {
when(securityRoles.impliesClusterPermissionPermission(action)).thenReturn(true);

PrivilegesEvaluatorResponse response = privilegesEvaluator.evaluate(TEST_USER, Set.of(action));

assertTrue(privilegesEvaluator.isInitialized());
assertTrue(response.allowed);
verify(securityRoles).impliesClusterPermissionPermission(any());
}
Expand Down Expand Up @@ -160,4 +160,16 @@ public void testEvaluate_Unsuccessful() {
assertFalse(response.allowed);
verify(securityRoles).impliesClusterPermissionPermission(any());
}

@Test
public void testIsInitialized_False() {
privilegesEvaluator = new RestLayerPrivilegesEvaluator(
clusterService,
threadPool,
mock(AuditLog.class),
mock(ClusterInfoHolder.class),
namedXContentRegistry
);
assertFalse(privilegesEvaluator.isInitialized());
}
}