mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 07:25:44 +01:00
Merged in feature/lfs_over_ssh (pull request #336)
Feature LFS over ssh
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
package sonia.scm.web.lfs;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
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 sonia.scm.config.ScmConfiguration;
|
||||
import sonia.scm.protocolcommand.CommandContext;
|
||||
import sonia.scm.protocolcommand.CommandInterpreter;
|
||||
import sonia.scm.protocolcommand.RepositoryContext;
|
||||
import sonia.scm.protocolcommand.git.GitRepositoryContextResolver;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.security.AccessToken;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.Optional;
|
||||
|
||||
import static java.time.Instant.parse;
|
||||
import static java.util.Date.from;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class LFSAuthCommandTest {
|
||||
|
||||
static final Repository REPOSITORY = new Repository("1", "git", "space", "X");
|
||||
static final Date EXPIRATION = from(parse("2007-05-03T10:15:30.00Z"));
|
||||
|
||||
@Mock
|
||||
LfsAccessTokenFactory tokenFactory;
|
||||
@Mock
|
||||
GitRepositoryContextResolver gitRepositoryContextResolver;
|
||||
@Mock
|
||||
ScmConfiguration configuration;
|
||||
|
||||
@InjectMocks
|
||||
LFSAuthCommand lfsAuthCommand;
|
||||
|
||||
@BeforeEach
|
||||
void initAuthorizationToken() {
|
||||
AccessToken accessToken = mock(AccessToken.class);
|
||||
lenient().when(this.tokenFactory.createReadAccessToken(REPOSITORY)).thenReturn(accessToken);
|
||||
lenient().when(this.tokenFactory.createWriteAccessToken(REPOSITORY)).thenReturn(accessToken);
|
||||
lenient().when(accessToken.getExpiration()).thenReturn(EXPIRATION);
|
||||
lenient().when(accessToken.compact()).thenReturn("ACCESS_TOKEN");
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void initConfig() {
|
||||
lenient().when(configuration.getBaseUrl()).thenReturn("http://example.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldHandleGitLfsAuthenticate() {
|
||||
Optional<CommandInterpreter> commandInterpreter = lfsAuthCommand.canHandle("git-lfs-authenticate repo/space/X upload");
|
||||
assertThat(commandInterpreter).isPresent();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotHandleOtherCommands() {
|
||||
Optional<CommandInterpreter> commandInterpreter = lfsAuthCommand.canHandle("git-lfs-something repo/space/X upload");
|
||||
assertThat(commandInterpreter).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldExtractRepositoryArgument() {
|
||||
CommandInterpreter commandInterpreter = lfsAuthCommand.canHandle("git-lfs-authenticate repo/space/X\t upload").get();
|
||||
assertThat(commandInterpreter.getParsedArgs()).containsOnly("repo/space/X");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCreateJsonResponse() throws IOException {
|
||||
CommandInterpreter commandInterpreter = lfsAuthCommand.canHandle("git-lfs-authenticate repo/space/X\t upload").get();
|
||||
CommandContext commandContext = createCommandContext();
|
||||
commandInterpreter.getProtocolHandler().handle(commandContext, createRepositoryContext());
|
||||
assertThat(commandContext.getOutputStream().toString())
|
||||
.isEqualTo("{\"href\":\"http://example.com/repo/space/X.git/info/lfs/\",\"header\":{\"Authorization\":\"Bearer ACCESS_TOKEN\"},\"expires_at\":\"2007-05-03T10:15:30Z\"}");
|
||||
}
|
||||
|
||||
private CommandContext createCommandContext() {
|
||||
return new CommandContext(null, null, null, new ByteArrayOutputStream(), null);
|
||||
}
|
||||
|
||||
private RepositoryContext createRepositoryContext() {
|
||||
return new RepositoryContext(REPOSITORY, null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package sonia.scm.web.lfs;
|
||||
|
||||
import org.eclipse.jgit.lfs.lib.LongObjectId;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.security.AccessToken;
|
||||
import sonia.scm.store.BlobStore;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import static java.time.Instant.parse;
|
||||
import static java.util.Date.from;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.eclipse.jgit.lfs.lib.LongObjectId.fromString;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class ScmBlobLfsRepositoryTest {
|
||||
|
||||
static final Repository REPOSITORY = new Repository("1", "git", "space", "X");
|
||||
static final Date EXPIRATION = from(parse("2007-05-03T10:15:30.00Z"));
|
||||
static final LongObjectId OBJECT_ID = fromString("976ed944c37cc5d1606af316937edb9d286ecf6c606af316937edb9d286ecf6c");
|
||||
|
||||
@Mock
|
||||
BlobStore blobStore;
|
||||
@Mock
|
||||
LfsAccessTokenFactory tokenFactory;
|
||||
|
||||
ScmBlobLfsRepository lfsRepository;
|
||||
|
||||
@BeforeEach
|
||||
void initializeLfsRepository() {
|
||||
lfsRepository = new ScmBlobLfsRepository(REPOSITORY, blobStore, tokenFactory, "http://scm.org/");
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void initAuthorizationToken() {
|
||||
AccessToken readToken = createToken("READ_TOKEN");
|
||||
lenient().when(this.tokenFactory.createReadAccessToken(REPOSITORY))
|
||||
.thenReturn(readToken);
|
||||
AccessToken writeToken = createToken("WRITE_TOKEN");
|
||||
lenient().when(this.tokenFactory.createWriteAccessToken(REPOSITORY))
|
||||
.thenReturn(writeToken);
|
||||
}
|
||||
|
||||
AccessToken createToken(String mockedValue) {
|
||||
AccessToken accessToken = mock(AccessToken.class);
|
||||
lenient().when(accessToken.getExpiration()).thenReturn(EXPIRATION);
|
||||
lenient().when(accessToken.compact()).thenReturn(mockedValue);
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldTakeExpirationFromToken() {
|
||||
ExpiringAction downloadAction = lfsRepository.getDownloadAction(OBJECT_ID);
|
||||
assertThat(downloadAction.expires_at).isEqualTo("2007-05-03T10:15:30Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldContainReadTokenForDownlo() {
|
||||
ExpiringAction downloadAction = lfsRepository.getDownloadAction(OBJECT_ID);
|
||||
assertThat(downloadAction.header.get("Authorization")).isEqualTo("Bearer READ_TOKEN");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldContainWriteTokenForUpload() {
|
||||
ExpiringAction downloadAction = lfsRepository.getUploadAction(OBJECT_ID, 42L);
|
||||
assertThat(downloadAction.header.get("Authorization")).isEqualTo("Bearer WRITE_TOKEN");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldContainUrl() {
|
||||
ExpiringAction downloadAction = lfsRepository.getDownloadAction(OBJECT_ID);
|
||||
assertThat(downloadAction.href).isEqualTo("http://scm.org/976ed944c37cc5d1606af316937edb9d286ecf6c606af316937edb9d286ecf6c");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCreateTokenForDownloadActionOnlyOnce() {
|
||||
lfsRepository.getDownloadAction(OBJECT_ID);
|
||||
lfsRepository.getDownloadAction(OBJECT_ID);
|
||||
verify(tokenFactory, times(1)).createReadAccessToken(REPOSITORY);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCreateTokenForUploadActionOnlyOnce() {
|
||||
lfsRepository.getUploadAction(OBJECT_ID, 42L);
|
||||
lfsRepository.getUploadAction(OBJECT_ID, 42L);
|
||||
verify(tokenFactory, times(1)).createWriteAccessToken(REPOSITORY);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,41 +11,26 @@ import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Created by omilke on 18.05.2017.
|
||||
*/
|
||||
public class LfsServletFactoryTest {
|
||||
|
||||
private static final String NAMESPACE = "space";
|
||||
private static final String NAME = "git-lfs-demo";
|
||||
private static final Repository REPOSITORY = new Repository("", "GIT", NAMESPACE, NAME);
|
||||
|
||||
@Test
|
||||
public void buildBaseUri() {
|
||||
|
||||
String repositoryNamespace = "space";
|
||||
String repositoryName = "git-lfs-demo";
|
||||
|
||||
String result = LfsServletFactory.buildBaseUri(new Repository("", "GIT", repositoryNamespace, repositoryName), RequestWithUri(repositoryName, true));
|
||||
assertThat(result, is(equalTo("http://localhost:8081/scm/repo/space/git-lfs-demo.git/info/lfs/objects/")));
|
||||
|
||||
|
||||
//result will be with dot-git suffix, ide
|
||||
result = LfsServletFactory.buildBaseUri(new Repository("", "GIT", repositoryNamespace, repositoryName), RequestWithUri(repositoryName, false));
|
||||
public void shouldBuildBaseUri() {
|
||||
String result = LfsServletFactory.buildBaseUri(REPOSITORY, requestWithUri("git-lfs-demo"));
|
||||
assertThat(result, is(equalTo("http://localhost:8081/scm/repo/space/git-lfs-demo.git/info/lfs/objects/")));
|
||||
}
|
||||
|
||||
private HttpServletRequest RequestWithUri(String repositoryName, boolean withDotGitSuffix) {
|
||||
private HttpServletRequest requestWithUri(String repositoryName) {
|
||||
|
||||
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/repo/%s%s/info/lfs/objects/batch", repositoryName, suffix)));
|
||||
when(mockedRequest.getRequestURI()).thenReturn(String.format("/scm/repo/%s%s/info/lfs/objects/batch", repositoryName, suffix));
|
||||
new StringBuffer(String.format("http://localhost:8081/scm/repo/%s/info/lfs/objects/batch", repositoryName)));
|
||||
when(mockedRequest.getRequestURI()).thenReturn(String.format("/scm/repo/%s/info/lfs/objects/batch", repositoryName));
|
||||
when(mockedRequest.getContextPath()).thenReturn("/scm");
|
||||
|
||||
return mockedRequest;
|
||||
|
||||
Reference in New Issue
Block a user