package org.apereo.cas.web.flow;

import lombok.extern.slf4j.Slf4j;
import org.apereo.cas.AbstractCentralAuthenticationServiceTests;
import org.apereo.cas.authentication.AuthenticationResult;
import org.apereo.cas.authentication.CoreAuthenticationTestUtils;
import org.apereo.cas.mock.MockTicketGrantingTicket;
import org.apereo.cas.ticket.TicketGrantingTicket;
import org.apereo.cas.web.config.CasSupportActionsConfiguration;
import org.apereo.cas.web.flow.login.TicketGrantingTicketCheckAction;
import org.apereo.cas.web.support.WebUtils;
import org.junit.Test;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.test.MockRequestContext;

import static org.junit.Assert.*;

/**
 * Handles tests for {@link TicketGrantingTicketCheckAction}.
 *
 * @author Misagh Moayyed
 * @since 4.1.0
 */
@DirtiesContext
@Import(CasSupportActionsConfiguration.class)
@Slf4j
public class TicketGrantingTicketCheckActionTests extends AbstractCentralAuthenticationServiceTests {

    @Test
    public void verifyNullTicket() {
        final MockRequestContext ctx = new MockRequestContext();
        final TicketGrantingTicketCheckAction action = new
            TicketGrantingTicketCheckAction(this.getCentralAuthenticationService());
        final Event event = action.doExecute(ctx);
        assertEquals(TicketGrantingTicketCheckAction.NOT_EXISTS, event.getId());
    }

    @Test
    public void verifyInvalidTicket() {

        final MockRequestContext ctx = new MockRequestContext();
        final MockTicketGrantingTicket tgt = new MockTicketGrantingTicket("user");

        WebUtils.putTicketGrantingTicketInScopes(ctx, tgt);
        final TicketGrantingTicketCheckAction action = new
            TicketGrantingTicketCheckAction(this.getCentralAuthenticationService());
        final Event event = action.doExecute(ctx);
        assertEquals(TicketGrantingTicketCheckAction.INVALID, event.getId());
    }

    @Test
    public void verifyValidTicket() {

        final MockRequestContext ctx = new MockRequestContext();
        final AuthenticationResult ctxAuthN = CoreAuthenticationTestUtils.getAuthenticationResult(getAuthenticationSystemSupport());

        final TicketGrantingTicket tgt = this.getCentralAuthenticationService().createTicketGrantingTicket(ctxAuthN);

        WebUtils.putTicketGrantingTicketInScopes(ctx, tgt);
        final TicketGrantingTicketCheckAction action = new
            TicketGrantingTicketCheckAction(this.getCentralAuthenticationService());
        final Event event = action.doExecute(ctx);
        assertEquals(TicketGrantingTicketCheckAction.VALID, event.getId());
    }


}

@@ -670,16 +670,69 @@
 ontext;%0A
+import org.springframework.webflow.execution.Action;%0A
 import o
@@ -1202,16 +1202,33 @@
 Ticket()
+ throws Exception
  %7B%0A     
@@ -1305,33 +1305,8 @@
 nal 
-TicketGrantingTicketCheck
 Acti
@@ -1316,36 +1316,24 @@
 action = new
-%0A           
  TicketGrant
@@ -1421,35 +1421,33 @@
  event = action.
-doE
+e
 xecute(ctx);%0A   
@@ -1468,40 +1468,46 @@
 als(
-TicketGrantingTicketCheckAction.
+CasWebflowConstants.TRANSITION_ID_TGT_
 NOT_
@@ -1577,34 +1577,50 @@
 InvalidTicket() 
+throws Exception 
 %7B
-%0A
 %0A        final M
@@ -1752,17 +1752,16 @@
 user%22);%0A
-%0A
         
@@ -1830,33 +1830,8 @@
 nal 
-TicketGrantingTicketCheck
 Acti
@@ -1841,36 +1841,24 @@
 action = new
-%0A           
  TicketGrant
@@ -1946,35 +1946,33 @@
  event = action.
-doE
+e
 xecute(ctx);%0A   
@@ -1993,40 +1993,46 @@
 als(
-TicketGrantingTicketCheckAction.
+CasWebflowConstants.TRANSITION_ID_TGT_
 INVA
@@ -2105,18 +2105,34 @@
 icket() 
+throws Exception 
 %7B
-%0A
 %0A       
@@ -2322,17 +2322,16 @@
 ort());%0A
-%0A
         
@@ -2440,17 +2440,16 @@
 AuthN);%0A
-%0A
         
@@ -2518,33 +2518,8 @@
 nal 
-TicketGrantingTicketCheck
 Acti
@@ -2537,20 +2537,8 @@
  new
-%0A           
  Tic
@@ -2646,11 +2646,9 @@
 ion.
-doE
+e
 xecu
@@ -2681,40 +2681,46 @@
 als(
-TicketGrantingTicketCheckAction.
+CasWebflowConstants.TRANSITION_ID_TGT_
 VALI
@@ -2744,12 +2744,10 @@
 ;%0A    %7D%0A
-%0A%0A
 %7D%0A

clean up tests