Skip to content
This repository has been archived by the owner on Jul 6, 2023. It is now read-only.

Commit

Permalink
Code review: clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Feb 6, 2020
1 parent a18bcd6 commit 9b3c2af
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,53 +183,54 @@ public void allowUserToUpdateExpiredPasswordInteractivelyWithoutBeingPrompted()
//given a user that require a password change
int majorVersion = getVersionAndCreateUserWithPasswordChangeRequired();

//in 4.0 and later when the user attempts a non-interactive password update
if (majorVersion >= 4 ) {
baos.reset();
assertEquals( EXIT_SUCCESS, main.runShell( args( SYSTEM_DB_NAME, "foo", "pass",
"ALTER CURRENT USER SET PASSWORD from \"pass\" to \"pass2\";" ), shell, mock(Logger.class) ) );
//we shouldn't ask for a new password
assertEquals("", baos.toString());

//then the new user should be able to successfully connect, and run a command
assertEquals( format( "n%n42%n" ),
executeNonInteractively( args( DEFAULT_DEFAULT_DB_NAME,
"foo", "pass2", "RETURN 42 AS n" ) ) );
}
//when the user attempts a non-interactive password update
assumeTrue(majorVersion >= 4 );
baos.reset();
assertEquals( EXIT_SUCCESS, main.runShell( args( SYSTEM_DB_NAME, "foo", "pass",
"ALTER CURRENT USER SET PASSWORD from \"pass\" to \"pass2\";" ), shell, mock( Logger.class ) ) );
//we shouldn't ask for a new password
assertEquals( "", baos.toString() );

//then the new user should be able to successfully connect, and run a command
assertEquals( format( "n%n42%n" ),
executeNonInteractively( args( DEFAULT_DEFAULT_DB_NAME,
"foo", "pass2", "RETURN 42 AS n" ) ) );
}

@Test
public void shouldFailIfNonInteractivelySettingPasswordOnNonSystemDb() throws Exception {
//given a user that require a password change
int majorVersion = getVersionAndCreateUserWithPasswordChangeRequired();

//in 4.0 and later when the user attempts a non-interactive password update
if (majorVersion >= 4 ) {
assertEquals(EXIT_FAILURE, main.runShell( args( DEFAULT_DEFAULT_DB_NAME, "foo", "pass",
"ALTER CURRENT USER SET PASSWORD from \"pass\" to \"pass2\";"), shell, mock(Logger.class) ) );
}
//when
assumeTrue( majorVersion >= 4 );

//then
assertEquals( EXIT_FAILURE, main.runShell( args( DEFAULT_DEFAULT_DB_NAME, "foo", "pass",
"ALTER CURRENT USER SET PASSWORD from \"pass\" to \"pass2\";" ), shell, mock( Logger.class ) ) );
}

@Test
public void shouldBePromptedIfRunningNonInteractiveCypherThatDoesntUpdatePassword() throws Exception {
//given a user that require a password change
int majorVersion = getVersionAndCreateUserWithPasswordChangeRequired();

//in 4.0 and later when the user attempts a non-interactive password update
if (majorVersion >= 4 ) {
//when asked for a password use this
inputBuffer.put(String.format("pass2%n").getBytes());
baos.reset();
assertEquals( EXIT_SUCCESS, main.runShell( args( DEFAULT_DEFAULT_DB_NAME, "foo", "pass",
"MATCH (n) RETURN n" ), shell, mock(Logger.class) ) );
//we should ask for a new password
assertEquals( format( "Password change required%nnew password: *****%n" ), baos.toString());

//then the new user should be able to successfully connect, and run a command
assertEquals( format( "n%n42%n" ),
executeNonInteractively( args( DEFAULT_DEFAULT_DB_NAME,
"foo", "pass2", "RETURN 42 AS n" ) ) );
}
//when
assumeTrue( majorVersion >= 4 );

//when interactively asked for a password use this
inputBuffer.put( String.format( "pass2%n" ).getBytes() );
baos.reset();
assertEquals( EXIT_SUCCESS, main.runShell( args( DEFAULT_DEFAULT_DB_NAME, "foo", "pass",
"MATCH (n) RETURN n" ), shell, mock( Logger.class ) ) );

//then should ask for a new password
assertEquals( format( "Password change required%nnew password: *****%n" ), baos.toString() );

//then the new user should be able to successfully connect, and run a command
assertEquals( format( "n%n42%n" ),
executeNonInteractively( args( DEFAULT_DEFAULT_DB_NAME,
"foo", "pass2", "RETURN 42 AS n" ) ) );
}

@Test
Expand Down Expand Up @@ -602,12 +603,11 @@ public void switchingToUnavailableDefaultDatabaseIfInteractive() throws Exceptio
}
}

private String executeFileNonInteractively(String filename) throws Exception {
private String executeFileNonInteractively(String filename) {
return executeFileNonInteractively(filename, mock(Logger.class));
}

private String executeFileNonInteractively(String filename, Logger logger) throws Exception
{
private String executeFileNonInteractively(String filename, Logger logger) {
CliArgs cliArgs = new CliArgs();
cliArgs.setUsername( USER, "" );
cliArgs.setPassword( PASSWORD, "" );
Expand All @@ -616,18 +616,16 @@ private String executeFileNonInteractively(String filename, Logger logger) throw
return executeNonInteractively( cliArgs, logger );
}

private String executeNonInteractively(CliArgs cliArgs) throws Exception {
private String executeNonInteractively(CliArgs cliArgs) {
return executeNonInteractively(cliArgs, mock(Logger.class));
}

private String executeNonInteractively(CliArgs cliArgs, Logger logger) throws Exception
private String executeNonInteractively(CliArgs cliArgs, Logger logger)
{
ToStringLinePrinter linePrinter = new ToStringLinePrinter();
ShellAndConnection sac = getShell( cliArgs, linePrinter );
CypherShell shell = sac.shell;
main.runShell(cliArgs, shell, logger);
// ConnectionConfig connectionConfig = sac.connectionConfig;
// main.connectMaybeInteractively( shell, connectionConfig, false, false );
return linePrinter.result();
}

Expand Down
3 changes: 2 additions & 1 deletion cypher-shell/src/main/java/org/neo4j/shell/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ void startShell(@Nonnull CliArgs cliArgs) {

CypherShell shell = new CypherShell( logger, prettyConfig, ShellRunner.shouldBeInteractive( cliArgs ),
cliArgs.getParameters() );
System.exit( runShell( cliArgs, shell, logger ) );
int exitCode = runShell( cliArgs, shell, logger );
System.exit( exitCode );
}

int runShell(@Nonnull CliArgs cliArgs, @Nonnull CypherShell shell, Logger logger )
Expand Down

0 comments on commit 9b3c2af

Please sign in to comment.