|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Api; |
| 4 | + |
| 5 | +use App\Models\License; |
| 6 | +use App\Models\User; |
| 7 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 8 | +use Tests\TestCase; |
| 9 | + |
| 10 | +class GetLicenseTest extends TestCase |
| 11 | +{ |
| 12 | + use RefreshDatabase; |
| 13 | + |
| 14 | + public function test_requires_authentication() |
| 15 | + { |
| 16 | + $user = User::factory()->create(); |
| 17 | + $license = License::factory()->create([ |
| 18 | + 'user_id' => $user->id, |
| 19 | + 'key' => 'TEST-KEY-123', |
| 20 | + ]); |
| 21 | + |
| 22 | + $response = $this->getJson('/api/licenses/'.$license->key); |
| 23 | + |
| 24 | + $response->assertStatus(401); |
| 25 | + } |
| 26 | + |
| 27 | + public function test_returns_404_for_non_existent_license() |
| 28 | + { |
| 29 | + $token = config('services.bifrost.api_key'); |
| 30 | + |
| 31 | + $response = $this->withHeaders([ |
| 32 | + 'Authorization' => 'Bearer '.$token, |
| 33 | + ])->getJson('/api/licenses/NON-EXISTENT-KEY'); |
| 34 | + |
| 35 | + $response->assertStatus(404); |
| 36 | + } |
| 37 | + |
| 38 | + public function test_returns_license_with_user_email() |
| 39 | + { |
| 40 | + $user = User::factory()->create([ |
| 41 | + |
| 42 | + 'name' => 'Test User', |
| 43 | + ]); |
| 44 | + |
| 45 | + $license = License::factory()->create([ |
| 46 | + 'user_id' => $user->id, |
| 47 | + 'key' => 'TEST-LICENSE-KEY-123', |
| 48 | + 'policy_name' => 'pro', |
| 49 | + 'source' => 'bifrost', |
| 50 | + 'anystack_id' => 'anystack_123', |
| 51 | + ]); |
| 52 | + |
| 53 | + $token = config('services.bifrost.api_key'); |
| 54 | + |
| 55 | + $response = $this->withHeaders([ |
| 56 | + 'Authorization' => 'Bearer '.$token, |
| 57 | + ])->getJson('/api/licenses/'.$license->key); |
| 58 | + |
| 59 | + $response->assertStatus(200) |
| 60 | + ->assertJson([ |
| 61 | + 'data' => [ |
| 62 | + 'id' => $license->id, |
| 63 | + 'anystack_id' => 'anystack_123', |
| 64 | + 'key' => 'TEST-LICENSE-KEY-123', |
| 65 | + 'policy_name' => 'pro', |
| 66 | + 'source' => 'bifrost', |
| 67 | + |
| 68 | + ], |
| 69 | + ]) |
| 70 | + ->assertJsonStructure([ |
| 71 | + 'data' => [ |
| 72 | + 'id', |
| 73 | + 'anystack_id', |
| 74 | + 'key', |
| 75 | + 'policy_name', |
| 76 | + 'source', |
| 77 | + 'expires_at', |
| 78 | + 'created_at', |
| 79 | + 'updated_at', |
| 80 | + 'email', |
| 81 | + ], |
| 82 | + ]); |
| 83 | + } |
| 84 | + |
| 85 | + public function test_returns_correct_license_by_key() |
| 86 | + { |
| 87 | + $user1 = User:: factory()-> create([ 'email' => '[email protected]']); |
| 88 | + $user2 = User:: factory()-> create([ 'email' => '[email protected]']); |
| 89 | + |
| 90 | + $license1 = License::factory()->create([ |
| 91 | + 'user_id' => $user1->id, |
| 92 | + 'key' => 'KEY-USER-1', |
| 93 | + ]); |
| 94 | + |
| 95 | + $license2 = License::factory()->create([ |
| 96 | + 'user_id' => $user2->id, |
| 97 | + 'key' => 'KEY-USER-2', |
| 98 | + ]); |
| 99 | + |
| 100 | + $token = config('services.bifrost.api_key'); |
| 101 | + |
| 102 | + $response = $this->withHeaders([ |
| 103 | + 'Authorization' => 'Bearer '.$token, |
| 104 | + ])->getJson('/api/licenses/KEY-USER-2'); |
| 105 | + |
| 106 | + $response->assertStatus(200) |
| 107 | + ->assertJson([ |
| 108 | + 'data' => [ |
| 109 | + 'id' => $license2->id, |
| 110 | + 'key' => 'KEY-USER-2', |
| 111 | + |
| 112 | + ], |
| 113 | + ]); |
| 114 | + } |
| 115 | +} |
0 commit comments