68 lines
2.9 KiB
Java
68 lines
2.9 KiB
Java
package com.baiyue.file.controller;
|
|
|
|
import com.baiyue.common.core.exception.BizException;
|
|
import com.baiyue.file.service.PriceBookFileService;
|
|
import jakarta.annotation.Resource;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
import org.springframework.mock.web.MockMultipartFile;
|
|
import org.springframework.test.context.TestPropertySource;
|
|
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
import static org.mockito.Mockito.verify;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
@SpringJUnitConfig(InternalPriceBookFileController.class)
|
|
@TestPropertySource(properties = "baiyue.security.internal-service-token=internal-token")
|
|
class InternalPriceBookFileControllerTest {
|
|
@MockBean PriceBookFileService service;
|
|
@Resource InternalPriceBookFileController controller;
|
|
|
|
@Test
|
|
void uploadsWithAuthenticatedOperatorAndBusinessId() {
|
|
var file = new MockMultipartFile("file", "price.xlsx", "application/octet-stream", new byte[]{1});
|
|
var metadata = metadata();
|
|
when(service.upload(41L, 7L, file)).thenReturn(metadata);
|
|
|
|
var response = controller.upload("internal-token", 7L, 41L, file);
|
|
|
|
assertThat(response.data()).isEqualTo(metadata);
|
|
verify(service).upload(41L, 7L, file);
|
|
}
|
|
|
|
@Test
|
|
void rejectsInvalidToken() {
|
|
assertThatThrownBy(() -> controller.metadata("bad-token", 7L, 41L, 99L))
|
|
.isInstanceOf(BizException.class).hasMessageContaining("凭证无效");
|
|
}
|
|
|
|
@Test
|
|
void rejectsMissingOperatorOrBusinessOwnership() {
|
|
assertThatThrownBy(() -> controller.metadata("internal-token", 0L, 41L, 99L))
|
|
.isInstanceOf(BizException.class).hasMessageContaining("必须大于0");
|
|
assertThatThrownBy(() -> controller.metadata("internal-token", 7L, 0L, 99L))
|
|
.isInstanceOf(BizException.class).hasMessageContaining("必须大于0");
|
|
}
|
|
|
|
@Test
|
|
void downloadsAsAttachmentWithoutCaching() {
|
|
PriceBookFileService.FileMetadata metadata = metadata();
|
|
when(service.download(41L, 99L)).thenReturn(
|
|
new PriceBookFileService.Download(metadata, new byte[]{1, 2, 3}));
|
|
|
|
var response = controller.download("internal-token", 7L, 41L, 99L);
|
|
|
|
assertThat(response.getBody()).containsExactly(1, 2, 3);
|
|
assertThat(response.getHeaders().getCacheControl()).contains("no-store");
|
|
assertThat(response.getHeaders().getContentDisposition().isAttachment()).isTrue();
|
|
}
|
|
|
|
private static PriceBookFileService.FileMetadata metadata() {
|
|
return new PriceBookFileService.FileMetadata(99L, 41L, "price.xlsx",
|
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 3L, "hash",
|
|
PriceBookFileService.BUSINESS_TYPE);
|
|
}
|
|
}
|