Skip to content

Commit

Permalink
Refactored tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OliviaYtterbrink committed Sep 28, 2016
1 parent f3d39b1 commit 01e68cf
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 153 deletions.
Expand Up @@ -110,21 +110,26 @@ String[] args(String... args)
return args; return args;
} }


protected abstract String command(); protected String[] makeArgs( String command, String subCommand, String... args )

protected String[] makeArgs( String subCommand, String... args )
{ {
String[] allArgs = new String[args.length + 2]; String[] allArgs;
System.arraycopy( args, 0, allArgs, 2, args.length ); if ( subCommand.isEmpty() ) {
allArgs[0] = command(); allArgs = new String[1];
allArgs[1] = subCommand; }
else
{
allArgs = new String[args.length + 2];
System.arraycopy( args, 0, allArgs, 2, args.length );
allArgs[1] = subCommand;
}
allArgs[0] = command;
return allArgs; return allArgs;
} }


void assertFailedSubCommand( String command, String[] args, String... errors ) void assertFailedSubCommand( String command, String subCommand, String[] args, String... errors )
{ {
resetOutsideWorldMock(); resetOutsideWorldMock();
tool.execute( graphDir.toPath(), confDir.toPath(), makeArgs( command, args ) ); tool.execute( graphDir.toPath(), confDir.toPath(), makeArgs( command, subCommand, args ) );


// Then we get the expected error // Then we get the expected error
for ( String error : errors ) for ( String error : errors )
Expand All @@ -135,10 +140,10 @@ void assertFailedSubCommand( String command, String[] args, String... errors )
verify( out ).exit( 1 ); verify( out ).exit( 1 );
} }


void assertSuccessfulSubCommand( String command, String[] args, String... messages ) void assertSuccessfulSubCommand( String command, String subCommand, String[] args, String... messages )
{ {
resetOutsideWorldMock(); resetOutsideWorldMock();
tool.execute( graphDir.toPath(), confDir.toPath(), makeArgs( command, args )); tool.execute( graphDir.toPath(), confDir.toPath(), makeArgs( command, subCommand, args ));


// Then we get the expected output messages // Then we get the expected output messages
for ( String message : messages ) for ( String message : messages )
Expand Down
Expand Up @@ -24,14 +24,6 @@
import org.junit.Test; import org.junit.Test;
import org.junit.rules.RuleChain; import org.junit.rules.RuleChain;


import org.neo4j.commandline.admin.AdminTool;
import org.neo4j.commandline.admin.CommandLocator;

import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.contains;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class UsersCommandIT extends UsersCommandTestBase public class UsersCommandIT extends UsersCommandTestBase
{ {
@Rule @Rule
Expand All @@ -45,15 +37,15 @@ public void setup()
// environment that the actual tested commands will encounter. In particular some auth state is created // environment that the actual tested commands will encounter. In particular some auth state is created
// on demand in both the UserCommand and in the real server. We want that state created before the tests // on demand in both the UserCommand and in the real server. We want that state created before the tests
// are run. // are run.
tool.execute( graphDir.toPath(), confDir.toPath(), makeArgs( "list") ); tool.execute( graphDir.toPath(), confDir.toPath(), makeArgs( "users", "list" ) );
resetOutsideWorldMock(); resetOutsideWorldMock();
} }


@Test @Test
public void shouldGetUsageErrorsWithNoSubCommand() throws Throwable public void shouldGetUsageErrorsWithNoSubCommand() throws Throwable
{ {
// When running 'users' with no subcommand, expect usage errors // When running 'users' with no subcommand, expect usage errors
assertFailedUserCommand( null, assertFailedUserCommand( "", new String[0],
"Missing arguments: expected at least one sub-command as argument", "Missing arguments: expected at least one sub-command as argument",
"neo4j-admin users <subcommand> [<username>] [<password>]", "neo4j-admin users <subcommand> [<username>] [<password>]",
"Runs several possible sub-commands for managing the native users" ); "Runs several possible sub-commands for managing the native users" );
Expand All @@ -69,7 +61,7 @@ public void shouldListDefaultUser() throws Throwable
// Given only default user // Given only default user


// When running 'list', expect default user // When running 'list', expect default user
assertSuccessfulSubCommand( "list", args(), "neo4j" ); assertSuccessfulUserCommand( "list", args(), "neo4j" );
} }


@Test @Test
Expand All @@ -79,7 +71,7 @@ public void shouldListNewUser() throws Throwable
createTestUser( "another", "neo4j" ); createTestUser( "another", "neo4j" );


// When running 'list', expect only new user (default not created if a new user is created first) // When running 'list', expect only new user (default not created if a new user is created first)
assertSuccessfulSubCommand( "list", args(), "another" ); assertSuccessfulUserCommand( "list", args(), "another" );
} }


@Test @Test
Expand All @@ -89,7 +81,7 @@ public void shouldListSpecifiedUser() throws Throwable
createTestUser( "another", "neo4j" ); createTestUser( "another", "neo4j" );


// When running 'list' with filter, expect specified user // When running 'list' with filter, expect specified user
assertSuccessfulSubCommand( "list", args("other"), "another" ); assertSuccessfulUserCommand( "list", args("other"), "another" );
} }


// //
Expand All @@ -100,7 +92,7 @@ public void shouldListSpecifiedUser() throws Throwable
public void shouldGetUsageErrorsWithSetPasswordCommandAndNoArgs() throws Throwable public void shouldGetUsageErrorsWithSetPasswordCommandAndNoArgs() throws Throwable
{ {
// When running 'set-password' with arguments, expect usage errors // When running 'set-password' with arguments, expect usage errors
assertFailedUserCommand( "set-password", assertFailedUserCommand( "set-password", new String[0],
"Missing arguments: 'users set-password' expects username and password arguments", "Missing arguments: 'users set-password' expects username and password arguments",
"neo4j-admin users <subcommand> [<username>] [<password>]", "neo4j-admin users <subcommand> [<username>] [<password>]",
"Runs several possible sub-commands for managing the native users" ); "Runs several possible sub-commands for managing the native users" );
Expand All @@ -112,7 +104,7 @@ public void shouldNotSetPasswordOnNonExistentUser() throws Throwable
// Given no previously existing user // Given no previously existing user


// When running 'set-password' with correct parameters, expect an error // When running 'set-password' with correct parameters, expect an error
assertFailedSubCommand( "set-password", args("another", "abc"), "User 'another' does not exist" ); assertFailedUserCommand( "set-password", args("another", "abc"), "User 'another' does not exist" );
} }


@Test @Test
Expand All @@ -121,7 +113,7 @@ public void shouldSetPasswordOnDefaultUser() throws Throwable
// Given default state (only default user) // Given default state (only default user)


// When running 'set-password' with correct parameters, expect correct output // When running 'set-password' with correct parameters, expect correct output
assertSuccessfulSubCommand( "set-password", args("neo4j", "abc"), "Changed password for user 'neo4j'" ); assertSuccessfulUserCommand( "set-password", args("neo4j", "abc"), "Changed password for user 'neo4j'" );


// And the user no longer requires password change // And the user no longer requires password change
assertUserDoesNotRequirePasswordChange( "neo4j" ); assertUserDoesNotRequirePasswordChange( "neo4j" );
Expand All @@ -135,7 +127,7 @@ public void shouldSetPasswordOnNewUser() throws Throwable
assertUserRequiresPasswordChange( "another" ); assertUserRequiresPasswordChange( "another" );


// When running 'set-password' with correct parameters, expect correct output // When running 'set-password' with correct parameters, expect correct output
assertSuccessfulSubCommand( "set-password", args("another", "abc"), "Changed password for user 'another'" ); assertSuccessfulUserCommand( "set-password", args("another", "abc"), "Changed password for user 'another'" );


// And the user no longer requires password change // And the user no longer requires password change
assertUserDoesNotRequirePasswordChange( "another" ); assertUserDoesNotRequirePasswordChange( "another" );
Expand All @@ -149,13 +141,13 @@ public void shouldSetPasswordOnNewUserButNotChangeToSamePassword() throws Throwa
assertUserRequiresPasswordChange( "another" ); assertUserRequiresPasswordChange( "another" );


// When running 'set-password' with correct parameters, expect correct output // When running 'set-password' with correct parameters, expect correct output
assertSuccessfulSubCommand( "set-password", args("another", "abc"), "Changed password for user 'another'" ); assertSuccessfulUserCommand( "set-password", args("another", "abc"), "Changed password for user 'another'" );


// And password change is no longer required // And password change is no longer required
assertUserDoesNotRequirePasswordChange( "another" ); assertUserDoesNotRequirePasswordChange( "another" );


// Then when running another password set to same password expect error // Then when running another password set to same password expect error
assertFailedSubCommand( "set-password", args("another", "abc"), "Old password and new password cannot be the same" ); assertFailedUserCommand( "set-password", args("another", "abc"), "Old password and new password cannot be the same" );
} }


@Test @Test
Expand All @@ -166,13 +158,13 @@ public void shouldSetPasswordOnNewUserAndChangeToAnotherPassword() throws Throwa
assertUserRequiresPasswordChange( "another" ); assertUserRequiresPasswordChange( "another" );


// When running 'set-password' with correct parameters, expect correct output // When running 'set-password' with correct parameters, expect correct output
assertSuccessfulSubCommand( "set-password", args(args("another", "abc")), "Changed password for user 'another'" ); assertSuccessfulUserCommand( "set-password", args(args("another", "abc")), "Changed password for user 'another'" );


// And password change is no longer required // And password change is no longer required
assertUserDoesNotRequirePasswordChange( "another" ); assertUserDoesNotRequirePasswordChange( "another" );


// And then when changing to a different password, expect correct output // And then when changing to a different password, expect correct output
assertSuccessfulSubCommand( "set-password", args("another", "123"), "Changed password for user 'another'" ); assertSuccessfulUserCommand( "set-password", args("another", "123"), "Changed password for user 'another'" );
} }


// //
Expand All @@ -183,7 +175,7 @@ public void shouldSetPasswordOnNewUserAndChangeToAnotherPassword() throws Throwa
public void shouldGetUsageErrorsWithCreateCommandAndNoArgs() throws Throwable public void shouldGetUsageErrorsWithCreateCommandAndNoArgs() throws Throwable
{ {
// When running 'create' with arguments, expect usage errors // When running 'create' with arguments, expect usage errors
assertFailedUserCommand( "create", assertFailedUserCommand( "create", new String[0],
"Missing arguments: 'users create' expects username and password arguments", "Missing arguments: 'users create' expects username and password arguments",
"neo4j-admin users <subcommand> [<username>] [<password>]", "neo4j-admin users <subcommand> [<username>] [<password>]",
"Runs several possible sub-commands for managing the native users" ); "Runs several possible sub-commands for managing the native users" );
Expand All @@ -195,7 +187,7 @@ public void shouldCreateMissingUserWithoutPasswordChangeRequired() throws Throwa
// Given no previously existing user // Given no previously existing user


// When running 'create' with correct parameters, expect success // When running 'create' with correct parameters, expect success
assertSuccessfulSubCommand( "create", args("another", "abc"), "Created new user 'another'" ); assertSuccessfulUserCommand( "create", args("another", "abc"), "Created new user 'another'" );


// And the user requires password change // And the user requires password change
assertUserRequiresPasswordChange( "another" ); assertUserRequiresPasswordChange( "another" );
Expand All @@ -207,7 +199,7 @@ public void shouldCreateMissingUserWithPasswordChangeRequired() throws Throwable
// Given no previously existing user // Given no previously existing user


// When running 'create' with correct parameters, expect success // When running 'create' with correct parameters, expect success
assertSuccessfulSubCommand( "create", args("another", "abc", "--requires-password-change=true"), "Created new user 'another'" ); assertSuccessfulUserCommand( "create", args("another", "abc", "--requires-password-change=true"), "Created new user 'another'" );


// And the user requires password change // And the user requires password change
assertUserRequiresPasswordChange( "another" ); assertUserRequiresPasswordChange( "another" );
Expand All @@ -219,7 +211,7 @@ public void shouldCreateMissingUserWithPasswordChangeRequiredFalse() throws Thro
// Given no previously existing user // Given no previously existing user


// When running 'create' with correct parameters, expect success // When running 'create' with correct parameters, expect success
assertSuccessfulSubCommand( "create", args("another", "abc", "--requires-password-change=false"), "Created new user 'another'" ); assertSuccessfulUserCommand( "create", args("another", "abc", "--requires-password-change=false"), "Created new user 'another'" );


// And the user requires password change // And the user requires password change
assertUserDoesNotRequirePasswordChange( "another" ); assertUserDoesNotRequirePasswordChange( "another" );
Expand All @@ -231,7 +223,7 @@ public void shouldNotCreateDefaultUser() throws Throwable
// Given default state (only default user) // Given default state (only default user)


// When running 'create' with correct parameters, expect error // When running 'create' with correct parameters, expect error
assertFailedSubCommand( "create", args("neo4j", "abc"), "The specified user 'neo4j' already exists" ); assertFailedUserCommand( "create", args("neo4j", "abc"), "The specified user 'neo4j' already exists" );
} }


@Test @Test
Expand All @@ -242,7 +234,7 @@ public void shouldNotCreateExistingUser() throws Throwable
assertUserRequiresPasswordChange( "another" ); assertUserRequiresPasswordChange( "another" );


// When running 'create' with correct parameters, expect correct output // When running 'create' with correct parameters, expect correct output
assertFailedSubCommand( "create", args("another", "abc"), "The specified user 'another' already exists" ); assertFailedUserCommand( "create", args("another", "abc"), "The specified user 'another' already exists" );


// And the user still requires password change // And the user still requires password change
assertUserRequiresPasswordChange( "another" ); assertUserRequiresPasswordChange( "another" );
Expand All @@ -256,7 +248,7 @@ public void shouldNotCreateExistingUser() throws Throwable
public void shouldGetUsageErrorsWithDeleteCommandAndNoArgs() throws Throwable public void shouldGetUsageErrorsWithDeleteCommandAndNoArgs() throws Throwable
{ {
// When running 'create' with arguments, expect usage errors // When running 'create' with arguments, expect usage errors
assertFailedUserCommand( "delete", assertFailedUserCommand( "delete", new String[0],
"Missing arguments: 'users delete' expects username argument", "Missing arguments: 'users delete' expects username argument",
"neo4j-admin users <subcommand> [<username>] [<password>]", "neo4j-admin users <subcommand> [<username>] [<password>]",
"Runs several possible sub-commands for managing the native users" ); "Runs several possible sub-commands for managing the native users" );
Expand All @@ -268,7 +260,7 @@ public void shouldNotDeleteMissingUser() throws Throwable
// Given no previously existing user // Given no previously existing user


// When running 'delete' with correct parameters, expect error // When running 'delete' with correct parameters, expect error
assertFailedSubCommand( "delete", args("another"), "User 'another' does not exist" ); assertFailedUserCommand( "delete", args("another"), "User 'another' does not exist" );
} }


@Test @Test
Expand All @@ -277,7 +269,7 @@ public void shouldNotDeleteDefaultUserIfNoOtherUserExists() throws Throwable
// Given default state (only default user) // Given default state (only default user)


// When running 'delete' with correct parameters, expect error // When running 'delete' with correct parameters, expect error
assertFailedSubCommand( "delete", args("neo4j"), "Deleting the only remaining user 'neo4j' is not allowed" ); assertFailedUserCommand( "delete", args("neo4j"), "Deleting the only remaining user 'neo4j' is not allowed" );
} }


@Test @Test
Expand All @@ -287,7 +279,7 @@ public void shouldDeleteDefaultUserIfAnotherUserExists() throws Throwable
createTestUser( "another", "neo4j" ); createTestUser( "another", "neo4j" );


// When running 'delete' with correct parameters, expect success // When running 'delete' with correct parameters, expect success
assertSuccessfulSubCommand( "delete", args("neo4j"), "Deleted user 'neo4j'" ); assertSuccessfulUserCommand( "delete", args("neo4j"), "Deleted user 'neo4j'" );
} }


@Test @Test
Expand All @@ -297,10 +289,10 @@ public void shouldNotDeleteExistingUserIfNoOtherUserExists() throws Throwable
createTestUser( "another", "neo4j" ); createTestUser( "another", "neo4j" );


// When running 'delete' with correct parameters, expect success // When running 'delete' with correct parameters, expect success
assertSuccessfulSubCommand( "delete", args("neo4j"), "Deleted user 'neo4j'" ); assertSuccessfulUserCommand( "delete", args("neo4j"), "Deleted user 'neo4j'" );


// When running 'delete' with correct parameters, expect error // When running 'delete' with correct parameters, expect error
assertFailedSubCommand( "delete", args("another"), "Deleting the only remaining user 'another' is not allowed" ); assertFailedUserCommand( "delete", args("another"), "Deleting the only remaining user 'another' is not allowed" );
} }


@Test @Test
Expand All @@ -310,32 +302,20 @@ public void shouldDeleteExistingUser() throws Throwable
createTestUser( "another", "neo4j" ); createTestUser( "another", "neo4j" );


// When running 'create' with correct parameters, expect correct output // When running 'create' with correct parameters, expect correct output
assertSuccessfulSubCommand( "delete", args("another"), "Deleted user 'another'" ); assertSuccessfulUserCommand( "delete", args("another"), "Deleted user 'another'" );
} }


// //
// Utilities for testing AdminTool // Utilities for testing AdminTool
// //


private void assertFailedUserCommand( String command, String... errors ) private void assertSuccessfulUserCommand( String subCommand, String[] args, String... messages )
{
assertSuccessfulSubCommand( "users", subCommand, args, messages );
}

private void assertFailedUserCommand( String subCommand, String[] args, String... messages )
{ {
// When running users command without a command or with incorrect command assertFailedSubCommand( "users", subCommand, args, messages );
AdminTool tool = new AdminTool( CommandLocator.fromServiceLocator(), out, true );
if ( command == null )
{
tool.execute( graphDir.toPath(), confDir.toPath(), "users" );
}
else
{
tool.execute( graphDir.toPath(), confDir.toPath(), "users", command );
}

// Then we get the expected error
for ( String error : errors )
{
verify( out ).stdErrLine( contains( error ) );
}
verify( out, times( 0 ) ).stdOutLine( anyString() );
verify( out ).exit( 1 );
} }
} }
Expand Up @@ -29,12 +29,6 @@ class UsersCommandTestBase extends CommandTestBase
{ {
protected static String password_change_required = "password_change_required"; protected static String password_change_required = "password_change_required";


@Override
protected String command()
{
return "users";
}

void assertUserRequiresPasswordChange( String username ) throws Throwable void assertUserRequiresPasswordChange( String username ) throws Throwable
{ {
User user = getUser( username ); User user = getUser( username );
Expand Down

0 comments on commit 01e68cf

Please sign in to comment.