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
public Optional<String> arguments()
{
return Optional.of( "<password> [--force]" );
return Optional.of( "<password>" );
}

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

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

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

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

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

@Test
public void shouldSetPasswordAgainWithForce() throws Throwable
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", "--force" );
tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "muchBetter" );
verify( out, times( 2 ) ).stdOutLine( "Changed password for user 'neo4j'." );
assertAuthIniFile( "muchBetter" );
}

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

@Test
public void shouldGetUsageOnWrongArguments() throws Throwable
{
tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD );
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() );
}

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

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

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

// Then
assertAuthIniFile( "123" );
}

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

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

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

private void assertAuthIniFile( String password ) throws Throwable
Expand Down

0 comments on commit fcce98e

Please sign in to comment.