package org.apereo.cas.authentication.mfa.trigger;

import org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest;
import org.apereo.cas.authentication.adaptive.geo.GeoLocationResponse;
import org.apereo.cas.authentication.principal.Service;
import org.apereo.cas.configuration.CasConfigurationProperties;

import lombok.val;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

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

/**
 * This is {@link AdaptiveMultifactorAuthenticationTriggerTests}.
 *
 * @author Misagh Moayyed
 * @since 6.1.0
 */
@Tag("MFA")
public class AdaptiveMultifactorAuthenticationTriggerTests extends BaseMultifactorAuthenticationTriggerTests {
    @Test
    public void verifyOperationByRequestIP() {
        val props = new CasConfigurationProperties();
        props.getAuthn().getAdaptive().getRequireMultifactor().put("mfa-dummy", "185.86.151.11");
        val trigger = new AdaptiveMultifactorAuthenticationTrigger(this.geoLocationService, props, this.applicationContext);
        val result = trigger.isActivated(authentication, registeredService, this.httpRequest, mock(Service.class));
        assertTrue(result.isPresent());
    }

    @Test
    public void verifyOperationByRequestUserAgent() {
        val props = new CasConfigurationProperties();
        props.getAuthn().getAdaptive().getRequireMultifactor().put("mfa-dummy", "^Mozilla.+");
        val trigger = new AdaptiveMultifactorAuthenticationTrigger(this.geoLocationService, props, this.applicationContext);
        val result = trigger.isActivated(authentication, registeredService, this.httpRequest, mock(Service.class));
        assertTrue(result.isPresent());
    }

    @Test
    public void verifyOperationByRequestGeoLocation() {
        val props = new CasConfigurationProperties();
        props.getAuthn().getAdaptive().getRequireMultifactor().put("mfa-dummy", ".+London.+");
        val geoResponse = new GeoLocationResponse();
        geoResponse.addAddress("123 Main St London UK");
        when(this.geoLocationService.locate(anyString(), any(GeoLocationRequest.class))).thenReturn(geoResponse);
        val trigger = new AdaptiveMultifactorAuthenticationTrigger(this.geoLocationService, props, this.applicationContext);
        val result = trigger.isActivated(authentication, registeredService, this.httpRequest, mock(Service.class));
        assertTrue(result.isPresent());
    }
}

@@ -45,16 +45,78 @@
 igger;%0A%0A
+import org.apereo.cas.authentication.AuthenticationException;%0A
 import o
@@ -421,45 +421,230 @@
 api.
-Tag;%0Aimport org.junit.jupiter.api.Tes
+MethodOrderer;%0Aimport org.junit.jupiter.api.Order;%0Aimport org.junit.jupiter.api.Tag;%0Aimport org.junit.jupiter.api.Test;%0Aimport org.junit.jupiter.api.TestMethodOrder;%0Aimport org.springframework.test.annotation.DirtiesContex
 t;%0A%0A
@@ -862,16 +862,86 @@
 (%22MFA%22)%0A
+@DirtiesContext%0A@TestMethodOrder(MethodOrderer.OrderAnnotation.class)%0A
 public c
@@ -1045,32 +1045,46 @@
 sts %7B%0A    @Test%0A
+    @Order(1)%0A
     public void 
@@ -1556,32 +1556,46 @@
    %7D%0A%0A    @Test%0A
+    @Order(2)%0A
     public void 
@@ -2075,24 +2075,38 @@
 %0A%0A    @Test%0A
+    @Order(3)%0A
     public v
@@ -2808,11 +2808,1531 @@
 );%0A    %7D
+%0A%0A    @Test%0A    @Order(4)%0A    @Tag(%22DisableProviderRegistration%22)%0A    public void verifyNoProviders() %7B%0A        val props = new CasConfigurationProperties();%0A        props.getAuthn().getAdaptive().getRequireMultifactor().put(%22mfa-dummy%22, %22.+London.+%22);%0A        val trigger = new AdaptiveMultifactorAuthenticationTrigger(null, props, this.applicationContext);%0A        assertThrows(AuthenticationException.class,%0A            () -%3E trigger.isActivated(authentication, registeredService, this.httpRequest, mock(Service.class)));%0A    %7D%0A%0A    @Test%0A    @Order(5)%0A    public void verifyMissingProviders() %7B%0A        val props = new CasConfigurationProperties();%0A        props.getAuthn().getAdaptive().getRequireMultifactor().put(%22mfa-xyz%22, %22.+London.+%22);%0A        val trigger = new AdaptiveMultifactorAuthenticationTrigger(null, props, this.applicationContext);%0A        assertThrows(AuthenticationException.class,%0A            () -%3E trigger.isActivated(authentication, registeredService, this.httpRequest, mock(Service.class)));%0A    %7D%0A%0A%0A    @Test%0A    @Order(7)%0A    public void verifyNoLocation() %7B%0A        val props = new CasConfigurationProperties();%0A        props.getAuthn().getAdaptive().getRequireMultifactor().put(%22mfa-dummy%22, %22.+London.+%22);%0A        val trigger = new AdaptiveMultifactorAuthenticationTrigger(this.geoLocationService, props, this.applicationContext);%0A        val result = trigger.isActivated(authentication, registeredService, this.httpRequest, mock(Service.class));%0A        assertTrue(result.isEmpty());%0A    %7D
 %0A%7D%0A

add tests