Skip to content

Commit

Permalink
Add testing of legacy Cypher HTTP endpoint
Browse files Browse the repository at this point in the history
This works very much like the transactional REST/HTTP endpoint, so it inherits
and uses most of the pre-existing functionality, with some minor alterations in
the JSON input and output.
  • Loading branch information
Mats-SX committed Oct 13, 2016
1 parent 90cc660 commit 8c5ec60
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 9 deletions.
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.server.rest.security;

import org.junit.Rule;

import java.util.Map;

import org.neo4j.server.security.enterprise.auth.AuthScenariosInteractionTestBase;
import org.neo4j.server.security.enterprise.auth.NeoInteractionLevel;
import org.neo4j.test.rule.SuppressOutput;

import static org.neo4j.test.rule.SuppressOutput.suppressAll;

public class CypherRESTAuthScenariosInteractionTest extends AuthScenariosInteractionTestBase<RESTSubject>
{
@Rule
public SuppressOutput suppressOutput = suppressAll();

public CypherRESTAuthScenariosInteractionTest()
{
super();
CHANGE_PWD_ERR_MSG = "User is required to change their password.";
PWD_CHANGE_CHECK_FIRST = true;
IS_EMBEDDED = false;
}

@Override
protected NeoInteractionLevel<RESTSubject> setUpNeoServer( Map<String,String> config ) throws Throwable
{
return new CypherRESTInteraction( config );
}
}
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.server.rest.security;

import org.junit.Rule;

import java.util.Map;

import org.neo4j.server.security.enterprise.auth.BuiltInProceduresInteractionTestBase;
import org.neo4j.server.security.enterprise.auth.NeoInteractionLevel;
import org.neo4j.test.rule.SuppressOutput;

import static org.neo4j.test.rule.SuppressOutput.suppressAll;

public class CypherRESTBuiltInProceduresInteractionTest extends BuiltInProceduresInteractionTestBase<RESTSubject>
{
@Rule
public SuppressOutput suppressOutput = suppressAll();

public CypherRESTBuiltInProceduresInteractionTest()
{
super();
CHANGE_PWD_ERR_MSG = "User is required to change their password.";
PWD_CHANGE_CHECK_FIRST = true;
IS_EMBEDDED = false;
}

@Override
public NeoInteractionLevel<RESTSubject> setUpNeoServer( Map<String, String> config ) throws Throwable
{
return new CypherRESTInteraction( config );
}
}
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.server.rest.security;

import org.codehaus.jackson.JsonNode;

import java.io.IOException;
import java.util.Map;
import java.util.function.Consumer;
import javax.ws.rs.core.HttpHeaders;

import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.server.rest.domain.JsonHelper;
import org.neo4j.server.rest.domain.JsonParseException;
import org.neo4j.test.server.HTTP;

import static org.junit.Assert.fail;
import static org.neo4j.test.server.HTTP.RawPayload.quotedJson;

class CypherRESTInteraction extends RESTInteraction
{

@Override
String commitPath()
{
return "db/data/cypher";
}

CypherRESTInteraction( Map<String,String> config ) throws IOException
{
super( config );
}

@Override
public String executeQuery( RESTSubject subject, String call, Map<String,Object> params,
Consumer<ResourceIterator<Map<String, Object>>> resultConsumer )
{
HTTP.RawPayload payload = constructQuery( call );
HTTP.Response response = HTTP.withHeaders( HttpHeaders.AUTHORIZATION, subject.principalCredentials )
.request( POST, commitURL(), payload );

try
{
String error = parseErrorMessage( response );
if ( !error.isEmpty() )
{
return error;
}
JsonNode data = JsonHelper.jsonNode( response.rawContent() );
if ( data.has( "data" ) && data.get( "data" ).has( 0 ) )
{
resultConsumer.accept( new CypherRESTResult( data ) );
}
}
catch ( JsonParseException e )
{
fail( "Unexpected error parsing Json!" );
}

return "";
}

private HTTP.RawPayload constructQuery( String query )
{
return quotedJson( " { 'query': '" + query.replace( "'", "\\'" ).replace( "\"", "\\\"" ) + "' }" );
}

@Override
protected HTTP.Response authenticate( String principalCredentials )
{
return HTTP.withHeaders( HttpHeaders.AUTHORIZATION, principalCredentials )
.request( POST, commitURL(), constructQuery( "RETURN 1" ) );
}

private class CypherRESTResult extends RESTResult
{
CypherRESTResult( JsonNode fullResult )
{
super( fullResult );
}

@Override
JsonNode getRow( JsonNode data, int i )
{
return data.get( i );
}
}
}
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.server.rest.security;

import org.junit.Rule;

import java.util.Map;

import org.neo4j.server.security.enterprise.auth.AuthProceduresInteractionTestBase;
import org.neo4j.server.security.enterprise.auth.NeoInteractionLevel;
import org.neo4j.test.rule.SuppressOutput;

import static org.neo4j.test.rule.SuppressOutput.suppressAll;

public class CypherRESTUserManagementProceduresInteractionTest extends AuthProceduresInteractionTestBase<RESTSubject>
{
@Rule
public SuppressOutput suppressOutput = suppressAll();

public CypherRESTUserManagementProceduresInteractionTest()
{
super();
CHANGE_PWD_ERR_MSG = "User is required to change their password.";
PWD_CHANGE_CHECK_FIRST = true;
IS_EMBEDDED = false;
}

@Override
public NeoInteractionLevel<RESTSubject> setUpNeoServer( Map<String,String> config ) throws Throwable
{
return new CypherRESTInteraction( config );
}
}
Expand Up @@ -64,10 +64,14 @@

class RESTInteraction extends CommunityServerTestBase implements NeoInteractionLevel<RESTSubject>
{
private String COMMIT_PATH = "db/data/transaction/commit";
private String POST = "POST";
String commitPath()
{
return "db/data/transaction/commit";
}

static final String POST = "POST";

EnterpriseAuthManager authManager;
private EnterpriseAuthManager authManager;

RESTInteraction( Map<String,String> config ) throws IOException
{
Expand Down Expand Up @@ -161,7 +165,7 @@ public RESTSubject login( String username, String password ) throws Exception
return new RESTSubject( username, password, principalCredentials );
}

private HTTP.Response authenticate( String principalCredentials )
protected HTTP.Response authenticate( String principalCredentials )
{
return HTTP.withHeaders( HttpHeaders.AUTHORIZATION, principalCredentials ).request( POST, commitURL() );
}
Expand Down Expand Up @@ -222,7 +226,7 @@ public String getConnectionDetails()
return "server-session";
}

private String parseErrorMessage( HTTP.Response response )
String parseErrorMessage( HTTP.Response response )
{
try
{
Expand All @@ -243,12 +247,12 @@ private String parseErrorMessage( HTTP.Response response )
return "";
}

private String commitURL()
String commitURL()
{
return server.baseUri().resolve( COMMIT_PATH ).toString();
return server.baseUri().resolve( commitPath() ).toString();
}

private class RESTResult implements ResourceIterator<Map<String,Object>>
class RESTResult implements ResourceIterator<Map<String,Object>>
{
private JsonNode data;
private JsonNode columns;
Expand All @@ -275,7 +279,7 @@ public boolean hasNext()
@Override
public Map<String,Object> next()
{
JsonNode row = data.get( index++ ).get( "row" );
JsonNode row = getRow( data, index++ );
TreeMap<String,Object> map = new TreeMap<>();
for ( int i = 0; i < columns.size(); i++ )
{
Expand All @@ -285,6 +289,11 @@ public Map<String,Object> next()
}
return map;
}

JsonNode getRow( JsonNode data, int i )
{
return data.get( i ).get( "row" );
}
}

private Object getValue( JsonNode valueNode )
Expand Down

0 comments on commit 8c5ec60

Please sign in to comment.