LdapHelper { public boolean isRegistered(String remoteUserName) throws IOException { String ldapServerName = PropertiesWrapper.get("ldap.server.name"); String ldapSshKeyFile = PropertiesWrapper.get("ldap.ssh.key"); CommandLineWrapper cmd = CommandLineWrapperBuilder.create() .withWorkingDirectoryPath(".") .withCommand("ssh") .withCommandArgs("-t", "-t", "-i", ldapSshKeyFile, ldapServerName, "id", remoteUserName) .withDefaultAsyncCommandLineIO() .build(); CommandLineIO commandLineIO = cmd.executeAsync(); CommandLineResultHandler resultHandler = commandLineIO.getResultHandler(); try { resultHandler.waitFor(DEFAULT_TIMEOUT); } catch (InterruptedException exception) { throw new CommandLineException("Checking user '" + remoteUserName + "' from LDAP took too long. The operation was aborted."); } if (resultHandler.getExitValue() == 0) { List<String> groupList = parseLdapIdResponse(commandLineIO.getOutputStringList()); for (String group : groupList) { if (ALLOWABLE_GROUP.equals(group)) { return true; } } CalvalusLogger.getLogger().warning("remote user " + remoteUserName + " groups " + String.join(",", groupList) + " not member in " + ALLOWABLE_GROUP); } return false; }  boolean isRegistered(String remoteUserName); void register(String remoteUserName);  }
@Test public void canReturnFalseWhenLdapCommandFails() throws Exception { mockCmdWrapper = mock(CommandLineWrapper.class); mockCmdIO = mock(CommandLineIO.class); mockResultHandler = mock(CommandLineResultHandler.class); when(mockResultHandler.getExitValue()).thenReturn(1); when(mockResultHandler.hasResult()).thenReturn(true); when(mockCmdIO.getResultHandler()).thenReturn(mockResultHandler); when(mockCmdWrapper.executeAsync()).thenReturn(mockCmdIO); whenNew(CommandLineWrapper.class).withAnyArguments().thenReturn(mockCmdWrapper); ldapHelper = new LdapHelper(); boolean registered = ldapHelper.isRegistered(MOCK_REMOTE_USER_NAME); assertThat(registered, equalTo(false)); }
@Test(expected = CommandLineException.class) public void canThrowExceptionWhenCommandTimesOut() throws Exception { mockCmdWrapper = mock(CommandLineWrapper.class); mockCmdIO = mock(CommandLineIO.class); mockResultHandler = mock(CommandLineResultHandler.class); doThrow(new InterruptedException()).when(mockResultHandler).waitFor(anyLong()); when(mockResultHandler.hasResult()).thenReturn(true); when(mockCmdIO.getResultHandler()).thenReturn(mockResultHandler); when(mockCmdWrapper.executeAsync()).thenReturn(mockCmdIO); whenNew(CommandLineWrapper.class).withAnyArguments().thenReturn(mockCmdWrapper); ldapHelper = new LdapHelper(); ldapHelper.isRegistered(MOCK_REMOTE_USER_NAME); }
@Test public void canReturnFalseWhenUserNotInAllowedGroup() throws Exception { mockCmdWrapper = mock(CommandLineWrapper.class); mockCmdIO = mock(CommandLineIO.class); mockResultHandler = mock(CommandLineResultHandler.class); when(mockResultHandler.getExitValue()).thenReturn(0); when(mockResultHandler.hasResult()).thenReturn(true); List<String> mockOutputStringList = new ArrayList<>(); mockOutputStringList.add("uid=10224(tep_hans) gid=10118(calwps) groups=10118(dummy)"); mockOutputStringList.add("Connection to auth closed."); when(mockCmdIO.getOutputStringList()).thenReturn(mockOutputStringList); when(mockCmdIO.getResultHandler()).thenReturn(mockResultHandler); when(mockCmdWrapper.executeAsync()).thenReturn(mockCmdIO); whenNew(CommandLineWrapper.class).withAnyArguments().thenReturn(mockCmdWrapper); ldapHelper = new LdapHelper(); boolean registered = ldapHelper.isRegistered(MOCK_REMOTE_USER_NAME); assertThat(registered, equalTo(false)); }
@Test public void canReturnTrue() throws Exception { mockCmdWrapper = mock(CommandLineWrapper.class); mockCmdIO = mock(CommandLineIO.class); mockResultHandler = mock(CommandLineResultHandler.class); when(mockResultHandler.getExitValue()).thenReturn(0); when(mockResultHandler.hasResult()).thenReturn(true); List<String> mockOutputStringList = new ArrayList<>(); mockOutputStringList.add("uid=10224(tep_hans) gid=10118(calwps) groups=10118(calwps)"); mockOutputStringList.add("Connection to auth closed."); when(mockCmdIO.getOutputStringList()).thenReturn(mockOutputStringList); when(mockCmdIO.getResultHandler()).thenReturn(mockResultHandler); when(mockCmdWrapper.executeAsync()).thenReturn(mockCmdIO); whenNew(CommandLineWrapper.class).withAnyArguments().thenReturn(mockCmdWrapper); ldapHelper = new LdapHelper(); boolean registered = ldapHelper.isRegistered(MOCK_REMOTE_USER_NAME); assertThat(registered, equalTo(true)); }