@Before public void setUpMocks() { wrapperMock = WrapperMock.createInstance(); cacheMock = wrapperMock.getPlayerCache(); commandService = mock(CommandService.class); when(commandService.getProperty(SecuritySettings.MIN_PASSWORD_LENGTH)).thenReturn(2); when(commandService.getProperty(SecuritySettings.MAX_PASSWORD_LENGTH)).thenReturn(50); // Only allow passwords with alphanumerical characters for the test when(commandService.getProperty(RestrictionSettings.ALLOWED_PASSWORD_REGEX)) .thenReturn("[a-zA-Z0-9]+"); when(commandService.getProperty(SecuritySettings.UNSAFE_PASSWORDS)) .thenReturn(Collections.EMPTY_LIST); }
@Test public void shouldRejectNonPlayerSender() { // given CommandSender sender = mock(BlockCommandSender.class); ChangePasswordCommand command = new ChangePasswordCommand(); // when command.executeCommand(sender, new ArrayList<String>(), commandService); // then assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false)); }
@Test public void shouldRejectPasswordEqualToNick() { // given CommandSender sender = initPlayerWithName("tester", true); ChangePasswordCommand command = new ChangePasswordCommand(); // when command.executeCommand(sender, Arrays.asList("old_", "Tester"), commandService); // then verify(commandService).send(sender, MessageKey.PASSWORD_IS_USERNAME_ERROR); assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false)); }
@Test public void shouldDenyInvalidPassword() { // given CommandSender sender = initPlayerWithName("name", true); ChangePasswordCommand command = new ChangePasswordCommand(); // when command.executeCommand(sender, Arrays.asList("old123", "!pass"), commandService); // then verify(commandService).send(sender, MessageKey.PASSWORD_MATCH_ERROR); assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false)); }
@Test public void shouldRejectNotLoggedInPlayer() { // given CommandSender sender = initPlayerWithName("name", false); ChangePasswordCommand command = new ChangePasswordCommand(); // when command.executeCommand(sender, Arrays.asList("pass", "pass"), commandService); // then verify(commandService).send(sender, MessageKey.NOT_LOGGED_IN); assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false)); }
@Test public void shouldRejectTooShortPassword() { // given CommandSender sender = initPlayerWithName("abc12", true); ChangePasswordCommand command = new ChangePasswordCommand(); given(commandService.getProperty(SecuritySettings.MIN_PASSWORD_LENGTH)).willReturn(7); // when command.executeCommand(sender, Arrays.asList("oldverylongpassword", "tester"), commandService); // then verify(commandService).send(sender, MessageKey.INVALID_PASSWORD_LENGTH); assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false)); }
@Test public void shouldRejectUnsafeCustomPassword() { // given CommandSender sender = initPlayerWithName("player", true); ChangePasswordCommand command = new ChangePasswordCommand(); given(commandService.getProperty(SecuritySettings.UNSAFE_PASSWORDS)) .willReturn(Arrays.asList("test", "abc123")); // when command.executeCommand(sender, Arrays.asList("oldpw", "abc123"), commandService); // then verify(commandService).send(sender, MessageKey.PASSWORD_UNSAFE_ERROR); assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false)); }