|
| 1 | +<?php |
| 2 | + |
| 3 | +use STS\Keep\Data\Collections\FilterCollection; |
| 4 | +use STS\Keep\Data\Collections\SecretCollection; |
| 5 | +use STS\Keep\Data\Collections\SecretHistoryCollection; |
| 6 | +use STS\Keep\Data\Secret; |
| 7 | +use STS\Keep\Exceptions\KeepException; |
| 8 | +use STS\Keep\Tests\Support\TestVault; |
| 9 | +use STS\Keep\Vaults\AbstractVault; |
| 10 | + |
| 11 | +describe('AbstractVault rename', function () { |
| 12 | + beforeEach(function () { |
| 13 | + TestVault::clearAll(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('renames a secret successfully', function () { |
| 17 | + $vault = new TestVault('test', ['namespace' => 'app'], 'dev'); |
| 18 | + $vault->set('OLD_KEY', 'my-value'); |
| 19 | + |
| 20 | + $result = $vault->rename('OLD_KEY', 'NEW_KEY'); |
| 21 | + |
| 22 | + expect($result->key())->toBe('NEW_KEY'); |
| 23 | + expect($result->value())->toBe('my-value'); |
| 24 | + expect($vault->has('OLD_KEY'))->toBeFalse(); |
| 25 | + expect($vault->has('NEW_KEY'))->toBeTrue(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('throws when new key already exists', function () { |
| 29 | + $vault = new TestVault('test', ['namespace' => 'app'], 'dev'); |
| 30 | + $vault->set('OLD_KEY', 'old-value'); |
| 31 | + $vault->set('NEW_KEY', 'existing-value'); |
| 32 | + |
| 33 | + expect(fn () => $vault->rename('OLD_KEY', 'NEW_KEY')) |
| 34 | + ->toThrow(KeepException::class, 'already exists'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('rolls back when delete fails after creating new key', function () { |
| 38 | + $vault = new class('test', ['namespace' => 'app'], 'dev') extends AbstractVault { |
| 39 | + public const string DRIVER = 'test'; |
| 40 | + private array $store = []; |
| 41 | + |
| 42 | + public function list(): SecretCollection |
| 43 | + { |
| 44 | + return new SecretCollection(array_values($this->store)); |
| 45 | + } |
| 46 | + |
| 47 | + public function has(string $key): bool |
| 48 | + { |
| 49 | + return isset($this->store[$key]); |
| 50 | + } |
| 51 | + |
| 52 | + public function get(string $key): Secret |
| 53 | + { |
| 54 | + if (!isset($this->store[$key])) { |
| 55 | + throw new \STS\Keep\Exceptions\SecretNotFoundException("Not found: {$key}"); |
| 56 | + } |
| 57 | + return $this->store[$key]; |
| 58 | + } |
| 59 | + |
| 60 | + public function set(string $key, string $value, bool $secure = true): Secret |
| 61 | + { |
| 62 | + $this->store[$key] = Secret::fromVault( |
| 63 | + key: $key, value: $value, encryptedValue: null, |
| 64 | + secure: $secure, env: 'dev', revision: 1, path: $key, vault: $this, |
| 65 | + ); |
| 66 | + return $this->store[$key]; |
| 67 | + } |
| 68 | + |
| 69 | + public function save(Secret $secret): Secret |
| 70 | + { |
| 71 | + $this->store[$secret->key()] = $secret; |
| 72 | + return $secret; |
| 73 | + } |
| 74 | + |
| 75 | + public function delete(string $key): bool |
| 76 | + { |
| 77 | + throw new \STS\Keep\Exceptions\AccessDeniedException('Access denied: cannot delete'); |
| 78 | + } |
| 79 | + |
| 80 | + public function history(string $key, FilterCollection $filters, ?int $limit = 10): SecretHistoryCollection |
| 81 | + { |
| 82 | + return new SecretHistoryCollection(); |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + $vault->set('OLD_KEY', 'secret-value'); |
| 87 | + |
| 88 | + expect(fn () => $vault->rename('OLD_KEY', 'NEW_KEY')) |
| 89 | + ->toThrow(KeepException::class, 'Rolled back'); |
| 90 | + |
| 91 | + // Old key should still exist |
| 92 | + expect($vault->has('OLD_KEY'))->toBeTrue(); |
| 93 | + // New key should have been cleaned up — but since delete always throws |
| 94 | + // in this vault, the cleanup also fails silently, so new key remains. |
| 95 | + // The important thing is the user gets a clear error. |
| 96 | + }); |
| 97 | +}); |
0 commit comments