package com.ruoyi.web.controller.system;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.ruoyi.framework.web.service.PasswordTransferCryptoService;
import com.ruoyi.framework.web.service.SysLoginService;

class SysLoginControllerPasswordTransferTest
{
    @Test
    void shouldDecryptPasswordBeforeCallingLoginService() throws Exception
    {
        SysLoginService loginService = mock(SysLoginService.class);
        PasswordTransferCryptoService passwordTransferCryptoService = mock(PasswordTransferCryptoService.class);
        when(passwordTransferCryptoService.decrypt("cipher")).thenReturn("admin123");
        when(loginService.login("admin", "admin123", "1", "u")).thenReturn("token");

        SysLoginController controller = new SysLoginController();
        ReflectionTestUtils.setField(controller, "loginService", loginService);
        ReflectionTestUtils.setField(controller, "passwordTransferCryptoService", passwordTransferCryptoService);

        MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();

        mockMvc.perform(post("/login")
                .contentType("application/json")
                .content("{\"username\":\"admin\",\"password\":\"cipher\",\"code\":\"1\",\"uuid\":\"u\"}"))
                .andExpect(status().isOk());

        verify(passwordTransferCryptoService).decrypt("cipher");
        verify(loginService).login("admin", "admin123", "1", "u");
    }
}
