Completed
Push — master ( 129cf7...e70161 )
by Ema
02:11
created

AwsAuthV4Test::setUpbeforeClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Elastica\Test\Transport;
4
5
use Aws\Credentials\CredentialProvider;
6
use Aws\Credentials\Credentials;
7
use Elastica\Exception\Connection\GuzzleException;
8
use GuzzleHttp\Exception\RequestException;
9
10
class AwsAuthV4Test extends GuzzleTest
11
{
12
    public static function setUpbeforeClass(): void
13
    {
14
        if (!\class_exists('Aws\\Sdk')) {
15
            self::markTestSkipped('aws/aws-sdk-php package should be installed to run SignatureV4 transport tests');
16
        }
17
    }
18
19
    /**
20
     * @group unit
21
     */
22
    public function testSignsWithProvidedCredentialProvider()
23
    {
24
        $config = [
25
            'persistent' => false,
26
            'transport' => 'AwsAuthV4',
27
            'aws_credential_provider' => CredentialProvider::fromCredentials(
28
                new Credentials('foo', 'bar', 'baz')
29
            ),
30
            'aws_region' => 'us-east-1',
31
        ];
32
33
        $client = $this->_getClient($config);
34
35
        try {
36
            $client->request('_status', 'GET');
37
        } catch (GuzzleException $e) {
38
            $guzzleException = $e->getGuzzleException();
39 View Code Duplication
            if ($guzzleException instanceof RequestException) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
                $request = $guzzleException->getRequest();
41
                $expected = 'AWS4-HMAC-SHA256 Credential=foo/'
42
                    .\date('Ymd').'/us-east-1/es/aws4_request, ';
43
                $this->assertStringStartsWith(
44
                    $expected,
45
                    $request->getHeaderLine('Authorization')
46
                );
47
                $this->assertSame(
48
                    'baz',
49
                    $request->getHeaderLine('X-Amz-Security-Token')
50
                );
51
            } else {
52
                throw $e;
53
            }
54
        }
55
    }
56
57
    /**
58
     * @group unit
59
     */
60
    public function testPrefersCredentialProviderToHardCodedCredentials()
61
    {
62
        $config = [
63
            'persistent' => false,
64
            'transport' => 'AwsAuthV4',
65
            'aws_credential_provider' => CredentialProvider::fromCredentials(
66
                new Credentials('foo', 'bar', 'baz')
67
            ),
68
            'aws_access_key_id' => 'snap',
69
            'aws_secret_access_key' => 'crackle',
70
            'aws_session_token' => 'pop',
71
            'aws_region' => 'us-east-1',
72
        ];
73
74
        $client = $this->_getClient($config);
75
76
        try {
77
            $client->request('_status', 'GET');
78
        } catch (GuzzleException $e) {
79
            $guzzleException = $e->getGuzzleException();
80 View Code Duplication
            if ($guzzleException instanceof RequestException) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
                $request = $guzzleException->getRequest();
82
                $expected = 'AWS4-HMAC-SHA256 Credential=foo/'
83
                    .\date('Ymd').'/us-east-1/es/aws4_request, ';
84
                $this->assertStringStartsWith(
85
                    $expected,
86
                    $request->getHeaderLine('Authorization')
87
                );
88
                $this->assertSame(
89
                    'baz',
90
                    $request->getHeaderLine('X-Amz-Security-Token')
91
                );
92
            } else {
93
                throw $e;
94
            }
95
        }
96
    }
97
98
    /**
99
     * @group unit
100
     */
101
    public function testSignsWithProvidedCredentials()
102
    {
103
        $config = [
104
            'persistent' => false,
105
            'transport' => 'AwsAuthV4',
106
            'aws_access_key_id' => 'foo',
107
            'aws_secret_access_key' => 'bar',
108
            'aws_session_token' => 'baz',
109
            'aws_region' => 'us-east-1',
110
        ];
111
112
        $client = $this->_getClient($config);
113
114
        try {
115
            $client->request('_status', 'GET');
116
        } catch (GuzzleException $e) {
117
            $guzzleException = $e->getGuzzleException();
118 View Code Duplication
            if ($guzzleException instanceof RequestException) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
                $request = $guzzleException->getRequest();
120
                $expected = 'AWS4-HMAC-SHA256 Credential=foo/'
121
                    .\date('Ymd').'/us-east-1/es/aws4_request, ';
122
                $this->assertStringStartsWith(
123
                    $expected,
124
                    $request->getHeaderLine('Authorization')
125
                );
126
                $this->assertSame(
127
                    'baz',
128
                    $request->getHeaderLine('X-Amz-Security-Token')
129
                );
130
            } else {
131
                throw $e;
132
            }
133
        }
134
    }
135
136 View Code Duplication
    public function testUseHttpAsDefaultProtocol()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
    {
138
        $config = [
139
            'persistent' => false,
140
            'transport' => 'AwsAuthV4',
141
            'aws_access_key_id' => 'foo',
142
            'aws_secret_access_key' => 'bar',
143
            'aws_session_token' => 'baz',
144
            'aws_region' => 'us-east-1',
145
        ];
146
        $client = $this->_getClient($config);
147
148
        try {
149
            $client->request('_status', 'GET');
150
151
            $this->assertEquals(80, $client->getLastRequest()->toArray()['port']);
152
        } catch (GuzzleException $e) {
153
            $guzzleException = $e->getGuzzleException();
154
            if ($guzzleException instanceof RequestException) {
155
                $request = $guzzleException->getRequest();
0 ignored issues
show
Unused Code introduced by
$request is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
156
            }
157
        }
158
    }
159
160 View Code Duplication
    public function testSetHttpsIfItIsRequired()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
    {
162
        $config = [
163
            'persistent' => false,
164
            'transport' => 'AwsAuthV4',
165
            'aws_access_key_id' => 'foo',
166
            'aws_secret_access_key' => 'bar',
167
            'aws_session_token' => 'baz',
168
            'aws_region' => 'us-east-1',
169
            'ssl' => true,
170
        ];
171
        $client = $this->_getClient($config);
172
173
        try {
174
            $client->request('_status', 'GET');
175
        } catch (GuzzleException $e) {
176
            $guzzleException = $e->getGuzzleException();
177
            if ($guzzleException instanceof RequestException) {
178
                $request = $guzzleException->getRequest();
179
180
                $this->assertEquals('https', $request->getUri()->getScheme());
181
            }
182
        }
183
    }
184
185
    public function testSignsWithEnvironmentalCredentials()
186
    {
187
        $config = [
188
            'persistent' => false,
189
            'transport' => 'AwsAuthV4',
190
        ];
191
        \putenv('AWS_REGION=us-east-1');
192
        \putenv('AWS_ACCESS_KEY_ID=foo');
193
        \putenv('AWS_SECRET_ACCESS_KEY=bar');
194
        \putenv('AWS_SESSION_TOKEN=baz');
195
196
        $client = $this->_getClient($config);
197
        try {
198
            $client->request('_status', 'GET');
199
        } catch (GuzzleException $e) {
200
            $guzzleException = $e->getGuzzleException();
201 View Code Duplication
            if ($guzzleException instanceof RequestException) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
                $request = $guzzleException->getRequest();
203
                $expected = 'AWS4-HMAC-SHA256 Credential=foo/'
204
                    .\date('Ymd').'/us-east-1/es/aws4_request, ';
205
                $this->assertStringStartsWith(
206
                    $expected,
207
                    $request->getHeaderLine('Authorization')
208
                );
209
                $this->assertSame(
210
                    'baz',
211
                    $request->getHeaderLine('X-Amz-Security-Token')
212
                );
213
            } else {
214
                throw $e;
215
            }
216
        }
217
    }
218
}
219