-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
143 additions
and
119 deletions.
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
81 changes: 80 additions & 1 deletion
81
api-admin/src/test/java/bio/terra/pearl/api/admin/AuthTestUtilsTest.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 |
---|---|---|
@@ -1,2 +1,81 @@ | ||
package bio.terra.pearl.api.admin;public class AuthTestUtilsTest { | ||
package bio.terra.pearl.api.admin; | ||
|
||
import bio.terra.pearl.api.admin.service.auth.EnforcePortalPermission; | ||
import bio.terra.pearl.api.admin.service.auth.SandboxOnly; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class AuthTestUtilsTest { | ||
@Test | ||
public void testChecksAnnotatedMethod() { | ||
AuthTestUtils.assertAllMethodsAnnotated( | ||
new MethodAnnotatedClass(), | ||
Map.of("doSomethingSecure", AuthAnnotationSpec.withPortalPerm("BASE"))); | ||
|
||
// fails if the wrong permission is specced | ||
Assertions.assertThrows( | ||
AssertionError.class, | ||
() -> { | ||
AuthTestUtils.assertAllMethodsAnnotated( | ||
new MethodAnnotatedClass(), | ||
Map.of("doSomethingSecure", AuthAnnotationSpec.withPortalPerm("wrongPerm"))); | ||
}); | ||
|
||
// fails if the spec also expects a SandboxOnly annotation | ||
Assertions.assertThrows( | ||
AssertionError.class, | ||
() -> { | ||
AuthTestUtils.assertAllMethodsAnnotated( | ||
new MethodAnnotatedClass(), | ||
Map.of( | ||
"doSomethingSecure", | ||
AuthAnnotationSpec.withPortalPerm("wrongPerm", List.of(SandboxOnly.class)))); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testFailsUnspecifiedMethod() { | ||
Assertions.assertThrows( | ||
IllegalArgumentException.class, | ||
() -> { | ||
AuthTestUtils.assertAllMethodsAnnotated(new MethodAnnotatedClass(), Map.of()); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testFailsMethodNotAnnotated() { | ||
Assertions.assertThrows( | ||
AssertionError.class, | ||
() -> { | ||
AuthTestUtils.assertAllMethodsAnnotated( | ||
new MethodNotAnnotatedClass(), | ||
Map.of("doSomethingSecure", AuthAnnotationSpec.withPortalPerm("BASE"))); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testChecksSandboxAnnotated() { | ||
AuthTestUtils.assertAllMethodsAnnotated( | ||
new SandboxMethodAnnotatedClass(), | ||
Map.of( | ||
"doSomethingSecure", | ||
AuthAnnotationSpec.withPortalPerm("BASE", List.of(SandboxOnly.class)))); | ||
} | ||
|
||
public static class MethodAnnotatedClass { | ||
@EnforcePortalPermission(permission = "BASE") | ||
public void doSomethingSecure() {} | ||
} | ||
|
||
public static class MethodNotAnnotatedClass { | ||
public void doSomethingSecure() {} | ||
} | ||
|
||
public static class SandboxMethodAnnotatedClass { | ||
@SandboxOnly | ||
@EnforcePortalPermission(permission = "BASE") | ||
public void doSomethingSecure() {} | ||
} | ||
} |
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