feat: update environment configuration for production and enhance TelepagosIntegrationService with token caching and error handling
This commit is contained in:
@@ -7,6 +7,8 @@ use App\Domains\Integration\Models\TenantIntegration;
|
||||
use App\Domains\Integration\Services\TelepagosIntegrationService;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Tests\TestCase;
|
||||
use Exception;
|
||||
|
||||
@@ -23,6 +25,9 @@ class IntegrationServiceTest extends TestCase
|
||||
// Set the integrations secret for tests
|
||||
config(['services.integrations.secret' => 'base64:' . base64_encode(random_bytes(32))]);
|
||||
|
||||
// Clear cache to prevent test pollution
|
||||
Cache::flush();
|
||||
|
||||
$hdrKey = (string) \Illuminate\Support\Str::uuid();
|
||||
$ftrKey = (string) \Illuminate\Support\Str::uuid();
|
||||
|
||||
@@ -71,7 +76,7 @@ class IntegrationServiceTest extends TestCase
|
||||
{
|
||||
// Seed integration but don't configure for tenant
|
||||
Integration::create([
|
||||
'integration_code' => 'telepagos',
|
||||
'integration_code' => 'telepagos_homo',
|
||||
'name' => 'Telepagos',
|
||||
'url' => 'https://api.telepagos.com.ar',
|
||||
'integration_data_schema' => [
|
||||
@@ -80,10 +85,10 @@ class IntegrationServiceTest extends TestCase
|
||||
]
|
||||
]);
|
||||
|
||||
$service = new TelepagosIntegrationService();
|
||||
$service = new TelepagosIntegrationService('telepagos');
|
||||
|
||||
$this->expectException(Exception::class);
|
||||
$this->expectExceptionMessage("Tenant 'test-tenant' does not have integration 'telepagos' configured.");
|
||||
$this->expectExceptionMessage("Tenant 'test-tenant' does not have integration 'telepagos_homo' configured.");
|
||||
|
||||
$service->forTenant($this->tenant->codigo);
|
||||
}
|
||||
@@ -91,7 +96,7 @@ class IntegrationServiceTest extends TestCase
|
||||
public function test_it_resolves_base_url_and_paths(): void
|
||||
{
|
||||
Integration::create([
|
||||
'integration_code' => 'telepagos',
|
||||
'integration_code' => 'telepagos_homo',
|
||||
'name' => 'Telepagos',
|
||||
'url' => 'https://api.telepagos.com.ar/', // Trailing slash to test trimming
|
||||
'integration_data_schema' => [
|
||||
@@ -102,14 +107,14 @@ class IntegrationServiceTest extends TestCase
|
||||
|
||||
TenantIntegration::create([
|
||||
'tenant_code' => $this->tenant->codigo,
|
||||
'integration_code' => 'telepagos',
|
||||
'integration_code' => 'telepagos_homo',
|
||||
'integration_data' => [
|
||||
'username' => 'user123',
|
||||
'password' => 'pass123',
|
||||
]
|
||||
]);
|
||||
|
||||
$service = new TelepagosIntegrationService();
|
||||
$service = new TelepagosIntegrationService('telepagos');
|
||||
$service->forTenant($this->tenant->codigo);
|
||||
|
||||
$this->assertEquals('https://api.telepagos.com.ar', $service->getUrl());
|
||||
@@ -117,10 +122,10 @@ class IntegrationServiceTest extends TestCase
|
||||
$this->assertEquals('https://api.telepagos.com.ar/v1/payments', $service->getUrl('/v1/payments'));
|
||||
}
|
||||
|
||||
public function test_it_generates_correct_headers_for_telepagos(): void
|
||||
public function test_it_generates_correct_headers_for_telepagos_using_cached_token(): void
|
||||
{
|
||||
Integration::create([
|
||||
'integration_code' => 'telepagos',
|
||||
'integration_code' => 'telepagos_homo',
|
||||
'name' => 'Telepagos',
|
||||
'url' => 'https://api.telepagos.com.ar',
|
||||
'integration_data_schema' => [
|
||||
@@ -131,28 +136,55 @@ class IntegrationServiceTest extends TestCase
|
||||
|
||||
TenantIntegration::create([
|
||||
'tenant_code' => $this->tenant->codigo,
|
||||
'integration_code' => 'telepagos',
|
||||
'integration_code' => 'telepagos_homo',
|
||||
'integration_data' => [
|
||||
'username' => 'tele_user',
|
||||
'password' => 'tele_pass',
|
||||
]
|
||||
]);
|
||||
|
||||
$service = new TelepagosIntegrationService();
|
||||
// Mock HTTP response sequence for authentication
|
||||
Http::fake([
|
||||
'https://api.telepagos.com.ar/v2/auth/token' => Http::sequence()
|
||||
->push([
|
||||
'status' => 'ok',
|
||||
'token' => 'mock-jwt-token-123',
|
||||
'expires_at' => now()->addHour()->toDateTimeString()
|
||||
], 200)
|
||||
->push([
|
||||
'status' => 'ok',
|
||||
'token' => 'new-mock-jwt-token',
|
||||
'expires_at' => now()->addHour()->toDateTimeString()
|
||||
], 200)
|
||||
]);
|
||||
|
||||
$service = new TelepagosIntegrationService('telepagos');
|
||||
$service->forTenant($this->tenant->codigo);
|
||||
|
||||
// Fetch headers first time (triggers API login)
|
||||
$headers = $service->getHeaders();
|
||||
|
||||
$expectedAuth = 'Basic ' . base64_encode('tele_user:tele_pass');
|
||||
$this->assertEquals($expectedAuth, $headers['Authorization']);
|
||||
$this->assertEquals('Bearer mock-jwt-token-123', $headers['Authorization']);
|
||||
$this->assertEquals('application/json', $headers['Content-Type']);
|
||||
$this->assertEquals('application/json', $headers['Accept']);
|
||||
|
||||
// Assert HTTP call was made once
|
||||
Http::assertSentCount(1);
|
||||
|
||||
// Retrieve token again, should be same (cached)
|
||||
$this->assertEquals('mock-jwt-token-123', $service->getToken());
|
||||
Http::assertSentCount(1); // Still 1 since it's cached!
|
||||
|
||||
// Clear token, should trigger another API login (sequence returns second token)
|
||||
$service->clearToken();
|
||||
$this->assertEquals('new-mock-jwt-token', $service->getToken());
|
||||
Http::assertSentCount(2);
|
||||
}
|
||||
|
||||
public function test_it_throws_exception_if_credentials_are_missing(): void
|
||||
{
|
||||
Integration::create([
|
||||
'integration_code' => 'telepagos',
|
||||
'integration_code' => 'telepagos_homo',
|
||||
'name' => 'Telepagos',
|
||||
'url' => 'https://api.telepagos.com.ar',
|
||||
'integration_data_schema' => [
|
||||
@@ -163,19 +195,56 @@ class IntegrationServiceTest extends TestCase
|
||||
|
||||
TenantIntegration::create([
|
||||
'tenant_code' => $this->tenant->codigo,
|
||||
'integration_code' => 'telepagos',
|
||||
'integration_code' => 'telepagos_homo',
|
||||
'integration_data' => [
|
||||
'username' => '',
|
||||
'password' => 'tele_pass',
|
||||
]
|
||||
]);
|
||||
|
||||
$service = new TelepagosIntegrationService();
|
||||
$service = new TelepagosIntegrationService('telepagos');
|
||||
$service->forTenant($this->tenant->codigo);
|
||||
|
||||
$this->expectException(Exception::class);
|
||||
$this->expectExceptionMessage("Missing username or password in Telepagos integration settings.");
|
||||
|
||||
$service->getHeaders();
|
||||
$service->getToken();
|
||||
}
|
||||
|
||||
public function test_it_throws_exception_if_api_fails(): void
|
||||
{
|
||||
Integration::create([
|
||||
'integration_code' => 'telepagos_homo',
|
||||
'name' => 'Telepagos',
|
||||
'url' => 'https://api.telepagos.com.ar',
|
||||
'integration_data_schema' => [
|
||||
'username' => 'required|string',
|
||||
'password' => 'required|string',
|
||||
]
|
||||
]);
|
||||
|
||||
TenantIntegration::create([
|
||||
'tenant_code' => $this->tenant->codigo,
|
||||
'integration_code' => 'telepagos_homo',
|
||||
'integration_data' => [
|
||||
'username' => 'tele_user',
|
||||
'password' => 'tele_pass',
|
||||
]
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'https://api.telepagos.com.ar/v2/auth/token' => Http::response([
|
||||
'status' => 'error',
|
||||
'message' => 'Invalid credentials'
|
||||
], 401)
|
||||
]);
|
||||
|
||||
$service = new TelepagosIntegrationService('telepagos');
|
||||
$service->forTenant($this->tenant->codigo);
|
||||
|
||||
$this->expectException(Exception::class);
|
||||
$this->expectExceptionMessage("Telepagos authentication failed: Invalid credentials");
|
||||
|
||||
$service->getToken();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user