forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Spring Security play with @testsecurity
Follows up on: quarkusio#10487 Resolves: quarkusio#10490
- Loading branch information
Showing
10 changed files
with
182 additions
and
84 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
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
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
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
7 changes: 7 additions & 0 deletions
7
integration-tests/spring-web/src/test/java/io/quarkus/it/spring/web/SecurityIT.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,7 @@ | ||
package io.quarkus.it.spring.web; | ||
|
||
import io.quarkus.test.junit.NativeImageTest; | ||
|
||
@NativeImageTest | ||
public class SecurityIT extends SecurityTest { | ||
} |
89 changes: 89 additions & 0 deletions
89
integration-tests/spring-web/src/test/java/io/quarkus/it/spring/web/SecurityTest.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,89 @@ | ||
package io.quarkus.it.spring.web; | ||
|
||
import java.util.Optional; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.restassured.RestAssured; | ||
import io.restassured.response.ValidatableResponse; | ||
import io.restassured.specification.RequestSpecification; | ||
|
||
@QuarkusTest | ||
public class SecurityTest { | ||
|
||
@Test | ||
public void shouldRestrictAccessToSpecificRole() { | ||
String path = "/api/securedMethod"; | ||
assertForAnonymous(path, 401, Optional.empty()); | ||
assertStatusAndContent(RestAssured.given().auth().preemptive().basic("stuart", "test"), path, 403, Optional.empty()); | ||
assertStatusAndContent(RestAssured.given().auth().preemptive().basic("scott", "jb0ss"), path, 200, | ||
Optional.of("accessibleForAdminOnly")); | ||
} | ||
|
||
@Test | ||
public void testAllowedForAdminOrViewer() { | ||
String path = "/api/allowedForUserOrViewer"; | ||
assertForAnonymous(path, 401, Optional.empty()); | ||
assertStatusAndContent(RestAssured.given().auth().preemptive().basic("aurea", "auri"), path, 403, Optional.empty()); | ||
assertStatusAndContent(RestAssured.given().auth().preemptive().basic("stuart", "test"), path, 200, | ||
Optional.of("allowedForUserOrViewer")); | ||
assertStatusAndContent(RestAssured.given().auth().preemptive().basic("george", "geo"), path, 200, | ||
Optional.of("allowedForUserOrViewer")); | ||
} | ||
|
||
@Test | ||
public void testWithAlwaysFalseChecker() { | ||
String path = "/api/withAlwaysFalseChecker"; | ||
assertForAnonymous(path, 401, Optional.empty()); | ||
assertStatusAndContent(RestAssured.given().auth().preemptive().basic("george", "geo"), path, 403, Optional.empty()); | ||
} | ||
|
||
@Test | ||
public void testPreAuthorizeOnController() { | ||
String path = "/api/preAuthorizeOnController"; | ||
assertForAnonymous(path, 401, Optional.empty()); | ||
assertStatusAndContent(RestAssured.given().auth().preemptive().basic("stuart", "test"), path, 200, | ||
Optional.of("preAuthorizeOnController")); | ||
assertStatusAndContent(RestAssured.given().auth().preemptive().basic("aurea", "auri"), path, 200, | ||
Optional.of("preAuthorizeOnController")); | ||
} | ||
|
||
@Test | ||
public void shouldAccessAllowed() { | ||
assertForAnonymous("/api/accessibleForAllMethod", 200, Optional.of("accessibleForAll")); | ||
assertForUsers("/api/accessibleForAllMethod", 200, Optional.of("accessibleForAll")); | ||
} | ||
|
||
@Test | ||
public void shouldRestrictAccessOnClass() { | ||
assertForAnonymous("/api/restrictedOnClass", 401, Optional.empty()); | ||
assertForUsers("/api/restrictedOnClass", 200, Optional.of("restrictedOnClass")); | ||
} | ||
|
||
@Test | ||
public void shouldFailToAccessRestrictedOnClass() { | ||
assertForAnonymous("/api/restrictedOnMethod", 401, Optional.empty()); | ||
assertStatusAndContent(RestAssured.given().auth().preemptive().basic("stuart", "test"), "/api/restrictedOnMethod", 403, | ||
Optional.empty()); | ||
assertStatusAndContent(RestAssured.given().auth().preemptive().basic("scott", "jb0ss"), "/api/restrictedOnMethod", 200, | ||
Optional.of("restrictedOnMethod")); | ||
} | ||
|
||
private void assertForAnonymous(String path, int status, Optional<String> content) { | ||
assertStatusAndContent(RestAssured.given(), path, status, content); | ||
} | ||
|
||
private void assertForUsers(String path, int status, Optional<String> content) { | ||
assertStatusAndContent(RestAssured.given().auth().preemptive().basic("stuart", "test"), path, status, content); | ||
assertStatusAndContent(RestAssured.given().auth().preemptive().basic("scott", "jb0ss"), path, status, content); | ||
} | ||
|
||
private void assertStatusAndContent(RequestSpecification request, String path, int status, Optional<String> content) { | ||
ValidatableResponse validatableResponse = request.when().get(path) | ||
.then() | ||
.statusCode(status); | ||
content.ifPresent(text -> validatableResponse.body(Matchers.equalTo(text))); | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
integration-tests/spring-web/src/test/java/io/quarkus/it/spring/web/TestSecurityTest.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,40 @@ | ||
package io.quarkus.it.spring.web; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.security.TestSecurity; | ||
import io.restassured.RestAssured; | ||
|
||
@QuarkusTest | ||
@TestSecurity(authorizationEnabled = false) | ||
public class TestSecurityTest { | ||
|
||
@Test | ||
public void testSecuredWithDisabledAuth() { | ||
RestAssured.when().get("/api/securedMethod").then() | ||
.body(is("accessibleForAdminOnly")); | ||
} | ||
|
||
@Test | ||
public void testPreAuthorizeWithDisabledAuth() { | ||
RestAssured.when().get("/api/allowedForUserOrViewer").then() | ||
.body(is("allowedForUserOrViewer")); | ||
} | ||
|
||
@Test | ||
@TestSecurity(user = "dummy", roles = "viewer") | ||
public void testWithTestSecurityAndWrongRole() { | ||
RestAssured.when().get("/api/securedMethod").then() | ||
.statusCode(403); | ||
} | ||
|
||
@Test | ||
@TestSecurity(user = "dummy", roles = "admin") | ||
public void testWithTestSecurityAndCorrectRole() { | ||
RestAssured.when().get("/api/securedMethod").then() | ||
.body(is("accessibleForAdminOnly")); | ||
} | ||
} |