Skip to content

Commit

Permalink
Keep request mock reachable in UserServiceTest.
Browse files Browse the repository at this point in the history
  • Loading branch information
MishaDemianenko committed May 9, 2019
1 parent 269c8d3 commit ef0ec4f
Showing 1 changed file with 31 additions and 43 deletions.
Expand Up @@ -41,8 +41,8 @@
import org.neo4j.server.rest.repr.formats.JsonFormat; import org.neo4j.server.rest.repr.formats.JsonFormat;
import org.neo4j.server.security.auth.AuthenticationStrategy; import org.neo4j.server.security.auth.AuthenticationStrategy;
import org.neo4j.server.security.auth.BasicAuthManager; import org.neo4j.server.security.auth.BasicAuthManager;
import org.neo4j.server.security.auth.BasicPasswordPolicy;
import org.neo4j.server.security.auth.BasicLoginContext; import org.neo4j.server.security.auth.BasicLoginContext;
import org.neo4j.server.security.auth.BasicPasswordPolicy;
import org.neo4j.server.security.auth.InMemoryUserRepository; import org.neo4j.server.security.auth.InMemoryUserRepository;
import org.neo4j.server.security.auth.UserRepository; import org.neo4j.server.security.auth.UserRepository;
import org.neo4j.test.server.EntityOutputFormat; import org.neo4j.test.server.EntityOutputFormat;
Expand All @@ -66,19 +66,20 @@ public class UserServiceTest
protected UserManagerSupplier userManagerSupplier; protected UserManagerSupplier userManagerSupplier;
protected LoginContext neo4jContext; protected LoginContext neo4jContext;
protected Principal neo4jPrinciple; protected Principal neo4jPrinciple;
private HttpServletRequest request;


protected void setupAuthManagerAndSubject() protected void setupAuthManagerAndSubject()
{ {
BasicAuthManager basicAuthManager = new BasicAuthManager( userRepository, passwordPolicy,
mock( AuthenticationStrategy.class), new InMemoryUserRepository() );


userManagerSupplier = basicAuthManager; userManagerSupplier = new BasicAuthManager( userRepository, passwordPolicy,
mock( AuthenticationStrategy.class), new InMemoryUserRepository() );
neo4jContext = new BasicLoginContext( NEO4J_USER, AuthenticationResult.SUCCESS ); neo4jContext = new BasicLoginContext( NEO4J_USER, AuthenticationResult.SUCCESS );
} }


@Before @Before
public void setUp() throws InvalidArgumentsException, IOException public void setUp() throws InvalidArgumentsException, IOException
{ {
request = mock( HttpServletRequest.class );
userRepository.create( NEO4J_USER ); userRepository.create( NEO4J_USER );
setupAuthManagerAndSubject(); setupAuthManagerAndSubject();
neo4jPrinciple = new DelegatingPrincipal( "neo4j", neo4jContext ); neo4jPrinciple = new DelegatingPrincipal( "neo4j", neo4jContext );
Expand All @@ -94,14 +95,13 @@ public void tearDown() throws IOException
public void shouldReturnValidUserRepresentation() throws Exception public void shouldReturnValidUserRepresentation() throws Exception
{ {
// Given // Given
HttpServletRequest req = mock( HttpServletRequest.class ); when( request.getUserPrincipal() ).thenReturn( neo4jPrinciple );
when( req.getUserPrincipal() ).thenReturn( neo4jPrinciple );


OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null ); OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null );
UserService userService = new UserService( userManagerSupplier, new JsonFormat(), outputFormat ); UserService userService = new UserService( userManagerSupplier, new JsonFormat(), outputFormat );


// When // When
Response response = userService.getUser( "neo4j", req ); Response response = userService.getUser( "neo4j", request );


// Then // Then
assertThat( response.getStatus(), equalTo( 200 ) ); assertThat( response.getStatus(), equalTo( 200 ) );
Expand All @@ -116,14 +116,13 @@ public void shouldReturnValidUserRepresentation() throws Exception
public void shouldReturn404WhenRequestingUserIfNotAuthenticated() throws Exception public void shouldReturn404WhenRequestingUserIfNotAuthenticated() throws Exception
{ {
// Given // Given
HttpServletRequest req = mock( HttpServletRequest.class ); when( request.getUserPrincipal() ).thenReturn( null );
when( req.getUserPrincipal() ).thenReturn( null );


OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null ); OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null );
UserService userService = new UserService( userManagerSupplier, new JsonFormat(), outputFormat ); UserService userService = new UserService( userManagerSupplier, new JsonFormat(), outputFormat );


// When // When
Response response = userService.getUser( "neo4j", req ); Response response = userService.getUser( "neo4j", request );


// Then // Then
assertThat( response.getStatus(), equalTo( 404 ) ); assertThat( response.getStatus(), equalTo( 404 ) );
Expand All @@ -133,14 +132,13 @@ public void shouldReturn404WhenRequestingUserIfNotAuthenticated() throws Excepti
public void shouldReturn404WhenRequestingUserIfDifferentUser() throws Exception public void shouldReturn404WhenRequestingUserIfDifferentUser() throws Exception
{ {
// Given // Given
HttpServletRequest req = mock( HttpServletRequest.class ); when( request.getUserPrincipal() ).thenReturn( neo4jPrinciple );
when( req.getUserPrincipal() ).thenReturn( neo4jPrinciple );


OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null ); OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null );
UserService userService = new UserService( mock( BasicAuthManager.class ), new JsonFormat(), outputFormat ); UserService userService = new UserService( mock( BasicAuthManager.class ), new JsonFormat(), outputFormat );


// When // When
Response response = userService.getUser( "fred", req ); Response response = userService.getUser( "fred", request );


// Then // Then
assertThat( response.getStatus(), equalTo( 404 ) ); assertThat( response.getStatus(), equalTo( 404 ) );
Expand All @@ -150,16 +148,15 @@ public void shouldReturn404WhenRequestingUserIfDifferentUser() throws Exception
public void shouldReturn404WhenRequestingUserIfUnknownUser() throws Exception public void shouldReturn404WhenRequestingUserIfUnknownUser() throws Exception
{ {
// Given // Given
HttpServletRequest req = mock( HttpServletRequest.class ); when( request.getUserPrincipal() ).thenReturn( neo4jPrinciple );
when( req.getUserPrincipal() ).thenReturn( neo4jPrinciple );


userManagerSupplier.getUserManager().deleteUser( "neo4j" ); userManagerSupplier.getUserManager().deleteUser( "neo4j" );


OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null ); OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null );
UserService userService = new UserService( userManagerSupplier, new JsonFormat(), outputFormat ); UserService userService = new UserService( userManagerSupplier, new JsonFormat(), outputFormat );


// When // When
Response response = userService.getUser( "neo4j", req ); Response response = userService.getUser( "neo4j", request );


// Then // Then
assertThat( response.getStatus(), equalTo( 404 ) ); assertThat( response.getStatus(), equalTo( 404 ) );
Expand All @@ -169,14 +166,13 @@ public void shouldReturn404WhenRequestingUserIfUnknownUser() throws Exception
public void shouldChangePasswordAndReturnSuccess() throws Exception public void shouldChangePasswordAndReturnSuccess() throws Exception
{ {
// Given // Given
HttpServletRequest req = mock( HttpServletRequest.class ); when( request.getUserPrincipal() ).thenReturn( neo4jPrinciple );
when( req.getUserPrincipal() ).thenReturn( neo4jPrinciple );


OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null ); OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null );
UserService userService = new UserService( userManagerSupplier, new JsonFormat(), outputFormat ); UserService userService = new UserService( userManagerSupplier, new JsonFormat(), outputFormat );


// When // When
Response response = userService.setPassword( "neo4j", req, "{ \"password\" : \"test\" }" ); Response response = userService.setPassword( "neo4j", request, "{ \"password\" : \"test\" }" );


// Then // Then
assertThat( response.getStatus(), equalTo( 200 ) ); assertThat( response.getStatus(), equalTo( 200 ) );
Expand All @@ -187,14 +183,13 @@ public void shouldChangePasswordAndReturnSuccess() throws Exception
public void shouldReturn404WhenChangingPasswordIfNotAuthenticated() throws Exception public void shouldReturn404WhenChangingPasswordIfNotAuthenticated() throws Exception
{ {
// Given // Given
HttpServletRequest req = mock( HttpServletRequest.class ); when( request.getUserPrincipal() ).thenReturn( null );
when( req.getUserPrincipal() ).thenReturn( null );


OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null ); OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null );
UserService userService = new UserService( mock( BasicAuthManager.class ), new JsonFormat(), outputFormat ); UserService userService = new UserService( mock( BasicAuthManager.class ), new JsonFormat(), outputFormat );


// When // When
Response response = userService.setPassword( "neo4j", req, "{ \"password\" : \"test\" }" ); Response response = userService.setPassword( "neo4j", request, "{ \"password\" : \"test\" }" );


// Then // Then
assertThat( response.getStatus(), equalTo( 404 ) ); assertThat( response.getStatus(), equalTo( 404 ) );
Expand All @@ -204,16 +199,15 @@ public void shouldReturn404WhenChangingPasswordIfNotAuthenticated() throws Excep
public void shouldReturn404WhenChangingPasswordIfDifferentUser() throws Exception public void shouldReturn404WhenChangingPasswordIfDifferentUser() throws Exception
{ {
// Given // Given
HttpServletRequest req = mock( HttpServletRequest.class ); when( request.getUserPrincipal() ).thenReturn( neo4jPrinciple );
when( req.getUserPrincipal() ).thenReturn( neo4jPrinciple );


UserManager userManager = mock( UserManager.class ); UserManager userManager = mock( UserManager.class );


OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null ); OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null );
UserService userService = new UserService( userManagerSupplier, new JsonFormat(), outputFormat ); UserService userService = new UserService( userManagerSupplier, new JsonFormat(), outputFormat );


// When // When
Response response = userService.setPassword( "fred", req, "{ \"password\" : \"test\" }" ); Response response = userService.setPassword( "fred", request, "{ \"password\" : \"test\" }" );


// Then // Then
assertThat( response.getStatus(), equalTo( 404 ) ); assertThat( response.getStatus(), equalTo( 404 ) );
Expand All @@ -224,16 +218,15 @@ public void shouldReturn404WhenChangingPasswordIfDifferentUser() throws Exceptio
public void shouldReturn422WhenChangingPasswordIfUnknownUser() throws Exception public void shouldReturn422WhenChangingPasswordIfUnknownUser() throws Exception
{ {
// Given // Given
HttpServletRequest req = mock( HttpServletRequest.class ); when( request.getUserPrincipal() ).thenReturn( neo4jPrinciple );
when( req.getUserPrincipal() ).thenReturn( neo4jPrinciple );


OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null ); OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null );
UserService userService = new UserService( userManagerSupplier, new JsonFormat(), outputFormat ); UserService userService = new UserService( userManagerSupplier, new JsonFormat(), outputFormat );


userRepository.delete( NEO4J_USER ); userRepository.delete( NEO4J_USER );


// When // When
Response response = userService.setPassword( "neo4j", req, "{ \"password\" : \"test\" }" ); Response response = userService.setPassword( "neo4j", request, "{ \"password\" : \"test\" }" );


// Then // Then
assertThat( response.getStatus(), equalTo( 422 ) ); assertThat( response.getStatus(), equalTo( 422 ) );
Expand All @@ -243,14 +236,13 @@ public void shouldReturn422WhenChangingPasswordIfUnknownUser() throws Exception
public void shouldReturn400IfPayloadIsInvalid() throws Exception public void shouldReturn400IfPayloadIsInvalid() throws Exception
{ {
// Given // Given
HttpServletRequest req = mock( HttpServletRequest.class ); when( request.getUserPrincipal() ).thenReturn( neo4jPrinciple );
when( req.getUserPrincipal() ).thenReturn( neo4jPrinciple );


OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null ); OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null );
UserService userService = new UserService( mock( BasicAuthManager.class ), new JsonFormat(), outputFormat ); UserService userService = new UserService( mock( BasicAuthManager.class ), new JsonFormat(), outputFormat );


// When // When
Response response = userService.setPassword( "neo4j", req, "xxx" ); Response response = userService.setPassword( "neo4j", request, "xxx" );


// Then // Then
assertThat( response.getStatus(), equalTo( 400 ) ); assertThat( response.getStatus(), equalTo( 400 ) );
Expand All @@ -263,14 +255,13 @@ public void shouldReturn400IfPayloadIsInvalid() throws Exception
public void shouldReturn422IfMissingPassword() throws Exception public void shouldReturn422IfMissingPassword() throws Exception
{ {
// Given // Given
HttpServletRequest req = mock( HttpServletRequest.class ); when( request.getUserPrincipal() ).thenReturn( neo4jPrinciple );
when( req.getUserPrincipal() ).thenReturn( neo4jPrinciple );


OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null ); OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null );
UserService userService = new UserService( mock( BasicAuthManager.class ), new JsonFormat(), outputFormat ); UserService userService = new UserService( mock( BasicAuthManager.class ), new JsonFormat(), outputFormat );


// When // When
Response response = userService.setPassword( "neo4j", req, "{ \"unknown\" : \"unknown\" }" ); Response response = userService.setPassword( "neo4j", request, "{ \"unknown\" : \"unknown\" }" );


// Then // Then
assertThat( response.getStatus(), equalTo( 422 ) ); assertThat( response.getStatus(), equalTo( 422 ) );
Expand All @@ -284,14 +275,13 @@ public void shouldReturn422IfMissingPassword() throws Exception
public void shouldReturn422IfInvalidPasswordType() throws Exception public void shouldReturn422IfInvalidPasswordType() throws Exception
{ {
// Given // Given
HttpServletRequest req = mock( HttpServletRequest.class ); when( request.getUserPrincipal() ).thenReturn( neo4jPrinciple );
when( req.getUserPrincipal() ).thenReturn( neo4jPrinciple );


OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null ); OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null );
UserService userService = new UserService( mock( BasicAuthManager.class ), new JsonFormat(), outputFormat ); UserService userService = new UserService( mock( BasicAuthManager.class ), new JsonFormat(), outputFormat );


// When // When
Response response = userService.setPassword( "neo4j", req, "{ \"password\" : 1 }" ); Response response = userService.setPassword( "neo4j", request, "{ \"password\" : 1 }" );


// Then // Then
assertThat( response.getStatus(), equalTo( 422 ) ); assertThat( response.getStatus(), equalTo( 422 ) );
Expand All @@ -305,14 +295,13 @@ public void shouldReturn422IfInvalidPasswordType() throws Exception
public void shouldReturn422IfEmptyPassword() throws Exception public void shouldReturn422IfEmptyPassword() throws Exception
{ {
// Given // Given
HttpServletRequest req = mock( HttpServletRequest.class ); when( request.getUserPrincipal() ).thenReturn( neo4jPrinciple );
when( req.getUserPrincipal() ).thenReturn( neo4jPrinciple );


OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null ); OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null );
UserService userService = new UserService( userManagerSupplier, new JsonFormat(), outputFormat ); UserService userService = new UserService( userManagerSupplier, new JsonFormat(), outputFormat );


// When // When
Response response = userService.setPassword( "neo4j", req, "{ \"password\" : \"\" }" ); Response response = userService.setPassword( "neo4j", request, "{ \"password\" : \"\" }" );


// Then // Then
assertThat( response.getStatus(), equalTo( 422 ) ); assertThat( response.getStatus(), equalTo( 422 ) );
Expand All @@ -326,14 +315,13 @@ public void shouldReturn422IfEmptyPassword() throws Exception
public void shouldReturn422IfPasswordIdentical() throws Exception public void shouldReturn422IfPasswordIdentical() throws Exception
{ {
// Given // Given
HttpServletRequest req = mock( HttpServletRequest.class ); when( request.getUserPrincipal() ).thenReturn( neo4jPrinciple );
when( req.getUserPrincipal() ).thenReturn( neo4jPrinciple );


OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null ); OutputFormat outputFormat = new EntityOutputFormat( new JsonFormat(), new URI( "http://www.example.com" ), null );
UserService userService = new UserService( userManagerSupplier, new JsonFormat(), outputFormat ); UserService userService = new UserService( userManagerSupplier, new JsonFormat(), outputFormat );


// When // When
Response response = userService.setPassword( "neo4j", req, "{ \"password\" : \"neo4j\" }" ); Response response = userService.setPassword( "neo4j", request, "{ \"password\" : \"neo4j\" }" );


// Then // Then
assertThat( response.getStatus(), equalTo( 422 ) ); assertThat( response.getStatus(), equalTo( 422 ) );
Expand Down

0 comments on commit ef0ec4f

Please sign in to comment.