mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-07-04 09:09:18 +02:00
Implement file lock for git (#1838)
Adds a "file lock" command that can be used to mark files as locked by a specific user. This command is implemented for git using a store to keep the locks. Additionally, the Git LFS locking API is implemented. To display locks, the scm-manager/scm-file-lock-plugin can be used. Co-authored-by: Eduard Heimbuch <eduard.heimbuch@cloudogu.com>
This commit is contained in:
@@ -32,7 +32,6 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
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.NotFoundException;
|
||||
@@ -47,6 +46,7 @@ import sonia.scm.repository.api.RepositoryService;
|
||||
import sonia.scm.repository.api.RepositoryServiceFactory;
|
||||
import sonia.scm.repository.spi.HttpScmProtocol;
|
||||
import sonia.scm.util.HttpUtil;
|
||||
import sonia.scm.web.ScmClientDetector;
|
||||
import sonia.scm.web.UserAgent;
|
||||
import sonia.scm.web.UserAgentParser;
|
||||
|
||||
@@ -55,7 +55,11 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import static java.util.Collections.emptySet;
|
||||
import static java.util.Collections.singleton;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -78,7 +82,6 @@ class HttpProtocolServletTest {
|
||||
@Mock
|
||||
private ScmConfiguration configuration;
|
||||
|
||||
@InjectMocks
|
||||
private HttpProtocolServlet servlet;
|
||||
|
||||
@Mock
|
||||
@@ -97,108 +100,72 @@ class HttpProtocolServletTest {
|
||||
private HttpScmProtocol protocol;
|
||||
|
||||
@Nested
|
||||
class Browser {
|
||||
class WithoutAdditionalScmClientDetector {
|
||||
|
||||
@BeforeEach
|
||||
void prepareMocks() {
|
||||
when(userAgentParser.parse(request)).thenReturn(userAgent);
|
||||
when(userAgent.isScmClient()).thenReturn(false);
|
||||
when(request.getRequestURI()).thenReturn("uri");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDispatchBrowserRequests() throws ServletException, IOException {
|
||||
servlet.service(request, response);
|
||||
|
||||
verify(dispatcher).dispatch(request, response, "uri");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ScmClient {
|
||||
|
||||
@BeforeEach
|
||||
void prepareMocks() {
|
||||
when(userAgentParser.parse(request)).thenReturn(userAgent);
|
||||
when(userAgent.isScmClient()).thenReturn(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldHandleBadPaths() throws IOException, ServletException {
|
||||
when(request.getPathInfo()).thenReturn("/illegal");
|
||||
|
||||
servlet.service(request, response);
|
||||
|
||||
verify(response).setStatus(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldHandleNotExistingRepository() throws IOException, ServletException {
|
||||
when(request.getPathInfo()).thenReturn("/not/exists");
|
||||
|
||||
NamespaceAndName repo = new NamespaceAndName("not", "exists");
|
||||
when(extractor.fromUri("/not/exists")).thenReturn(Optional.of(repo));
|
||||
when(serviceFactory.create(repo)).thenThrow(new NotFoundException("Test", "a"));
|
||||
|
||||
servlet.service(request, response);
|
||||
|
||||
verify(response).setStatus(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDelegateToProvider() throws IOException, ServletException {
|
||||
NamespaceAndName repo = new NamespaceAndName("space", "name");
|
||||
when(extractor.fromUri("/space/name")).thenReturn(Optional.of(repo));
|
||||
when(serviceFactory.create(repo)).thenReturn(repositoryService);
|
||||
|
||||
when(request.getPathInfo()).thenReturn("/space/name");
|
||||
Repository repository = RepositoryTestData.createHeartOfGold();
|
||||
when(repositoryService.getRepository()).thenReturn(repository);
|
||||
when(repositoryService.getProtocol(HttpScmProtocol.class)).thenReturn(protocol);
|
||||
|
||||
servlet.service(request, response);
|
||||
|
||||
verify(request).setAttribute(DefaultRepositoryProvider.ATTRIBUTE_NAME, repository);
|
||||
verify(protocol).serve(request, response, null);
|
||||
verify(repositoryService).close();
|
||||
void initServlet() {
|
||||
servlet = new HttpProtocolServlet(
|
||||
configuration,
|
||||
serviceFactory,
|
||||
extractor,
|
||||
dispatcher,
|
||||
userAgentParser,
|
||||
emptySet()
|
||||
);
|
||||
}
|
||||
|
||||
@Nested
|
||||
class WithSubject {
|
||||
|
||||
@Mock
|
||||
private Subject subject;
|
||||
class Browser {
|
||||
|
||||
@BeforeEach
|
||||
void setUpSubject() {
|
||||
ThreadContext.bind(subject);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDownSubject() {
|
||||
ThreadContext.unbindSubject();
|
||||
void prepareMocks() {
|
||||
when(userAgentParser.parse(request)).thenReturn(userAgent);
|
||||
when(userAgent.isScmClient()).thenReturn(false);
|
||||
when(request.getRequestURI()).thenReturn("uri");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSendUnauthorizedWithCustomRealmDescription() throws IOException, ServletException {
|
||||
when(subject.getPrincipal()).thenReturn(SCMContext.USER_ANONYMOUS);
|
||||
when(configuration.getRealmDescription()).thenReturn("Hitchhikers finest");
|
||||
void shouldDispatchBrowserRequests() throws ServletException, IOException {
|
||||
servlet.service(request, response);
|
||||
|
||||
callServiceWithAuthorizationException();
|
||||
verify(dispatcher).dispatch(request, response, "uri");
|
||||
}
|
||||
|
||||
verify(response).setHeader(HttpUtil.HEADER_WWW_AUTHENTICATE, "Basic realm=\"Hitchhikers finest\"");
|
||||
verify(response).sendError(HttpServletResponse.SC_UNAUTHORIZED, HttpUtil.STATUS_UNAUTHORIZED_MESSAGE);
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ScmClient {
|
||||
|
||||
@BeforeEach
|
||||
void prepareMocks() {
|
||||
when(userAgentParser.parse(request)).thenReturn(userAgent);
|
||||
when(userAgent.isScmClient()).thenReturn(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSendForbidden() throws IOException, ServletException {
|
||||
callServiceWithAuthorizationException();
|
||||
void shouldHandleBadPaths() throws IOException, ServletException {
|
||||
when(request.getPathInfo()).thenReturn("/illegal");
|
||||
|
||||
verify(response).setStatus(HttpServletResponse.SC_FORBIDDEN);
|
||||
servlet.service(request, response);
|
||||
|
||||
verify(response).setStatus(400);
|
||||
}
|
||||
|
||||
private void callServiceWithAuthorizationException() throws IOException, ServletException {
|
||||
@Test
|
||||
void shouldHandleNotExistingRepository() throws IOException, ServletException {
|
||||
when(request.getPathInfo()).thenReturn("/not/exists");
|
||||
|
||||
NamespaceAndName repo = new NamespaceAndName("not", "exists");
|
||||
when(extractor.fromUri("/not/exists")).thenReturn(Optional.of(repo));
|
||||
when(serviceFactory.create(repo)).thenThrow(new NotFoundException("Test", "a"));
|
||||
|
||||
servlet.service(request, response);
|
||||
|
||||
verify(response).setStatus(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDelegateToProvider() throws IOException, ServletException {
|
||||
NamespaceAndName repo = new NamespaceAndName("space", "name");
|
||||
when(extractor.fromUri("/space/name")).thenReturn(Optional.of(repo));
|
||||
when(serviceFactory.create(repo)).thenReturn(repositoryService);
|
||||
@@ -206,14 +173,93 @@ class HttpProtocolServletTest {
|
||||
when(request.getPathInfo()).thenReturn("/space/name");
|
||||
Repository repository = RepositoryTestData.createHeartOfGold();
|
||||
when(repositoryService.getRepository()).thenReturn(repository);
|
||||
when(repositoryService.getProtocol(HttpScmProtocol.class)).thenThrow(
|
||||
new AuthorizationException("failed")
|
||||
);
|
||||
when(repositoryService.getProtocol(HttpScmProtocol.class)).thenReturn(protocol);
|
||||
|
||||
servlet.service(request, response);
|
||||
|
||||
verify(request).setAttribute(DefaultRepositoryProvider.ATTRIBUTE_NAME, repository);
|
||||
verify(protocol).serve(request, response, null);
|
||||
verify(repositoryService).close();
|
||||
}
|
||||
|
||||
@Nested
|
||||
class WithSubject {
|
||||
|
||||
@Mock
|
||||
private Subject subject;
|
||||
|
||||
@BeforeEach
|
||||
void setUpSubject() {
|
||||
ThreadContext.bind(subject);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDownSubject() {
|
||||
ThreadContext.unbindSubject();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSendUnauthorizedWithCustomRealmDescription() throws IOException, ServletException {
|
||||
when(subject.getPrincipal()).thenReturn(SCMContext.USER_ANONYMOUS);
|
||||
when(configuration.getRealmDescription()).thenReturn("Hitchhikers finest");
|
||||
|
||||
callServiceWithAuthorizationException();
|
||||
|
||||
verify(response).setHeader(HttpUtil.HEADER_WWW_AUTHENTICATE, "Basic realm=\"Hitchhikers finest\"");
|
||||
verify(response).sendError(HttpServletResponse.SC_UNAUTHORIZED, HttpUtil.STATUS_UNAUTHORIZED_MESSAGE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSendForbidden() throws IOException, ServletException {
|
||||
callServiceWithAuthorizationException();
|
||||
|
||||
verify(response).setStatus(HttpServletResponse.SC_FORBIDDEN);
|
||||
}
|
||||
|
||||
private void callServiceWithAuthorizationException() throws IOException, ServletException {
|
||||
NamespaceAndName repo = new NamespaceAndName("space", "name");
|
||||
when(extractor.fromUri("/space/name")).thenReturn(Optional.of(repo));
|
||||
when(serviceFactory.create(repo)).thenReturn(repositoryService);
|
||||
|
||||
when(request.getPathInfo()).thenReturn("/space/name");
|
||||
Repository repository = RepositoryTestData.createHeartOfGold();
|
||||
when(repositoryService.getRepository()).thenReturn(repository);
|
||||
when(repositoryService.getProtocol(HttpScmProtocol.class)).thenThrow(
|
||||
new AuthorizationException("failed")
|
||||
);
|
||||
|
||||
servlet.service(request, response);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class WithAdditionalDetector {
|
||||
|
||||
@Mock
|
||||
private ScmClientDetector detector;
|
||||
|
||||
@BeforeEach
|
||||
void createServlet() {
|
||||
servlet = new HttpProtocolServlet(
|
||||
configuration,
|
||||
serviceFactory,
|
||||
extractor,
|
||||
dispatcher,
|
||||
userAgentParser,
|
||||
singleton(detector)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldConsultScmDetector() throws ServletException, IOException {
|
||||
when(userAgentParser.parse(request)).thenReturn(userAgent);
|
||||
when(detector.isScmClient(request, userAgent)).thenReturn(true);
|
||||
|
||||
servlet.service(request, response);
|
||||
|
||||
verify(response).setStatus(400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user