Files
SCM-Manager/scm-webapp/src/test/java/sonia/scm/plugin/DefaultPluginManagerTest.java

292 lines
9.8 KiB
Java
Raw Normal View History

2019-08-20 12:29:59 +02:00
package sonia.scm.plugin;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
2019-08-21 09:25:44 +02:00
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
2019-08-20 12:29:59 +02:00
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Answers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
2019-08-21 09:25:44 +02:00
import sonia.scm.NotFoundException;
2019-08-20 12:29:59 +02:00
import java.util.List;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
2019-08-20 14:43:48 +02:00
import static org.mockito.Mockito.*;
2019-08-20 12:29:59 +02:00
@ExtendWith(MockitoExtension.class)
class DefaultPluginManagerTest {
@Mock
private PluginLoader loader;
@Mock
private PluginCenter center;
2019-08-20 14:43:48 +02:00
@Mock
private PluginInstaller installer;
2019-08-20 12:29:59 +02:00
@InjectMocks
private DefaultPluginManager manager;
2019-08-21 09:25:44 +02:00
@Mock
private Subject subject;
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
@Nested
class WithAdminPermissions {
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
@BeforeEach
void setUpSubject() {
ThreadContext.bind(subject);
}
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
@AfterEach
void clearThreadContext() {
ThreadContext.unbindSubject();
}
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
@Test
void shouldReturnInstalledPlugins() {
InstalledPlugin review = createInstalled("scm-review-plugin");
InstalledPlugin git = createInstalled("scm-git-plugin");
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
when(loader.getInstalledPlugins()).thenReturn(ImmutableList.of(review, git));
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
List<InstalledPlugin> installed = manager.getInstalled();
assertThat(installed).containsOnly(review, git);
}
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
@Test
void shouldReturnReviewPlugin() {
InstalledPlugin review = createInstalled("scm-review-plugin");
InstalledPlugin git = createInstalled("scm-git-plugin");
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
when(loader.getInstalledPlugins()).thenReturn(ImmutableList.of(review, git));
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
Optional<InstalledPlugin> plugin = manager.getInstalled("scm-review-plugin");
assertThat(plugin).contains(review);
}
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
@Test
void shouldReturnEmptyForNonInstalledPlugin() {
when(loader.getInstalledPlugins()).thenReturn(ImmutableList.of());
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
Optional<InstalledPlugin> plugin = manager.getInstalled("scm-review-plugin");
assertThat(plugin).isEmpty();
}
@Test
void shouldReturnAvailablePlugins() {
AvailablePlugin review = createAvailable("scm-review-plugin");
AvailablePlugin git = createAvailable("scm-git-plugin");
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
when(center.getAvailable()).thenReturn(ImmutableSet.of(review, git));
List<AvailablePlugin> available = manager.getAvailable();
assertThat(available).containsOnly(review, git);
}
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
@Test
void shouldFilterOutAllInstalled() {
InstalledPlugin installedGit = createInstalled("scm-git-plugin");
when(loader.getInstalledPlugins()).thenReturn(ImmutableList.of(installedGit));
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
AvailablePlugin review = createAvailable("scm-review-plugin");
AvailablePlugin git = createAvailable("scm-git-plugin");
when(center.getAvailable()).thenReturn(ImmutableSet.of(review, git));
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
List<AvailablePlugin> available = manager.getAvailable();
assertThat(available).containsOnly(review);
}
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
@Test
void shouldReturnAvailable() {
AvailablePlugin review = createAvailable("scm-review-plugin");
AvailablePlugin git = createAvailable("scm-git-plugin");
when(center.getAvailable()).thenReturn(ImmutableSet.of(review, git));
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
Optional<AvailablePlugin> available = manager.getAvailable("scm-git-plugin");
assertThat(available).contains(git);
}
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
@Test
void shouldReturnEmptyForNonExistingAvailable() {
AvailablePlugin review = createAvailable("scm-review-plugin");
when(center.getAvailable()).thenReturn(ImmutableSet.of(review));
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
Optional<AvailablePlugin> available = manager.getAvailable("scm-git-plugin");
assertThat(available).isEmpty();
}
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
@Test
void shouldReturnEmptyForInstalledPlugin() {
InstalledPlugin installedGit = createInstalled("scm-git-plugin");
when(loader.getInstalledPlugins()).thenReturn(ImmutableList.of(installedGit));
2019-08-20 12:29:59 +02:00
2019-08-21 09:25:44 +02:00
AvailablePlugin git = createAvailable("scm-git-plugin");
when(center.getAvailable()).thenReturn(ImmutableSet.of(git));
2019-08-20 14:43:48 +02:00
2019-08-21 09:25:44 +02:00
Optional<AvailablePlugin> available = manager.getAvailable("scm-git-plugin");
assertThat(available).isEmpty();
}
2019-08-20 14:43:48 +02:00
2019-08-21 09:25:44 +02:00
@Test
void shouldInstallThePlugin() {
AvailablePlugin git = createAvailable("scm-git-plugin");
when(center.getAvailable()).thenReturn(ImmutableSet.of(git));
2019-08-20 14:43:48 +02:00
2019-08-21 09:25:44 +02:00
manager.install("scm-git-plugin");
2019-08-20 14:43:48 +02:00
2019-08-21 09:25:44 +02:00
verify(installer).install(git);
}
2019-08-20 14:43:48 +02:00
2019-08-21 09:25:44 +02:00
@Test
void shouldInstallDependingPlugins() {
AvailablePlugin review = createAvailable("scm-review-plugin");
when(review.getDescriptor().getDependencies()).thenReturn(ImmutableSet.of("scm-mail-plugin"));
AvailablePlugin mail = createAvailable("scm-mail-plugin");
when(center.getAvailable()).thenReturn(ImmutableSet.of(review, mail));
manager.install("scm-review-plugin");
verify(installer).install(mail);
verify(installer).install(review);
}
@Test
void shouldNotInstallAlreadyInstalledDependencies() {
AvailablePlugin review = createAvailable("scm-review-plugin");
when(review.getDescriptor().getDependencies()).thenReturn(ImmutableSet.of("scm-mail-plugin"));
AvailablePlugin mail = createAvailable("scm-mail-plugin");
when(center.getAvailable()).thenReturn(ImmutableSet.of(review, mail));
2019-08-20 14:43:48 +02:00
2019-08-21 09:25:44 +02:00
InstalledPlugin installedMail = createInstalled("scm-mail-plugin");
when(loader.getInstalledPlugins()).thenReturn(ImmutableList.of(installedMail));
2019-08-20 14:43:48 +02:00
2019-08-21 09:25:44 +02:00
manager.install("scm-review-plugin");
2019-08-20 14:43:48 +02:00
2019-08-21 09:25:44 +02:00
verify(installer).install(review);
}
@Test
void shouldRollbackOnFailedInstallation() {
AvailablePlugin review = createAvailable("scm-review-plugin");
when(review.getDescriptor().getDependencies()).thenReturn(ImmutableSet.of("scm-mail-plugin"));
AvailablePlugin mail = createAvailable("scm-mail-plugin");
when(mail.getDescriptor().getDependencies()).thenReturn(ImmutableSet.of("scm-notification-plugin"));
AvailablePlugin notification = createAvailable("scm-notification-plugin");
when(center.getAvailable()).thenReturn(ImmutableSet.of(review, mail, notification));
PendingPluginInstallation pendingNotification = mock(PendingPluginInstallation.class);
doReturn(pendingNotification).when(installer).install(notification);
PendingPluginInstallation pendingMail = mock(PendingPluginInstallation.class);
doReturn(pendingMail).when(installer).install(mail);
doThrow(new PluginChecksumMismatchException("checksum does not match")).when(installer).install(review);
assertThrows(PluginInstallException.class, () -> manager.install("scm-review-plugin"));
verify(pendingNotification).cancel();
verify(pendingMail).cancel();
}
@Test
void shouldInstallNothingIfOneOfTheDependenciesIsNotAvailable() {
AvailablePlugin review = createAvailable("scm-review-plugin");
when(review.getDescriptor().getDependencies()).thenReturn(ImmutableSet.of("scm-mail-plugin"));
AvailablePlugin mail = createAvailable("scm-mail-plugin");
when(mail.getDescriptor().getDependencies()).thenReturn(ImmutableSet.of("scm-notification-plugin"));
when(center.getAvailable()).thenReturn(ImmutableSet.of(review, mail));
assertThrows(NotFoundException.class, () -> manager.install("scm-review-plugin"));
verify(installer, never()).install(any());
}
2019-08-20 14:43:48 +02:00
}
2019-08-21 09:25:44 +02:00
@Nested
class WithoutReadPermissions {
@BeforeEach
void setUpSubject() {
ThreadContext.bind(subject);
doThrow(AuthorizationException.class).when(subject).checkPermission("plugin:read");
}
@AfterEach
void clearThreadContext() {
ThreadContext.unbindSubject();
}
@Test
void shouldThrowAuthorizationExceptionsForReadMethods() {
assertThrows(AuthorizationException.class, () -> manager.getInstalled());
assertThrows(AuthorizationException.class, () -> manager.getInstalled("test"));
assertThrows(AuthorizationException.class, () -> manager.getAvailable());
assertThrows(AuthorizationException.class, () -> manager.getAvailable("test"));
}
}
2019-08-21 09:25:44 +02:00
@Nested
class WithoutManagePermissions {
2019-08-21 09:25:44 +02:00
@BeforeEach
void setUpSubject() {
ThreadContext.bind(subject);
doThrow(AuthorizationException.class).when(subject).checkPermission("plugin:manage");
}
2019-08-21 09:25:44 +02:00
@AfterEach
void clearThreadContext() {
ThreadContext.unbindSubject();
}
2019-08-21 09:25:44 +02:00
@Test
void shouldThrowAuthorizationExceptionsForInstallMethod() {
assertThrows(AuthorizationException.class, () -> manager.install("test"));
}
}
2019-08-20 12:29:59 +02:00
private AvailablePlugin createAvailable(String name) {
PluginInformation information = new PluginInformation();
information.setName(name);
return createAvailable(information);
}
private InstalledPlugin createInstalled(String name) {
PluginInformation information = new PluginInformation();
information.setName(name);
return createInstalled(information);
}
private InstalledPlugin createInstalled(PluginInformation information) {
InstalledPlugin plugin = mock(InstalledPlugin.class, Answers.RETURNS_DEEP_STUBS);
returnInformation(plugin, information);
return plugin;
}
private AvailablePlugin createAvailable(PluginInformation information) {
AvailablePlugin plugin = mock(AvailablePlugin.class, Answers.RETURNS_DEEP_STUBS);
returnInformation(plugin, information);
return plugin;
}
private void returnInformation(Plugin mockedPlugin, PluginInformation information) {
when(mockedPlugin.getDescriptor().getInformation()).thenReturn(information);
}
}