Skip to content

Commit e69016a

Browse files
committed
Fix tests (due to new Bukkit/GeoIP updates)
1 parent 6eec2c8 commit e69016a

File tree

6 files changed

+36
-27
lines changed

6 files changed

+36
-27
lines changed

src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import fr.xephi.authme.data.limbo.LimboService;
66
import fr.xephi.authme.datasource.DataSource;
77
import fr.xephi.authme.events.ProtectInventoryEvent;
8-
import fr.xephi.authme.output.ConsoleLoggerFactory;
98
import fr.xephi.authme.message.MessageKey;
9+
import fr.xephi.authme.output.ConsoleLoggerFactory;
1010
import fr.xephi.authme.permission.PlayerStatePermission;
1111
import fr.xephi.authme.process.AsynchronousProcess;
1212
import fr.xephi.authme.process.login.AsynchronousLogin;
@@ -27,11 +27,8 @@
2727
import org.bukkit.GameMode;
2828
import org.bukkit.Server;
2929
import org.bukkit.entity.Player;
30-
import org.bukkit.potion.PotionEffect;
31-
import org.bukkit.potion.PotionEffectType;
3230

3331
import javax.inject.Inject;
34-
3532
import java.util.Locale;
3633

3734
import static fr.xephi.authme.service.BukkitService.TICKS_PER_SECOND;
@@ -197,7 +194,7 @@ private void processJoinSync(Player player, boolean isAuthAvailable) {
197194
if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) {
198195
// Allow infinite blindness effect
199196
int blindTimeOut = (registrationTimeout <= 0) ? 99999 : registrationTimeout;
200-
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, blindTimeOut, 2));
197+
player.addPotionEffect(bukkitService.createBlindnessEffect(blindTimeOut));
201198
}
202199
commandManager.runCommandsOnJoin(player);
203200
});

src/main/java/fr/xephi/authme/process/logout/ProcessSyncPlayerLogout.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import fr.xephi.authme.ConsoleLogger;
44
import fr.xephi.authme.data.limbo.LimboService;
55
import fr.xephi.authme.events.LogoutEvent;
6-
import fr.xephi.authme.output.ConsoleLoggerFactory;
76
import fr.xephi.authme.listener.protocollib.ProtocolLibService;
87
import fr.xephi.authme.message.MessageKey;
8+
import fr.xephi.authme.output.ConsoleLoggerFactory;
99
import fr.xephi.authme.process.SynchronousProcess;
1010
import fr.xephi.authme.service.BukkitService;
1111
import fr.xephi.authme.service.CommonService;
@@ -14,8 +14,6 @@
1414
import fr.xephi.authme.settings.properties.RegistrationSettings;
1515
import fr.xephi.authme.settings.properties.RestrictionSettings;
1616
import org.bukkit.entity.Player;
17-
import org.bukkit.potion.PotionEffect;
18-
import org.bukkit.potion.PotionEffectType;
1917

2018
import javax.inject.Inject;
2119

@@ -75,7 +73,7 @@ private void applyLogoutEffect(Player player) {
7573
// Apply Blindness effect
7674
if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) {
7775
int timeout = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND;
78-
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, timeout, 2));
76+
player.addPotionEffect(bukkitService.createBlindnessEffect(timeout));
7977
}
8078

8179
// Set player's data to unauthenticated

src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,20 @@
77
import fr.xephi.authme.datasource.DataSource;
88
import fr.xephi.authme.events.UnregisterByAdminEvent;
99
import fr.xephi.authme.events.UnregisterByPlayerEvent;
10-
import fr.xephi.authme.output.ConsoleLoggerFactory;
1110
import fr.xephi.authme.message.MessageKey;
11+
import fr.xephi.authme.output.ConsoleLoggerFactory;
1212
import fr.xephi.authme.process.AsynchronousProcess;
1313
import fr.xephi.authme.security.PasswordSecurity;
1414
import fr.xephi.authme.service.BukkitService;
1515
import fr.xephi.authme.service.CommonService;
1616
import fr.xephi.authme.service.TeleportationService;
17+
import fr.xephi.authme.service.bungeecord.BungeeSender;
1718
import fr.xephi.authme.service.bungeecord.MessageType;
1819
import fr.xephi.authme.settings.commandconfig.CommandManager;
1920
import fr.xephi.authme.settings.properties.RegistrationSettings;
2021
import fr.xephi.authme.settings.properties.RestrictionSettings;
21-
import fr.xephi.authme.service.bungeecord.BungeeSender;
2222
import org.bukkit.command.CommandSender;
2323
import org.bukkit.entity.Player;
24-
import org.bukkit.potion.PotionEffect;
25-
import org.bukkit.potion.PotionEffectType;
2624

2725
import javax.inject.Inject;
2826

@@ -144,7 +142,7 @@ private void performPostUnregisterActions(String name, Player player) {
144142
private void applyBlindEffect(Player player) {
145143
if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) {
146144
int timeout = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND;
147-
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, timeout, 2));
145+
player.addPotionEffect(bukkitService.createBlindnessEffect(timeout));
148146
}
149147
}
150148

src/main/java/fr/xephi/authme/service/BukkitService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import org.bukkit.configuration.file.YamlConfiguration;
1414
import org.bukkit.entity.Player;
1515
import org.bukkit.event.Event;
16+
import org.bukkit.potion.PotionEffect;
17+
import org.bukkit.potion.PotionEffectType;
1618
import org.bukkit.scheduler.BukkitRunnable;
1719
import org.bukkit.scheduler.BukkitTask;
1820

@@ -338,6 +340,16 @@ public Optional<Boolean> isBungeeCordConfiguredForSpigot() {
338340
}
339341
}
340342

343+
/**
344+
* Creates a PotionEffect with blindness for the given duration in ticks.
345+
*
346+
* @param timeoutInTicks duration of the effect in ticks
347+
* @return blindness potion effect
348+
*/
349+
public PotionEffect createBlindnessEffect(int timeoutInTicks) {
350+
return new PotionEffect(PotionEffectType.BLINDNESS, timeoutInTicks, 2);
351+
}
352+
341353
/**
342354
* @return the IP string that this server is bound to, otherwise empty string
343355
*/

src/test/java/fr/xephi/authme/process/unregister/AsynchronousUnregisterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public void shouldPerformUnregister() {
115115
given(service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)).willReturn(true);
116116
given(service.getProperty(RestrictionSettings.TIMEOUT)).willReturn(21);
117117
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
118+
given(bukkitService.createBlindnessEffect(21 * 20)).willReturn(mock(PotionEffect.class));
118119

119120
// when
120121
asynchronousUnregister.unregister(player, userPassword);
@@ -127,7 +128,6 @@ public void shouldPerformUnregister() {
127128
verify(teleportationService).teleportOnJoin(player);
128129
verifyCalledUnregisterEventFor(player);
129130
verify(commandManager).runCommandsOnUnregister(player);
130-
verify(player).addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 21 * 20, 2));
131131
}
132132

133133
@Test

src/test/java/fr/xephi/authme/service/GeoIpServiceTest.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.net.InetAddress;
6+
import java.util.Collections;
7+
import java.util.List;
8+
import java.util.Map;
69

10+
import com.google.common.collect.ImmutableMap;
711
import com.maxmind.geoip2.DatabaseReader;
812
import com.maxmind.geoip2.model.CountryResponse;
13+
import com.maxmind.geoip2.record.Continent;
914
import com.maxmind.geoip2.record.Country;
1015
import fr.xephi.authme.settings.Settings;
1116
import fr.xephi.authme.settings.properties.ProtectionSettings;
@@ -58,12 +63,7 @@ public void shouldGetCountry() throws Exception {
5863
// given
5964
InetAddress ip = InetAddress.getByName("123.45.67.89");
6065
String countryCode = "XX";
61-
62-
Country country = mock(Country.class);
63-
given(country.getIsoCode()).willReturn(countryCode);
64-
65-
CountryResponse response = mock(CountryResponse.class);
66-
given(response.getCountry()).willReturn(country);
66+
CountryResponse response = createCountryResponse(countryCode, "Unknown");
6767
given(lookupService.country(ip)).willReturn(response);
6868
given(settings.getProperty(ProtectionSettings.ENABLE_GEOIP)).willReturn(true);
6969

@@ -93,12 +93,7 @@ public void shouldLookUpCountryName() throws Exception {
9393
// given
9494
InetAddress ip = InetAddress.getByName("24.45.167.89");
9595
String countryName = "Ecuador";
96-
97-
Country country = mock(Country.class);
98-
given(country.getName()).willReturn(countryName);
99-
100-
CountryResponse response = mock(CountryResponse.class);
101-
given(response.getCountry()).willReturn(country);
96+
CountryResponse response = createCountryResponse("EC", countryName);
10297
given(lookupService.country(ip)).willReturn(response);
10398
given(settings.getProperty(ProtectionSettings.ENABLE_GEOIP)).willReturn(true);
10499

@@ -136,4 +131,13 @@ public void shouldNotLookUpCountryNameIfDisabled() throws Exception {
136131
assertThat(result, equalTo("N/A"));
137132
verifyNoInteractions(lookupService);
138133
}
134+
135+
private static CountryResponse createCountryResponse(String countryCode, String countryName) {
136+
List<String> locales = Collections.singletonList("en");
137+
Continent continent = new Continent(locales, "XX", 1L, Collections.emptyMap());
138+
139+
Map<String, String> countryNames = ImmutableMap.of("en", countryName);
140+
Country country = new Country(locales, 100, 3L, false, countryCode, countryNames);
141+
return new CountryResponse(continent, country, null, country, null, null);
142+
}
139143
}

0 commit comments

Comments
 (0)