项目下载ZIP、TAR.GZ

This commit is contained in:
lxp
2026-11-02 16:53:17 +08:00
parent 556dab4377
commit 674f0400db
13 changed files with 923 additions and 3 deletions
@@ -0,0 +1,206 @@
package com.linkerbot.vibemake.api;
import static org.assertj.core.api.Assertions.*;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linkerbot.vibemake.service.gitea.GiteaClient;
import com.linkerbot.vibemake.service.gitea.GiteaFailure;
import com.linkerbot.vibemake.testing.ArchiveFixtures;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrint;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
@SpringBootTest(properties = {
"spring.datasource.url=jdbc:h2:mem:archives;MODE=MySQL;DB_CLOSE_DELAY=-1",
"spring.datasource.username=sa", "spring.datasource.password=",
"spring.datasource.driver-class-name=org.h2.Driver",
"vibemake.ai.enabled=false", "vibemake.oss.enabled=false",
"vibemake.limits.enabled=false", "vibemake.repository-write.worker-enabled=false",
"logging.level.root=WARN"})
@ActiveProfiles("test")
@AutoConfigureMockMvc(print = MockMvcPrint.NONE)
@Import(WorkflowTest.Config.class)
class ArchiveApiTest {
@Autowired MockMvc mvc;
@Autowired ObjectMapper json;
@Autowired JdbcTemplate jdbc;
@MockitoSpyBean FakeGiteaClient gitea;
@BeforeEach
void resetState() throws Exception {
reset(gitea);
gitea.reset();
SchemaSupport.reset(jdbc);
}
private JsonNode call(MockHttpServletRequestBuilder request) throws Exception {
return json.readTree(mvc.perform(request).andExpect(status().isOk()).andReturn()
.getResponse().getContentAsByteArray()).path("data");
}
private MockHttpServletRequestBuilder body(MockHttpServletRequestBuilder request, Object input) throws Exception {
return request.contentType(MediaType.APPLICATION_JSON).content(json.writeValueAsBytes(input));
}
private MockHttpServletRequestBuilder auth(MockHttpServletRequestBuilder request, String token) {
return request.header("Authorization", "Bearer " + token);
}
private String login(String email) throws Exception {
String captcha = call(get("/api/auth/captcha")).path("captchaCode").asText();
return call(body(post("/api/auth/login"), Map.of("email", email,
"captchaCode", captcha, "captchaAnswer", "ABCDE"))).path("token").asText();
}
private String create(String token) throws Exception {
return call(body(auth(post("/api/workspace/projects"), token),
Map.of("name", "归档测试", "projectType", "GENERAL"))).path("projectCode").asText();
}
private String head(String token, String code) throws Exception {
return call(auth(get("/api/workspace/projects/" + code + "/tree"), token)).path("commitSha").asText();
}
private String update(String token, String code, String sha, String content) throws Exception {
return call(body(auth(post("/api/workspace/projects/" + code + "/commits"), token),
Map.of("expectedCommitSha", sha, "message", "版本更新", "changes", java.util.List.of(
Map.of("action", "UPDATE_FILE", "path", "README.md", "content", content)))))
.path("commitSha").asText();
}
@ParameterizedTest
@ValueSource(strings = {"zip", "tar.gz"})
void archivesSeparateWorkspaceAndPublishedVersions(String format) throws Exception {
String token = login("archive-versions@example.invalid");
String code = create(token);
String first = head(token, code);
call(body(auth(put("/api/workspace/projects/" + code + "/publication"), token), Map.of("commitSha", first)));
String second = update(token, code, first, "版本 B");
for (String context : java.util.List.of("community", "workspace", "history")) {
boolean workspace = !context.equals("community");
String expected = context.equals("workspace") ? second : first;
MockHttpServletRequestBuilder request = get("/api/" + (workspace ? "workspace" : "community")
+ "/projects/" + code + "/archive").param("format", format)
.header("Origin", "https://dev-vibemake.robotquan.com");
if (workspace) auth(request, token);
if (context.equals("history")) request.param("commitSha", first);
MvcResult result = mvc.perform(request).andExpect(status().isOk())
.andExpect(content().contentType(format.equals("zip") ? "application/zip" : "application/gzip"))
.andExpect(header().string("Cache-Control", "no-store"))
.andExpect(header().string("Content-Disposition", containsString(code + "-" + expected.substring(0, 12))))
.andExpect(header().exists("X-Request-Id"))
.andExpect(header().string("Access-Control-Expose-Headers", containsString("Content-Disposition")))
.andReturn();
Map<String, byte[]> files = ArchiveFixtures.read(format, result.getResponse().getContentAsByteArray());
assertThat(new String(files.get("README.md"), StandardCharsets.UTF_8))
.isEqualTo(context.equals("workspace") ? "版本 B" : "initial");
}
}
@Test
void permissionsVisibilityAndReadinessFollowExistingRules() throws Exception {
String token = login("archive-owner@example.invalid");
String other = login("archive-other@example.invalid");
String code = create(token);
String workspace = "/api/workspace/projects/" + code + "/archive";
String community = "/api/community/projects/" + code + "/archive";
mvc.perform(get(workspace)).andExpect(status().isUnauthorized());
mvc.perform(auth(get(workspace), other)).andExpect(status().isNotFound());
mvc.perform(get(community)).andExpect(status().isNotFound());
// 社区管理员登录后也没有访问他人工作区的特权。
jdbc.update("UPDATE sys_user SET role='ADMIN' WHERE email=?", "archive-other@example.invalid");
mvc.perform(auth(get(workspace), other)).andExpect(status().isNotFound());
String sha = head(token, code);
call(body(auth(put("/api/workspace/projects/" + code + "/publication"), token), Map.of("commitSha", sha)));
mvc.perform(get(community)).andExpect(status().isOk()).andExpect(content().contentType("application/zip"));
jdbc.update("UPDATE project SET repo_status='CREATING' WHERE project_code=?", code);
mvc.perform(auth(get(workspace), token)).andExpect(status().isConflict());
mvc.perform(get(community)).andExpect(status().isNotFound());
jdbc.update("UPDATE project SET repo_status='READY' WHERE project_code=?", code);
call(auth(delete("/api/workspace/projects/" + code + "/publication"), token));
mvc.perform(get(community)).andExpect(status().isNotFound());
call(auth(delete("/api/workspace/projects/" + code), token));
mvc.perform(auth(get(workspace), token)).andExpect(status().isNotFound());
mvc.perform(get(community)).andExpect(status().isNotFound());
}
@Test
void rejectsInvalidFormatsAndCommunityShaIncludingEmptyValues() throws Exception {
String token = login("archive-validation@example.invalid");
String code = create(token);
String workspace = "/api/workspace/projects/" + code + "/archive";
String community = "/api/community/projects/" + code + "/archive";
for (String format : java.util.List.of("", "ZIP", "bundle", "../zip"))
mvc.perform(auth(get(workspace).param("format", format), token)).andExpect(status().isBadRequest());
mvc.perform(auth(get(workspace).param("commitSha", "main"), token)).andExpect(status().isBadRequest());
for (String sha : java.util.List.of("", "a".repeat(40)))
mvc.perform(get(community).param("commitSha", sha)).andExpect(status().isBadRequest());
mvc.perform(auth(get(workspace).param("commitSha", "f".repeat(40)), token)).andExpect(status().isNotFound());
}
@ParameterizedTest
@ValueSource(booleans = {true, false})
void streamFailureResetsUncommittedAttachmentsButNeverAppendsJsonAfterCommit(boolean commit) throws Exception {
String token = login("archive-failure@example.invalid");
String code = create(token);
AtomicBoolean closed = new AtomicBoolean();
InputStream failing = new InputStream() {
private int count;
@Override public int read() { throw new UnsupportedOperationException(); }
@Override public int read(byte[] bytes, int offset, int length) {
if (count == 0) {
byte[] prefix = {'P', 'K', 3, 4};
System.arraycopy(prefix, 0, bytes, offset, 4);
count += 4;
return 4;
}
if (commit && count == 4) {
int size = Math.min(length, 16384);
java.util.Arrays.fill(bytes, offset, offset + size, (byte) 'x');
count += size;
return size;
}
throw new GiteaFailure(504, "ARCHIVE_TIMEOUT", "timeout", false);
}
@Override public void close() { closed.set(true); }
};
doReturn(new GiteaClient.Archive(failing, -1)).when(gitea)
.archive(any(), anyString(), any(), any(), anyLong());
MvcResult result = mvc.perform(auth(get("/api/workspace/projects/" + code + "/archive"), token)).andReturn();
assertThat(closed.get()).isTrue();
if (commit) {
assertThat(result.getResponse().getStatus()).isEqualTo(200);
assertThat(result.getResponse().getContentAsString()).startsWith("PK").doesNotContain("ARCHIVE_TIMEOUT");
} else {
assertThat(result.getResponse().getStatus()).isEqualTo(504);
assertThat(result.getResponse().getHeader("Content-Disposition")).isNull();
assertThat(result.getResponse().getHeader("Content-Length")).isNull();
assertThat(json.readTree(result.getResponse().getContentAsByteArray()).path("code").asText())
.isEqualTo("ARCHIVE_TIMEOUT");
}
}
}
@@ -3,7 +3,12 @@ package com.linkerbot.vibemake.api;
import com.linkerbot.vibemake.bean.Contracts.*;
import com.linkerbot.vibemake.common.PageResponse;
import com.linkerbot.vibemake.service.gitea.*;
import com.linkerbot.vibemake.service.archive.ArchiveFormat;
import com.linkerbot.vibemake.testing.ArchiveFixtures;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.time.Duration;
import java.security.MessageDigest;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
@@ -93,6 +98,19 @@ final class FakeGiteaClient implements GiteaClient {
return repo(repository).head;
}
@Override
public synchronized Archive archive(Repository repository, String sha, ArchiveFormat format,
Duration timeout, long maxBytes) {
try {
byte[] bytes = ArchiveFixtures.create(format.extension(), snapshot(repository, sha));
if (bytes.length > maxBytes)
throw new GiteaFailure(413, "ARCHIVE_TOO_LARGE", "归档过大", false);
return new Archive(new ByteArrayInputStream(bytes), bytes.length);
} catch (IOException error) {
throw new IllegalStateException(error);
}
}
@Override
public synchronized List<TreeEntry> tree(Repository repository, String sha, String path) {
Map<String, byte[]> snapshot = repo(repository).snapshots.get(sha);
@@ -11,6 +11,7 @@ import com.linkerbot.vibemake.service.config.VibemakeProperties;
import com.linkerbot.vibemake.service.fork.ForkDeadline;
import com.linkerbot.vibemake.service.fork.GitCommandRunner;
import com.linkerbot.vibemake.service.gitea.GiteaClient;
import com.linkerbot.vibemake.testing.ArchiveFixtures;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
@@ -143,6 +144,55 @@ class GiteaLiveTest {
.asText();
}
@Test
void realRepositoryArchivesKeepPublishedVersionAndBothFormats() throws Exception {
String token = login("live-archive@example.invalid");
String code = perform(body(auth(post("/api/workspace/projects"), token),
Map.of("name", "仓库归档联调", "projectType", "GENERAL")), 200).path("projectCode").asText();
String original = perform(auth(get("/api/workspace/projects/" + code + "/tree"), token), 200)
.path("commitSha").asText();
String pointer = "version https://git-lfs.github.com/spec/v1\noid sha256:" + "a".repeat(64) + "\nsize 123\n";
String first = perform(body(auth(post("/api/workspace/projects/" + code + "/commits"), token),
Map.of("expectedCommitSha", original, "message", "发布归档 A", "changes", List.of(
Map.of("action", "UPDATE_FILE", "path", "README.md", "content", "版本 A"),
Map.of("action", "CREATE_FILE", "path", "目录/中文.txt", "content", "中文内容"),
Map.of("action", "CREATE_FILE", "path", "model.lfs", "content", pointer)))), 200)
.path("commitSha").asText();
byte[] binary = new byte[] {0, 1, -1, 2};
first = perform(auth(multipart("/api/workspace/projects/" + code + "/uploads/commit")
.file(new MockMultipartFile("files", "mesh.bin", "application/octet-stream", binary))
.param("expectedCommitSha", first).param("message", "添加归档二进制"), token), 200)
.path("commitSha").asText();
perform(body(auth(put("/api/workspace/projects/" + code + "/publication"), token),
Map.of("commitSha", first)), 200);
String second = perform(body(auth(post("/api/workspace/projects/" + code + "/commits"), token),
Map.of("expectedCommitSha", first, "message", "未发布归档 B", "changes", List.of(
Map.of("action", "UPDATE_FILE", "path", "README.md", "content", "版本 B")))), 200)
.path("commitSha").asText();
for (String format : List.of("zip", "tar.gz")) {
for (String context : List.of("community", "workspace", "history")) {
boolean workspace = !context.equals("community");
String expected = context.equals("workspace") ? second : first;
MockHttpServletRequestBuilder request = get("/api/" + (workspace ? "workspace" : "community")
+ "/projects/" + code + "/archive").param("format", format);
if (workspace) auth(request, token);
if (context.equals("history")) request.param("commitSha", first);
MvcResult result = mvc.perform(request).andExpect(status().isOk())
.andExpect(header().string("Cache-Control", "no-store"))
.andExpect(header().string("Content-Disposition", org.hamcrest.Matchers.containsString(
code + "-" + expected.substring(0, 12) + "." + format))).andReturn();
Map<String, byte[]> files = ArchiveFixtures.read(format, result.getResponse().getContentAsByteArray());
String root = files.keySet().stream().filter(name -> name.endsWith("README.md"))
.findFirst().orElseThrow().replace("README.md", "");
assertThat(new String(files.get(root + "README.md"), StandardCharsets.UTF_8))
.isEqualTo(context.equals("workspace") ? "版本 B" : "版本 A");
assertThat(files.get(root + "mesh.bin")).isEqualTo(binary);
assertThat(new String(files.get(root + "目录/中文.txt"), StandardCharsets.UTF_8)).isEqualTo("中文内容");
assertThat(new String(files.get(root + "model.lfs"), StandardCharsets.UTF_8)).isEqualTo(pointer);
}
}
}
@Test
void realSnapshotForkPreservesLargeCadGitlinksSymlinksAndLfsWithoutUnpublishedHistory()
throws Exception {