You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BanTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.commandparser.commands.channel;
  23. import com.dmdirc.Channel;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.commandparser.commands.context.ChannelCommandContext;
  26. import com.dmdirc.events.CommandErrorEvent;
  27. import com.dmdirc.interfaces.CommandController;
  28. import com.dmdirc.interfaces.Connection;
  29. import com.dmdirc.events.eventbus.EventBus;
  30. import com.dmdirc.interfaces.GroupChatUser;
  31. import com.dmdirc.interfaces.User;
  32. import com.dmdirc.interfaces.WindowModel;
  33. import java.util.Optional;
  34. import org.junit.Before;
  35. import org.junit.Test;
  36. import org.junit.runner.RunWith;
  37. import org.mockito.Mock;
  38. import org.mockito.runners.MockitoJUnitRunner;
  39. import static org.mockito.ArgumentMatchers.isA;
  40. import static org.mockito.Mockito.verify;
  41. import static org.mockito.Mockito.when;
  42. @RunWith(MockitoJUnitRunner.class)
  43. public class BanTest {
  44. @Mock private Connection connection;
  45. @Mock private User user1;
  46. @Mock private User user2;
  47. @Mock private GroupChatUser groupChatUser;
  48. @Mock private Channel channel;
  49. @Mock private CommandController controller;
  50. @Mock private WindowModel container;
  51. @Mock private EventBus eventbus;
  52. private Ban command;
  53. @Before
  54. public void setup() {
  55. when(channel.getConnection()).thenReturn(Optional.of(connection));
  56. when(connection.getUser("user")).thenReturn(user1);
  57. when(connection.getUser("*!*@my.host.name")).thenReturn(user2);
  58. when(groupChatUser.getHostname()).thenReturn(Optional.of("HOSTNAME"));
  59. when(channel.getUser(user1)).thenReturn(Optional.of(groupChatUser));
  60. when(channel.getUser(user2)).thenReturn(Optional.empty());
  61. when(controller.getCommandChar()).thenReturn('/');
  62. when(controller.getSilenceChar()).thenReturn('.');
  63. when(container.getEventBus()).thenReturn(eventbus);
  64. command = new Ban(controller);
  65. }
  66. @Test
  67. public void testUsage() {
  68. command.execute(container, new CommandArguments(controller, "/ban"),
  69. new ChannelCommandContext(null, Ban.INFO, channel));
  70. verify(eventbus).publishAsync(isA(CommandErrorEvent.class));
  71. }
  72. /** Tests that the ban command uses the correct hostname if given a user. */
  73. @Test
  74. public void testKnownUser() {
  75. command.execute(container, new CommandArguments(controller, "/ban user"),
  76. new ChannelCommandContext(null, Ban.INFO, channel));
  77. verify(channel).setMode('b', "*!*@HOSTNAME");
  78. verify(channel).flushModes();
  79. }
  80. /** Tests that the ban command works if given a mask not a username. */
  81. @Test
  82. public void testHostmask() {
  83. command.execute(container, new CommandArguments(controller, "/ban *!*@my.host.name"),
  84. new ChannelCommandContext(null, Ban.INFO, channel));
  85. verify(channel).setMode('b', "*!*@my.host.name");
  86. verify(channel).flushModes();
  87. }
  88. }