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

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

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 org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.ruoyi.web.modules.screen.domain.vo.ScreenLoginRsaPublicKeyVo;
import com.ruoyi.web.modules.screen.support.ScreenLoginService;

@ExtendWith(MockitoExtension.class)
@DisplayName("ScreenLoginController")
class ScreenLoginControllerApiTest
{

    @InjectMocks
    private ScreenLoginController controller;

    @Mock
    private ScreenLoginService screenLoginService;

    @Test
    @DisplayName("获取 RSA 公钥")
    void rsaPublicKey() throws Exception
    {
        when(screenLoginService.getRsaPublicKey())
                .thenReturn(new ScreenLoginRsaPublicKeyVo("pub-key-1"));
        MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();

        mockMvc.perform(get("/bigScreen/publicKey"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.publicKey").value("pub-key-1"));
    }

    @Test
    @DisplayName("请求头 Token 登录")
    void loginByHeader() throws Exception
    {
        when(screenLoginService.login(eq("cipher-1"))).thenReturn("jwt-abc");
        MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();

        mockMvc.perform(post("/bigScreen/login")
                .header(ScreenLoginController.HEADER_SCREEN_TOKEN, "cipher-1"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.token").value("jwt-abc"));
    }

    @Test
    @DisplayName("Body Token 登录")
    void loginByBody() throws Exception
    {
        when(screenLoginService.login(eq("cipher-2"))).thenReturn("jwt-def");
        MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();

        mockMvc.perform(post("/bigScreen/login")
                .contentType(MediaType.APPLICATION_JSON)
                .content("{\"token\":\"cipher-2\"}"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.token").value("jwt-def"));
    }
}
