Skip to content

feat: add API endpoint to retrieve license details by key #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion app/Http/Controllers/Api/LicenseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
29 changes: 29 additions & 0 deletions app/Http/Resources/Api/LicenseResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Resources\Api;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class LicenseResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
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,
];
}
}
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
44 changes: 32 additions & 12 deletions tests/Feature/Api/CreateLicenseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]',
],
]);

// Verify the license was created with correct attributes
Expand Down Expand Up @@ -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' => '[email protected]',
],
]);

// Verify license was created for the existing user
Expand Down
115 changes: 115 additions & 0 deletions tests/Feature/Api/GetLicenseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Tests\Feature\Api;

use App\Models\License;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class GetLicenseTest extends TestCase
{
use RefreshDatabase;

public function test_requires_authentication()
{
$user = User::factory()->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' => '[email protected]',
'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' => '[email protected]',
],
])
->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' => '[email protected]']);
$user2 = User::factory()->create(['email' => '[email protected]']);

$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' => '[email protected]',
],
]);
}
}