package org.apereo.cas.pm.web.flow.actions;

import org.apereo.cas.authentication.Credential;
import org.apereo.cas.services.RegisteredServiceTestUtils;
import org.apereo.cas.web.flow.CasWebflowConstants;

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.binding.message.MessageContext;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import org.springframework.webflow.context.servlet.ServletExternalContext;
import org.springframework.webflow.core.collection.LocalAttributeMap;
import org.springframework.webflow.execution.Action;
import org.springframework.webflow.execution.RequestContext;
import org.springframework.webflow.test.MockParameterMap;

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

/**
 * This is {@link AccountUnlockStatusActionTests}.
 *
 * @author Misagh Moayyed
 * @since 6.6.0
 */
@Tag("WebflowActions")
public class AccountUnlockStatusActionTests extends BasePasswordManagementActionTests {
    @Autowired
    @Qualifier(CasWebflowConstants.ACTION_ID_UNLOCK_ACCOUNT_STATUS)
    protected Action action;

    @Test
    public void verifyBadCaptcha() throws Exception {
        val context = getRequestContext("good", "bad");
        val result = action.execute(context);
        assertEquals(CasWebflowConstants.TRANSITION_ID_ERROR, result.getId());
    }

    @Test
    public void verifyAccountUnlock() throws Exception {
        val context = getRequestContext("good", "good");
        val result = action.execute(context);
        assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, result.getId());
    }

    private static RequestContext getRequestContext(final String captcha, final String givenCaptcha) {
        val context = mock(RequestContext.class);
        when(context.getMessageContext()).thenReturn(mock(MessageContext.class));
        when(context.getFlowScope()).thenReturn(new LocalAttributeMap<>());
        val requestParameters = new MockParameterMap();
        requestParameters.put("captchaValue", givenCaptcha);
        when(context.getRequestParameters()).thenReturn(requestParameters);
        when(context.getConversationScope()).thenReturn(new LocalAttributeMap<>());
        when(context.getExternalContext()).thenReturn(new ServletExternalContext(new MockServletContext(),
            new MockHttpServletRequest(), new MockHttpServletResponse()));
        val credential = RegisteredServiceTestUtils.getCredentialsWithSameUsernameAndPassword("casuser");
        context.getConversationScope().put(Credential.class.getName(), credential);
        context.getConversationScope().put("captchaValue", captcha);
        return context;
    }
}

@@ -2409,263 +2409,51 @@
     
-when(context.getConversationScope()).thenReturn(new LocalAttributeMap%3C%3E());%0A        when(context.getExternalContext()).thenReturn(new ServletExternalContext(new MockServletContext(),%0A            new MockHttpServletRequest(), new MockHttpServletResponse())
+val conversation = new LocalAttributeMap%3C%3E(
 );%0A 
@@ -2564,35 +2564,24 @@
 %0A        con
-text.getCon
 versationSco
@@ -2573,31 +2573,24 @@
 conversation
-Scope()
 .put(Credent
@@ -2638,63 +2638,298 @@
  con
-text.getConversationScope().put(%22captchaValue%22, captcha
+versation.put(%22captchaValue%22, captcha);%0A        when(context.getConversationScope()).thenReturn(conversation);%0A        when(context.getExternalContext()).thenReturn(new ServletExternalContext(new MockServletContext(),%0A            new MockHttpServletRequest(), new MockHttpServletResponse())
 );%0A 

fix tests