From 36d2723c50e7f181c4bf7f1ffe237d66deaeac1e Mon Sep 17 00:00:00 2001 From: Rene Pfeuffer Date: Mon, 21 Oct 2019 11:11:44 +0200 Subject: [PATCH] Add unit test --- .../scm/web/lfs/ScmBlobLfsRepository.java | 10 +- .../scm/web/lfs/ScmBlobLfsRepositoryTest.java | 98 +++++++++++++++++++ 2 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 scm-plugins/scm-git-plugin/src/test/java/sonia/scm/web/lfs/ScmBlobLfsRepositoryTest.java diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/lfs/ScmBlobLfsRepository.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/lfs/ScmBlobLfsRepository.java index 5d606379a1..c646a194b7 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/lfs/ScmBlobLfsRepository.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/lfs/ScmBlobLfsRepository.java @@ -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. diff --git a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/web/lfs/ScmBlobLfsRepositoryTest.java b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/web/lfs/ScmBlobLfsRepositoryTest.java new file mode 100644 index 0000000000..eefa4314c2 --- /dev/null +++ b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/web/lfs/ScmBlobLfsRepositoryTest.java @@ -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); + } +} +