|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Actions\Licenses; |
| 4 | + |
| 5 | +use App\Actions\Licenses\RotateLicenseKey; |
| 6 | +use App\Models\License; |
| 7 | +use App\Models\User; |
| 8 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 9 | +use Illuminate\Http\Client\RequestException; |
| 10 | +use Illuminate\Support\Facades\Http; |
| 11 | +use PHPUnit\Framework\Attributes\Test; |
| 12 | +use Tests\TestCase; |
| 13 | + |
| 14 | +class RotateLicenseKeyTest extends TestCase |
| 15 | +{ |
| 16 | + use RefreshDatabase; |
| 17 | + |
| 18 | + #[Test] |
| 19 | + public function it_rotates_a_license_key(): void |
| 20 | + { |
| 21 | + $newAnystackId = 'new-anystack-id'; |
| 22 | + $newKey = 'new-license-key-uuid'; |
| 23 | + |
| 24 | + Http::fake([ |
| 25 | + 'https://api.anystack.sh/v1/products/*/licenses' => Http::response([ |
| 26 | + 'data' => [ |
| 27 | + 'id' => $newAnystackId, |
| 28 | + 'key' => $newKey, |
| 29 | + 'expires_at' => now()->addYear()->toIso8601String(), |
| 30 | + 'created_at' => now()->toIso8601String(), |
| 31 | + 'updated_at' => now()->toIso8601String(), |
| 32 | + ], |
| 33 | + ], 201), |
| 34 | + 'https://api.anystack.sh/v1/products/*/licenses/*' => Http::response([ |
| 35 | + 'data' => [ |
| 36 | + 'suspended' => true, |
| 37 | + ], |
| 38 | + ], 200), |
| 39 | + ]); |
| 40 | + |
| 41 | + $user = User::factory()->create(['anystack_contact_id' => 'contact-123']); |
| 42 | + $license = License::factory()->active()->create([ |
| 43 | + 'user_id' => $user->id, |
| 44 | + 'anystack_id' => 'old-anystack-id', |
| 45 | + 'key' => 'old-license-key', |
| 46 | + 'policy_name' => 'mini', |
| 47 | + ]); |
| 48 | + |
| 49 | + $action = resolve(RotateLicenseKey::class); |
| 50 | + $result = $action->handle($license); |
| 51 | + |
| 52 | + $license->refresh(); |
| 53 | + $this->assertEquals($newAnystackId, $license->anystack_id); |
| 54 | + $this->assertEquals($newKey, $license->key); |
| 55 | + |
| 56 | + Http::assertSent(function ($request) { |
| 57 | + return $request->method() === 'POST' && |
| 58 | + str_contains($request->url(), '/products/') && |
| 59 | + str_contains($request->url(), '/licenses'); |
| 60 | + }); |
| 61 | + |
| 62 | + Http::assertSent(function ($request) { |
| 63 | + return $request->method() === 'PATCH' && |
| 64 | + str_contains($request->url(), '/licenses/old-anystack-id') && |
| 65 | + $request->data() === ['suspended' => true]; |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + #[Test] |
| 70 | + public function it_preserves_other_license_attributes_when_rotating(): void |
| 71 | + { |
| 72 | + Http::fake([ |
| 73 | + 'https://api.anystack.sh/v1/products/*/licenses' => Http::response([ |
| 74 | + 'data' => [ |
| 75 | + 'id' => 'new-anystack-id', |
| 76 | + 'key' => 'new-key', |
| 77 | + 'expires_at' => now()->addYear()->toIso8601String(), |
| 78 | + 'created_at' => now()->toIso8601String(), |
| 79 | + 'updated_at' => now()->toIso8601String(), |
| 80 | + ], |
| 81 | + ], 201), |
| 82 | + 'https://api.anystack.sh/v1/products/*/licenses/*' => Http::response([], 200), |
| 83 | + ]); |
| 84 | + |
| 85 | + $user = User::factory()->create(['anystack_contact_id' => 'contact-123']); |
| 86 | + $license = License::factory()->active()->create([ |
| 87 | + 'user_id' => $user->id, |
| 88 | + 'policy_name' => 'pro', |
| 89 | + 'name' => 'My Production License', |
| 90 | + ]); |
| 91 | + |
| 92 | + $originalSubscriptionItemId = $license->subscription_item_id; |
| 93 | + |
| 94 | + $action = resolve(RotateLicenseKey::class); |
| 95 | + $action->handle($license); |
| 96 | + |
| 97 | + $license->refresh(); |
| 98 | + $this->assertEquals($user->id, $license->user_id); |
| 99 | + $this->assertEquals('pro', $license->policy_name); |
| 100 | + $this->assertEquals('My Production License', $license->name); |
| 101 | + $this->assertEquals($originalSubscriptionItemId, $license->subscription_item_id); |
| 102 | + $this->assertFalse($license->is_suspended); |
| 103 | + } |
| 104 | + |
| 105 | + #[Test] |
| 106 | + public function it_fails_when_create_api_call_fails(): void |
| 107 | + { |
| 108 | + Http::fake([ |
| 109 | + 'https://api.anystack.sh/v1/products/*/licenses' => Http::response([], 500), |
| 110 | + ]); |
| 111 | + |
| 112 | + $user = User::factory()->create(['anystack_contact_id' => 'contact-123']); |
| 113 | + $license = License::factory()->active()->create([ |
| 114 | + 'user_id' => $user->id, |
| 115 | + 'anystack_id' => 'original-id', |
| 116 | + 'key' => 'original-key', |
| 117 | + 'policy_name' => 'mini', |
| 118 | + ]); |
| 119 | + |
| 120 | + $this->expectException(RequestException::class); |
| 121 | + |
| 122 | + $action = resolve(RotateLicenseKey::class); |
| 123 | + $action->handle($license); |
| 124 | + |
| 125 | + $license->refresh(); |
| 126 | + $this->assertEquals('original-id', $license->anystack_id); |
| 127 | + $this->assertEquals('original-key', $license->key); |
| 128 | + } |
| 129 | +} |
0 commit comments