2017-06-30 18:29:10 +02:00
|
|
|
package org.telegram.abilitybots.api.bot;
|
|
|
|
|
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
|
|
import com.google.common.io.Files;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
2019-07-08 21:22:51 +02:00
|
|
|
import org.junit.jupiter.api.AfterEach;
|
|
|
|
import org.junit.jupiter.api.Assertions;
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
import org.junit.jupiter.api.Test;
|
2019-07-09 08:18:08 +02:00
|
|
|
import org.mockito.ArgumentMatchers;
|
2020-03-01 07:50:52 +01:00
|
|
|
import org.mockito.Mockito;
|
2017-06-30 18:29:10 +02:00
|
|
|
import org.telegram.abilitybots.api.db.DBContext;
|
|
|
|
import org.telegram.abilitybots.api.objects.*;
|
|
|
|
import org.telegram.abilitybots.api.sender.MessageSender;
|
2017-10-29 03:15:53 +01:00
|
|
|
import org.telegram.abilitybots.api.sender.SilentSender;
|
2020-04-26 22:22:05 +02:00
|
|
|
import org.telegram.abilitybots.api.util.AbilityUtils;
|
2017-06-30 18:29:10 +02:00
|
|
|
import org.telegram.abilitybots.api.util.Pair;
|
|
|
|
import org.telegram.abilitybots.api.util.Trio;
|
2018-07-08 01:41:21 +02:00
|
|
|
import org.telegram.telegrambots.meta.api.methods.groupadministration.GetChatAdministrators;
|
2019-09-26 08:25:11 +02:00
|
|
|
import org.telegram.telegrambots.meta.api.objects.*;
|
2018-07-08 01:41:21 +02:00
|
|
|
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
import java.io.BufferedWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Map;
|
2018-02-04 09:23:41 +01:00
|
|
|
import java.util.Optional;
|
2017-06-30 18:29:10 +02:00
|
|
|
import java.util.Set;
|
2020-05-09 23:02:36 +02:00
|
|
|
import java.util.function.Consumer;
|
2017-06-30 18:29:10 +02:00
|
|
|
|
2018-02-04 09:23:41 +01:00
|
|
|
import static com.google.common.collect.Lists.newArrayList;
|
2017-06-30 18:29:10 +02:00
|
|
|
import static com.google.common.collect.Sets.newHashSet;
|
|
|
|
import static java.lang.String.format;
|
|
|
|
import static java.util.Collections.emptySet;
|
2018-02-04 09:23:41 +01:00
|
|
|
import static java.util.Optional.empty;
|
2017-10-29 03:15:53 +01:00
|
|
|
import static org.apache.commons.io.FileUtils.deleteQuietly;
|
2017-06-30 18:29:10 +02:00
|
|
|
import static org.apache.commons.lang3.ArrayUtils.addAll;
|
|
|
|
import static org.apache.commons.lang3.StringUtils.EMPTY;
|
2019-09-26 08:25:11 +02:00
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
import static org.mockito.Mockito.*;
|
2017-06-30 18:29:10 +02:00
|
|
|
import static org.mockito.internal.verification.VerificationModeFactory.times;
|
2021-01-25 17:57:48 +01:00
|
|
|
import static org.telegram.abilitybots.api.bot.DefaultBot.*;
|
2019-09-26 08:25:11 +02:00
|
|
|
import static org.telegram.abilitybots.api.bot.TestUtils.CREATOR;
|
2020-04-26 22:22:05 +02:00
|
|
|
import static org.telegram.abilitybots.api.bot.TestUtils.*;
|
2017-06-30 18:29:10 +02:00
|
|
|
import static org.telegram.abilitybots.api.db.MapDBContext.offlineInstance;
|
|
|
|
import static org.telegram.abilitybots.api.objects.Flag.DOCUMENT;
|
|
|
|
import static org.telegram.abilitybots.api.objects.Flag.MESSAGE;
|
|
|
|
import static org.telegram.abilitybots.api.objects.Locality.ALL;
|
|
|
|
import static org.telegram.abilitybots.api.objects.Locality.GROUP;
|
|
|
|
import static org.telegram.abilitybots.api.objects.MessageContext.newContext;
|
2018-02-04 09:23:41 +01:00
|
|
|
import static org.telegram.abilitybots.api.objects.Privacy.*;
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
public class AbilityBotTest {
|
2018-05-02 11:06:39 +02:00
|
|
|
// Messages
|
2018-05-08 11:10:37 +02:00
|
|
|
private static final String RECOVERY_MESSAGE = "I am ready to receive the backup file. Please reply to this message with the backup file attached.";
|
|
|
|
private static final String RECOVER_SUCCESS = "I have successfully recovered.";
|
2018-05-02 11:06:39 +02:00
|
|
|
|
2017-07-20 22:24:23 +02:00
|
|
|
private static final String[] EMPTY_ARRAY = {};
|
|
|
|
private static final long GROUP_ID = 10L;
|
|
|
|
private static final String TEST = "test";
|
|
|
|
private static final String[] TEXT = {TEST};
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
private DefaultBot bot;
|
2019-09-30 08:00:16 +02:00
|
|
|
private DefaultAbilities defaultAbs;
|
2017-06-30 18:29:10 +02:00
|
|
|
private DBContext db;
|
|
|
|
private MessageSender sender;
|
2017-10-29 03:15:53 +01:00
|
|
|
private SilentSender silent;
|
2017-06-30 18:29:10 +02:00
|
|
|
|
2019-07-08 21:22:51 +02:00
|
|
|
@BeforeEach
|
|
|
|
void setUp() {
|
2017-06-30 18:29:10 +02:00
|
|
|
db = offlineInstance("db");
|
|
|
|
bot = new DefaultBot(EMPTY, EMPTY, db);
|
2020-10-05 16:59:39 +02:00
|
|
|
bot.onRegister();
|
2019-09-30 08:00:16 +02:00
|
|
|
defaultAbs = new DefaultAbilities(bot);
|
2017-10-29 03:15:53 +01:00
|
|
|
|
2017-06-30 18:29:10 +02:00
|
|
|
sender = mock(MessageSender.class);
|
2017-10-29 03:15:53 +01:00
|
|
|
silent = mock(SilentSender.class);
|
|
|
|
|
|
|
|
bot.sender = sender;
|
|
|
|
bot.silent = silent;
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
2019-07-08 21:22:51 +02:00
|
|
|
@AfterEach
|
|
|
|
void tearDown() throws IOException {
|
|
|
|
db.clear();
|
|
|
|
db.close();
|
|
|
|
}
|
|
|
|
|
2017-06-30 18:29:10 +02:00
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void sendsPrivacyViolation() {
|
2019-09-26 08:25:11 +02:00
|
|
|
Update update = mockFullUpdate(bot, USER, "/admin");
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
bot.onUpdateReceived(update);
|
|
|
|
|
2018-05-21 13:01:45 +02:00
|
|
|
verify(silent, times(1)).send("Sorry, you don't have the required access level to do that.", USER.getId());
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void sendsLocalityViolation() {
|
2019-09-26 08:25:11 +02:00
|
|
|
Update update = mockFullUpdate(bot, USER, "/group");
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
bot.onUpdateReceived(update);
|
|
|
|
|
2018-05-21 13:01:45 +02:00
|
|
|
verify(silent, times(1)).send(format("Sorry, %s-only feature.", "group"), USER.getId());
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void sendsInputArgsViolation() {
|
2019-09-26 08:25:11 +02:00
|
|
|
Update update = mockFullUpdate(bot, USER, "/count 1 2 3");
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
bot.onUpdateReceived(update);
|
|
|
|
|
2018-05-21 13:01:45 +02:00
|
|
|
verify(silent, times(1)).send(format("Sorry, this feature requires %d additional inputs.", 4), USER.getId());
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canProcessRepliesIfSatisfyRequirements() {
|
2019-09-26 08:25:11 +02:00
|
|
|
Update update = mockFullUpdate(bot, USER, "must reply");
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
// False means the update was not pushed down the stream since it has been consumed by the reply
|
|
|
|
assertFalse(bot.filterReply(update));
|
2018-05-21 13:01:45 +02:00
|
|
|
verify(silent, times(1)).send("reply", USER.getId());
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
2020-04-26 22:22:05 +02:00
|
|
|
@Test
|
|
|
|
void canProcessUpdatesWithoutUserInfo() {
|
|
|
|
Update update = mock(Update.class);
|
|
|
|
// At the moment, only poll updates carry no user information
|
|
|
|
when(update.hasPoll()).thenReturn(true);
|
|
|
|
|
|
|
|
bot.onUpdateReceived(update);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void getUserHasAllMethodsDefined() {
|
|
|
|
Arrays.stream(Update.class.getMethods())
|
|
|
|
// filter to all these methods of hasXXX (hasPoll, hasMessage, etc...)
|
|
|
|
.filter(method -> method.getName().startsWith("has"))
|
|
|
|
// Gotta filter out hashCode
|
|
|
|
.filter(method -> method.getReturnType().getName().equals("boolean"))
|
|
|
|
.forEach(method -> {
|
|
|
|
Update update = mock(Update.class);
|
|
|
|
try {
|
|
|
|
// Mock the method and make sure it returns true so that it gets processed by the following method
|
|
|
|
when(method.invoke(update)).thenReturn(true);
|
|
|
|
// Call the getUser function, throws an IllegalStateException if there's an update that can't be processed
|
|
|
|
AbilityUtils.getUser(update);
|
|
|
|
} catch (IllegalStateException e) {
|
|
|
|
throw new RuntimeException(
|
|
|
|
format("Found an update variation that is not handled by the getUser util method [%s]", method.getName()), e);
|
|
|
|
} catch (NullPointerException | ReflectiveOperationException e) {
|
|
|
|
// This is fine, the mock isn't complete and we're only
|
|
|
|
// looking for IllegalStateExceptions thrown by the method
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-09 23:02:36 +02:00
|
|
|
@Test
|
|
|
|
void getChatIdCanHandleAllKindsOfUpdates() {
|
|
|
|
handlesAllUpdates(AbilityUtils::getUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void getUserCanHandleAllKindsOfUpdates() {
|
|
|
|
handlesAllUpdates(AbilityUtils::getChatId);
|
|
|
|
}
|
|
|
|
|
2017-06-30 18:29:10 +02:00
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canBackupDB() throws TelegramApiException {
|
2017-06-30 18:29:10 +02:00
|
|
|
MessageContext context = defaultContext();
|
|
|
|
|
2019-09-30 08:00:16 +02:00
|
|
|
defaultAbs.backupDB().action().accept(context);
|
2017-10-29 03:15:53 +01:00
|
|
|
deleteQuietly(new java.io.File("backup.json"));
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
verify(sender, times(1)).sendDocument(any());
|
|
|
|
}
|
|
|
|
|
2020-03-01 07:50:52 +01:00
|
|
|
@Test
|
|
|
|
void canReportStatistics() {
|
|
|
|
MessageContext context = defaultContext();
|
|
|
|
|
|
|
|
defaultAbs.reportStats().action().accept(context);
|
|
|
|
|
|
|
|
verify(silent, times(1)).send("count: 0\nmustreply: 0", GROUP_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void canReportUpdatedStatistics() {
|
|
|
|
Update upd1 = mockFullUpdate(bot, CREATOR, "/count 1 2 3 4");
|
|
|
|
bot.onUpdateReceived(upd1);
|
|
|
|
Update upd2 = mockFullUpdate(bot, CREATOR, "must reply");
|
|
|
|
bot.onUpdateReceived(upd2);
|
|
|
|
|
|
|
|
Mockito.reset(silent);
|
|
|
|
|
|
|
|
Update statUpd = mockFullUpdate(bot, CREATOR, "/stats");
|
|
|
|
bot.onUpdateReceived(statUpd);
|
|
|
|
|
|
|
|
verify(silent, times(1)).send("count: 1\nmustreply: 1", CREATOR.getId());
|
|
|
|
}
|
|
|
|
|
2017-06-30 18:29:10 +02:00
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canRecoverDB() throws TelegramApiException, IOException {
|
2017-06-30 18:29:10 +02:00
|
|
|
Update update = mockBackupUpdate();
|
|
|
|
Object backup = getDbBackup();
|
|
|
|
java.io.File backupFile = createBackupFile(backup);
|
|
|
|
|
2019-07-09 08:18:08 +02:00
|
|
|
// Support for null parameter matching since due to mocking API changes
|
|
|
|
when(sender.downloadFile(ArgumentMatchers.<File>isNull())).thenReturn(backupFile);
|
|
|
|
|
2020-11-24 17:02:44 +01:00
|
|
|
defaultAbs.recoverDB().replies().get(0).actOn(bot, update);
|
2017-06-30 18:29:10 +02:00
|
|
|
|
2017-10-29 03:15:53 +01:00
|
|
|
verify(silent, times(1)).send(RECOVER_SUCCESS, GROUP_ID);
|
2019-07-08 21:22:51 +02:00
|
|
|
assertEquals(db.getSet(TEST), newHashSet(TEST), "Bot recovered but the DB is still not in sync");
|
|
|
|
assertTrue(backupFile.delete(), "Could not delete backup file");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canFilterOutReplies() {
|
2017-06-30 18:29:10 +02:00
|
|
|
Update update = mock(Update.class);
|
|
|
|
when(update.hasMessage()).thenReturn(false);
|
|
|
|
|
|
|
|
assertTrue(bot.filterReply(update));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canDemote() {
|
2018-05-21 13:01:45 +02:00
|
|
|
addUsers(USER);
|
|
|
|
bot.admins().add(USER.getId());
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
MessageContext context = defaultContext();
|
|
|
|
|
2019-09-30 08:00:16 +02:00
|
|
|
defaultAbs.demoteAdmin().action().accept(context);
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
Set<Integer> actual = bot.admins();
|
|
|
|
Set<Integer> expected = emptySet();
|
2019-07-08 21:22:51 +02:00
|
|
|
assertEquals(expected, actual, "Could not sudont super-admin");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canPromote() {
|
2018-05-21 13:01:45 +02:00
|
|
|
addUsers(USER);
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
MessageContext context = defaultContext();
|
|
|
|
|
2019-09-30 08:00:16 +02:00
|
|
|
defaultAbs.promoteAdmin().action().accept(context);
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
Set<Integer> actual = bot.admins();
|
2018-05-21 13:01:45 +02:00
|
|
|
Set<Integer> expected = newHashSet(USER.getId());
|
2019-07-08 21:22:51 +02:00
|
|
|
assertEquals(expected, actual, "Could not sudo user");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canBanUser() {
|
2018-05-21 13:01:45 +02:00
|
|
|
addUsers(USER);
|
2017-06-30 18:29:10 +02:00
|
|
|
MessageContext context = defaultContext();
|
|
|
|
|
2019-09-30 08:00:16 +02:00
|
|
|
defaultAbs.banUser().action().accept(context);
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
Set<Integer> actual = bot.blacklist();
|
2018-05-21 13:01:45 +02:00
|
|
|
Set<Integer> expected = newHashSet(USER.getId());
|
2019-07-08 21:22:51 +02:00
|
|
|
assertEquals(expected, actual, "The ban was not emplaced");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canUnbanUser() {
|
2018-05-21 13:01:45 +02:00
|
|
|
addUsers(USER);
|
|
|
|
bot.blacklist().add(USER.getId());
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
MessageContext context = defaultContext();
|
|
|
|
|
2019-09-30 08:00:16 +02:00
|
|
|
defaultAbs.unbanUser().action().accept(context);
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
Set<Integer> actual = bot.blacklist();
|
|
|
|
Set<Integer> expected = newHashSet();
|
2019-07-08 21:22:51 +02:00
|
|
|
assertEquals(expected, actual, "The ban was not lifted");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@NotNull
|
|
|
|
private MessageContext defaultContext() {
|
2018-05-24 00:59:03 +02:00
|
|
|
return mockContext(CREATOR, GROUP_ID, USER.getUserName());
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void cannotBanCreator() {
|
2018-05-21 13:01:45 +02:00
|
|
|
addUsers(USER, CREATOR);
|
|
|
|
MessageContext context = mockContext(USER, GROUP_ID, CREATOR.getUserName());
|
2017-06-30 18:29:10 +02:00
|
|
|
|
2019-09-30 08:00:16 +02:00
|
|
|
defaultAbs.banUser().action().accept(context);
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
Set<Integer> actual = bot.blacklist();
|
2018-05-21 13:01:45 +02:00
|
|
|
Set<Integer> expected = newHashSet(USER.getId());
|
2019-07-08 21:22:51 +02:00
|
|
|
assertEquals(expected, actual, "Impostor was not added to the blacklist");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
2018-05-21 13:01:45 +02:00
|
|
|
private void addUsers(User... users) {
|
2017-06-30 18:29:10 +02:00
|
|
|
Arrays.stream(users).forEach(user -> {
|
2018-05-21 13:01:45 +02:00
|
|
|
bot.users().put(user.getId(), user);
|
|
|
|
bot.userIds().put(user.getUserName().toLowerCase(), user.getId());
|
2017-06-30 18:29:10 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void creatorCanClaimBot() {
|
2018-05-21 13:01:45 +02:00
|
|
|
MessageContext context = mockContext(CREATOR, GROUP_ID);
|
2017-06-30 18:29:10 +02:00
|
|
|
|
2019-09-30 08:00:16 +02:00
|
|
|
defaultAbs.claimCreator().action().accept(context);
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
Set<Integer> actual = bot.admins();
|
2018-05-21 13:01:45 +02:00
|
|
|
Set<Integer> expected = newHashSet(CREATOR.getId());
|
2019-07-08 21:22:51 +02:00
|
|
|
assertEquals(expected, actual, "Creator was not properly added to the super admins set");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void bannedCreatorPassesBlacklistCheck() {
|
2018-05-21 13:01:45 +02:00
|
|
|
bot.blacklist().add(CREATOR.getId());
|
2017-06-30 18:29:10 +02:00
|
|
|
Update update = mock(Update.class);
|
|
|
|
Message message = mock(Message.class);
|
|
|
|
User user = mock(User.class);
|
|
|
|
|
|
|
|
mockUser(update, message, user);
|
|
|
|
|
|
|
|
boolean notBanned = bot.checkBlacklist(update);
|
2019-07-08 21:22:51 +02:00
|
|
|
assertTrue(notBanned, "Creator is banned");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canAddUser() {
|
2017-06-30 18:29:10 +02:00
|
|
|
Update update = mock(Update.class);
|
|
|
|
Message message = mock(Message.class);
|
|
|
|
|
2018-05-21 13:01:45 +02:00
|
|
|
mockAlternateUser(update, message, USER);
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
bot.addUser(update);
|
|
|
|
|
2018-05-21 13:01:45 +02:00
|
|
|
Map<String, Integer> expectedUserIds = ImmutableMap.of(USER.getUserName(), USER.getId());
|
|
|
|
Map<Integer, User> expectedUsers = ImmutableMap.of(USER.getId(), USER);
|
2019-07-08 21:22:51 +02:00
|
|
|
assertEquals(expectedUserIds, bot.userIds(), "User was not added");
|
|
|
|
assertEquals(expectedUsers, bot.users(), "User was not added");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canEditUser() {
|
2018-05-21 13:01:45 +02:00
|
|
|
addUsers(USER);
|
2017-06-30 18:29:10 +02:00
|
|
|
Update update = mock(Update.class);
|
|
|
|
Message message = mock(Message.class);
|
|
|
|
|
2018-05-21 13:01:45 +02:00
|
|
|
String newUsername = USER.getUserName() + "-test";
|
|
|
|
String newFirstName = USER.getFirstName() + "-test";
|
|
|
|
String newLastName = USER.getLastName() + "-test";
|
|
|
|
int sameId = USER.getId();
|
2020-10-31 18:43:32 +01:00
|
|
|
User changedUser = new User(sameId, newFirstName, false, newLastName, newUsername, "en", false, false, false);
|
2017-06-30 18:29:10 +02:00
|
|
|
|
2018-05-21 13:01:45 +02:00
|
|
|
mockAlternateUser(update, message, changedUser);
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
bot.addUser(update);
|
|
|
|
|
2018-05-21 13:01:45 +02:00
|
|
|
Map<String, Integer> expectedUserIds = ImmutableMap.of(changedUser.getUserName(), changedUser.getId());
|
|
|
|
Map<Integer, User> expectedUsers = ImmutableMap.of(changedUser.getId(), changedUser);
|
2019-07-08 21:22:51 +02:00
|
|
|
assertEquals(bot.userIds(), expectedUserIds, "User was not properly edited");
|
|
|
|
assertEquals(expectedUsers, expectedUsers, "User was not properly edited");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canValidateAbility() {
|
2017-06-30 18:29:10 +02:00
|
|
|
Trio<Update, Ability, String[]> invalidPair = Trio.of(null, null, null);
|
|
|
|
Ability validAbility = getDefaultBuilder().build();
|
|
|
|
Trio<Update, Ability, String[]> validPair = Trio.of(null, validAbility, null);
|
|
|
|
|
2019-07-08 21:22:51 +02:00
|
|
|
assertFalse(bot.validateAbility(invalidPair), "Bot can't validate ability properly");
|
|
|
|
assertTrue(bot.validateAbility(validPair), "Bot can't validate ability properly");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canCheckInput() {
|
2019-09-26 08:25:11 +02:00
|
|
|
Update update = mockFullUpdate(bot, USER, "/something");
|
2017-06-30 18:29:10 +02:00
|
|
|
Ability abilityWithOneInput = getDefaultBuilder()
|
|
|
|
.build();
|
|
|
|
Ability abilityWithZeroInput = getDefaultBuilder()
|
|
|
|
.input(0)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
Trio<Update, Ability, String[]> trioOneArg = Trio.of(update, abilityWithOneInput, TEXT);
|
|
|
|
Trio<Update, Ability, String[]> trioZeroArg = Trio.of(update, abilityWithZeroInput, TEXT);
|
|
|
|
|
2019-07-08 21:22:51 +02:00
|
|
|
assertTrue(bot.checkInput(trioOneArg), "Unexpected result when applying token filter");
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
trioOneArg = Trio.of(update, abilityWithOneInput, addAll(TEXT, TEXT));
|
2019-07-08 21:22:51 +02:00
|
|
|
assertFalse(bot.checkInput(trioOneArg), "Unexpected result when applying token filter");
|
2017-06-30 18:29:10 +02:00
|
|
|
|
2019-07-08 21:22:51 +02:00
|
|
|
assertTrue(bot.checkInput(trioZeroArg), "Unexpected result when applying token filter");
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
trioZeroArg = Trio.of(update, abilityWithZeroInput, EMPTY_ARRAY);
|
2019-07-08 21:22:51 +02:00
|
|
|
assertTrue(bot.checkInput(trioZeroArg), "Unexpected result when applying token filter");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canCheckPrivacy() {
|
2017-06-30 18:29:10 +02:00
|
|
|
Update update = mock(Update.class);
|
|
|
|
Message message = mock(Message.class);
|
2018-07-08 01:41:21 +02:00
|
|
|
User user = mock(User.class);
|
2017-06-30 18:29:10 +02:00
|
|
|
Ability publicAbility = getDefaultBuilder().privacy(PUBLIC).build();
|
2018-02-04 09:23:41 +01:00
|
|
|
Ability groupAdminAbility = getDefaultBuilder().privacy(GROUP_ADMIN).build();
|
2017-06-30 18:29:10 +02:00
|
|
|
Ability adminAbility = getDefaultBuilder().privacy(ADMIN).build();
|
|
|
|
Ability creatorAbility = getDefaultBuilder().privacy(Privacy.CREATOR).build();
|
|
|
|
|
|
|
|
Trio<Update, Ability, String[]> publicTrio = Trio.of(update, publicAbility, TEXT);
|
2018-02-04 09:23:41 +01:00
|
|
|
Trio<Update, Ability, String[]> groupAdminTrio = Trio.of(update, groupAdminAbility, TEXT);
|
2017-06-30 18:29:10 +02:00
|
|
|
Trio<Update, Ability, String[]> adminTrio = Trio.of(update, adminAbility, TEXT);
|
|
|
|
Trio<Update, Ability, String[]> creatorTrio = Trio.of(update, creatorAbility, TEXT);
|
|
|
|
|
|
|
|
mockUser(update, message, user);
|
|
|
|
|
2019-07-08 21:22:51 +02:00
|
|
|
assertTrue(bot.checkPrivacy(publicTrio), "Unexpected result when checking for privacy");
|
|
|
|
assertFalse(bot.checkPrivacy(groupAdminTrio), "Unexpected result when checking for privacy");
|
|
|
|
assertFalse(bot.checkPrivacy(adminTrio), "Unexpected result when checking for privacy");
|
|
|
|
assertFalse(bot.checkPrivacy(creatorTrio), "Unexpected result when checking for privacy");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
2018-02-04 09:23:41 +01:00
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canValidateGroupAdminPrivacy() {
|
2018-02-04 09:23:41 +01:00
|
|
|
Update update = mock(Update.class);
|
|
|
|
Message message = mock(Message.class);
|
2018-07-08 01:41:21 +02:00
|
|
|
User user = mock(User.class);
|
2018-02-04 09:23:41 +01:00
|
|
|
Ability groupAdminAbility = getDefaultBuilder().privacy(GROUP_ADMIN).build();
|
|
|
|
|
|
|
|
Trio<Update, Ability, String[]> groupAdminTrio = Trio.of(update, groupAdminAbility, TEXT);
|
|
|
|
|
|
|
|
mockUser(update, message, user);
|
|
|
|
when(message.isGroupMessage()).thenReturn(true);
|
|
|
|
|
|
|
|
ChatMember member = mock(ChatMember.class);
|
|
|
|
when(member.getUser()).thenReturn(user);
|
|
|
|
when(member.getUser()).thenReturn(user);
|
|
|
|
|
|
|
|
when(silent.execute(any(GetChatAdministrators.class))).thenReturn(Optional.of(newArrayList(member)));
|
|
|
|
|
2019-07-08 21:22:51 +02:00
|
|
|
assertTrue(bot.checkPrivacy(groupAdminTrio), "Unexpected result when checking for privacy");
|
2018-02-04 09:23:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canRestrictNormalUsersFromGroupAdminAbilities() {
|
2018-02-04 09:23:41 +01:00
|
|
|
Update update = mock(Update.class);
|
|
|
|
Message message = mock(Message.class);
|
2018-07-08 01:41:21 +02:00
|
|
|
User user = mock(User.class);
|
2018-02-04 09:23:41 +01:00
|
|
|
Ability groupAdminAbility = getDefaultBuilder().privacy(GROUP_ADMIN).build();
|
|
|
|
|
|
|
|
Trio<Update, Ability, String[]> groupAdminTrio = Trio.of(update, groupAdminAbility, TEXT);
|
|
|
|
|
|
|
|
mockUser(update, message, user);
|
|
|
|
when(message.isGroupMessage()).thenReturn(true);
|
|
|
|
|
|
|
|
when(silent.execute(any(GetChatAdministrators.class))).thenReturn(empty());
|
|
|
|
|
2019-07-08 21:22:51 +02:00
|
|
|
assertFalse(bot.checkPrivacy(groupAdminTrio), "Unexpected result when checking for privacy");
|
2018-02-04 09:23:41 +01:00
|
|
|
}
|
|
|
|
|
2017-06-30 18:29:10 +02:00
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canBlockAdminsFromCreatorAbilities() {
|
2017-06-30 18:29:10 +02:00
|
|
|
Update update = mock(Update.class);
|
|
|
|
Message message = mock(Message.class);
|
2018-07-08 01:41:21 +02:00
|
|
|
User user = mock(User.class);
|
2017-06-30 18:29:10 +02:00
|
|
|
Ability creatorAbility = getDefaultBuilder().privacy(Privacy.CREATOR).build();
|
|
|
|
|
|
|
|
Trio<Update, Ability, String[]> creatorTrio = Trio.of(update, creatorAbility, TEXT);
|
|
|
|
|
2018-05-21 13:01:45 +02:00
|
|
|
bot.admins().add(USER.getId());
|
2017-06-30 18:29:10 +02:00
|
|
|
mockUser(update, message, user);
|
|
|
|
|
2019-07-08 21:22:51 +02:00
|
|
|
assertFalse(bot.checkPrivacy(creatorTrio), "Unexpected result when checking for privacy");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canCheckLocality() {
|
2017-06-30 18:29:10 +02:00
|
|
|
Update update = mock(Update.class);
|
|
|
|
Message message = mock(Message.class);
|
|
|
|
User user = mock(User.class);
|
|
|
|
Ability allAbility = getDefaultBuilder().locality(ALL).build();
|
|
|
|
Ability userAbility = getDefaultBuilder().locality(Locality.USER).build();
|
|
|
|
Ability groupAbility = getDefaultBuilder().locality(GROUP).build();
|
|
|
|
|
|
|
|
Trio<Update, Ability, String[]> publicTrio = Trio.of(update, allAbility, TEXT);
|
|
|
|
Trio<Update, Ability, String[]> userTrio = Trio.of(update, userAbility, TEXT);
|
|
|
|
Trio<Update, Ability, String[]> groupTrio = Trio.of(update, groupAbility, TEXT);
|
|
|
|
|
|
|
|
mockUser(update, message, user);
|
|
|
|
when(message.isUserMessage()).thenReturn(true);
|
|
|
|
|
2019-07-08 21:22:51 +02:00
|
|
|
assertTrue(bot.checkLocality(publicTrio), "Unexpected result when checking for locality");
|
|
|
|
assertTrue(bot.checkLocality(userTrio), "Unexpected result when checking for locality");
|
|
|
|
assertFalse(bot.checkLocality(groupTrio), "Unexpected result when checking for locality");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canRetrieveContext() {
|
2017-06-30 18:29:10 +02:00
|
|
|
Update update = mock(Update.class);
|
|
|
|
Message message = mock(Message.class);
|
|
|
|
Ability ability = getDefaultBuilder().build();
|
|
|
|
Trio<Update, Ability, String[]> trio = Trio.of(update, ability, TEXT);
|
|
|
|
|
|
|
|
when(message.getChatId()).thenReturn(GROUP_ID);
|
2018-05-21 13:01:45 +02:00
|
|
|
mockUser(update, message, USER);
|
2017-06-30 18:29:10 +02:00
|
|
|
|
|
|
|
Pair<MessageContext, Ability> actualPair = bot.getContext(trio);
|
2020-10-02 21:57:57 +02:00
|
|
|
Pair<MessageContext, Ability> expectedPair = Pair.of(newContext(update, USER, GROUP_ID, bot, TEXT), ability);
|
2017-06-30 18:29:10 +02:00
|
|
|
|
2019-07-08 21:22:51 +02:00
|
|
|
assertEquals(expectedPair, actualPair, "Unexpected result when fetching for context");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void defaultGlobalFlagIsTrue() {
|
2017-06-30 18:29:10 +02:00
|
|
|
Update update = mock(Update.class);
|
2019-07-08 21:22:51 +02:00
|
|
|
assertTrue(bot.checkGlobalFlags(update), "Unexpected result when checking for the default global flags");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 18:43:32 +01:00
|
|
|
@SuppressWarnings({"NumericOverflow", "divzero"})
|
2019-07-08 21:22:51 +02:00
|
|
|
@Test
|
|
|
|
void canConsumeUpdate() {
|
2017-06-30 18:29:10 +02:00
|
|
|
Ability ability = getDefaultBuilder()
|
|
|
|
.action((context) -> {
|
|
|
|
int x = 1 / 0;
|
|
|
|
}).build();
|
|
|
|
MessageContext context = mock(MessageContext.class);
|
|
|
|
|
|
|
|
Pair<MessageContext, Ability> pair = Pair.of(context, ability);
|
|
|
|
|
2019-07-08 21:37:04 +02:00
|
|
|
Assertions.assertThrows(ArithmeticException.class, () -> bot.consumeUpdate(pair));
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canFetchAbility() {
|
2017-06-30 18:29:10 +02:00
|
|
|
Update update = mock(Update.class);
|
|
|
|
Message message = mock(Message.class);
|
|
|
|
|
|
|
|
String text = "/test";
|
|
|
|
when(update.hasMessage()).thenReturn(true);
|
|
|
|
when(update.getMessage()).thenReturn(message);
|
|
|
|
when(update.getMessage().hasText()).thenReturn(true);
|
|
|
|
when(message.getText()).thenReturn(text);
|
|
|
|
|
|
|
|
Trio<Update, Ability, String[]> trio = bot.getAbility(update);
|
|
|
|
|
|
|
|
Ability expected = bot.testAbility();
|
|
|
|
Ability actual = trio.b();
|
|
|
|
|
2019-07-08 21:22:51 +02:00
|
|
|
assertEquals(expected, actual, "Wrong ability was fetched");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
2018-02-04 09:23:41 +01:00
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canFetchAbilityCaseInsensitive() {
|
2018-02-04 09:23:41 +01:00
|
|
|
Update update = mock(Update.class);
|
|
|
|
Message message = mock(Message.class);
|
|
|
|
|
|
|
|
String text = "/tESt";
|
|
|
|
when(update.hasMessage()).thenReturn(true);
|
|
|
|
when(update.getMessage()).thenReturn(message);
|
|
|
|
when(update.getMessage().hasText()).thenReturn(true);
|
|
|
|
when(message.getText()).thenReturn(text);
|
|
|
|
|
|
|
|
Trio<Update, Ability, String[]> trio = bot.getAbility(update);
|
|
|
|
|
|
|
|
Ability expected = bot.testAbility();
|
|
|
|
Ability actual = trio.b();
|
|
|
|
|
2019-07-08 21:22:51 +02:00
|
|
|
assertEquals(expected, actual, "Wrong ability was fetched");
|
2018-02-04 09:23:41 +01:00
|
|
|
}
|
|
|
|
|
2017-06-30 18:29:10 +02:00
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canFetchDefaultAbility() {
|
2017-06-30 18:29:10 +02:00
|
|
|
Update update = mock(Update.class);
|
|
|
|
Message message = mock(Message.class);
|
|
|
|
|
|
|
|
String text = "test tags";
|
|
|
|
when(update.getMessage()).thenReturn(message);
|
|
|
|
when(message.getText()).thenReturn(text);
|
|
|
|
|
|
|
|
Trio<Update, Ability, String[]> trio = bot.getAbility(update);
|
|
|
|
|
|
|
|
Ability expected = bot.defaultAbility();
|
|
|
|
Ability actual = trio.b();
|
|
|
|
|
2019-07-08 21:22:51 +02:00
|
|
|
assertEquals(expected, actual, "Wrong ability was fetched");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canCheckAbilityFlags() {
|
2017-06-30 18:29:10 +02:00
|
|
|
Update update = mock(Update.class);
|
|
|
|
Message message = mock(Message.class);
|
|
|
|
|
|
|
|
when(update.hasMessage()).thenReturn(true);
|
|
|
|
when(update.getMessage()).thenReturn(message);
|
|
|
|
when(message.hasDocument()).thenReturn(false);
|
|
|
|
when(message.hasText()).thenReturn(true);
|
|
|
|
|
|
|
|
Ability documentAbility = getDefaultBuilder().flag(DOCUMENT, MESSAGE).build();
|
|
|
|
Ability textAbility = getDefaultBuilder().flag(Flag.TEXT, MESSAGE).build();
|
|
|
|
|
|
|
|
Trio<Update, Ability, String[]> docTrio = Trio.of(update, documentAbility, TEXT);
|
|
|
|
Trio<Update, Ability, String[]> textTrio = Trio.of(update, textAbility, TEXT);
|
|
|
|
|
2019-07-08 21:22:51 +02:00
|
|
|
assertFalse(bot.checkMessageFlags(docTrio), "Unexpected result when checking for message flags");
|
|
|
|
assertTrue(bot.checkMessageFlags(textTrio), "Unexpected result when checking for message flags");
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canReportCommands() {
|
2018-05-21 13:01:45 +02:00
|
|
|
MessageContext context = mockContext(USER, GROUP_ID);
|
2018-05-18 10:40:27 +02:00
|
|
|
|
2019-09-30 08:00:16 +02:00
|
|
|
defaultAbs.reportCommands().action().accept(context);
|
2018-05-18 10:40:27 +02:00
|
|
|
|
|
|
|
verify(silent, times(1)).send("default - dis iz default command", GROUP_ID);
|
|
|
|
}
|
|
|
|
|
2018-05-24 00:59:03 +02:00
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void canPrintCommandsBasedOnPrivacy() {
|
2018-05-24 00:59:03 +02:00
|
|
|
Update update = mock(Update.class);
|
|
|
|
Message message = mock(Message.class);
|
|
|
|
|
|
|
|
when(update.hasMessage()).thenReturn(true);
|
|
|
|
when(update.getMessage()).thenReturn(message);
|
|
|
|
when(message.hasText()).thenReturn(true);
|
2020-10-02 21:57:57 +02:00
|
|
|
MessageContext creatorCtx = newContext(update, CREATOR, GROUP_ID, bot);
|
2018-05-24 00:59:03 +02:00
|
|
|
|
2019-09-30 08:00:16 +02:00
|
|
|
defaultAbs.commands().action().accept(creatorCtx);
|
2018-05-24 00:59:03 +02:00
|
|
|
|
2020-03-01 07:50:52 +01:00
|
|
|
String expected = "PUBLIC\n/commands\n/count\n/default - dis iz default command\n/group\n/test\nADMIN\n/admin\n/ban\n/demote\n/promote\n/stats\n/unban\nCREATOR\n/backup\n/claim\n/recover\n/report";
|
2018-05-24 00:59:03 +02:00
|
|
|
verify(silent, times(1)).send(expected, GROUP_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-08 21:22:51 +02:00
|
|
|
void printsOnlyPublicCommandsForNormalUser() {
|
2018-05-24 00:59:03 +02:00
|
|
|
Update update = mock(Update.class);
|
|
|
|
Message message = mock(Message.class);
|
|
|
|
|
|
|
|
when(update.hasMessage()).thenReturn(true);
|
|
|
|
when(update.getMessage()).thenReturn(message);
|
|
|
|
when(message.hasText()).thenReturn(true);
|
|
|
|
|
2020-10-02 21:57:57 +02:00
|
|
|
MessageContext userCtx = newContext(update, USER, GROUP_ID, bot);
|
2018-05-24 00:59:03 +02:00
|
|
|
|
2019-09-30 08:00:16 +02:00
|
|
|
defaultAbs.commands().action().accept(userCtx);
|
2018-05-24 00:59:03 +02:00
|
|
|
|
|
|
|
String expected = "PUBLIC\n/commands\n/count\n/default - dis iz default command\n/group\n/test";
|
|
|
|
verify(silent, times(1)).send(expected, GROUP_ID);
|
2017-06-30 18:29:10 +02:00
|
|
|
}
|
|
|
|
|
2020-07-18 09:43:28 +02:00
|
|
|
@Test
|
|
|
|
void canProcessChannelPosts() {
|
|
|
|
Update update = mock(Update.class);
|
|
|
|
Message message = mock(Message.class);
|
|
|
|
when(message.getChatId()).thenReturn(1L);
|
|
|
|
|
|
|
|
when(update.getChannelPost()).thenReturn(message);
|
|
|
|
when(update.hasChannelPost()).thenReturn(true);
|
|
|
|
|
|
|
|
bot.onUpdateReceived(update);
|
|
|
|
|
|
|
|
String expected = "test channel post";
|
|
|
|
verify(silent, times(1)).send(expected, 1);
|
|
|
|
}
|
|
|
|
|
2021-01-25 17:57:48 +01:00
|
|
|
@Test
|
|
|
|
void canProcessRepliesRegisteredInCollection() {
|
|
|
|
Update firstUpdate = mock(Update.class);
|
|
|
|
Message firstMessage = mock(Message.class);
|
|
|
|
when(firstMessage.getText()).thenReturn(FIRST_REPLY_KEY_MESSAGE);
|
|
|
|
when(firstMessage.getChatId()).thenReturn(1L);
|
|
|
|
|
|
|
|
Update secondUpdate = mock(Update.class);
|
|
|
|
Message secondMessage = mock(Message.class);
|
|
|
|
when(secondMessage.getText()).thenReturn(SECOND_REPLY_KEY_MESSAGE);
|
|
|
|
when(secondMessage.getChatId()).thenReturn(1L);
|
|
|
|
|
|
|
|
mockUser(firstUpdate, firstMessage, USER);
|
|
|
|
mockUser(secondUpdate, secondMessage, USER);
|
|
|
|
|
|
|
|
|
|
|
|
bot.onUpdateReceived(firstUpdate);
|
|
|
|
bot.onUpdateReceived(secondUpdate);
|
|
|
|
|
|
|
|
verify(silent, times(2)).send(anyString(), anyLong());
|
|
|
|
verify(silent, times(1)).send("first reply answer", 1);
|
|
|
|
verify(silent, times(1)).send("second reply answer", 1);
|
|
|
|
}
|
|
|
|
|
2020-05-09 23:02:36 +02:00
|
|
|
private void handlesAllUpdates(Consumer<Update> utilMethod) {
|
|
|
|
Arrays.stream(Update.class.getMethods())
|
|
|
|
// filter to all these methods of hasXXX (hasPoll, hasMessage, etc...)
|
|
|
|
.filter(method -> method.getName().startsWith("has"))
|
|
|
|
// Gotta filter out hashCode
|
|
|
|
.filter(method -> method.getReturnType().getName().equals("boolean"))
|
|
|
|
.forEach(method -> {
|
|
|
|
Update update = mock(Update.class);
|
|
|
|
try {
|
|
|
|
// Mock the method and make sure it returns true so that it gets processed by the following method
|
|
|
|
when(method.invoke(update)).thenReturn(true);
|
|
|
|
// Call the function, throws an IllegalStateException if there's an update that can't be processed
|
|
|
|
utilMethod.accept(update);
|
|
|
|
} catch (IllegalStateException e) {
|
|
|
|
throw new RuntimeException(
|
|
|
|
format("Found an update variation that is not handled by the getChatId util method [%s]", method.getName()), e);
|
|
|
|
} catch (NullPointerException | ReflectiveOperationException e) {
|
|
|
|
// This is fine, the mock isn't complete and we're only
|
|
|
|
// looking for IllegalStateExceptions thrown by the method
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-30 18:29:10 +02:00
|
|
|
private void mockUser(Update update, Message message, User user) {
|
|
|
|
when(update.hasMessage()).thenReturn(true);
|
|
|
|
when(update.getMessage()).thenReturn(message);
|
|
|
|
when(message.getFrom()).thenReturn(user);
|
|
|
|
}
|
|
|
|
|
2018-05-21 13:01:45 +02:00
|
|
|
private void mockAlternateUser(Update update, Message message, User user) {
|
2017-06-30 18:29:10 +02:00
|
|
|
when(message.getFrom()).thenReturn(user);
|
|
|
|
when(update.hasMessage()).thenReturn(true);
|
|
|
|
when(update.getMessage()).thenReturn(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Update mockBackupUpdate() {
|
|
|
|
Update update = mock(Update.class);
|
|
|
|
Message message = mock(Message.class);
|
|
|
|
Message botMessage = mock(Message.class);
|
|
|
|
Document document = mock(Document.class);
|
|
|
|
|
2020-11-01 23:46:36 +01:00
|
|
|
when(document.getFileId()).thenReturn("FAKEFILEID");
|
2018-05-21 13:01:45 +02:00
|
|
|
when(message.getFrom()).thenReturn(CREATOR);
|
2017-06-30 18:29:10 +02:00
|
|
|
when(update.getMessage()).thenReturn(message);
|
|
|
|
when(message.getDocument()).thenReturn(document);
|
|
|
|
when(botMessage.getText()).thenReturn(RECOVERY_MESSAGE);
|
|
|
|
when(message.isReply()).thenReturn(true);
|
2018-05-21 13:01:45 +02:00
|
|
|
when(update.hasMessage()).thenReturn(true);
|
2017-06-30 18:29:10 +02:00
|
|
|
when(message.hasDocument()).thenReturn(true);
|
|
|
|
when(message.getReplyToMessage()).thenReturn(botMessage);
|
|
|
|
when(message.getChatId()).thenReturn(GROUP_ID);
|
|
|
|
return update;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Object getDbBackup() {
|
|
|
|
db.getSet(TEST).add(TEST);
|
|
|
|
Object backup = db.backup();
|
|
|
|
db.clear();
|
|
|
|
return backup;
|
|
|
|
}
|
|
|
|
|
|
|
|
private java.io.File createBackupFile(Object backup) throws IOException {
|
|
|
|
java.io.File backupFile = new java.io.File(TEST);
|
|
|
|
BufferedWriter writer = Files.newWriter(backupFile, Charset.defaultCharset());
|
|
|
|
writer.write(backup.toString());
|
|
|
|
writer.flush();
|
|
|
|
writer.close();
|
|
|
|
return backupFile;
|
|
|
|
}
|
|
|
|
}
|