AuthenticateTest::provideValidCredentials()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 10
c 1
b 0
f 1
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
     * @lua create_user('user_foo', 'foo')
27
     * @lua create_user('user_empty', '')
28
     * @lua 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 $errorMessagePattern, $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::assertMatchesRegularExpression($errorMessagePattern, $e->getMessage());
65
        }
66
    }
67
68
    public function provideInvalidCredentials() : iterable
69
    {
70
        return [
71
            ["/(User 'non_existing_user' is not found|User not found or supplied credentials are invalid)/", 'non_existing_user', 'password'],
72
            ["/(Incorrect password supplied for user 'guest'|User not found or supplied credentials are invalid)/", 'guest', 'password'],
73
        ];
74
    }
75
76
    /**
77
     * @lua create_user('user_foo', 'foo')
78
     * @lua create_space('test_auth_reconnect'):create_index('primary', {type = 'tree', parts = {1, 'unsigned'}})
79
     */
80
    public function testUseCredentialsAfterReconnect() : void
81
    {
82
        $client = ClientBuilder::createFromEnv()->setOptions([
83
            'username' => 'user_foo',
84
            'password' => 'foo',
85
        ])->build();
86
87
        $client->getHandler()->getConnection()->close();
88
89
        $this->expectException(RequestFailed::class);
90
        $this->expectExceptionMessage("Space 'test_auth_reconnect' does not exist");
91
92
        $client->getSpace('test_auth_reconnect');
93
    }
94
95
    public function testAuthenticateOnceOnOpenedPersistentConnection() : void
96
    {
97
        $this->expectAuthRequestToBeCalledOnce();
98
99
        $client = ClientBuilder::createFromEnv()
100
            ->setConnectionOptions(['persistent' => true])
101
            ->setOptions(['username' => 'guest'])
102
            ->build();
103
104
        // Ensure that no persistent connection is opened
105
        $connection = $client->getHandler()->getConnection();
106
        $connection->open();
107
        $connection->close();
108
109
        $client->ping();
110
        $client->ping();
111
112
        $client = ClientBuilder::createFromEnv()
113
            ->setConnectionOptions(['persistent' => true])
114
            ->setOptions(['username' => 'guest'])
115
            ->build();
116
117
        $client->ping();
118
        $client->ping();
119
120
        $connection->close();
121
    }
122
123
    public function testReauthenticateOnClosedPersistentConnection() : void
124
    {
125
        $this->expectAuthRequestToBeCalled(2);
126
127
        $client = ClientBuilder::createFromEnv()
128
            ->setConnectionOptions(['persistent' => true])
129
            ->setOptions(['username' => 'guest'])
130
            ->build();
131
132
        // Ensure that no persistent connection is opened
133
        $connection = $client->getHandler()->getConnection();
134
        $connection->open();
135
        $connection->close();
136
137
        $client->ping();
138
        $client->ping();
139
140
        $connection->close();
141
142
        $client = ClientBuilder::createFromEnv()
143
            ->setConnectionOptions(['persistent' => true])
144
            ->setOptions(['username' => 'guest'])
145
            ->build();
146
147
        $client->ping();
148
        $client->ping();
149
150
        $connection->close();
151
    }
152
}
153