Skip to content

Commit ce6c445

Browse files
committed
Fixes from code review
1 parent ba172e3 commit ce6c445

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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;
20+
21+
import org.junit.Test;
22+
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
import org.neo4j.driver.internal.security.InternalAuthToken;
27+
import org.neo4j.driver.internal.value.ListValue;
28+
import org.neo4j.driver.internal.value.MapValue;
29+
import org.neo4j.driver.internal.value.StringValue;
30+
31+
import static java.util.Arrays.asList;
32+
import static org.hamcrest.core.IsEqual.equalTo;
33+
import static org.junit.Assert.assertThat;
34+
import static org.neo4j.driver.v1.AuthTokens.basic;
35+
import static org.neo4j.driver.v1.AuthTokens.custom;
36+
import static org.neo4j.driver.v1.Values.values;
37+
38+
public class AuthTokensTest
39+
{
40+
41+
@Test
42+
public void basicAuthWithoutRealm()
43+
{
44+
InternalAuthToken basic = (InternalAuthToken) basic( "foo", "bar" );
45+
46+
Map<String,Value> map = basic.toMap();
47+
48+
assertThat( map.size(), equalTo( 3 ) );
49+
assertThat( map.get( "scheme" ), equalTo( (Value) new StringValue( "basic" ) ) );
50+
assertThat( map.get( "principal" ), equalTo( (Value) new StringValue( "foo" ) ) );
51+
assertThat( map.get( "credentials" ), equalTo( (Value) new StringValue( "bar" ) ) );
52+
}
53+
54+
@Test
55+
public void basicAuthWithRealm()
56+
{
57+
InternalAuthToken basic = (InternalAuthToken) basic( "foo", "bar", "baz" );
58+
59+
Map<String,Value> map = basic.toMap();
60+
61+
assertThat( map.size(), equalTo( 4 ) );
62+
assertThat( map.get( "scheme" ), equalTo( (Value) new StringValue( "basic" ) ) );
63+
assertThat( map.get( "principal" ), equalTo( (Value) new StringValue( "foo" ) ) );
64+
assertThat( map.get( "credentials" ), equalTo( (Value) new StringValue( "bar" ) ) );
65+
assertThat( map.get( "realm" ), equalTo( (Value) new StringValue( "baz" ) ) );
66+
}
67+
68+
@Test
69+
public void customAuthWithoutParameters()
70+
{
71+
InternalAuthToken basic = (InternalAuthToken) custom( "foo", "bar", "baz", "my_scheme" );
72+
73+
Map<String,Value> map = basic.toMap();
74+
75+
assertThat( map.size(), equalTo( 4 ) );
76+
assertThat( map.get( "scheme" ), equalTo( (Value) new StringValue( "my_scheme" ) ) );
77+
assertThat( map.get( "principal" ), equalTo( (Value) new StringValue( "foo" ) ) );
78+
assertThat( map.get( "credentials" ), equalTo( (Value) new StringValue( "bar" ) ) );
79+
assertThat( map.get( "realm" ), equalTo( (Value) new StringValue( "baz" ) ) );
80+
}
81+
82+
@Test
83+
public void customAuthParameters()
84+
{
85+
HashMap<String,Object> parameters = new HashMap<>();
86+
parameters.put( "list", asList( 1, 2, 3 ) );
87+
InternalAuthToken basic = (InternalAuthToken) custom( "foo", "bar", "baz", "my_scheme", parameters );
88+
89+
90+
Map<String,Value> expectedParameters = new HashMap<>();
91+
expectedParameters.put( "list", new ListValue( values( 1, 2, 3 ) ) );
92+
Map<String,Value> map = basic.toMap();
93+
94+
assertThat( map.size(), equalTo( 5 ) );
95+
assertThat( map.get( "scheme" ), equalTo( (Value) new StringValue( "my_scheme" ) ) );
96+
assertThat( map.get( "principal" ), equalTo( (Value) new StringValue( "foo" ) ) );
97+
assertThat( map.get( "credentials" ), equalTo( (Value) new StringValue( "bar" ) ) );
98+
assertThat( map.get( "realm" ), equalTo( (Value) new StringValue( "baz" ) ) );
99+
assertThat( map.get( "parameters" ), equalTo( (Value) new MapValue( expectedParameters ) ) );
100+
}
101+
}

driver/src/test/java/org/neo4j/driver/v1/integration/CredentialsIT.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ public void shouldGetHelpfulErrorOnInvalidCredentials() throws Throwable
8888
}
8989
}
9090

91+
@Test
92+
public void shouldBeAbleToProvideRealmWithBasicAuth() throws Throwable
93+
{
94+
// Given
95+
String password = "secret";
96+
enableAuth( password );
97+
98+
// When & Then
99+
try( Driver driver = GraphDatabase.driver( neo4j.uri(),
100+
basic("neo4j", password, "native") );
101+
Session session = driver.session() )
102+
{
103+
Value single = session.run( "CREATE () RETURN 1" ).single().get( 0 );
104+
assertThat( single.asLong(), equalTo( 1L ) );
105+
}
106+
}
107+
91108
@Test
92109
public void shouldBeAbleToConnectWithCustomToken() throws Throwable
93110
{

0 commit comments

Comments
 (0)