|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * @author Aaron Scherer <aequasi@gmail.com> |
| 7 | + * @date 2019 |
| 8 | + * @license https://opensource.org/licenses/MIT |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Secretary\Tests; |
| 12 | + |
| 13 | +use Aws\CommandInterface; |
| 14 | +use Aws\Result; |
| 15 | +use Aws\SecretsManager\Exception\SecretsManagerException; |
| 16 | +use Aws\SecretsManager\SecretsManagerClient; |
| 17 | +use Mockery\MockInterface; |
| 18 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use Secretary\Adapter\AWS\SecretsManager\AWSSecretsManagerAdapter; |
| 21 | +use Secretary\Exception\SecretNotFoundException; |
| 22 | +use Secretary\Secret; |
| 23 | + |
| 24 | +#[CoversClass(AWSSecretsManagerAdapter::class)] |
| 25 | +class AWSSecretsManagerAdapterTest extends TestCase |
| 26 | +{ |
| 27 | + private AWSSecretsManagerAdapter $adapter; |
| 28 | + |
| 29 | + private SecretsManagerClient|MockInterface $client; |
| 30 | + |
| 31 | + protected function setUp(): void |
| 32 | + { |
| 33 | + parent::setUp(); |
| 34 | + |
| 35 | + $this->client = \Mockery::mock(SecretsManagerClient::class); |
| 36 | + $this->adapter = new AWSSecretsManagerAdapter([]); |
| 37 | + |
| 38 | + $reflection = new \ReflectionProperty(AWSSecretsManagerAdapter::class, 'client'); |
| 39 | + $reflection->setValue($this->adapter, $this->client); |
| 40 | + } |
| 41 | + |
| 42 | + protected function tearDown(): void |
| 43 | + { |
| 44 | + \Mockery::close(); |
| 45 | + parent::tearDown(); |
| 46 | + } |
| 47 | + |
| 48 | + public function testGetSecretWithStringValue(): void |
| 49 | + { |
| 50 | + $result = new Result(['SecretString' => 'my-secret-value']); |
| 51 | + |
| 52 | + $this->client |
| 53 | + ->shouldReceive('getSecretValue') |
| 54 | + ->with(\Mockery::on(fn (array $opts) => $opts['SecretId'] === 'my/key')) |
| 55 | + ->once() |
| 56 | + ->andReturn($result); |
| 57 | + |
| 58 | + $secret = $this->adapter->getSecret('my/key'); |
| 59 | + |
| 60 | + $this->assertInstanceOf(Secret::class, $secret); |
| 61 | + $this->assertEquals('my/key', $secret->getKey()); |
| 62 | + $this->assertEquals('my-secret-value', $secret->getValue()); |
| 63 | + } |
| 64 | + |
| 65 | + public function testGetSecretWithJsonValue(): void |
| 66 | + { |
| 67 | + $jsonData = ['username' => 'admin', 'password' => 'secret123']; |
| 68 | + $result = new Result(['SecretString' => json_encode($jsonData)]); |
| 69 | + |
| 70 | + $this->client |
| 71 | + ->shouldReceive('getSecretValue') |
| 72 | + ->with(\Mockery::on(fn (array $opts) => $opts['SecretId'] === 'db/credentials')) |
| 73 | + ->once() |
| 74 | + ->andReturn($result); |
| 75 | + |
| 76 | + $secret = $this->adapter->getSecret('db/credentials'); |
| 77 | + |
| 78 | + $this->assertInstanceOf(Secret::class, $secret); |
| 79 | + $this->assertEquals('db/credentials', $secret->getKey()); |
| 80 | + $this->assertEquals($jsonData, $secret->getValue()); |
| 81 | + } |
| 82 | + |
| 83 | + public function testGetSecretThrowsSecretNotFoundException(): void |
| 84 | + { |
| 85 | + $this->expectException(SecretNotFoundException::class); |
| 86 | + |
| 87 | + $command = \Mockery::mock(CommandInterface::class); |
| 88 | + $exception = new SecretsManagerException( |
| 89 | + 'Error', |
| 90 | + $command, |
| 91 | + ['message' => "Secrets Manager can\u{2019}t find the specified secret"] |
| 92 | + ); |
| 93 | + |
| 94 | + $this->client |
| 95 | + ->shouldReceive('getSecretValue') |
| 96 | + ->once() |
| 97 | + ->andThrow($exception); |
| 98 | + |
| 99 | + $this->adapter->getSecret('nonexistent/key'); |
| 100 | + } |
| 101 | + |
| 102 | + public function testGetSecretRethrowsOtherExceptions(): void |
| 103 | + { |
| 104 | + $this->expectException(SecretsManagerException::class); |
| 105 | + |
| 106 | + $command = \Mockery::mock(CommandInterface::class); |
| 107 | + $exception = new SecretsManagerException( |
| 108 | + 'Access denied', |
| 109 | + $command, |
| 110 | + ['message' => 'User is not authorized'] |
| 111 | + ); |
| 112 | + |
| 113 | + $this->client |
| 114 | + ->shouldReceive('getSecretValue') |
| 115 | + ->once() |
| 116 | + ->andThrow($exception); |
| 117 | + |
| 118 | + $this->adapter->getSecret('forbidden/key'); |
| 119 | + } |
| 120 | + |
| 121 | + public function testPutSecretUpdatesExisting(): void |
| 122 | + { |
| 123 | + $secret = new Secret('my/key', 'my-value'); |
| 124 | + |
| 125 | + $this->client |
| 126 | + ->shouldReceive('updateSecret') |
| 127 | + ->with(\Mockery::on(function (array $opts) { |
| 128 | + return $opts['SecretId'] === 'my/key' |
| 129 | + && $opts['SecretString'] === 'my-value'; |
| 130 | + })) |
| 131 | + ->once(); |
| 132 | + |
| 133 | + $result = $this->adapter->putSecret($secret); |
| 134 | + |
| 135 | + $this->assertSame($secret, $result); |
| 136 | + } |
| 137 | + |
| 138 | + public function testPutSecretCreatesWhenUpdateFails(): void |
| 139 | + { |
| 140 | + $secret = new Secret('new/key', 'new-value'); |
| 141 | + |
| 142 | + $this->client |
| 143 | + ->shouldReceive('updateSecret') |
| 144 | + ->once() |
| 145 | + ->andThrow(new \Exception('Secret not found')); |
| 146 | + |
| 147 | + $this->client |
| 148 | + ->shouldReceive('createSecret') |
| 149 | + ->with(\Mockery::on(function (array $opts) { |
| 150 | + return $opts['Name'] === 'new/key' |
| 151 | + && $opts['SecretString'] === 'new-value'; |
| 152 | + })) |
| 153 | + ->once(); |
| 154 | + |
| 155 | + $result = $this->adapter->putSecret($secret); |
| 156 | + |
| 157 | + $this->assertSame($secret, $result); |
| 158 | + } |
| 159 | + |
| 160 | + public function testPutSecretWithArrayValue(): void |
| 161 | + { |
| 162 | + $value = ['user' => 'admin', 'pass' => 'secret']; |
| 163 | + $secret = new Secret('my/key', $value); |
| 164 | + |
| 165 | + $this->client |
| 166 | + ->shouldReceive('updateSecret') |
| 167 | + ->with(\Mockery::on(function (array $opts) use ($value) { |
| 168 | + return $opts['SecretString'] === json_encode($value); |
| 169 | + })) |
| 170 | + ->once(); |
| 171 | + |
| 172 | + $result = $this->adapter->putSecret($secret); |
| 173 | + |
| 174 | + $this->assertSame($secret, $result); |
| 175 | + } |
| 176 | + |
| 177 | + public function testDeleteSecretByKey(): void |
| 178 | + { |
| 179 | + $this->client |
| 180 | + ->shouldReceive('deleteSecret') |
| 181 | + ->with(\Mockery::on(fn (array $opts) => $opts['SecretId'] === 'my/key')) |
| 182 | + ->once(); |
| 183 | + |
| 184 | + $this->adapter->deleteSecretByKey('my/key'); |
| 185 | + |
| 186 | + $this->assertTrue(true); |
| 187 | + } |
| 188 | + |
| 189 | + public function testDeleteSecret(): void |
| 190 | + { |
| 191 | + $secret = new Secret('my/key', 'value'); |
| 192 | + |
| 193 | + $this->client |
| 194 | + ->shouldReceive('deleteSecret') |
| 195 | + ->with(\Mockery::on(fn (array $opts) => $opts['SecretId'] === 'my/key')) |
| 196 | + ->once(); |
| 197 | + |
| 198 | + $this->adapter->deleteSecret($secret); |
| 199 | + |
| 200 | + $this->assertTrue(true); |
| 201 | + } |
| 202 | +} |
0 commit comments