Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Jobs/ReportThemeUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Http;
use Statamic\Licensing\Outpost;
use Statamic\Statamic;

class ReportThemeUsage implements ShouldQueue
{
Expand All @@ -34,7 +35,7 @@ public function handle(Outpost $outpost): void
return;
}

Http::post('https://outpost.statamic.com/v3/theme', ['theme' => $newThemeId]);
Http::withUserAgent('Statamic/'.Statamic::version())->post('https://outpost.statamic.com/v3/theme', ['theme' => $newThemeId]);
}

private function themeId($theme)
Expand Down
5 changes: 4 additions & 1 deletion src/Licensing/Outpost.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ private function performRequest()
}

$response = $this->client->request('POST', self::ENDPOINT, [
'headers' => ['accept' => 'application/json'],
'headers' => [
'accept' => 'application/json',
'user-agent' => 'Statamic/'.Statamic::version(),
],
'json' => $this->payload(),
'timeout' => self::REQUEST_TIMEOUT,
]);
Expand Down
2 changes: 2 additions & 0 deletions src/Marketplace/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Contracts\Cache\LockTimeoutException;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Support\Facades\Cache;
use Statamic\Statamic;

class Client
{
Expand Down Expand Up @@ -72,6 +73,7 @@ private function request(string $method, string $endpoint, array $params = [])
$response = Guzzle::request($method, $endpoint, [
'verify' => $this->verifySsl,
'timeout' => 5,
'headers' => ['user-agent' => 'Statamic/'.Statamic::version()],
($method === 'GET' ? 'query' : 'json') => $params,
]);

Expand Down
7 changes: 4 additions & 3 deletions src/StarterKits/LicenseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use Statamic\Console\NullConsole;
use Statamic\Statamic;

use function Laravel\Prompts\text;

Expand Down Expand Up @@ -51,7 +52,7 @@ public function isValid(): bool
*/
public function completeInstall(): void
{
Http::post(self::OUTPOST_ENDPOINT.'installed', [
Http::withUserAgent('Statamic/'.Statamic::version())->post(self::OUTPOST_ENDPOINT.'installed', [
'license' => $this->licenseKey,
'configured_site_license' => config('statamic.system.license_key'),
'package' => $this->package,
Expand Down Expand Up @@ -107,7 +108,7 @@ private function performValidation(): self
*/
private function outpostGetStarterKitDetails(): self
{
$response = Http::get(self::OUTPOST_ENDPOINT.$this->package);
$response = Http::withUserAgent('Statamic/'.Statamic::version())->get(self::OUTPOST_ENDPOINT.$this->package);

if ($response->status() !== 200) {
return false;
Expand Down Expand Up @@ -139,7 +140,7 @@ private function outpostValidatesLicense(): bool
return false;
}

$response = Http::post(self::OUTPOST_ENDPOINT.'validate', [
$response = Http::withUserAgent('Statamic/'.Statamic::version())->post(self::OUTPOST_ENDPOINT.'validate', [
'license' => $this->licenseKey,
'configured_site_license' => config('statamic.system.license_key'),
'package' => $this->package,
Expand Down
16 changes: 16 additions & 0 deletions tests/Jobs/ReportThemeUsageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ public static function reportProvider()
];
}

#[Test]
public function it_sends_the_statamic_user_agent()
{
Http::preventStrayRequests();
Http::fake();

$outpost = Mockery::mock(Outpost::class);
$outpost->shouldReceive('usingLicenseKeyFile')->andReturnFalse();

$job = new ReportThemeUsage(null, ['id' => 1]);

$job->handle($outpost);

Http::assertSent(fn (Request $request) => $request->header('User-Agent') === ['Statamic/3.0.0-testing']);
}

#[Test]
public function it_doesnt_report_if_using_license_key_file()
{
Expand Down
14 changes: 14 additions & 0 deletions tests/Licensing/OutpostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Illuminate\Encryption\Encrypter;
Expand Down Expand Up @@ -75,6 +77,18 @@ public function it_contacts_the_outpost_and_caches_the_response_for_50_to_59_min
$this->assertCachedUntilExpiry($response);
}

#[Test]
public function it_sends_the_statamic_user_agent()
{
$history = [];
$handler = HandlerStack::create(new MockHandler([new Response(200, [], '{}')]));
$handler->push(Middleware::history($history));

$this->outpost(new Client(['handler' => $handler]))->response();

$this->assertEquals('Statamic/3.0.0-testing', $history[0]['request']->getHeaderLine('User-Agent'));
}

#[Test]
public function the_cached_response_is_used()
{
Expand Down
23 changes: 23 additions & 0 deletions tests/Marketplace/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Tests\Marketplace;

use Facades\GuzzleHttp\Client as Guzzle;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Marketplace\Client;
use Tests\TestCase;

class ClientTest extends TestCase
{
#[Test]
public function it_sends_the_statamic_user_agent()
{
Guzzle::shouldReceive('request')
->once()
->withArgs(fn ($method, $endpoint, $options) => $options['headers']['user-agent'] === 'Statamic/3.0.0-testing')
->andReturn(new Response(200, [], '{}'));

(new Client)->get('packages');
}
}
26 changes: 26 additions & 0 deletions tests/StarterKits/InstallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,32 @@ public function it_installs_paid_starter_kit_with_valid_license_key()
$this->assertFileExists(base_path('copied.md'));
}

#[Test]
public function it_sends_the_statamic_user_agent_to_the_outpost()
{
Config::set('statamic.system.license_key', 'site-key');

$this->installCoolRunnings([], [
'outpost.*/v3/starter-kits/statamic/cool-runnings' => Http::response(['data' => [
'price' => 100,
'slug' => 'cool-runnings',
'seller' => ['slug' => 'statamic'],
]], 200),
'outpost.*/v3/starter-kits/validate' => Http::response(['data' => [
'valid' => true,
]], 200),
'*' => Http::response('', 200),
]);

$outpostRequests = Http::recorded(fn ($request) => str_contains($request->url(), 'outpost.statamic.com'));

$this->assertCount(3, $outpostRequests);

$outpostRequests->each(function ($pair) {
$this->assertEquals(['Statamic/3.0.0-testing'], $pair[0]->header('User-Agent'));
});
}

#[Test]
public function it_doesnt_install_paid_starter_kit_with_invalid_license_key()
{
Expand Down
Loading