Add unit test

This commit is contained in:
Rene Pfeuffer
2019-10-21 11:11:44 +02:00
parent 287ee9efe1
commit 36d2723c50
2 changed files with 103 additions and 5 deletions

View File

@@ -52,7 +52,7 @@ public class ScmBlobLfsRepository implements LargeFileRepository {
}
@Override
public Response.Action getDownloadAction(AnyLongObjectId id) {
public ExpiringAction getDownloadAction(AnyLongObjectId id) {
if (accessToken == null) {
accessToken = tokenFactory.createReadAccessToken(repository);
}
@@ -60,7 +60,7 @@ public class ScmBlobLfsRepository implements LargeFileRepository {
}
@Override
public Response.Action getUploadAction(AnyLongObjectId id, long size) {
public ExpiringAction getUploadAction(AnyLongObjectId id, long size) {
if (accessToken == null) {
accessToken = tokenFactory.createWriteAccessToken(repository);
}
@@ -68,14 +68,14 @@ public class ScmBlobLfsRepository implements LargeFileRepository {
}
@Override
public Response.Action getVerifyAction(AnyLongObjectId id) {
public ExpiringAction getVerifyAction(AnyLongObjectId id) {
//validation is optional. We do not support it.
return null;
}
@Override
public long getSize(AnyLongObjectId id) throws IOException {
public long getSize(AnyLongObjectId id) {
//this needs to be size of what is will be written into the response of the download. Clients are likely to
// verify it.
@@ -93,7 +93,7 @@ public class ScmBlobLfsRepository implements LargeFileRepository {
/**
* Constructs the Download / Upload actions to be supplied to the client.
*/
private Response.Action getAction(AnyLongObjectId id, AccessToken token) {
private ExpiringAction getAction(AnyLongObjectId id, AccessToken token) {
//LFS protocol has to provide the information on where to put or get the actual content, i. e.
//the actual URI for up- and download.

View File

@@ -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);
}
}