Skip to content

Commit

Permalink
Fix tests (due to new Bukkit/GeoIP updates)
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacqu committed Apr 27, 2024
1 parent 6eec2c8 commit e133a37
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private void processJoinSync(Player player, boolean isAuthAvailable) {
if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) {
// Allow infinite blindness effect
int blindTimeOut = (registrationTimeout <= 0) ? 99999 : registrationTimeout;
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, blindTimeOut, 2));
player.addPotionEffect(bukkitService.createBlindnessEffect(blindTimeOut));
}
commandManager.runCommandsOnJoin(player);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void applyLogoutEffect(Player player) {
// Apply Blindness effect
if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) {
int timeout = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND;
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, timeout, 2));
player.addPotionEffect(bukkitService.createBlindnessEffect(timeout));
}

// Set player's data to unauthenticated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private void performPostUnregisterActions(String name, Player player) {
private void applyBlindEffect(Player player) {
if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) {
int timeout = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND;
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, timeout, 2));
player.addPotionEffect(bukkitService.createBlindnessEffect(timeout));
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/fr/xephi/authme/service/BukkitService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;

Expand Down Expand Up @@ -338,6 +340,16 @@ public Optional<Boolean> isBungeeCordConfiguredForSpigot() {
}
}

/**
* Creates a PotionEffect with blindness for the given duration in ticks.
*
* @param timeoutInTicks duration of the effect in ticks
* @return blindness potion effect
*/
public PotionEffect createBlindnessEffect(int timeoutInTicks) {
return new PotionEffect(PotionEffectType.BLINDNESS, timeoutInTicks, 2);
}

/**
* @return the IP string that this server is bound to, otherwise empty string
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public void shouldPerformUnregister() {
given(service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)).willReturn(true);
given(service.getProperty(RestrictionSettings.TIMEOUT)).willReturn(21);
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
given(bukkitService.createBlindnessEffect(21 * 20)).willReturn(mock(PotionEffect.class));

// when
asynchronousUnregister.unregister(player, userPassword);
Expand All @@ -127,7 +128,6 @@ public void shouldPerformUnregister() {
verify(teleportationService).teleportOnJoin(player);
verifyCalledUnregisterEventFor(player);
verify(commandManager).runCommandsOnUnregister(player);
verify(player).addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 21 * 20, 2));
}

@Test
Expand Down
28 changes: 16 additions & 12 deletions src/test/java/fr/xephi/authme/service/GeoIpServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.google.common.collect.ImmutableMap;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.model.CountryResponse;
import com.maxmind.geoip2.record.Continent;
import com.maxmind.geoip2.record.Country;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.ProtectionSettings;
Expand Down Expand Up @@ -58,12 +63,7 @@ public void shouldGetCountry() throws Exception {
// given
InetAddress ip = InetAddress.getByName("123.45.67.89");
String countryCode = "XX";

Country country = mock(Country.class);
given(country.getIsoCode()).willReturn(countryCode);

CountryResponse response = mock(CountryResponse.class);
given(response.getCountry()).willReturn(country);
CountryResponse response = createCountryResponse(countryCode, "Unknown");
given(lookupService.country(ip)).willReturn(response);
given(settings.getProperty(ProtectionSettings.ENABLE_GEOIP)).willReturn(true);

Expand Down Expand Up @@ -93,12 +93,7 @@ public void shouldLookUpCountryName() throws Exception {
// given
InetAddress ip = InetAddress.getByName("24.45.167.89");
String countryName = "Ecuador";

Country country = mock(Country.class);
given(country.getName()).willReturn(countryName);

CountryResponse response = mock(CountryResponse.class);
given(response.getCountry()).willReturn(country);
CountryResponse response = createCountryResponse("EC", countryName);
given(lookupService.country(ip)).willReturn(response);
given(settings.getProperty(ProtectionSettings.ENABLE_GEOIP)).willReturn(true);

Expand Down Expand Up @@ -136,4 +131,13 @@ public void shouldNotLookUpCountryNameIfDisabled() throws Exception {
assertThat(result, equalTo("N/A"));
verifyNoInteractions(lookupService);
}

private static CountryResponse createCountryResponse(String countryCode, String countryName) {
List<String> locales = Collections.singletonList("en");
Continent continent = new Continent(locales, "XX", 1L, Collections.emptyMap());

Map<String, String> countryNames = ImmutableMap.of("en", countryName);
Country country = new Country(locales, 100, 3L, false, countryCode, countryNames);
return new CountryResponse(continent, country, null, country, null, null);
}
}

0 comments on commit e133a37

Please sign in to comment.