mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-27 14:23:56 +00:00
[AB-177] adding ability to restrict the allowed mentions and configure them via commands on a server basis, default is defined via property file
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
package dev.sheldan.abstracto.core.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.config.AllowedMentionConfig;
|
||||
import dev.sheldan.abstracto.core.exception.UnknownMentionTypeException;
|
||||
import dev.sheldan.abstracto.core.models.database.AllowedMention;
|
||||
import dev.sheldan.abstracto.core.service.management.AllowedMentionManagementService;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class AllowedMentionServiceBeanTest {
|
||||
|
||||
@InjectMocks
|
||||
private AllowedMentionServiceBean testUnit;
|
||||
|
||||
@Mock
|
||||
private AllowedMentionConfig allowedMentionConfig;
|
||||
|
||||
@Mock
|
||||
private AllowedMentionManagementService allowedMentionManagementService;
|
||||
|
||||
@Mock
|
||||
private AllowedMention allowedMention;
|
||||
|
||||
private static final Long SERVER_ID = 4L;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
testUnit.postConstruct();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allMentionsAllowedDefaultAll() {
|
||||
allDefaultConfigAllowed();
|
||||
when(allowedMentionManagementService.getCustomAllowedMentionFor(SERVER_ID)).thenReturn(Optional.empty());
|
||||
Assert.assertTrue(testUnit.allMentionsAllowed(SERVER_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allMentionsAllowedDefaultNotAll() {
|
||||
when(allowedMentionConfig.getEveryone()).thenReturn(true);
|
||||
when(allowedMentionConfig.getRole()).thenReturn(false);
|
||||
when(allowedMentionConfig.getUser()).thenReturn(true);
|
||||
when(allowedMentionManagementService.getCustomAllowedMentionFor(SERVER_ID)).thenReturn(Optional.empty());
|
||||
Assert.assertFalse(testUnit.allMentionsAllowed(SERVER_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allMentionsAllowedCustomAll() {
|
||||
when(allowedMention.allAllowed()).thenReturn(true);
|
||||
when(allowedMentionManagementService.getCustomAllowedMentionFor(SERVER_ID)).thenReturn(Optional.of(allowedMention));
|
||||
Assert.assertTrue(testUnit.allMentionsAllowed(SERVER_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllowedMentionTypesForServerEmpty() {
|
||||
allDefaultConfigAllowed();
|
||||
when(allowedMentionManagementService.getCustomAllowedMentionFor(SERVER_ID)).thenReturn(Optional.empty());
|
||||
List<Message.MentionType> allowedMentions = testUnit.getAllowedMentionTypesForServer(SERVER_ID);
|
||||
Assert.assertNull(allowedMentions);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllowedMentionTypesAllAllowed() {
|
||||
when(allowedMentionConfig.getEveryone()).thenReturn(true);
|
||||
when(allowedMentionConfig.getRole()).thenReturn(false);
|
||||
when(allowedMentionConfig.getUser()).thenReturn(false);
|
||||
when(allowedMentionManagementService.getCustomAllowedMentionFor(SERVER_ID)).thenReturn(Optional.empty());
|
||||
List<Message.MentionType> allowedMentions = testUnit.getAllowedMentionTypesForServer(SERVER_ID);
|
||||
Assert.assertEquals(1, allowedMentions.size());
|
||||
Assert.assertEquals(Message.MentionType.EVERYONE, allowedMentions.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllowedMentionTypesAllDisallowed() {
|
||||
when(allowedMentionConfig.getEveryone()).thenReturn(false);
|
||||
when(allowedMentionConfig.getRole()).thenReturn(false);
|
||||
when(allowedMentionConfig.getUser()).thenReturn(false);
|
||||
when(allowedMentionManagementService.getCustomAllowedMentionFor(SERVER_ID)).thenReturn(Optional.empty());
|
||||
List<Message.MentionType> allowedMentions = testUnit.getAllowedMentionTypesForServer(SERVER_ID);
|
||||
Assert.assertEquals(0, allowedMentions.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allowMentionForServerWithExistingCustom() {
|
||||
when(allowedMentionManagementService.getCustomAllowedMentionFor(SERVER_ID)).thenReturn(Optional.of(allowedMention));
|
||||
testUnit.allowMentionForServer(Message.MentionType.EVERYONE, SERVER_ID);
|
||||
verify(allowedMention, times(1)).setEveryone(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allowMentionForServerWithNewCustom() {
|
||||
when(allowedMentionManagementService.getCustomAllowedMentionFor(SERVER_ID)).thenReturn(Optional.empty());
|
||||
testUnit.allowMentionForServer(Message.MentionType.EVERYONE, SERVER_ID);
|
||||
ArgumentCaptor<AllowedMention> argumentCaptor = ArgumentCaptor.forClass(AllowedMention.class);
|
||||
verify(allowedMentionManagementService, times(1)).createCustomAllowedMention(eq(SERVER_ID), argumentCaptor.capture());
|
||||
AllowedMention creationArgument = argumentCaptor.getValue();
|
||||
Assert.assertTrue(creationArgument.getEveryone());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disAllowMentionForServer() {
|
||||
when(allowedMentionManagementService.getCustomAllowedMentionFor(SERVER_ID)).thenReturn(Optional.empty());
|
||||
testUnit.disAllowMentionForServer(Message.MentionType.EVERYONE, SERVER_ID);
|
||||
ArgumentCaptor<AllowedMention> argumentCaptor = ArgumentCaptor.forClass(AllowedMention.class);
|
||||
verify(allowedMentionManagementService, times(1)).createCustomAllowedMention(eq(SERVER_ID), argumentCaptor.capture());
|
||||
AllowedMention creationArgument = argumentCaptor.getValue();
|
||||
Assert.assertFalse(creationArgument.getEveryone());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDefaultAllowedMention() {
|
||||
when(allowedMentionConfig.getEveryone()).thenReturn(true);
|
||||
AllowedMention defaultMention = testUnit.getDefaultAllowedMention();
|
||||
Assert.assertEquals(true, defaultMention.getEveryone());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getEffectiveAllowedMentionWithCustom() {
|
||||
when(allowedMentionManagementService.getCustomAllowedMentionFor(SERVER_ID)).thenReturn(Optional.of(allowedMention));
|
||||
AllowedMention effectiveMention = testUnit.getEffectiveAllowedMention(SERVER_ID);
|
||||
Assert.assertEquals(allowedMention, effectiveMention);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getEffectiveAllowedMentionNoCustom() {
|
||||
when(allowedMentionConfig.getEveryone()).thenReturn(true);
|
||||
when(allowedMentionManagementService.getCustomAllowedMentionFor(SERVER_ID)).thenReturn(Optional.empty());
|
||||
AllowedMention effectiveMention = testUnit.getEffectiveAllowedMention(SERVER_ID);
|
||||
Assert.assertNull(effectiveMention.getServer());
|
||||
Assert.assertTrue(effectiveMention.getEveryone());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMentionTypeFromString() {
|
||||
Message.MentionType result = testUnit.getMentionTypeFromString("everyone");
|
||||
Assert.assertEquals(Message.MentionType.EVERYONE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMentionTypeFromStringIgnoreCase() {
|
||||
Message.MentionType result = testUnit.getMentionTypeFromString("eVeRyOnE");
|
||||
Assert.assertEquals(Message.MentionType.EVERYONE, result);
|
||||
}
|
||||
|
||||
@Test(expected = UnknownMentionTypeException.class)
|
||||
public void getUnknownMentionType() {
|
||||
testUnit.getMentionTypeFromString("test");
|
||||
}
|
||||
|
||||
private void allDefaultConfigAllowed() {
|
||||
when(allowedMentionConfig.getEveryone()).thenReturn(true);
|
||||
when(allowedMentionConfig.getRole()).thenReturn(true);
|
||||
when(allowedMentionConfig.getUser()).thenReturn(true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package dev.sheldan.abstracto.core.service.management;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.models.database.AllowedMention;
|
||||
import dev.sheldan.abstracto.core.repository.AllowedMentionRepository;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class AllowedMentionManagementServiceBeanTest {
|
||||
|
||||
@InjectMocks
|
||||
private AllowedMentionManagementServiceBean testUnit;
|
||||
|
||||
@Mock
|
||||
private AllowedMentionRepository allowedMentionRepository;
|
||||
|
||||
@Mock
|
||||
private ServerManagementService serverManagementService;
|
||||
|
||||
@Mock
|
||||
private AllowedMention allowedMention;
|
||||
|
||||
@Mock
|
||||
private AServer server;
|
||||
|
||||
private static final Long SERVER_ID = 4L;
|
||||
|
||||
@Test
|
||||
public void getCustomAllowedMentionForIdExisting() {
|
||||
when(allowedMentionRepository.findById(SERVER_ID)).thenReturn(Optional.of(allowedMention));
|
||||
Optional<AllowedMention> foundMentionOptional = testUnit.getCustomAllowedMentionFor(SERVER_ID);
|
||||
Assert.assertTrue(foundMentionOptional.isPresent());
|
||||
foundMentionOptional.ifPresent(foundMention -> Assert.assertEquals(allowedMention, foundMention));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCustomAllowedMentionForIdNotExisting() {
|
||||
when(allowedMentionRepository.findById(SERVER_ID)).thenReturn(Optional.empty());
|
||||
Optional<AllowedMention> foundMentionOptional = testUnit.getCustomAllowedMentionFor(SERVER_ID);
|
||||
Assert.assertFalse(foundMentionOptional.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCustomAllowedMentionForServer() {
|
||||
when(allowedMentionRepository.findByServer(server)).thenReturn(Optional.of(allowedMention));
|
||||
Optional<AllowedMention> foundMentionOptional = testUnit.getCustomAllowedMentionFor(server);
|
||||
Assert.assertTrue(foundMentionOptional.isPresent());
|
||||
foundMentionOptional.ifPresent(foundMention -> Assert.assertEquals(allowedMention, foundMention));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hasCustomAllowedMention() {
|
||||
when(allowedMentionRepository.findById(SERVER_ID)).thenReturn(Optional.of(allowedMention));
|
||||
Assert.assertTrue(testUnit.hasCustomAllowedMention(SERVER_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createCustomAllowedMention() {
|
||||
when(serverManagementService.loadOrCreate(SERVER_ID)).thenReturn(server);
|
||||
AllowedMention createdMention = testUnit.createCustomAllowedMention(SERVER_ID, allowedMention);
|
||||
ArgumentCaptor<AllowedMention> mentionCaptor = ArgumentCaptor.forClass(AllowedMention.class);
|
||||
verify(allowedMentionRepository, times(1)).save(mentionCaptor.capture());
|
||||
Assert.assertEquals(createdMention, mentionCaptor.getValue());
|
||||
Assert.assertEquals(server, createdMention.getServer());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteCustomAllowedMention() {
|
||||
testUnit.deleteCustomAllowedMention(SERVER_ID);
|
||||
verify(allowedMentionRepository, times(1)).deleteById(SERVER_ID);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user