[java-idp-plugin-duo] branch main updated: Unit test for front-end passwordless action.

Scott Cantor cantor.2 at osu.edu
Tue Apr 16 23:38:31 UTC 2024


This is an automated email from the git hooks/post-receive script.

scantor pushed a commit to branch main
in repository java-idp-plugin-duo.

View the commit online:
http://git.shibboleth.net/view/?p=java-idp-plugin-duo.git;a=commit;h=f8e64811f23da1b516839c16dc3cd35e5ce223fa

The following commit(s) were added to refs/heads/main by this push:
     new f8e64811 Unit test for front-end passwordless action.
f8e64811 is described below

commit f8e64811f23da1b516839c16dc3cd35e5ce223fa
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Tue Apr 16 19:38:29 2024 -0400

    Unit test for front-end passwordless action.
---
 .../authn/duo/PasswordlessCookieManager.java       |   2 +-
 .../duo/impl/PasswordlessCookieManagerTest.java    |  17 ++--
 .../duo/impl/PopulatePasswordlessContextTest.java  | 103 +++++++++++++++++++++
 3 files changed, 112 insertions(+), 10 deletions(-)

diff --git a/idp-duo-api/src/main/java/net/shibboleth/idp/plugin/authn/duo/PasswordlessCookieManager.java b/idp-duo-api/src/main/java/net/shibboleth/idp/plugin/authn/duo/PasswordlessCookieManager.java
index 337d264c..551bc8d0 100644
--- a/idp-duo-api/src/main/java/net/shibboleth/idp/plugin/authn/duo/PasswordlessCookieManager.java
+++ b/idp-duo-api/src/main/java/net/shibboleth/idp/plugin/authn/duo/PasswordlessCookieManager.java
@@ -43,7 +43,7 @@ import net.shibboleth.shared.security.DataSealerException;
 public class PasswordlessCookieManager extends AbstractInitializableComponent {
 
     /** A negative signal to allow caching opt-out. */
-    @Nonnull @NotEmpty private static final String NEGATIVE_VALUE = "__NO";
+    @Nonnull @NotEmpty public static final String NEGATIVE_VALUE = "__NO";
     
     /** Class logger.*/
     @Nonnull private final Logger log = LoggerFactory.getLogger(PasswordlessCookieManager.class);
diff --git a/idp-duo-impl/src/test/java/net/shibboleth/idp/plugin/authn/duo/impl/PasswordlessCookieManagerTest.java b/idp-duo-impl/src/test/java/net/shibboleth/idp/plugin/authn/duo/impl/PasswordlessCookieManagerTest.java
index c45251c1..361cded3 100644
--- a/idp-duo-impl/src/test/java/net/shibboleth/idp/plugin/authn/duo/impl/PasswordlessCookieManagerTest.java
+++ b/idp-duo-impl/src/test/java/net/shibboleth/idp/plugin/authn/duo/impl/PasswordlessCookieManagerTest.java
@@ -46,17 +46,16 @@ import jakarta.servlet.http.HttpServletResponse;
 @SuppressWarnings("javadoc")
 public class PasswordlessCookieManagerTest {
 
-    @Nonnull @NotEmpty private static final String COOKIE_NAME = "_cookieName";
-    
+    @Nonnull @NotEmpty protected static final String COOKIE_NAME = "_cookieName";
+
+    protected PasswordlessCookieManager cookieManager;
+    protected MockHttpServletRequest request;
+    protected MockHttpServletResponse response;
+
     private Resource keystoreResource;
     private Resource versionResource;
-    
     private DataSealer dataSealer;
-    private PasswordlessCookieManager cookieManager;
     
-    private MockHttpServletRequest request;
-    private MockHttpServletResponse response;
-
     @BeforeClass
     public void beforeClass() throws DataSealerException, ComponentInitializationException {
         ClassPathResource resource =
@@ -73,7 +72,7 @@ public class PasswordlessCookieManagerTest {
     }
     
     @BeforeMethod
-    public void beforeMethod() throws ComponentInitializationException {
+    public void setUp() throws ComponentInitializationException {
         request = new MockHttpServletRequest();
         response = new MockHttpServletResponse();
 
@@ -140,7 +139,7 @@ public class PasswordlessCookieManagerTest {
         
         final Cookie cookie = response.getCookie(COOKIE_NAME);
         assert cookie != null;
-        Assert.assertEquals(cookie.getValue(), "__NO");
+        Assert.assertEquals(cookie.getValue(), PasswordlessCookieManager.NEGATIVE_VALUE);
 
         request.setCookies(cookie);
         Assert.assertNull(cookieManager.readCookie());
diff --git a/idp-duo-impl/src/test/java/net/shibboleth/idp/plugin/authn/duo/impl/PopulatePasswordlessContextTest.java b/idp-duo-impl/src/test/java/net/shibboleth/idp/plugin/authn/duo/impl/PopulatePasswordlessContextTest.java
new file mode 100644
index 00000000..35196c8a
--- /dev/null
+++ b/idp-duo-impl/src/test/java/net/shibboleth/idp/plugin/authn/duo/impl/PopulatePasswordlessContextTest.java
@@ -0,0 +1,103 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.idp.plugin.authn.duo.impl;
+
+import org.opensaml.profile.action.EventIds;
+import org.opensaml.profile.context.ProfileRequestContext;
+import org.springframework.webflow.execution.Event;
+import org.springframework.webflow.execution.RequestContext;
+import org.testng.Assert;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import jakarta.servlet.http.Cookie;
+import net.shibboleth.idp.authn.AuthnEventIds;
+import net.shibboleth.idp.authn.context.AuthenticationContext;
+import net.shibboleth.idp.plugin.authn.duo.PasswordlessCookieManager;
+import net.shibboleth.idp.plugin.authn.duo.context.DuoPasswordlessContext;
+import net.shibboleth.idp.profile.context.navigate.WebflowRequestContextProfileRequestContextLookup;
+import net.shibboleth.idp.profile.testing.ActionTestingSupport;
+import net.shibboleth.idp.profile.testing.RequestContextBuilder;
+import net.shibboleth.shared.component.ComponentInitializationException;
+
+/**
+ * Unit test for {@link PopulatePasswordlessContext} action.
+ */
+ at SuppressWarnings("javadoc")
+public class PopulatePasswordlessContextTest extends PasswordlessCookieManagerTest {
+
+    private PopulatePasswordlessContext action;
+    
+    private RequestContext src;
+    private ProfileRequestContext prc;
+    private AuthenticationContext ac;
+    private DuoPasswordlessContext dpc;
+
+    @Override
+    @BeforeMethod
+    public void setUp() throws ComponentInitializationException {
+        super.setUp();
+        
+        action = new PopulatePasswordlessContext();
+        action.setCookieManager(cookieManager);
+        action.initialize();
+        
+        src = new RequestContextBuilder().buildRequestContext();
+        prc = new WebflowRequestContextProfileRequestContextLookup().apply(src);
+        ac = prc.ensureSubcontext(AuthenticationContext.class);
+        dpc = ac.ensureSubcontext(DuoPasswordlessContext.class);
+    }
+    
+    @Test
+    public void testNoContext() {
+        ac.removeSubcontext(DuoPasswordlessContext.class);
+        final Event event = action.execute(src);
+        ActionTestingSupport.assertEvent(event, EventIds.INVALID_PROFILE_CTX);
+    }
+ 
+    @Test
+    public void testUsernamePopulated() {
+        dpc.setUsername("jdoe");
+        
+        final Event event = action.execute(src);
+        ActionTestingSupport.assertProceedEvent(event);
+    }
+
+    @Test
+    public void testOptOut() {
+        request.setCookies(new Cookie(COOKIE_NAME, PasswordlessCookieManager.NEGATIVE_VALUE));
+        
+        final Event event = action.execute(src);
+        ActionTestingSupport.assertEvent(event, AuthnEventIds.RESELECT_FLOW);
+    }
+
+    @Test
+    public void testNoCookie() {
+        final Event event = action.execute(src);
+        ActionTestingSupport.assertEvent(event, AuthnEventIds.REQUEST_UNSUPPORTED);
+    }
+    
+    @Test
+    public void testSuccess() {
+        cookieManager.writeCookie("jdoe");
+        request.setCookies(response.getCookie(COOKIE_NAME));
+        
+        final Event event = action.execute(src);
+        ActionTestingSupport.assertProceedEvent(event);
+        
+        Assert.assertEquals(dpc.getUsername(), "jdoe");
+    }
+    
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list