package com.ruoyi.system.service.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.Collections;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import com.ruoyi.common.enums.LoginPolicyType;
import com.ruoyi.system.domain.SysLoginPolicy;
import com.ruoyi.system.mapper.SysLoginPolicyMapper;

@ExtendWith(MockitoExtension.class)
@DisplayName("SysLoginPolicyServiceImpl")
class SysLoginPolicyServiceImplTest
{
    @InjectMocks
    private SysLoginPolicyServiceImpl loginPolicyService;

    @Mock
    private SysLoginPolicyMapper loginPolicyMapper;

    @Test
    @DisplayName("按角色匹配互联网双因子")
    void resolveByRoleKey()
    {
        SysLoginPolicy adminPolicy = new SysLoginPolicy();
        adminPolicy.setRoleKey("admin");
        adminPolicy.setPolicyType("SMS_2FA");
        when(loginPolicyMapper.selectEnabledByNetworkZone("INTERNET"))
                .thenReturn(Collections.singletonList(adminPolicy));

        LoginPolicyType type = loginPolicyService.resolvePolicyType("INTERNET",
                Collections.singletonList("admin"), null);
        assertEquals(LoginPolicyType.SMS_2FA, type);
    }

    @Test
    @DisplayName("用户级策略优先")
    void userPolicyOverrides()
    {
        LoginPolicyType type = loginPolicyService.resolvePolicyType("INTERNET",
                Arrays.asList("admin"), "PASSWORD");
        assertEquals(LoginPolicyType.PASSWORD, type);
    }
}
