sls/tests/Feature/IceServerControllerTest.php
kushal.saha 3aca6d90f3 feat(webrtc): implement IceCandidateProviderInterface with Metered and ConvexSol drivers
- Add Metered and ConvexSol (CoTURN) provider drivers with response validation and caching
- Add IceCandidateManager with automatic failover and structured logging
- Add IceCandidateProviderInterface and DTOs conforming to RTCIceServer format
2026-08-26 12:44:41 +00:00

118 lines
3.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Interview;
use App\Models\User;
use App\Services\IceCandidate\IceCandidateManager;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class IceServerControllerTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Cache::flush();
$this->app->forgetInstance(IceCandidateManager::class);
}
public function test_unauthenticated_request_is_denied(): void
{
$response = $this->getJson(route('ice-servers'));
$response->assertStatus(401)
->assertJson(['error' => 'Unauthenticated access.']);
}
public function test_authenticated_user_can_fetch_ice_servers(): void
{
$user = User::create([
'name' => 'John Interviewer',
'email' => 'john@company.com',
'role' => 'user',
]);
Http::fake([
'*metered*' => Http::response([
['urls' => 'stun:stun.relay.metered.ca:80'],
[
'urls' => 'turn:standard.relay.metered.ca:80',
'username' => 'metered_usr',
'credential' => 'metered_pwd',
],
], 200),
]);
$response = $this->actingAs($user)->getJson(route('ice-servers'));
$response->assertStatus(200);
$data = $response->json();
$this->assertIsArray($data);
$this->assertCount(2, $data);
$this->assertEquals('stun:stun.relay.metered.ca:80', $data[0]['urls']);
$this->assertEquals('metered_usr', $data[1]['username']);
}
public function test_authenticated_candidate_session_can_fetch_ice_servers(): void
{
$interview = Interview::create([
'candidate_name' => 'Alice Candidate',
'candidate_email' => 'alice@gmail.com',
'candidate_phone' => '9876543210',
'temp_password' => 'Pass-123456',
'expires_at' => now()->addHours(2),
'language' => 'python',
'status' => 'scheduled',
'submission_unique_id' => 'alice-candidate_9876543210_1752000099',
]);
Http::fake([
'*metered*' => Http::response([
['urls' => 'stun:stun.relay.metered.ca:80'],
], 200),
]);
$response = $this->withSession(['candidate_interview_id' => $interview->id])
->getJson(route('ice-servers'));
$response->assertStatus(200);
$data = $response->json();
$this->assertCount(1, $data);
$this->assertEquals('stun:stun.relay.metered.ca:80', $data[0]['urls']);
}
public function test_switches_to_convexsol_provider_via_configuration(): void
{
$user = User::create([
'name' => 'Admin User',
'email' => 'admin@company.com',
'role' => 'admin',
]);
config([
'services.ice.default' => 'convexsol',
'services.ice.providers.convexsol' => [
'username' => 'convex_corp_user',
'password' => 'convex_corp_pass',
'turn_urls' => ['turn:turn.convexsol.com:3478'],
'stun_urls' => ['stun:turn.convexsol.com:3478'],
],
]);
$response = $this->actingAs($user)->getJson(route('ice-servers'));
$response->assertStatus(200);
$data = $response->json();
$this->assertCount(2, $data);
$this->assertEquals(['stun:turn.convexsol.com:3478'], $data[0]['urls']);
$this->assertEquals('convex_corp_user', $data[1]['username']);
$this->assertEquals('convex_corp_pass', $data[1]['credential']);
}
}