Add a couple tests

This commit is contained in:
Gnat008 2017-03-21 16:48:20 -04:00
parent cfbb3f9a7f
commit ed0126d06c
2 changed files with 42 additions and 8 deletions

View File

@ -1,24 +1,27 @@
package fr.xephi.authme.service;
import ch.jalu.injector.testing.BeforeInjecting;
import ch.jalu.injector.testing.DelayedInjectionRunner;
import ch.jalu.injector.testing.InjectDelayed;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.mail.EmailService;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.settings.properties.SecuritySettings;
import org.junit.Ignore;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Tests for {@link PasswordRecoveryService}.
*/
@Ignore
@RunWith(MockitoJUnitRunner.class)
@RunWith(DelayedInjectionRunner.class)
public class PasswordRecoveryServiceTest {
@InjectDelayed
@ -39,16 +42,32 @@ public class PasswordRecoveryServiceTest {
@Mock
private PasswordSecurity passwordSecurity;
@Mock
private RecoveryCodeService recoveryCodeService;
@Mock
private Messages messages;
@BeforeInjecting
public void initSettings() {
given(commonService.getProperty(SecuritySettings.EMAIL_RECOVERY_COOLDOWN_SECONDS)).willReturn(40);
given(commonService.getProperty(SecuritySettings.PASSWORD_CHANGE_TIMEOUT)).willReturn(2);
}
//TODO: Write tests
@Test
public void shouldSendRecoveryCode() {
// given
Player player = mock(Player.class);
String name = "Carl";
given(player.getName()).willReturn(name);
String email = "test@example.com";
String code = "qwerty";
given(codeService.generateCode(name)).willReturn(code);
given(emailService.sendRecoveryCode(player.getName(), email, code)).willReturn(true);
// when
recoveryService.createAndSendRecoveryCode(player, email);
// then
verify(codeService).generateCode(name);
verify(emailService).sendRecoveryCode(name, email, code);
verify(commonService).send(player, MessageKey.RECOVERY_CODE_SENT);
}
}

View File

@ -37,6 +37,21 @@ public class TimedCounterTest {
assertThat(counter.get("moto"), equalTo(13));
}
@Test
public void shouldDecrementCount() {
// given
TimedCounter<String> counter = new TimedCounter<>(10, TimeUnit.MINUTES);
counter.put("moto", 12);
// when
counter.decrement("hello");
counter.decrement("moto");
// then
assertThat(counter.get("hello"), equalTo(0));
assertThat(counter.get("moto"), equalTo(11));
}
@Test
public void shouldSumUpEntries() {
// given