1313
1414namespace Laudis \Neo4j \Tests \Unit ;
1515
16+ use Bolt \enum \Message ;
17+ use Bolt \enum \Signature ;
18+ use Bolt \protocol \Response ;
19+ use Bolt \protocol \V4_4 ;
20+ use Bolt \protocol \V5_1 ;
1621use Laudis \Neo4j \Authentication \NoAuth ;
22+ use Laudis \Neo4j \Bolt \BoltConnection ;
1723use Laudis \Neo4j \Common \Neo4jLogger ;
24+ use Laudis \Neo4j \Databags \Neo4jError ;
25+ use Laudis \Neo4j \Enum \ConnectionProtocol ;
26+ use Laudis \Neo4j \Exception \Neo4jException ;
1827use PHPUnit \Framework \TestCase ;
1928use Psr \Http \Message \UriInterface ;
2029
@@ -27,6 +36,74 @@ protected function setUp(): void
2736 $ logger = $ this ->createMock (Neo4jLogger::class);
2837 $ this ->auth = new NoAuth ($ logger );
2938 }
39+ public function testAuthenticateBoltSuccessV5 (): void
40+ {
41+ $ userAgent = 'neo4j-client/1.0 ' ;
42+
43+ $ mockProtocol = $ this ->createMock (V5_1 ::class);
44+ $ mockProtocol ->method ('hello ' );
45+ $ mockProtocol ->method ('logon ' );
46+ $ mockProtocol ->method ('getResponse ' )->willReturn (new Response (
47+ Message::HELLO ,
48+ Signature::SUCCESS ,
49+ ['server ' => 'neo4j-server ' , 'connection_id ' => '12345 ' , 'hints ' => []]
50+ ));
51+
52+ $ mockConnection = $ this ->createMock (BoltConnection::class);
53+ $ mockConnection ->method ('protocol ' )->willReturn ($ mockProtocol );
54+ $ mockConnection ->method ('getProtocol ' )->willReturn (ConnectionProtocol::BOLT_V5_1 ());
55+
56+ $ result = $ this ->auth ->authenticateBolt ($ mockConnection , $ userAgent );
57+ $ this ->assertArrayHasKey ('server ' , $ result );
58+ $ this ->assertSame ('neo4j-server ' , $ result ['server ' ]);
59+ $ this ->assertSame ('12345 ' , $ result ['connection_id ' ]);
60+ }
61+
62+ public function testAuthenticateBoltFailureV5 (): void
63+ {
64+ $ this ->expectException (Neo4jException::class);
65+
66+ $ mockProtocol = $ this ->createMock (V5_1 ::class);
67+ $ mockProtocol ->method ('hello ' );
68+ $ mockProtocol ->method ('logon ' );
69+ $ mockProtocol ->method ('getResponse ' )->willReturn (new Response (
70+ Message::HELLO ,
71+ Signature::FAILURE ,
72+ ['code ' => 'Neo.ClientError.Security.Unauthorized ' , 'message ' => 'Invalid credentials ' ]
73+ ));
74+
75+ $ mockConnection = $ this ->createMock (BoltConnection::class);
76+ $ mockConnection ->method ('protocol ' )->willReturn ($ mockProtocol );
77+ $ mockConnection ->method ('getProtocol ' )->willReturn (ConnectionProtocol::BOLT_V5_1 ());
78+
79+ $ error = Neo4jError::fromMessageAndCode ('Neo.ClientError.Security.Unauthorized ' , 'Invalid credentials ' );
80+ $ exception = new Neo4jException ([$ error ]);
81+ $ mockConnection ->method ('assertNoFailure ' )->will ($ this ->throwException ($ exception ));
82+
83+ $ this ->auth ->authenticateBolt ($ mockConnection , 'neo4j-client/1.0 ' );
84+ }
85+
86+ public function testAuthenticateBoltSuccessV4 (): void
87+ {
88+ $ userAgent = 'neo4j-client/1.0 ' ;
89+
90+ $ mockProtocol = $ this ->createMock (V4_4 ::class);
91+ $ mockProtocol ->method ('hello ' );
92+ $ mockProtocol ->method ('getResponse ' )->willReturn (new Response (
93+ Message::HELLO ,
94+ Signature::SUCCESS ,
95+ ['server ' => 'neo4j-server ' , 'connection_id ' => '12345 ' , 'hints ' => []]
96+ ));
97+
98+ $ mockConnection = $ this ->createMock (BoltConnection::class);
99+ $ mockConnection ->method ('protocol ' )->willReturn ($ mockProtocol );
100+ $ mockConnection ->method ('getProtocol ' )->willReturn (ConnectionProtocol::BOLT_V44 ());
101+
102+ $ result = $ this ->auth ->authenticateBolt ($ mockConnection , $ userAgent );
103+ $ this ->assertArrayHasKey ('server ' , $ result );
104+ $ this ->assertSame ('neo4j-server ' , $ result ['server ' ]);
105+ $ this ->assertSame ('12345 ' , $ result ['connection_id ' ]);
106+ }
30107
31108 public function testToString (): void
32109 {
0 commit comments