RegistrationController { @RequestMapping(method = RequestMethod.GET) public String openRegistrationForm(HttpServletRequest request, Model model) { String ip = getIp(request); Player playerByIp = playerService.findPlayerByIp(ip); if (isLocalhost(ip) || playerByIp instanceof NullPlayer) { Player player = new Player(); model.addAttribute("player", player); player.setCallbackUrl("http: return "register"; } model.addAttribute("user", playerByIp.getName()); model.addAttribute("url", playerByIp.getCallbackUrl()); return "already_registered"; } RegistrationController();   RegistrationController(TetrisPlayerService playerService, GameSettings gameSettingsService); @RequestMapping(method = RequestMethod.GET) String openRegistrationForm(HttpServletRequest request, Model model); @RequestMapping(params = "remove_me", method = RequestMethod.GET) String removeUserFromGame(HttpServletRequest request, Model model); @RequestMapping(method = RequestMethod.POST) String submitRegistrationForm(Player player, BindingResult result, HttpServletRequest request);  }
@Test public void shouldEnterUserIPAtRegistration() { when(request.getRemoteAddr()).thenReturn("IP"); when(playerService.findPlayerByIp("IP")).thenReturn(new NullPlayer()); String jsp = controller.openRegistrationForm(request, model); assertEquals("register", jsp); verify(model).addAttribute(eq("player"), players.capture()); assertEquals("http: }
@Test public void shouldForbiddenRegistrationWhenUserAlreadyRegistered() { when(request.getRemoteAddr()).thenReturn("IP"); Player player = new Player("vasia", "URL", null, null, null); when(playerService.findPlayerByIp("IP")).thenReturn(player); String jsp = controller.openRegistrationForm(request, model); assertEquals("already_registered", jsp); verify(model).addAttribute("user", "vasia"); verify(model).addAttribute("url", "URL"); }
@Test public void shouldRegisterWhenLocalhost() { when(request.getRemoteAddr()).thenReturn("127.0.0.1"); Player player = new Player("vasia", "URL", null, null, null); when(playerService.findPlayerByIp("127.0.0.1")).thenReturn(player); String jsp = controller.openRegistrationForm(request, model); assertEquals("register", jsp); verify(model).addAttribute(eq("player"), players.capture()); assertEquals("http: }
@Test public void shouldChangeLocalIpToLocalhostAndRegister() { when(request.getRemoteAddr()).thenReturn("0:0:0:0:0:0:0:1"); Player player = new Player("vasia", "URL", null, null, null); when(playerService.findPlayerByIp("127.0.0.1")).thenReturn(player); String jsp = controller.openRegistrationForm(request, model); assertEquals("register", jsp); verify(model).addAttribute(eq("player"), players.capture()); assertEquals("http: }