1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Tarantool Client package. |
5
|
|
|
* |
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Tarantool\Client\Tests\Integration\Requests; |
15
|
|
|
|
16
|
|
|
use Tarantool\Client\Exception\RequestFailed; |
17
|
|
|
use Tarantool\Client\Tests\Integration\ClientBuilder; |
18
|
|
|
use Tarantool\Client\Tests\Integration\TestCase; |
19
|
|
|
|
20
|
|
|
final class AuthenticateTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @doesNotPerformAssertions |
24
|
|
|
* @dataProvider provideValidCredentials |
25
|
|
|
* |
26
|
|
|
* @eval create_user('user_foo', 'foo') |
27
|
|
|
* @eval create_user('user_empty', '') |
28
|
|
|
* @eval create_user('user_big', '123456789012345678901234567890123456789012345678901234567890') |
29
|
|
|
*/ |
30
|
|
|
public function testAuthenticateWithValidCredentials(string $username, string $password) : void |
31
|
|
|
{ |
32
|
|
|
$client = ClientBuilder::createFromEnv()->setOptions([ |
33
|
|
|
'username' => $username, |
34
|
|
|
'password' => $password, |
35
|
|
|
])->build(); |
36
|
|
|
|
37
|
|
|
$client->ping(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function provideValidCredentials() : iterable |
41
|
|
|
{ |
42
|
|
|
return [ |
43
|
|
|
['guest', ''], |
44
|
|
|
['user_foo', 'foo'], |
45
|
|
|
['user_empty', ''], |
46
|
|
|
['user_big', '123456789012345678901234567890123456789012345678901234567890'], |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @dataProvider provideInvalidCredentials |
52
|
|
|
*/ |
53
|
|
|
public function testAuthenticateWithInvalidCredentials(string $errorMessage, int $errorCode, $username, $password) : void |
54
|
|
|
{ |
55
|
|
|
$client = ClientBuilder::createFromEnv()->setOptions([ |
56
|
|
|
'username' => $username, |
57
|
|
|
'password' => $password, |
58
|
|
|
])->build(); |
59
|
|
|
|
60
|
|
|
try { |
61
|
|
|
$client->ping(); |
62
|
|
|
self::fail(sprintf('Client must throw an exception on authenticating "%s" with the password "%s".', $username, $password)); |
63
|
|
|
} catch (RequestFailed $e) { |
64
|
|
|
self::assertSame($errorMessage, $e->getMessage()); |
65
|
|
|
self::assertSame($errorCode, $e->getCode()); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function provideInvalidCredentials() : iterable |
70
|
|
|
{ |
71
|
|
|
return [ |
72
|
|
|
["User 'non_existing_user' is not found", 45, 'non_existing_user', 'password'], |
73
|
|
|
["Incorrect password supplied for user 'guest'", 47, 'guest', 'password'], |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @eval create_user('user_foo', 'foo') |
79
|
|
|
* @eval create_space('test_auth_reconnect'):create_index('primary', {type = 'tree', parts = {1, 'unsigned'}}) |
80
|
|
|
*/ |
81
|
|
|
public function testUseCredentialsAfterReconnect() : void |
82
|
|
|
{ |
83
|
|
|
$client = ClientBuilder::createFromEnv()->setOptions([ |
84
|
|
|
'username' => 'user_foo', |
85
|
|
|
'password' => 'foo', |
86
|
|
|
])->build(); |
87
|
|
|
|
88
|
|
|
$client->getHandler()->getConnection()->close(); |
89
|
|
|
|
90
|
|
|
$this->expectException(RequestFailed::class); |
91
|
|
|
$this->expectExceptionMessage("Space 'test_auth_reconnect' does not exist"); |
92
|
|
|
|
93
|
|
|
$client->getSpace('test_auth_reconnect'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testAuthenticateOnceOnOpenedPersistentConnection() : void |
97
|
|
|
{ |
98
|
|
|
$total = self::getTotalCalls(self::STAT_REQUEST_AUTH); |
99
|
|
|
|
100
|
|
|
$client = ClientBuilder::createFromEnv() |
101
|
|
|
->setConnectionOptions(['persistent' => true]) |
102
|
|
|
->setOptions(['username' => 'guest']) |
103
|
|
|
->build(); |
104
|
|
|
|
105
|
|
|
// ensure that no persistent connection is opened |
106
|
|
|
$connection = $client->getHandler()->getConnection(); |
107
|
|
|
$connection->open(); |
108
|
|
|
$connection->close(); |
109
|
|
|
|
110
|
|
|
$client->ping(); |
111
|
|
|
$client->ping(); |
112
|
|
|
|
113
|
|
|
$client = ClientBuilder::createFromEnv() |
114
|
|
|
->setConnectionOptions(['persistent' => true]) |
115
|
|
|
->setOptions(['username' => 'guest']) |
116
|
|
|
->build(); |
117
|
|
|
|
118
|
|
|
$client->ping(); |
119
|
|
|
$client->ping(); |
120
|
|
|
|
121
|
|
|
$connection->close(); |
122
|
|
|
|
123
|
|
|
self::assertSame(1, self::getTotalCalls(self::STAT_REQUEST_AUTH) - $total); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testReauthenticateOnClosedPersistentConnection() : void |
127
|
|
|
{ |
128
|
|
|
$total = self::getTotalCalls(self::STAT_REQUEST_AUTH); |
129
|
|
|
|
130
|
|
|
$client = ClientBuilder::createFromEnv() |
131
|
|
|
->setConnectionOptions(['persistent' => true]) |
132
|
|
|
->setOptions(['username' => 'guest']) |
133
|
|
|
->build(); |
134
|
|
|
|
135
|
|
|
// ensure that no persistent connection is opened |
136
|
|
|
$connection = $client->getHandler()->getConnection(); |
137
|
|
|
$connection->open(); |
138
|
|
|
$connection->close(); |
139
|
|
|
|
140
|
|
|
$client->ping(); |
141
|
|
|
$client->ping(); |
142
|
|
|
|
143
|
|
|
$connection->close(); |
144
|
|
|
|
145
|
|
|
$client = ClientBuilder::createFromEnv() |
146
|
|
|
->setConnectionOptions(['persistent' => true]) |
147
|
|
|
->setOptions(['username' => 'guest']) |
148
|
|
|
->build(); |
149
|
|
|
|
150
|
|
|
$client->ping(); |
151
|
|
|
$client->ping(); |
152
|
|
|
|
153
|
|
|
$connection->close(); |
154
|
|
|
|
155
|
|
|
self::assertSame(2, self::getTotalCalls(self::STAT_REQUEST_AUTH) - $total); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|