182 lines
6.3 KiB
PHP
182 lines
6.3 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 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))]);
|
|
|
|
$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',
|
|
'name' => 'Telepagos',
|
|
'url' => 'https://api.telepagos.com.ar',
|
|
'integration_data_schema' => [
|
|
'username' => 'required|string',
|
|
'password' => 'required|string',
|
|
]
|
|
]);
|
|
|
|
$service = new TelepagosIntegrationService();
|
|
|
|
$this->expectException(Exception::class);
|
|
$this->expectExceptionMessage("Tenant 'test-tenant' does not have integration 'telepagos' configured.");
|
|
|
|
$service->forTenant($this->tenant->codigo);
|
|
}
|
|
|
|
public function test_it_resolves_base_url_and_paths(): void
|
|
{
|
|
Integration::create([
|
|
'integration_code' => 'telepagos',
|
|
'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',
|
|
'integration_data' => [
|
|
'username' => 'user123',
|
|
'password' => 'pass123',
|
|
]
|
|
]);
|
|
|
|
$service = new TelepagosIntegrationService();
|
|
$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(): void
|
|
{
|
|
Integration::create([
|
|
'integration_code' => 'telepagos',
|
|
'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',
|
|
'integration_data' => [
|
|
'username' => 'tele_user',
|
|
'password' => 'tele_pass',
|
|
]
|
|
]);
|
|
|
|
$service = new TelepagosIntegrationService();
|
|
$service->forTenant($this->tenant->codigo);
|
|
|
|
$headers = $service->getHeaders();
|
|
|
|
$expectedAuth = 'Basic ' . base64_encode('tele_user:tele_pass');
|
|
$this->assertEquals($expectedAuth, $headers['Authorization']);
|
|
$this->assertEquals('application/json', $headers['Content-Type']);
|
|
$this->assertEquals('application/json', $headers['Accept']);
|
|
}
|
|
|
|
public function test_it_throws_exception_if_credentials_are_missing(): void
|
|
{
|
|
Integration::create([
|
|
'integration_code' => 'telepagos',
|
|
'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',
|
|
'integration_data' => [
|
|
'username' => '',
|
|
'password' => 'tele_pass',
|
|
]
|
|
]);
|
|
|
|
$service = new TelepagosIntegrationService();
|
|
$service->forTenant($this->tenant->codigo);
|
|
|
|
$this->expectException(Exception::class);
|
|
$this->expectExceptionMessage("Missing username or password in Telepagos integration settings.");
|
|
|
|
$service->getHeaders();
|
|
}
|
|
}
|