private void print( String text ) { outsideWorld.stdOutLine( text ); }
@Test public void shouldErrorWithNoSuchUser() { tool.execute( homeDir.toPath(), confDir.toPath(), SET_ADMIN, "bob" ); verify( out ).stdErrLine( "command failed: no such user: 'bob'" ); verify( out ).exit( 1 ); verify( out, never() ).stdOutLine( anyString() ); }
@Test public void shouldSetPassword() throws Throwable { tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "abc" ); assertAuthIniFile( "abc" ); verify( out ).stdOutLine( "Changed password for user 'neo4j'." ); }
@Test public void shouldOverwriteIfSetPasswordAgain() throws Throwable { tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "abc" ); assertAuthIniFile( "abc" ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "muchBetter" ); assertAuthIniFile( "muchBetter" ); verify( out, times( 2 ) ).stdOutLine( "Changed password for user 'neo4j'." ); }
@Test public void shouldWorkWithSamePassword() throws Throwable { tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "neo4j" ); assertAuthIniFile( "neo4j" ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "neo4j" ); assertAuthIniFile( "neo4j" ); verify( out, times( 2 ) ).stdOutLine( "Changed password for user 'neo4j'." ); }
@Test public void shouldOverwrite() throws Throwable { insertUser( "jane", false ); insertUser( "janette", false ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_ADMIN, "jane" ); assertAdminIniFile( "jane" ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_ADMIN, "janette" ); assertAdminIniFile( "janette" ); verify( out ).stdOutLine( "default admin user set to 'jane'" ); verify( out ).stdOutLine( "default admin user set to 'janette'" ); }
@Test public void shouldSetDefaultAdminForInitialUser() throws Throwable { insertUser( "jane", true ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_ADMIN, "jane" ); assertAdminIniFile( "jane" ); verify( out ).stdOutLine( "default admin user set to 'jane'" ); }
@Test public void shouldSetDefaultAdmin() throws Throwable { insertUser( "jane", false ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_ADMIN, "jane" ); assertAdminIniFile( "jane" ); verify( out ).stdOutLine( "default admin user set to 'jane'" ); }
@Test public void shouldIgnoreInitialUserIfUsersExist() throws Throwable { insertUser( "jane", false ); insertUser( "janette", true ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_ADMIN, "jane" ); assertAdminIniFile( "jane" ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_ADMIN, "janette" ); verify( out ).stdOutLine( "default admin user set to 'jane'" ); verify( out ).stdErrLine( "command failed: no such user: 'janette'" ); verify( out ).exit( 1 ); }
@Test public void shouldGetUsageOnWrongArguments2() { tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "foo", "bar" ); assertNoAuthIniFile(); verify( out ).stdErrLine( "unrecognized arguments: 'bar'" ); verify( out, times( 3 ) ).stdErrLine( "" ); verify( out ).stdErrLine( "usage: neo4j-admin set-initial-password <password>" ); verify( out ).stdErrLine( String.format( "environment variables:" ) ); verify( out ).stdErrLine( String.format( " NEO4J_CONF Path to directory which contains neo4j.conf." ) ); verify( out ).stdErrLine( String.format( " NEO4J_DEBUG Set to anything to enable debug output." ) ); verify( out ).stdErrLine( String.format( " NEO4J_HOME Neo4j home directory." ) ); verify( out ).stdErrLine( String.format( " HEAP_SIZE Set JVM maximum heap size during command execution." ) ); verify( out ).stdErrLine( String.format( " Takes a number and a unit, for example 512m." ) ); verify( out ).stdErrLine( "Sets the initial password of the initial admin user ('neo4j')." ); verify( out ).exit( 1 ); verifyNoMoreInteractions( out ); verify( out, never() ).stdOutLine( anyString() ); }
@Test public void shouldGetUsageOnWrongArguments1() { tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD ); assertNoAuthIniFile(); verify( out ).stdErrLine( "not enough arguments" ); verify( out, times( 3 ) ).stdErrLine( "" ); verify( out ).stdErrLine( "usage: neo4j-admin set-initial-password <password>" ); verify( out ).stdErrLine( String.format( "environment variables:" ) ); verify( out ).stdErrLine( String.format( " NEO4J_CONF Path to directory which contains neo4j.conf." ) ); verify( out ).stdErrLine( String.format( " NEO4J_DEBUG Set to anything to enable debug output." ) ); verify( out ).stdErrLine( String.format( " NEO4J_HOME Neo4j home directory." ) ); verify( out ).stdErrLine( String.format( " HEAP_SIZE Set JVM maximum heap size during command execution." ) ); verify( out ).stdErrLine( String.format( " Takes a number and a unit, for example 512m." ) ); verify( out ).stdErrLine( "Sets the initial password of the initial admin user ('neo4j')." ); verify( out ).exit( 1 ); verifyNoMoreInteractions( out ); verify( out, never() ).stdOutLine( anyString() ); }
@Test public void shouldGetUsageOnWrongArguments1() { tool.execute( homeDir.toPath(), confDir.toPath(), SET_ADMIN ); assertNoAuthIniFile(); verify( out ).stdErrLine( "not enough arguments" ); verify( out, times( 3 ) ).stdErrLine( "" ); verify( out ).stdErrLine( "usage: neo4j-admin set-default-admin <username>" ); verify( out, times( 3 ) ).stdErrLine( "" ); verify( out ).stdErrLine( String.format( "environment variables:" ) ); verify( out ).stdErrLine( String.format( " NEO4J_CONF Path to directory which contains neo4j.conf." ) ); verify( out ).stdErrLine( String.format( " NEO4J_DEBUG Set to anything to enable debug output." ) ); verify( out ).stdErrLine( String.format( " NEO4J_HOME Neo4j home directory." ) ); verify( out ).stdErrLine( String.format( " HEAP_SIZE Set JVM maximum heap size during command execution." ) ); verify( out ).stdErrLine( String.format( " Takes a number and a unit, for example 512m." ) ); verify( out ).stdErrLine( String.format( "Sets the user to become admin if users but no roles are present, for example%n" + "when upgrading to neo4j 3.1 enterprise." ) ); verify( out ).exit( 1 ); verifyNoMoreInteractions( out ); verify( out, never() ).stdOutLine( anyString() ); }
@Test public void shouldErrorIfRealUsersAlreadyExistCommunity() throws Throwable { // Given File authFile = getAuthFile( "auth" ); fileSystem.mkdirs( authFile.getParentFile() ); fileSystem.create( authFile ); // When tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "will-be-ignored" ); // Then assertNoAuthIniFile(); verify( out, times( 1 ) ) .stdErrLine( "command failed: the provided initial password was not set because existing Neo4j users were " + "detected at `" + authFile.getAbsolutePath() + "`. Please remove the existing `auth` file if you " + "want to reset your database to only have a default user with the provided password." ); verify( out ).exit( 1 ); verify( out, times( 0 ) ).stdOutLine( anyString() ); }
@Test public void shouldNotErrorIfOnlyTheUnmodifiedDefaultNeo4jUserAlreadyExists() throws Throwable { // Given // Create an `auth` file with the default neo4j user tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, UserManager.INITIAL_PASSWORD ); File authFile = getAuthFile( "auth" ); fileSystem.mkdirs( authFile.getParentFile() ); fileSystem.renameFile( getAuthFile( "auth.ini" ), authFile ); // When tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "should-not-be-ignored" ); // Then assertAuthIniFile( "should-not-be-ignored" ); verify( out, times( 2 ) ).stdOutLine( "Changed password for user 'neo4j'." ); }
@Test public void shouldGetUsageOnWrongArguments2() { tool.execute( homeDir.toPath(), confDir.toPath(), SET_ADMIN, "foo", "bar" ); assertNoAuthIniFile(); verify( out ).stdErrLine( "unrecognized arguments: 'bar'" ); verify( out, times( 3 ) ).stdErrLine( "" ); verify( out ).stdErrLine( "usage: neo4j-admin set-default-admin <username>" ); verify( out, times( 3 ) ).stdErrLine( "" ); verify( out ).stdErrLine( String.format( "environment variables:" ) ); verify( out ).stdErrLine( String.format( " NEO4J_CONF Path to directory which contains neo4j.conf." ) ); verify( out ).stdErrLine( String.format( " NEO4J_DEBUG Set to anything to enable debug output." ) ); verify( out ).stdErrLine( String.format( " NEO4J_HOME Neo4j home directory." ) ); verify( out ).stdErrLine( String.format( " HEAP_SIZE Set JVM maximum heap size during command execution." ) ); verify( out ).stdErrLine( String.format( " Takes a number and a unit, for example 512m." ) ); verify( out ).stdErrLine( String.format( "Sets the user to become admin if users but no roles are present, for example%n" + "when upgrading to neo4j 3.1 enterprise." ) ); verify( out ).exit( 1 ); verifyNoMoreInteractions( out ); verify( out, never() ).stdOutLine( anyString() ); }
@Test void versionArgumentPrintsVersionEvenWithCommand() { AdminCommand command = mock( AdminCommand.class ); OutsideWorld outsideWorld = mock( OutsideWorld.class ); new AdminTool( cannedCommand( "command", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "command", "--version" ); verifyNoMoreInteractions( command ); verify( outsideWorld ).stdOutLine( "neo4j-admin " + neo4jVersion() ); verify( outsideWorld ).exit( STATUS_SUCCESS ); }
@Test void versionArgumentPrintsVersion() { AdminCommand command = mock( AdminCommand.class ); OutsideWorld outsideWorld = mock( OutsideWorld.class ); new AdminTool( cannedCommand( "command", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "--version" ); verifyNoMoreInteractions( command ); verify( outsideWorld ).stdOutLine( "neo4j-admin " + neo4jVersion() ); verify( outsideWorld ).exit( STATUS_SUCCESS ); }
@Test public void shouldErrorIfRealUsersAlreadyExistV2() throws Throwable { // Given // Create an `auth` file with the default neo4j user, but not the default password tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "not-the-default-password" ); File authFile = getAuthFile( "auth" ); fileSystem.mkdirs( authFile.getParentFile() ); fileSystem.renameFile( getAuthFile( "auth.ini" ), authFile ); // When tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "will-be-ignored" ); // Then assertNoAuthIniFile(); verify( out, times( 1 ) ) .stdErrLine( "command failed: the provided initial password was not set because existing Neo4j users were " + "detected at `" + authFile.getAbsolutePath() + "`. Please remove the existing `auth` file if you " + "want to reset your database to only have a default user with the provided password." ); verify( out ).exit( 1 ); verify( out, times( 1 ) ).stdOutLine( "Changed password for user 'neo4j'." ); // This is from the initial setup }
@Test void shouldAddTheHelpCommandToThoseProvidedByTheLocator() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); new AdminTool( new NullCommandLocator(), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "help" ); verify( outsideWorld ).stdOutLine( " help" ); }
private void setPassword( String password ) throws Throwable { Config config = loadNeo4jConfig(); FileSystemAbstraction fileSystem = outsideWorld.fileSystem(); if ( realUsersExist( config ) ) { File authFile = CommunitySecurityModule.getUserRepositoryFile( config ); throw new CommandFailed( realUsersExistErrorMsg( fileSystem, authFile ) ); } else { File file = CommunitySecurityModule.getInitialUserRepositoryFile( config ); if ( fileSystem.fileExists( file ) ) { fileSystem.deleteFile( file ); } FileUserRepository userRepository = new FileUserRepository( fileSystem, file, NullLogProvider.getInstance() ); userRepository.start(); userRepository.create( new User.Builder( INITIAL_USER_NAME, LegacyCredential.forPassword( UTF8.encode( password ) ) ) .withRequiredPasswordChange( false ) .build() ); userRepository.shutdown(); outsideWorld.stdOutLine( "Changed password for user '" + INITIAL_USER_NAME + "'." ); } }