Skip to content

Commit

Permalink
Remove --force option
Browse files Browse the repository at this point in the history
`set-initial-password` will now always delete a previously existing file.
  • Loading branch information
Mats-SX committed Oct 5, 2016
1 parent 105d98e commit fcce98e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 33 deletions.
Expand Up @@ -55,7 +55,7 @@ public Provider()
@Override @Override
public Optional<String> arguments() public Optional<String> arguments()
{ {
return Optional.of( "<password> [--force]" ); return Optional.of( "<password>" );
} }


@Override @Override
Expand Down Expand Up @@ -89,7 +89,7 @@ public void execute( String[] args ) throws IncorrectUsage, CommandFailed


try try
{ {
setPassword( parsedArgs.orphans().get( 0 ), parsedArgs.getBoolean( "force" ) ); setPassword( parsedArgs.orphans().get( 0 ) );
} }
catch ( IncorrectUsage e ) catch ( IncorrectUsage e )
{ {
Expand All @@ -104,7 +104,7 @@ public void execute( String[] args ) throws IncorrectUsage, CommandFailed


private Args validateArgs( String[] args ) throws IncorrectUsage private Args validateArgs( String[] args ) throws IncorrectUsage
{ {
Args parsedArgs = Args.withFlags( "force" ).parse( args ); Args parsedArgs = Args.parse( args );
if ( parsedArgs.orphans().size() < 1 ) if ( parsedArgs.orphans().size() < 1 )
{ {
throw new IncorrectUsage( "No password specified." ); throw new IncorrectUsage( "No password specified." );
Expand All @@ -116,20 +116,13 @@ private Args validateArgs( String[] args ) throws IncorrectUsage
return parsedArgs; return parsedArgs;
} }


private void setPassword( String password, boolean force ) throws Throwable private void setPassword( String password ) throws Throwable
{ {
Config config = loadNeo4jConfig(); Config config = loadNeo4jConfig();
File file = CommunitySecurityModule.getInitialUserRepositoryFile( config ); File file = CommunitySecurityModule.getInitialUserRepositoryFile( config );
if ( outsideWorld.fileSystem().fileExists( file ) ) if ( outsideWorld.fileSystem().fileExists( file ) )
{ {
if ( force ) outsideWorld.fileSystem().deleteFile( file );
{
outsideWorld.fileSystem().deleteFile( file );
}
else
{
throw new CommandFailed( "Initial password already set. Overwrite this password with --force" );
}
} }


FileUserRepository userRepository = FileUserRepository userRepository =
Expand Down
Expand Up @@ -74,32 +74,31 @@ public void shouldSetPassword() throws Throwable
} }


@Test @Test
public void shouldSetPasswordAgainWithForce() throws Throwable public void shouldOverwriteIfSetPasswordAgain() throws Throwable
{ {
tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "abc" ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "abc" );
assertAuthIniFile( "abc" ); assertAuthIniFile( "abc" );
tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "muchBetter", "--force" ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "muchBetter" );
verify( out, times( 2 ) ).stdOutLine( "Changed password for user 'neo4j'." ); verify( out, times( 2 ) ).stdOutLine( "Changed password for user 'neo4j'." );
assertAuthIniFile( "muchBetter" ); assertAuthIniFile( "muchBetter" );
} }


@Test @Test
public void shouldFailToSetPasswordAgainWithoutForce() throws Throwable public void shouldWorkWithSamePassword() throws Throwable
{ {
tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "abc" ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "neo4j" );
assertAuthIniFile( "abc" ); assertAuthIniFile( "neo4j" );
verify( out ).stdOutLine( "Changed password for user 'neo4j'." ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "neo4j" );
tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "muchBetter" ); verify( out, times( 2 ) ).stdOutLine( "Changed password for user 'neo4j'." );
verify( out ).stdErrLine( "command failed: Failed to execute 'set-initial-password' command: Initial password already set. Overwrite this password with --force" ); assertAuthIniFile( "neo4j" );
assertAuthIniFile( "abc" );
} }


@Test @Test
public void shouldGetUsageOnWrongArguments() throws Throwable public void shouldGetUsageOnWrongArguments() throws Throwable
{ {
tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD );
tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "foo", "bar" ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "foo", "bar" );
verify( out, times( 2 ) ).stdErrLine( "neo4j-admin set-initial-password <password> [--force]" ); verify( out, times( 2 ) ).stdErrLine( "neo4j-admin set-initial-password <password>" );
verify( out, times( 0 ) ).stdOutLine( anyString() ); verify( out, times( 0 ) ).stdOutLine( anyString() );
} }


Expand Down
Expand Up @@ -92,31 +92,28 @@ public void shouldSetInitialPassword() throws Throwable
} }


@Test @Test
public void shouldFailToCreateInitialPasswordFileIfExists() throws Throwable public void shouldOverwriteInitialPasswordFileIfExists() throws Throwable
{ {
// Given // Given
fileSystem.mkdirs( authInitFile.getParentFile() ); fileSystem.mkdirs( authInitFile.getParentFile() );
fileSystem.create( authInitFile ); fileSystem.create( authInitFile );


// When // When
String[] arguments = {"123"}; String[] arguments = {"123"};
assertException( () -> setPasswordCommand.execute( arguments ), CommandFailed.class, setPasswordCommand.execute( arguments );
"Initial password already set. Overwrite this password with --force" );
// Then
assertAuthIniFile( "123" );
} }


@Test @Test
public void shouldCreateInitialPasswordFileEvenIfExistsWhenForced() throws Throwable public void shouldWorkAlsoWithSamePassword() throws Throwable
{ {
// Given String[] arguments = {"neo4j"};
fileSystem.mkdirs( authInitFile.getParentFile() );
fileSystem.create( authInitFile );

// When
String[] arguments = {"123", "--force"};
setPasswordCommand.execute( arguments ); setPasswordCommand.execute( arguments );


// Then // Then
assertAuthIniFile( "123" ); assertAuthIniFile( "neo4j" );
} }


private void assertAuthIniFile( String password ) throws Throwable private void assertAuthIniFile( String password ) throws Throwable
Expand Down

0 comments on commit fcce98e

Please sign in to comment.