|
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2016 "Neo Technology," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.neo4j.driver.v1.tck; |
| 20 | + |
| 21 | + |
| 22 | +import cucumber.api.java.After; |
| 23 | +import cucumber.api.java.Before; |
| 24 | +import cucumber.api.java.en.And; |
| 25 | +import cucumber.api.java.en.Given; |
| 26 | +import cucumber.api.java.en.Then; |
| 27 | +import cucumber.runtime.CucumberException; |
| 28 | + |
| 29 | +import java.io.File; |
| 30 | + |
| 31 | +import org.neo4j.driver.internal.auth.InternalAuthToken; |
| 32 | +import org.neo4j.driver.v1.Driver; |
| 33 | +import org.neo4j.driver.v1.GraphDatabase; |
| 34 | +import org.neo4j.driver.v1.Session; |
| 35 | +import org.neo4j.driver.v1.exceptions.ClientException; |
| 36 | +import org.neo4j.driver.v1.util.Neo4jSettings; |
| 37 | + |
| 38 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 39 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 40 | +import static org.neo4j.driver.v1.Values.ofValue; |
| 41 | +import static org.neo4j.driver.v1.Values.parameters; |
| 42 | +import static org.neo4j.driver.v1.tck.DriverComplianceIT.neo4j; |
| 43 | + |
| 44 | +public class DriverAuthSteps |
| 45 | +{ |
| 46 | + |
| 47 | + Driver driver = null; |
| 48 | + File tempFile = null; |
| 49 | + |
| 50 | + @Before( "@auth" ) |
| 51 | + public void setUp() |
| 52 | + { |
| 53 | + tempFile = new File( "auth" ); |
| 54 | + } |
| 55 | + |
| 56 | + @After( "@auth" ) |
| 57 | + public void reset() |
| 58 | + { |
| 59 | + |
| 60 | + try |
| 61 | + { |
| 62 | + driver.close(); |
| 63 | + neo4j.restartServerOnEmptyDatabase( Neo4jSettings.DEFAULT ); |
| 64 | + tempFile.delete(); |
| 65 | + } |
| 66 | + catch ( Exception e ) |
| 67 | + { |
| 68 | + e.printStackTrace(); |
| 69 | + throw new CucumberException( "Failed to reset database" ); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Given( "^a driver is configured with auth enabled and correct password is provided$" ) |
| 74 | + public void aDriverIsConfiguredWithAuthEnabledAndCorrectPasswordIsProvided() throws Throwable |
| 75 | + { |
| 76 | + driver = configureCredentials( "neo4j", "neo4j", "password" ); |
| 77 | + } |
| 78 | + |
| 79 | + @Then( "^reading and writing to the database should be possible$" ) |
| 80 | + public void readingAndWritingToTheDatabaseShouldBePossible() throws Throwable |
| 81 | + { |
| 82 | + Session session = driver.session(); |
| 83 | +// session.run( "CREATE (:label1)" ).consume(); |
| 84 | +// session.run( "MATCH (n:label1) RETURN n" ).single(); |
| 85 | + session.run( "RETURN 1" ); |
| 86 | + session.close(); |
| 87 | + } |
| 88 | + |
| 89 | + @Given( "^a driver is configured with auth enabled and the wrong password is provided$" ) |
| 90 | + public void aDriverIsConfiguredWithAuthEnabledAndTheWrongPasswordIsProvided() throws Throwable |
| 91 | + { |
| 92 | + driver = configureCredentials( "neo4j", "neo4j", "password" ); |
| 93 | + driver.close(); |
| 94 | + driver = GraphDatabase.driver( neo4j.address(), new InternalAuthToken( |
| 95 | + parameters( |
| 96 | + "scheme", "basic", |
| 97 | + "principal", "neo4j", |
| 98 | + "credentials", "wrong" ).asMap( ofValue() ) ) ); |
| 99 | + } |
| 100 | + |
| 101 | + @Then( "^reading and writing to the database should not be possible$" ) |
| 102 | + public void readingAndWritingToTheDatabaseShouldNotBePossible() throws Throwable |
| 103 | + { |
| 104 | + try(Session session = driver.session()) |
| 105 | + { |
| 106 | + session.run( "CREATE (:label1)" ).consume(); |
| 107 | + } |
| 108 | + catch ( ClientException e ) |
| 109 | + { |
| 110 | + assertThat(e.getMessage().startsWith( "The client is unauthorized due to authentication failure" ), |
| 111 | + equalTo(true)); |
| 112 | + return; |
| 113 | + } |
| 114 | + throw new RuntimeException( "Exception should have been thrown" ); |
| 115 | + } |
| 116 | + |
| 117 | + @And( "^a `Protocol Error` is raised$" ) |
| 118 | + public void aProtocolErrorIsRaised() throws Throwable |
| 119 | + {} |
| 120 | + |
| 121 | + private Driver configureCredentials( String name, String oldPassword, String newPassword ) throws Exception |
| 122 | + { |
| 123 | + neo4j.restartServerOnEmptyDatabase( Neo4jSettings.DEFAULT |
| 124 | + .updateWith( Neo4jSettings.AUTH_ENABLED, "true" ) |
| 125 | + .updateWith( Neo4jSettings.AUTH_FILE, tempFile.getAbsolutePath() ) ); |
| 126 | + |
| 127 | + Driver driver = GraphDatabase.driver( neo4j.address(), new InternalAuthToken( |
| 128 | + parameters( |
| 129 | + "scheme", "basic", |
| 130 | + "principal", name, |
| 131 | + "credentials", oldPassword, |
| 132 | + "new_credentials", newPassword ).asMap( ofValue() ) ) ); |
| 133 | + try(Session session = driver.session()) |
| 134 | + { |
| 135 | + session.run( "RETURN 1" ); |
| 136 | + } |
| 137 | + return driver; |
| 138 | + } |
| 139 | +} |
0 commit comments