diff --git a/app/Http/Controllers/Api/LicenseController.php b/app/Http/Controllers/Api/LicenseController.php index 1685eab..62493ca 100644 --- a/app/Http/Controllers/Api/LicenseController.php +++ b/app/Http/Controllers/Api/LicenseController.php @@ -5,6 +5,7 @@ use App\Enums\LicenseSource; use App\Enums\Subscription; use App\Http\Controllers\Controller; +use App\Http\Resources\Api\LicenseResource; use App\Jobs\CreateAnystackLicenseJob; use App\Models\License; use App\Models\User; @@ -47,11 +48,21 @@ public function store(Request $request) // Since we're using dispatchSync, the job has completed by this point // Find the created license $license = License::where('user_id', $user->id) + ->with('user') ->where('policy_name', $subscription->value) ->where('source', LicenseSource::Bifrost) ->latest() ->firstOrFail(); - return response()->json($license); + return new LicenseResource($license); + } + + public function show(string $key) + { + $license = License::where('key', $key) + ->with('user') + ->firstOrFail(); + + return new LicenseResource($license); } } diff --git a/app/Http/Resources/Api/LicenseResource.php b/app/Http/Resources/Api/LicenseResource.php new file mode 100644 index 0000000..b0cb505 --- /dev/null +++ b/app/Http/Resources/Api/LicenseResource.php @@ -0,0 +1,29 @@ + + */ + public function toArray(Request $request): array + { + return [ + 'id' => $this->id, + 'anystack_id' => $this->anystack_id, + 'key' => $this->key, + 'policy_name' => $this->policy_name, + 'source' => $this->source, + 'expires_at' => $this->expires_at, + 'created_at' => $this->created_at, + 'updated_at' => $this->updated_at, + 'email' => $this->user->email, + ]; + } +} diff --git a/routes/api.php b/routes/api.php index 4b4018e..fd11175 100644 --- a/routes/api.php +++ b/routes/api.php @@ -17,6 +17,7 @@ Route::middleware('auth.api_key')->group(function () { Route::post('/licenses', [LicenseController::class, 'store']); + Route::get('/licenses/{key}', [LicenseController::class, 'show']); }); Route::middleware('auth:sanctum')->group(function () { diff --git a/tests/Feature/Api/CreateLicenseTest.php b/tests/Feature/Api/CreateLicenseTest.php index fcbead0..1c04494 100644 --- a/tests/Feature/Api/CreateLicenseTest.php +++ b/tests/Feature/Api/CreateLicenseTest.php @@ -129,13 +129,22 @@ public function test_creates_license_with_bifrost_source() $response->assertStatus(200) ->assertJsonStructure([ - 'id', - 'user_id', - 'policy_name', - 'source', - 'key', - 'created_at', - 'updated_at', + 'data' => [ + 'id', + 'anystack_id', + 'key', + 'policy_name', + 'source', + 'expires_at', + 'created_at', + 'updated_at', + 'email', + ], + ]) + ->assertJson([ + 'data' => [ + 'email' => 'test@example.com', + ], ]); // Verify the license was created with correct attributes @@ -171,11 +180,22 @@ public function test_creates_license_for_existing_user() $response->assertStatus(200) ->assertJsonStructure([ - 'id', - 'user_id', - 'policy_name', - 'source', - 'key', + 'data' => [ + 'id', + 'anystack_id', + 'key', + 'policy_name', + 'source', + 'expires_at', + 'created_at', + 'updated_at', + 'email', + ], + ]) + ->assertJson([ + 'data' => [ + 'email' => 'existing@example.com', + ], ]); // Verify license was created for the existing user diff --git a/tests/Feature/Api/GetLicenseTest.php b/tests/Feature/Api/GetLicenseTest.php new file mode 100644 index 0000000..088542d --- /dev/null +++ b/tests/Feature/Api/GetLicenseTest.php @@ -0,0 +1,115 @@ +create(); + $license = License::factory()->create([ + 'user_id' => $user->id, + 'key' => 'TEST-KEY-123', + ]); + + $response = $this->getJson('/api/licenses/'.$license->key); + + $response->assertStatus(401); + } + + public function test_returns_404_for_non_existent_license() + { + $token = config('services.bifrost.api_key'); + + $response = $this->withHeaders([ + 'Authorization' => 'Bearer '.$token, + ])->getJson('/api/licenses/NON-EXISTENT-KEY'); + + $response->assertStatus(404); + } + + public function test_returns_license_with_user_email() + { + $user = User::factory()->create([ + 'email' => 'test@example.com', + 'name' => 'Test User', + ]); + + $license = License::factory()->create([ + 'user_id' => $user->id, + 'key' => 'TEST-LICENSE-KEY-123', + 'policy_name' => 'pro', + 'source' => 'bifrost', + 'anystack_id' => 'anystack_123', + ]); + + $token = config('services.bifrost.api_key'); + + $response = $this->withHeaders([ + 'Authorization' => 'Bearer '.$token, + ])->getJson('/api/licenses/'.$license->key); + + $response->assertStatus(200) + ->assertJson([ + 'data' => [ + 'id' => $license->id, + 'anystack_id' => 'anystack_123', + 'key' => 'TEST-LICENSE-KEY-123', + 'policy_name' => 'pro', + 'source' => 'bifrost', + 'email' => 'test@example.com', + ], + ]) + ->assertJsonStructure([ + 'data' => [ + 'id', + 'anystack_id', + 'key', + 'policy_name', + 'source', + 'expires_at', + 'created_at', + 'updated_at', + 'email', + ], + ]); + } + + public function test_returns_correct_license_by_key() + { + $user1 = User::factory()->create(['email' => 'user1@example.com']); + $user2 = User::factory()->create(['email' => 'user2@example.com']); + + $license1 = License::factory()->create([ + 'user_id' => $user1->id, + 'key' => 'KEY-USER-1', + ]); + + $license2 = License::factory()->create([ + 'user_id' => $user2->id, + 'key' => 'KEY-USER-2', + ]); + + $token = config('services.bifrost.api_key'); + + $response = $this->withHeaders([ + 'Authorization' => 'Bearer '.$token, + ])->getJson('/api/licenses/KEY-USER-2'); + + $response->assertStatus(200) + ->assertJson([ + 'data' => [ + 'id' => $license2->id, + 'key' => 'KEY-USER-2', + 'email' => 'user2@example.com', + ], + ]); + } +}