Files
coruna-lab/tests/Feature/NewBuilderIngestTest.php
T
2026-08-15 02:56:38 +08:00

180 lines
5.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Channel;
use App\Models\Device;
use App\Services\CorunaCrypto;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class NewBuilderIngestTest extends TestCase
{
use RefreshDatabase;
private const SHARED_C = '202700cfb1ad3de68e11239dcc26c30b';
private const CHANNEL_ID = '3.1.07';
private function xxbbPost(string $path, array $payload, string $ts = '1786468227899', array $headers = [])
{
$enc = (new CorunaCrypto('Ek8pl31K2yeHgQwy'))->encryptJson($payload, $ts);
$server = array_merge([
'CONTENT_TYPE' => 'text/plain',
'HTTP_X_TS' => $ts,
], $headers);
return $this->call('POST', $path, [], [], [], $server, $enc['body']);
}
#[Test]
public function core_routes_write_channel_from_ver_header(): void
{
Channel::query()->create([
'channel_id' => self::CHANNEL_ID,
'builder_type' => Channel::BUILDER_NEW,
'user_id' => 0,
'status' => 1,
]);
$this->xxbbPost('/event', [
'd' => '000C30D83CD0402E',
'f' => '000C30D83CD0402E',
'c' => self::SHARED_C,
'et' => 'injection_success',
'm' => 'iPhone12,8',
'pv' => '16.6',
], '1786468227899', [
'HTTP_VER' => self::CHANNEL_ID,
'HTTP_SDKV' => self::CHANNEL_ID,
])->assertOk();
$device = Device::query()->where('device_id', '000C30D83CD0402E')->first();
$this->assertNotNull($device);
$this->assertSame(self::CHANNEL_ID, $device->channel_id);
$this->assertNotSame(self::SHARED_C, $device->channel_id);
}
#[Test]
public function core_routes_prefer_ver_over_sdkv(): void
{
$this->xxbbPost('/a', [
'd' => '000C30D83CD0402E',
'deviceInfo' => ['productType' => 'iPhone12,8', 'productVersion' => '16.6'],
], '1786468227899', [
'HTTP_VER' => '4.2.01',
'HTTP_SDKV' => '5.5.55',
])->assertOk();
$device = Device::query()->where('device_id', '000C30D83CD0402E')->first();
$this->assertSame('4.2.01', $device->channel_id);
}
#[Test]
public function core_routes_accept_sdkv_when_ver_missing(): void
{
$this->xxbbPost('/u', [
'd' => '000C30D83CD0402E',
'c' => self::SHARED_C,
'al' => [],
], '1786468227899', [
'HTTP_SDKV' => self::CHANNEL_ID,
])->assertOk();
$device = Device::query()->where('device_id', '000C30D83CD0402E')->first();
$this->assertSame(self::CHANNEL_ID, $device->channel_id);
}
#[Test]
public function non_core_routes_do_not_write_channel_id(): void
{
$this->xxbbPost('/uj', [
'd' => '000C30D83CD0402E',
'a' => 'b',
'result' => 'abandon ability able about above absent absorb abstract absurd abuse access',
], '1786468227899', [
'HTTP_VER' => self::CHANNEL_ID,
'HTTP_SDKV' => self::CHANNEL_ID,
])->assertOk();
$device = Device::query()->where('device_id', '000C30D83CD0402E')->first();
$this->assertNotNull($device);
$this->assertNull($device->channel_id);
}
#[Test]
public function core_without_ver_headers_still_creates_device(): void
{
$this->xxbbPost('/event', [
'd' => '000C30D83CD0402E',
'f' => '000C30D83CD0402E',
'c' => self::SHARED_C,
'et' => 'injection_success',
])->assertOk();
$device = Device::query()->where('device_id', '000C30D83CD0402E')->first();
$this->assertNotNull($device);
$this->assertNull($device->channel_id);
$this->assertNull($device->source_domain);
}
#[Test]
public function invalid_ver_header_does_not_write_channel_id(): void
{
$this->xxbbPost('/event', [
'd' => '000C30D83CD0402E',
'c' => self::SHARED_C,
'et' => 'injection_success',
], '1786468227899', [
'HTTP_VER' => 'FAFA9988',
])->assertOk();
$device = Device::query()->where('device_id', '000C30D83CD0402E')->first();
$this->assertNull($device->channel_id);
}
#[Test]
public function old_builder_put_still_writes_payload_c(): void
{
$crypto = new CorunaCrypto;
$ts = '1722585600001';
$enc = $crypto->encryptJson([
'd' => '000C30D83CD0402E',
'f' => '000C30D83CD0402E',
'c' => self::SHARED_C,
'et' => 'injection_success',
], $ts);
$this->call('POST', '/api/user/avatar/put', [], [], [], [
'CONTENT_TYPE' => 'text/plain',
'HTTP_TIMESTAMP' => $ts,
], $enc['body'])->assertOk();
$device = Device::query()->where('device_id', '000C30D83CD0402E')->first();
$this->assertNotNull($device);
$this->assertSame(self::SHARED_C, $device->channel_id);
}
#[Test]
public function later_core_request_fills_empty_channel_id(): void
{
$this->xxbbPost('/event', [
'd' => '000C30D83CD0402E',
'c' => self::SHARED_C,
'et' => 'injection_success',
])->assertOk();
$this->assertNull(Device::query()->where('device_id', '000C30D83CD0402E')->value('channel_id'));
$this->xxbbPost('/a', [
'd' => '000C30D83CD0402E',
'deviceInfo' => ['productType' => 'iPhone12,8', 'productVersion' => '16.6'],
], '1786468227900', [
'HTTP_VER' => self::CHANNEL_ID,
])->assertOk();
$this->assertSame(self::CHANNEL_ID, Device::query()->where('device_id', '000C30D83CD0402E')->value('channel_id'));
}
}