251 lines
8.8 KiB
PHP
251 lines
8.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Integration;
|
|
|
|
use App\Domains\Integration\Models\Integration;
|
|
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;
|
|
|
|
class IntegrationServiceTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private Tenant $tenant;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
// 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();
|
|
|
|
$headerAttachment = \App\Domains\Attachable\Models\Attachment::create([
|
|
'key' => $hdrKey,
|
|
'path' => 'tenants/' . $hdrKey . '.png',
|
|
'filename' => 'logo_header.png',
|
|
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
|
|
'mime_type' => 'image/png',
|
|
]);
|
|
$footerAttachment = \App\Domains\Attachable\Models\Attachment::create([
|
|
'key' => $ftrKey,
|
|
'path' => 'tenants/' . $ftrKey . '.png',
|
|
'filename' => 'logo_footer.png',
|
|
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
|
|
'mime_type' => 'image/png',
|
|
]);
|
|
|
|
// Create a test tenant
|
|
$this->tenant = Tenant::create([
|
|
'codigo' => 'test-tenant',
|
|
'nombre' => 'Test Tenant',
|
|
'dominio' => 'test.com',
|
|
'primary_color' => '#ffffff',
|
|
'secondary_color' => '#ffffff',
|
|
'danger_color' => '#ffffff',
|
|
'success_color' => '#ffffff',
|
|
'header_bg_color' => '#ffffff',
|
|
'footer_bg_color' => '#ffffff',
|
|
'header_logo_id' => $headerAttachment->id,
|
|
'footer_logo_id' => $footerAttachment->id,
|
|
]);
|
|
}
|
|
|
|
public function test_it_throws_exception_if_integration_code_is_invalid(): void
|
|
{
|
|
$service = new TelepagosIntegrationService('invalid_code');
|
|
|
|
$this->expectException(Exception::class);
|
|
$this->expectExceptionMessage("Integration with code 'invalid_code' not found.");
|
|
|
|
$service->forTenant($this->tenant->codigo);
|
|
}
|
|
|
|
public function test_it_throws_exception_if_tenant_integration_is_not_configured(): void
|
|
{
|
|
// Seed integration but don't configure for tenant
|
|
Integration::create([
|
|
'integration_code' => 'telepagos_homo',
|
|
'name' => 'Telepagos',
|
|
'url' => 'https://api.telepagos.com.ar',
|
|
'integration_data_schema' => [
|
|
'username' => 'required|string',
|
|
'password' => 'required|string',
|
|
]
|
|
]);
|
|
|
|
$service = new TelepagosIntegrationService('telepagos');
|
|
|
|
$this->expectException(Exception::class);
|
|
$this->expectExceptionMessage("Tenant 'test-tenant' does not have integration 'telepagos_homo' configured.");
|
|
|
|
$service->forTenant($this->tenant->codigo);
|
|
}
|
|
|
|
public function test_it_resolves_base_url_and_paths(): void
|
|
{
|
|
Integration::create([
|
|
'integration_code' => 'telepagos_homo',
|
|
'name' => 'Telepagos',
|
|
'url' => 'https://api.telepagos.com.ar/', // Trailing slash to test trimming
|
|
'integration_data_schema' => [
|
|
'username' => 'required|string',
|
|
'password' => 'required|string',
|
|
]
|
|
]);
|
|
|
|
TenantIntegration::create([
|
|
'tenant_code' => $this->tenant->codigo,
|
|
'integration_code' => 'telepagos_homo',
|
|
'integration_data' => [
|
|
'username' => 'user123',
|
|
'password' => 'pass123',
|
|
]
|
|
]);
|
|
|
|
$service = new TelepagosIntegrationService('telepagos');
|
|
$service->forTenant($this->tenant->codigo);
|
|
|
|
$this->assertEquals('https://api.telepagos.com.ar', $service->getUrl());
|
|
$this->assertEquals('https://api.telepagos.com.ar/v1/payments', $service->getUrl('v1/payments'));
|
|
$this->assertEquals('https://api.telepagos.com.ar/v1/payments', $service->getUrl('/v1/payments'));
|
|
}
|
|
|
|
public function test_it_generates_correct_headers_for_telepagos_using_cached_token(): 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',
|
|
]
|
|
]);
|
|
|
|
// 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();
|
|
|
|
$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_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' => '',
|
|
'password' => 'tele_pass',
|
|
]
|
|
]);
|
|
|
|
$service = new TelepagosIntegrationService('telepagos');
|
|
$service->forTenant($this->tenant->codigo);
|
|
|
|
$this->expectException(Exception::class);
|
|
$this->expectExceptionMessage("Missing username or password in Telepagos integration settings.");
|
|
|
|
$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();
|
|
}
|
|
}
|