18
18
*/
19
19
package org .neo4j .driver .v1 .integration ;
20
20
21
- import org .junit .Rule ;
21
+ import org .junit .BeforeClass ;
22
+ import org .junit .ClassRule ;
22
23
import org .junit .Test ;
23
- import org .junit .rules .ExpectedException ;
24
24
import org .junit .rules .TemporaryFolder ;
25
25
26
26
import java .util .HashMap ;
34
34
import org .neo4j .driver .v1 .util .Neo4jSettings ;
35
35
import org .neo4j .driver .v1 .util .TestNeo4j ;
36
36
37
+ import static junit .framework .TestCase .fail ;
37
38
import static org .hamcrest .MatcherAssert .assertThat ;
39
+ import static org .hamcrest .Matchers .containsString ;
38
40
import static org .hamcrest .Matchers .equalTo ;
41
+ import static org .hamcrest .Matchers .instanceOf ;
39
42
import static org .neo4j .driver .v1 .AuthTokens .basic ;
40
43
import static org .neo4j .driver .v1 .AuthTokens .custom ;
41
44
import static org .neo4j .driver .v1 .Values .ofValue ;
42
45
import static org .neo4j .driver .v1 .Values .parameters ;
43
46
44
47
public class CredentialsIT
45
48
{
46
- @ Rule
47
- public TemporaryFolder tempDir = new TemporaryFolder ();
49
+ @ ClassRule
50
+ public static TemporaryFolder tempDir = new TemporaryFolder ();
48
51
49
- @ Rule
50
- public TestNeo4j neo4j = new TestNeo4j ();
52
+ @ ClassRule
53
+ public static TestNeo4j neo4j = new TestNeo4j ();
51
54
52
- @ Rule
53
- public ExpectedException exception = ExpectedException .none ();
55
+ private static String password = "secret" ;
54
56
55
57
@ Test
56
58
public void basicCredentialsShouldWork () throws Throwable
57
59
{
58
- // Given
59
- String password = "secret" ;
60
- enableAuth ( password );
61
-
62
60
// When & Then
63
61
try ( Driver driver = GraphDatabase .driver ( neo4j .uri (),
64
62
basic ("neo4j" , password ) );
@@ -73,28 +71,22 @@ public void basicCredentialsShouldWork() throws Throwable
73
71
@ Test
74
72
public void shouldGetHelpfulErrorOnInvalidCredentials () throws Throwable
75
73
{
76
- // Given
77
- String password = "secret" ;
78
- enableAuth ( password );
79
-
80
- // Expect
81
- exception .expect ( ClientException .class );
82
- exception .expectMessage ( "The client is unauthorized due to authentication failure." );
83
-
84
74
// When
85
75
try ( Driver driver = GraphDatabase .driver ( neo4j .uri (), basic ("thisisnotthepassword" , password ) );
86
76
Session ignored = driver .session () ) {
87
77
//empty
78
+ fail ( "Should fail with an auth error already" );
79
+ }
80
+ catch ( Throwable e )
81
+ {
82
+ assertThat ( e , instanceOf ( ClientException .class ) );
83
+ assertThat ( e .getMessage (), containsString ( "The client is unauthorized due to authentication failure." ) );
88
84
}
89
85
}
90
86
91
87
@ Test
92
88
public void shouldBeAbleToProvideRealmWithBasicAuth () throws Throwable
93
89
{
94
- // Given
95
- String password = "secret" ;
96
- enableAuth ( password );
97
-
98
90
// When & Then
99
91
try ( Driver driver = GraphDatabase .driver ( neo4j .uri (),
100
92
basic ("neo4j" , password , "native" ) );
@@ -108,10 +100,6 @@ public void shouldBeAbleToProvideRealmWithBasicAuth() throws Throwable
108
100
@ Test
109
101
public void shouldBeAbleToConnectWithCustomToken () throws Throwable
110
102
{
111
- // Given
112
- String password = "secret" ;
113
- enableAuth ( password );
114
-
115
103
// When & Then
116
104
try ( Driver driver = GraphDatabase .driver ( neo4j .uri (),
117
105
custom ("neo4j" , password , "native" , "basic" ) );
@@ -125,9 +113,6 @@ public void shouldBeAbleToConnectWithCustomToken() throws Throwable
125
113
@ Test
126
114
public void shouldBeAbleToConnectWithCustomTokenWithAdditionalParameters () throws Throwable
127
115
{
128
- // Given
129
- String password = "secret" ;
130
- enableAuth ( password );
131
116
HashMap <String ,Object > parameters = new HashMap <>();
132
117
parameters .put ( "secret" , 16 );
133
118
@@ -141,7 +126,8 @@ public void shouldBeAbleToConnectWithCustomTokenWithAdditionalParameters() throw
141
126
}
142
127
}
143
128
144
- private void enableAuth ( String password ) throws Exception
129
+ @ BeforeClass
130
+ public static void enableAuth () throws Exception
145
131
{
146
132
neo4j .restart ( Neo4jSettings .TEST_SETTINGS
147
133
.updateWith ( Neo4jSettings .AUTH_ENABLED , "true" )
0 commit comments