轻松聘AI开发
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

61 lines
2.8 KiB

package com.mydreamjob.talentinvitation;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mydreamjob.auth.JwtTokenProvider;
import com.mydreamjob.common.exception.BusinessException;
import com.mydreamjob.common.exception.ErrorCode;
import com.mydreamjob.talentinvitation.dto.TalentInvitationPageResponse;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import java.util.List;
import java.util.Map;
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.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("integration-test")
class TalentInvitationControllerTest {
@Autowired MockMvc mockMvc;
@Autowired JwtTokenProvider jwtTokenProvider;
@MockBean TalentInvitationService service;
@MockBean RedisTemplate<String, Object> redisTemplate;
@Test
void anonymousInvitationList_isUnauthorized() throws Exception {
mockMvc.perform(get("/companies/me/talent-invitations"))
.andExpect(status().isUnauthorized());
}
@Test
void companyInvitationList_isReachableThroughAuthenticatedRoute() throws Exception {
when(service.listCompany(eq(100L), eq(0), eq(20)))
.thenReturn(TalentInvitationPageResponse.<com.mydreamjob.talentinvitation.dto.CompanyTalentInvitationResponse>builder()
.items(List.of()).total(0).page(0).size(20).totalPages(0).build());
mockMvc.perform(get("/companies/me/talent-invitations").header("Authorization", token(100L, "COMPANY")))
.andExpect(status().isOk()).andExpect(jsonPath("$.code").value(200));
}
@Test
void studentOnCompanyInvitationList_isForbiddenByServiceRoleGuard() throws Exception {
when(service.listCompany(eq(100L), eq(0), eq(20))).thenThrow(new BusinessException(ErrorCode.FORBIDDEN));
mockMvc.perform(get("/companies/me/talent-invitations").header("Authorization", token(100L, "STUDENT")))
.andExpect(status().isForbidden()).andExpect(jsonPath("$.code").value(2002));
}
private String token(Long userId, String role) {
return com.mydreamjob.auth.support.TestAuthTokens.bearer(userId, role);
}
}