Add git-lfs support

This commit is contained in:
Oliver Milke
2017-05-19 17:27:18 +02:00
parent 9b932a325e
commit 33ea2273b0
16 changed files with 914 additions and 51 deletions

View File

@@ -0,0 +1,48 @@
package sonia.scm.web;
import org.junit.Test;
import javax.servlet.http.HttpServletRequest;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Created by omilke on 19.05.2017.
*/
public class GitPermissionFilterTest {
@Test
public void isLfsFileUpload() throws Exception {
HttpServletRequest mockedRequest = getRequestWithMethodAndPathInfo("PUT",
"/scm/git/git-lfs-demo.git/info/lfs/objects/8fcebeb5698230685f92028e560f8f1683ebc15ec82a620ffad5c12a3c19bdec");
assertThat((GitPermissionFilter.isLfsFileUpload(mockedRequest)), is(true));
mockedRequest = getRequestWithMethodAndPathInfo("GET",
"/scm/git/git-lfs-demo.git/info/lfs/objects/8fcebeb5698230685f92028e560f8f1683ebc15ec82a620ffad5c12a3c19bdec");
assertThat((GitPermissionFilter.isLfsFileUpload(mockedRequest)), is(false));
mockedRequest = getRequestWithMethodAndPathInfo("POST",
"/scm/git/git-lfs-demo.git/info/lfs/objects/8fcebeb5698230685f92028e560f8f1683ebc15ec82a620ffad5c12a3c19bdec");
assertThat((GitPermissionFilter.isLfsFileUpload(mockedRequest)), is(false));
mockedRequest = getRequestWithMethodAndPathInfo("POST",
"/scm/git/git-lfs-demo.git/info/lfs/objects/batch");
assertThat((GitPermissionFilter.isLfsFileUpload(mockedRequest)), is(false));
}
private HttpServletRequest getRequestWithMethodAndPathInfo(String method, String pathInfo) {
HttpServletRequest mock = mock(HttpServletRequest.class);
when(mock.getMethod()).thenReturn(method);
when(mock.getRequestURI()).thenReturn(pathInfo);
when(mock.getContextPath()).thenReturn("/scm");
return mock;
}
}

View File

@@ -58,6 +58,7 @@ public class GitUserAgentProviderTest
public void testParseUserAgent()
{
assertEquals(GitUserAgentProvider.GIT, parse("git/1.7.9.5"));
assertEquals(GitUserAgentProvider.GIT_LFS, parse("git-lfs/2.0.1 (GitHub; windows amd64; go 1.8; git 678cdbd4)"));
assertEquals(GitUserAgentProvider.MSYSGIT, parse("git/1.8.3.msysgit.0"));
assertNull(parse("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"));
}

View File

@@ -0,0 +1,26 @@
package sonia.scm.web;
import org.junit.Test;
import static org.eclipse.jgit.lfs.lib.Constants.CONTENT_TYPE_GIT_LFS_JSON;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
/**
* Created by omilke on 11.05.2017.
*/
public class ScmGitServletTest {
@Test
public void isContentTypeMatches() throws Exception {
assertThat(ScmGitServlet.isLfsContentHeaderField("application/vnd.git-lfs+json", CONTENT_TYPE_GIT_LFS_JSON), is(true));
assertThat(ScmGitServlet.isLfsContentHeaderField("application/vnd.git-lfs+json;", CONTENT_TYPE_GIT_LFS_JSON), is(true));
assertThat(ScmGitServlet.isLfsContentHeaderField("application/vnd.git-lfs+json; charset=utf-8", CONTENT_TYPE_GIT_LFS_JSON), is(true));
assertThat(ScmGitServlet.isLfsContentHeaderField("application/vnd.git-lfs-json;", CONTENT_TYPE_GIT_LFS_JSON), is(false));
assertThat(ScmGitServlet.isLfsContentHeaderField("", CONTENT_TYPE_GIT_LFS_JSON), is(false));
assertThat(ScmGitServlet.isLfsContentHeaderField(null, CONTENT_TYPE_GIT_LFS_JSON), is(false));
}
}

View File

@@ -0,0 +1,75 @@
package sonia.scm.web.lfs.servlet;
import org.junit.Test;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryTestData;
import sonia.scm.store.BlobStoreFactory;
import javax.servlet.http.HttpServletRequest;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import static org.mockito.Matchers.matches;
import static org.mockito.Mockito.*;
/**
* Created by omilke on 18.05.2017.
*/
public class LfsServletFactoryTest {
@Test
public void buildBaseUri() throws Exception {
String repositoryName = "git-lfs-demo";
String result = LfsServletFactory.buildBaseUri(new Repository("", "GIT", repositoryName), RequestWithUri(repositoryName, true));
assertThat(result, is(equalTo("http://localhost:8081/scm/git/git-lfs-demo.git/info/lfs/objects/")));
//result will be with dot-gix suffix, ide
result = LfsServletFactory.buildBaseUri(new Repository("", "GIT", repositoryName), RequestWithUri(repositoryName, false));
assertThat(result, is(equalTo("http://localhost:8081/scm/git/git-lfs-demo.git/info/lfs/objects/")));
}
@Test
public void getBlobStore() throws Exception {
BlobStoreFactory blobStoreFactoryMock = mock(BlobStoreFactory.class);
//TODO #239:
RepositoryTestData repositoryTestData;
new LfsServletFactory(blobStoreFactoryMock).getBlobStore(new Repository("the-id", "GIT", "the-name"));
//just make sure the right parameter is passed, as properly validating the return value is nearly impossible with the return value (and should not be
// part of this test)
verify(blobStoreFactoryMock).getBlobStore(matches("the-id-git-lfs"));
//make sure there have been no further usages of the factory
verifyNoMoreInteractions(blobStoreFactoryMock);
}
private HttpServletRequest RequestWithUri(String repositoryName, boolean withDotGitSuffix) {
HttpServletRequest mockedRequest = mock(HttpServletRequest.class);
final String suffix;
if (withDotGitSuffix) {
suffix = ".git";
} else {
suffix = "";
}
//build from valid live request data
when(mockedRequest.getRequestURL()).thenReturn(
new StringBuffer(String.format("http://localhost:8081/scm/git/%s%s/info/lfs/objects/batch", repositoryName, suffix)));
when(mockedRequest.getRequestURI()).thenReturn(String.format("/scm/git/%s%s/info/lfs/objects/batch", repositoryName, suffix));
when(mockedRequest.getContextPath()).thenReturn("/scm");
return mockedRequest;
}
}

View File

@@ -0,0 +1,42 @@
package sonia.scm.web.lfs.servlet;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.*;
/**
* Created by omilke on 16.05.2017.
*/
public class ScmFileTransferServletTest {
@Test
public void hasObjectId() throws Exception {
String SAMPLE_OBJECT_ID = "8fcebeb5698230685f92028e560f8f1683ebc15ec82a620ffad5c12a3c19bdec";
String path = "/git-lfs-demo.git/info/lfs/objects/" + SAMPLE_OBJECT_ID;
assertThat(ScmFileTransferServlet.objectIdFromPath(path), is(equalTo(SAMPLE_OBJECT_ID)));
path = "/" + SAMPLE_OBJECT_ID;
assertThat(ScmFileTransferServlet.objectIdFromPath(path), is(equalTo(SAMPLE_OBJECT_ID)));
path = SAMPLE_OBJECT_ID;
assertThat(ScmFileTransferServlet.objectIdFromPath(path), is(equalTo(SAMPLE_OBJECT_ID)));
String nonObjectId = "this-ist-last-to-found";
path = "/git-lfs-demo.git/info/lfs/objects/" + nonObjectId;
assertThat(ScmFileTransferServlet.objectIdFromPath(path), is(nullValue()));
nonObjectId = SAMPLE_OBJECT_ID.substring(1);
path = "/git-lfs-demo.git/info/lfs/objects/" + nonObjectId;
assertThat(ScmFileTransferServlet.objectIdFromPath(path), is(nullValue()));
nonObjectId = SAMPLE_OBJECT_ID + "X";
path = "/git-lfs-demo.git/info/lfs/objects/" + nonObjectId;
assertThat(ScmFileTransferServlet.objectIdFromPath(path), is(nullValue()));
}
}