package com.ruoyi.web.modules.screen.support;

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

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.exception.ServiceException;
import com.ruoyi.web.modules.screen.domain.vo.ScreenLoginRsaPublicKeyVo;

@ExtendWith(MockitoExtension.class)
@DisplayName("ScreenLoginService")
class ScreenLoginServiceTest
{
    private static final String PUBLIC_KEY = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH"
            + "nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ==";

    @InjectMocks
    private ScreenLoginService screenLoginService;

    @Mock
    private ScreenLoginProperties properties;

    @Test
    @DisplayName("未启用")
    void disabled()
    {
        when(properties.isEnabled()).thenReturn(false);
        assertThrows(ServiceException.class, () -> screenLoginService.login("any"));
    }

    @Test
    @DisplayName("获取 RSA 公钥")
    void getRsaPublicKey()
    {
        when(properties.isEnabled()).thenReturn(true);
        when(properties.getRsaPublicKey()).thenReturn(PUBLIC_KEY);
        when(properties.getRsaPrivateKey()).thenReturn("private-key");
        ScreenLoginRsaPublicKeyVo vo = screenLoginService.getRsaPublicKey();
        assertEquals(PUBLIC_KEY, vo.getPublicKey());
    }
}
