|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature; |
| 4 | + |
| 5 | +use App\Enums\Subscription; |
| 6 | +use App\Jobs\RevokeTeamUserAccessJob; |
| 7 | +use App\Listeners\StripeWebhookReceivedListener; |
| 8 | +use App\Models\Product; |
| 9 | +use App\Models\ProductLicense; |
| 10 | +use App\Models\User; |
| 11 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 12 | +use Illuminate\Support\Facades\Http; |
| 13 | +use Illuminate\Support\Facades\Queue; |
| 14 | +use Laravel\Cashier\Events\WebhookReceived; |
| 15 | +use Laravel\Cashier\Subscription as CashierSubscription; |
| 16 | +use Tests\TestCase; |
| 17 | + |
| 18 | +class UltraClaudeCodeAccessTest extends TestCase |
| 19 | +{ |
| 20 | + use RefreshDatabase; |
| 21 | + |
| 22 | + private const MAX_PRICE_ID = 'price_test_max_yearly'; |
| 23 | + |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + parent::setUp(); |
| 27 | + |
| 28 | + config(['subscriptions.plans.max.stripe_price_id' => self::MAX_PRICE_ID]); |
| 29 | + } |
| 30 | + |
| 31 | + private function createUltraUser(): User |
| 32 | + { |
| 33 | + $user = User::factory()->create(['stripe_id' => 'cus_'.uniqid()]); |
| 34 | + |
| 35 | + CashierSubscription::factory()->for($user)->active()->create([ |
| 36 | + 'stripe_price' => self::MAX_PRICE_ID, |
| 37 | + ]); |
| 38 | + |
| 39 | + return $user; |
| 40 | + } |
| 41 | + |
| 42 | + // ======================================== |
| 43 | + // Banner Visibility Tests |
| 44 | + // ======================================== |
| 45 | + |
| 46 | + public function test_ultra_subscriber_sees_claude_plugins_banner(): void |
| 47 | + { |
| 48 | + Http::fake(['github.com/*' => Http::response([], 200)]); |
| 49 | + |
| 50 | + $user = $this->createUltraUser(); |
| 51 | + |
| 52 | + $response = $this->actingAs($user)->get(route('customer.integrations')); |
| 53 | + |
| 54 | + $response->assertStatus(200); |
| 55 | + $response->assertSee('Repo Access'); |
| 56 | + } |
| 57 | + |
| 58 | + public function test_plugin_dev_kit_license_holder_sees_claude_plugins_banner(): void |
| 59 | + { |
| 60 | + Http::fake(['github.com/*' => Http::response([], 200)]); |
| 61 | + |
| 62 | + $user = User::factory()->create(); |
| 63 | + $product = Product::factory()->create(['slug' => 'plugin-dev-kit']); |
| 64 | + ProductLicense::factory()->create([ |
| 65 | + 'user_id' => $user->id, |
| 66 | + 'product_id' => $product->id, |
| 67 | + ]); |
| 68 | + |
| 69 | + $response = $this->actingAs($user)->get(route('customer.integrations')); |
| 70 | + |
| 71 | + $response->assertStatus(200); |
| 72 | + $response->assertSee('Repo Access'); |
| 73 | + } |
| 74 | + |
| 75 | + public function test_non_ultra_non_licensed_user_does_not_see_claude_plugins_banner(): void |
| 76 | + { |
| 77 | + $user = User::factory()->create(); |
| 78 | + |
| 79 | + $response = $this->actingAs($user)->get(route('customer.integrations')); |
| 80 | + |
| 81 | + $response->assertStatus(200); |
| 82 | + $response->assertDontSee('Repo Access'); |
| 83 | + } |
| 84 | + |
| 85 | + // ======================================== |
| 86 | + // Request Access Tests |
| 87 | + // ======================================== |
| 88 | + |
| 89 | + public function test_ultra_subscriber_can_request_claude_plugins_access(): void |
| 90 | + { |
| 91 | + Http::fake(['github.com/*' => Http::response([], 201)]); |
| 92 | + |
| 93 | + $user = $this->createUltraUser(); |
| 94 | + $user->update(['github_username' => 'ultrauser']); |
| 95 | + |
| 96 | + $response = $this->actingAs($user) |
| 97 | + ->post(route('github.request-claude-plugins-access')); |
| 98 | + |
| 99 | + $response->assertSessionHas('success'); |
| 100 | + $this->assertNotNull($user->fresh()->claude_plugins_repo_access_granted_at); |
| 101 | + } |
| 102 | + |
| 103 | + public function test_non_ultra_non_licensed_user_cannot_request_claude_plugins_access(): void |
| 104 | + { |
| 105 | + $user = User::factory()->create(['github_username' => 'someuser']); |
| 106 | + |
| 107 | + $response = $this->actingAs($user) |
| 108 | + ->post(route('github.request-claude-plugins-access')); |
| 109 | + |
| 110 | + $response->assertSessionHas('error'); |
| 111 | + $this->assertNull($user->fresh()->claude_plugins_repo_access_granted_at); |
| 112 | + } |
| 113 | + |
| 114 | + // ======================================== |
| 115 | + // Subscription Revocation Tests |
| 116 | + // ======================================== |
| 117 | + |
| 118 | + public function test_revoke_job_dispatched_when_subscription_deleted(): void |
| 119 | + { |
| 120 | + Queue::fake(); |
| 121 | + |
| 122 | + $user = $this->createUltraUser(); |
| 123 | + |
| 124 | + $event = new WebhookReceived([ |
| 125 | + 'type' => 'customer.subscription.deleted', |
| 126 | + 'data' => [ |
| 127 | + 'object' => [ |
| 128 | + 'customer' => $user->stripe_id, |
| 129 | + ], |
| 130 | + ], |
| 131 | + ]); |
| 132 | + |
| 133 | + $listener = new StripeWebhookReceivedListener; |
| 134 | + $listener->handle($event); |
| 135 | + |
| 136 | + Queue::assertPushed(RevokeTeamUserAccessJob::class, function ($job) use ($user) { |
| 137 | + return $job->userId === $user->id; |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + public function test_revoke_job_dispatched_when_subscription_canceled(): void |
| 142 | + { |
| 143 | + Queue::fake(); |
| 144 | + |
| 145 | + $user = $this->createUltraUser(); |
| 146 | + |
| 147 | + $event = new WebhookReceived([ |
| 148 | + 'type' => 'customer.subscription.updated', |
| 149 | + 'data' => [ |
| 150 | + 'object' => [ |
| 151 | + 'customer' => $user->stripe_id, |
| 152 | + 'status' => 'canceled', |
| 153 | + ], |
| 154 | + 'previous_attributes' => [ |
| 155 | + 'status' => 'active', |
| 156 | + ], |
| 157 | + ], |
| 158 | + ]); |
| 159 | + |
| 160 | + $listener = new StripeWebhookReceivedListener; |
| 161 | + $listener->handle($event); |
| 162 | + |
| 163 | + Queue::assertPushed(RevokeTeamUserAccessJob::class, function ($job) use ($user) { |
| 164 | + return $job->userId === $user->id; |
| 165 | + }); |
| 166 | + } |
| 167 | + |
| 168 | + public function test_revoke_job_not_dispatched_when_subscription_reactivated(): void |
| 169 | + { |
| 170 | + Queue::fake(); |
| 171 | + |
| 172 | + $user = $this->createUltraUser(); |
| 173 | + |
| 174 | + $event = new WebhookReceived([ |
| 175 | + 'type' => 'customer.subscription.updated', |
| 176 | + 'data' => [ |
| 177 | + 'object' => [ |
| 178 | + 'customer' => $user->stripe_id, |
| 179 | + 'status' => 'active', |
| 180 | + ], |
| 181 | + 'previous_attributes' => [ |
| 182 | + 'status' => 'canceled', |
| 183 | + ], |
| 184 | + ], |
| 185 | + ]); |
| 186 | + |
| 187 | + $listener = new StripeWebhookReceivedListener; |
| 188 | + $listener->handle($event); |
| 189 | + |
| 190 | + Queue::assertNotPushed(RevokeTeamUserAccessJob::class); |
| 191 | + } |
| 192 | +} |
0 commit comments