You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by lp...@apache.org on 2023/02/22 01:25:34 UTC

[shiro] branch main updated: [SHIRO-337] fix tests: make SecurityManager in startup bean, and destroy it

This is an automated email from the ASF dual-hosted git repository.

lprimak pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shiro.git


The following commit(s) were added to refs/heads/main by this push:
     new 0f317997 [SHIRO-337] fix tests: make SecurityManager in startup bean, and destroy it
0f317997 is described below

commit 0f317997c62347dfb64d25dd702807ca53547c78
Author: lprimak <le...@flowlogix.com>
AuthorDate: Tue Feb 21 19:24:32 2023 -0600

    [SHIRO-337] fix tests: make SecurityManager in startup bean, and destroy it
---
 .../shiro/testing/jaxrs/TestApplication.java       | 30 ++++++++++++++++++++++
 .../apache/shiro/testing/jaxrs/WhoamiResource.java | 10 +++-----
 .../shiro/testing/cdi/ComponentInjectionIT.java    | 23 +++++------------
 3 files changed, 39 insertions(+), 24 deletions(-)

diff --git a/integration-tests/jakarta-ee/src/main/java/org/apache/shiro/testing/jaxrs/TestApplication.java b/integration-tests/jakarta-ee/src/main/java/org/apache/shiro/testing/jaxrs/TestApplication.java
index 2e287059..96e2b354 100644
--- a/integration-tests/jakarta-ee/src/main/java/org/apache/shiro/testing/jaxrs/TestApplication.java
+++ b/integration-tests/jakarta-ee/src/main/java/org/apache/shiro/testing/jaxrs/TestApplication.java
@@ -13,11 +13,41 @@
  */
 package org.apache.shiro.testing.jaxrs;
 
+import java.util.List;
 import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.Destroyed;
+import javax.enterprise.context.Initialized;
+import javax.enterprise.event.Observes;
 import javax.ws.rs.ApplicationPath;
 import javax.ws.rs.core.Application;
+import lombok.Getter;
+import org.apache.shiro.authc.SimpleAccount;
+import org.apache.shiro.mgt.DefaultSecurityManager;
+import org.apache.shiro.realm.SimpleAccountRealm;
+import org.apache.shiro.subject.SimplePrincipalCollection;
+import org.apache.shiro.testing.jakarta.ee.PropertyPrincipal;
 
 @ApplicationPath("/")
 @ApplicationScoped
 public class TestApplication extends Application {
+    private @Getter DefaultSecurityManager securityManager;
+
+    void configureSecurityManager(@Observes @Initialized(ApplicationScoped.class) Object nothing) {
+        var realm = new SimpleAccountRealm("testRealm") {
+            @Override
+            public void addAccount(String username, String password) {
+                SimpleAccount account = new SimpleAccount(new SimplePrincipalCollection(
+                        List.of(username, new PropertyPrincipal(username)), getName()), password);
+                add(account);
+            }
+        };
+        securityManager = new DefaultSecurityManager(realm);
+        realm.addAccount("powerful", "awesome", "admin");
+        realm.addAccount("regular", "meh", "user");
+        realm.addAccount("user", "password");
+    }
+
+    void destroySecurityManager(@Observes @Destroyed(ApplicationScoped.class) Object nothing) {
+        securityManager.destroy();
+    }
 }
diff --git a/integration-tests/jakarta-ee/src/main/java/org/apache/shiro/testing/jaxrs/WhoamiResource.java b/integration-tests/jakarta-ee/src/main/java/org/apache/shiro/testing/jaxrs/WhoamiResource.java
index 6d1a5959..795d3c2d 100644
--- a/integration-tests/jakarta-ee/src/main/java/org/apache/shiro/testing/jaxrs/WhoamiResource.java
+++ b/integration-tests/jakarta-ee/src/main/java/org/apache/shiro/testing/jaxrs/WhoamiResource.java
@@ -25,8 +25,6 @@ import javax.ws.rs.core.Response.Status;
 import org.apache.shiro.SecurityUtils;
 import org.apache.shiro.authc.UsernamePasswordToken;
 import org.apache.shiro.lang.ShiroException;
-import org.apache.shiro.mgt.DefaultSecurityManager;
-import org.apache.shiro.realm.SimpleAccountRealm;
 import org.apache.shiro.util.ThreadContext;
 
 @Path("whoami")
@@ -35,6 +33,8 @@ public class WhoamiResource {
     WhoamiBean whoamiBean;
     @Inject
     RolesAllowedBean rolesAllowedBean;
+    @Inject
+    TestApplication testApplication;
 
     @GET
     @Path("whoami")
@@ -73,11 +73,7 @@ public class WhoamiResource {
 
     private <T> T check(Supplier<T> happy, Supplier<T> sad, String user, String password) {
         try {
-            var realm = new SimpleAccountRealm();
-            var sm = new DefaultSecurityManager(realm);
-            realm.addAccount("powerful", "awesome", "admin");
-            realm.addAccount("regular", "meh", "user");
-            ThreadContext.bind(sm);
+            ThreadContext.bind(testApplication.getSecurityManager());
             SecurityUtils.getSubject().login(new UsernamePasswordToken(user, password));
             return happy.get();
         } catch (ShiroException e) {
diff --git a/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/cdi/ComponentInjectionIT.java b/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/cdi/ComponentInjectionIT.java
index beded919..4b1d4ad1 100644
--- a/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/cdi/ComponentInjectionIT.java
+++ b/integration-tests/jakarta-ee/src/test/java/org/apache/shiro/testing/cdi/ComponentInjectionIT.java
@@ -13,18 +13,14 @@
  */
 package org.apache.shiro.testing.cdi;
 
-import java.util.List;
 import javax.inject.Inject;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.shiro.SecurityUtils;
-import org.apache.shiro.authc.SimpleAccount;
 import org.apache.shiro.authc.UsernamePasswordToken;
 import static org.apache.shiro.ee.util.JakartaTransformer.jakartify;
-import org.apache.shiro.mgt.DefaultSecurityManager;
-import org.apache.shiro.realm.SimpleAccountRealm;
-import org.apache.shiro.subject.SimplePrincipalCollection;
 import org.apache.shiro.testing.jakarta.ee.PropertyPrincipal;
 import org.apache.shiro.testing.jaxrs.NoIniJaxRsIT;
+import org.apache.shiro.testing.jaxrs.TestApplication;
 import org.apache.shiro.util.ThreadContext;
 import org.jboss.arquillian.container.test.api.Deployment;
 import org.jboss.arquillian.container.test.api.OperateOnDeployment;
@@ -45,7 +41,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
 
 /**
- * Tests @Injecting Subject, Session and SecurityManager with CDI
+ * Tests @Injecting Subject, Session, Principal and SecurityManager with CDI
  */
 @ExtendWith(ArquillianExtension.class)
 @Execution(ExecutionMode.SAME_THREAD)
@@ -56,24 +52,17 @@ public class ComponentInjectionIT {
 
     @Inject
     ComponentInjectionBean injectedComponents;
+    @Inject
+    TestApplication testApplication;
 
     @BeforeEach
     void bind() {
-        var realm = new SimpleAccountRealm("testRealm") {
-            @Override
-            public void addAccount(String username, String password) {
-                SimpleAccount account = new SimpleAccount(new SimplePrincipalCollection(
-                        List.of(username, new PropertyPrincipal(username)), getName()), password);
-                add(account);
-            }
-        };
-        var sm = new DefaultSecurityManager(realm);
-        realm.addAccount("user", "password");
-        ThreadContext.bind(sm);
+        ThreadContext.bind(testApplication.getSecurityManager());
     }
 
     @AfterEach
     void unbind() {
+        SecurityUtils.getSubject().logout();
         ThreadContext.unbindSecurityManager();
         ThreadContext.unbindSubject();
         ThreadContext.remove();