### Question:
TempbanManager implements SettingsDependent, HasCleanup { public void tempbanPlayer(final Player player) { if (isEnabled) { final String name = player.getName(); final String ip = PlayerUtils.getPlayerIp(player); final String reason = messages.retrieveSingle(player, MessageKey.TEMPBAN_MAX_LOGINS); final Date expires = new Date(); long newTime = expires.getTime() + (length * MILLIS_PER_MINUTE); expires.setTime(newTime); bukkitService.scheduleSyncDelayedTask(() -> { if(customCommand.isEmpty()) { bukkitService.banIp(ip, reason, expires, "AuthMe"); player.kickPlayer(reason); } else { String command = customCommand .replace("%player%", name) .replace("%ip%", ip); bukkitService.dispatchConsoleCommand(command); } }); ipLoginFailureCounts.remove(ip); } } @Inject  TempbanManager(BukkitService bukkitService, Messages messages, Settings settings); void increaseCount(String address, String name); void resetCount(String address, String name); boolean shouldTempban(String address); void tempbanPlayer(final Player player); @Override void reload(Settings settings); @Override void performCleanup();  }### Answer:
@Test public void shouldNotIssueBanIfDisabled() { Settings settings = mockSettings(0, 0, ""); given(settings.getProperty(SecuritySettings.TEMPBAN_ON_MAX_LOGINS)).willReturn(false); Player player = mock(Player.class); TempbanManager manager = new TempbanManager(bukkitService, messages, settings); manager.tempbanPlayer(player); verifyZeroInteractions(player, bukkitService); }
@Test public void shouldBanPlayerIp() { Player player = mock(Player.class); String ip = "123.45.67.89"; TestHelper.mockPlayerIp(player, ip); String banReason = "IP ban too many logins"; given(messages.retrieveSingle(player, MessageKey.TEMPBAN_MAX_LOGINS)).willReturn(banReason); Settings settings = mockSettings(2, 100, ""); TempbanManager manager = new TempbanManager(bukkitService, messages, settings); setBukkitServiceToScheduleSyncDelayedTask(bukkitService); manager.tempbanPlayer(player); verify(player).kickPlayer(banReason); ArgumentCaptor<Date> captor = ArgumentCaptor.forClass(Date.class); verify(bukkitService).banIp(eq(ip), eq(banReason), captor.capture(), eq("AuthMe")); Calendar cal = Calendar.getInstance(); cal.add(Calendar.MINUTE, 100); long expectedExpiration = cal.getTime().getTime(); assertThat(Math.abs(captor.getValue().getTime() - expectedExpiration), lessThan(DATE_TOLERANCE_MILLISECONDS)); }
@Test public void shouldBanPlayerIpCustom() { Player player = mock(Player.class); given(player.getName()).willReturn("Bob"); String ip = "143.45.77.89"; TestHelper.mockPlayerIp(player, ip); String banCommand = "banip %ip% 15d IP ban too many logins"; Settings settings = mockSettings(2, 100, banCommand); TempbanManager manager = new TempbanManager(bukkitService, messages, settings); setBukkitServiceToScheduleSyncDelayedTask(bukkitService); manager.tempbanPlayer(player); verify(bukkitService).dispatchConsoleCommand(banCommand.replace("%ip%", ip)); }