package org.apereo.cas.web.flow;

import org.apereo.cas.services.RegisteredServiceTestUtils;
import org.apereo.cas.services.UnauthorizedServiceException;
import org.apereo.cas.web.support.WebUtils;

import lombok.val;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.webflow.context.servlet.ServletExternalContext;
import org.springframework.webflow.execution.Action;
import org.springframework.webflow.test.MockRequestContext;

import static org.junit.jupiter.api.Assertions.*;

/**
 * This is {@link ServiceAuthorizationCheckActionTests}.
 *
 * @author Misagh Moayyed
 * @since 6.1.0
 */
@Tag("WebflowActions")
@DirtiesContext
public class ServiceAuthorizationCheckActionTests extends AbstractWebflowActionsTests {
    @Autowired
    @Qualifier(CasWebflowConstants.ACTION_ID_SERVICE_AUTHZ_CHECK)
    private Action action;

    @Test
    public void verifyNoServiceFound() {
        val request = new MockHttpServletRequest();
        val context = new MockRequestContext();
        context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
        WebUtils.putServiceIntoFlowScope(context, RegisteredServiceTestUtils.getService("invalid-service-123"));
        assertThrows(UnauthorizedServiceException.class, () -> this.action.execute(context));
    }

    @Test
    public void verifyDisabledServiceFound() {
        val request = new MockHttpServletRequest();
        val context = new MockRequestContext();
        context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
        WebUtils.putServiceIntoFlowScope(context, RegisteredServiceTestUtils.getService("cas-access-disabled"));
        assertThrows(UnauthorizedServiceException.class, () -> this.action.execute(context));
        assertNotNull(WebUtils.getUnauthorizedRedirectUrlFromFlowScope(context));
    }

    @Test
    public void verifyExclusiveAuthnDelegationMode() {
        val request = new MockHttpServletRequest();
        val context = new MockRequestContext();
        context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
        WebUtils.putServiceIntoFlowScope(context, RegisteredServiceTestUtils.getService("cas-access-delegation"));
        assertDoesNotThrow(() -> this.action.execute(context));
        assertFalse(WebUtils.isCasLoginFormViewable(context));
    }
}

@@ -62,34 +62,153 @@
 ces.
-RegisteredServiceTestUtils
+DefaultRegisteredServiceAccessStrategy;%0Aimport org.apereo.cas.services.RegisteredServiceTestUtils;%0Aimport org.apereo.cas.services.ServicesManager
 ;%0Aim
@@ -330,16 +330,57 @@
 ok.val;%0A
+import org.junit.jupiter.api.BeforeEach;%0A
 import o
@@ -991,16 +991,38 @@
 ntext;%0A%0A
+import java.net.URI;%0A%0A
 import s
@@ -1410,16 +1410,202 @@
 ction;%0A%0A
+    @Autowired%0A    @Qualifier(%22servicesManager%22)%0A    private ServicesManager servicesManager;%0A%0A    @BeforeEach%0A    public void beforeEach() %7B%0A        servicesManager.deleteAll();%0A    %7D%0A%0A
     @Tes
@@ -2093,32 +2093,32 @@
    %7D%0A%0A    @Test%0A
-
     public void 
@@ -2140,32 +2140,390 @@
 erviceFound() %7B%0A
+        val svc22 = RegisteredServiceTestUtils.getRegisteredService(%22cas-access-disabled%22);%0A        val strategy = new DefaultRegisteredServiceAccessStrategy();%0A        strategy.setEnabled(false);%0A        strategy.setUnauthorizedRedirectUrl(new URI(%22https://www.github.com%22));%0A        svc22.setAccessStrategy(strategy);%0A        servicesManager.save(svc22);%0A%0A
         val requ

update tests