Introduces App\Support\DashboardSchedule (modelled on LHITS_Dashboard's pattern) as a single settings-driven registry for recurring Artisan jobs, replacing the two hard-coded Schedule::command() lines in routes/console.php. Migrated unchanged: - integrations_refresh (integrations:refresh), every_30_seconds, withoutOverlapping(5) - alerts_scan (alerts:scan), every_30_seconds, withoutOverlapping(5) New: - etsy_sync (etsy:sync), every_15_minutes - forces a fresh pull of Etsy receipts/listings into the existing IntegrationCache entries, decoupled from the bundled integrations:refresh job so it can be tuned/disabled independently of Parcel2Go/BamBuddy/dryer refreshes. - etsy_token_refresh (etsy:refresh-token), every_45_minutes - proactively refreshes the Etsy OAuth token (tokens last ~1hr) via a new public Etsy::refreshAccessToken() wrapper around the existing on-demand accessToken() refresh logic, so the connection survives quiet periods. Frequency is persisted to storage/app/dashboard-schedules.json (no app_settings table exists in this app, unlike LHITS's AppSetting-backed store) and surfaced in a new 'Scheduler' section on the existing Settings page (ScheduleSettingsController + routes), with per-job frequency dropdowns and manual 'Run now' buttons, matching the page's existing Tailwind conventions. Tests: SchedulerTest (registry/frequency behavior), ScheduleSettingsTest (settings UI + authorization), EtsySyncScheduledJobsTest (new commands, Http::fake only, no live Etsy calls).
100 lines
2.9 KiB
PHP
100 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\EtsyAppCredential;
|
|
use App\Models\EtsyConnection;
|
|
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Tests\TestCase;
|
|
|
|
class EtsySyncScheduledJobsTest extends TestCase
|
|
{
|
|
use LazilyRefreshDatabase;
|
|
|
|
public function test_etsy_sync_skips_cleanly_when_shop_is_not_connected(): void
|
|
{
|
|
Http::fake(fn () => Http::response([], 500));
|
|
|
|
$this->artisan('etsy:sync')->assertExitCode(0);
|
|
|
|
Http::assertNothingSent();
|
|
}
|
|
|
|
public function test_etsy_sync_refreshes_receipts_and_listings(): void
|
|
{
|
|
$this->connectShop();
|
|
|
|
Http::fake([
|
|
'*/receipts*' => Http::response(['count' => 1, 'results' => [[
|
|
'receipt_id' => 1, 'status' => 'open', 'was_paid' => true, 'was_shipped' => false,
|
|
]]]),
|
|
'*/listings*' => Http::response(['count' => 1, 'results' => [['listing_id' => 11]]]),
|
|
]);
|
|
|
|
$this->artisan('etsy:sync')->assertExitCode(0);
|
|
|
|
Http::assertSentCount(2);
|
|
}
|
|
|
|
public function test_etsy_refresh_token_skips_cleanly_when_shop_is_not_connected(): void
|
|
{
|
|
Http::fake(fn () => Http::response([], 500));
|
|
|
|
$this->artisan('etsy:refresh-token')->assertExitCode(0);
|
|
|
|
Http::assertNothingSent();
|
|
}
|
|
|
|
public function test_etsy_refresh_token_proactively_refreshes_an_expired_token(): void
|
|
{
|
|
$connection = $this->connectShop(expiresAt: now()->subMinute());
|
|
|
|
Http::fake([
|
|
'*/oauth/token' => Http::response([
|
|
'access_token' => 'new-access-token',
|
|
'refresh_token' => 'new-refresh-token',
|
|
'expires_in' => 3600,
|
|
]),
|
|
]);
|
|
|
|
$this->artisan('etsy:refresh-token')->assertExitCode(0);
|
|
|
|
Http::assertSentCount(1);
|
|
$connection->refresh();
|
|
$this->assertSame('new-access-token', $connection->access_token);
|
|
$this->assertTrue($connection->expires_at->isFuture());
|
|
}
|
|
|
|
public function test_etsy_refresh_token_does_not_call_etsy_when_token_is_still_fresh(): void
|
|
{
|
|
$this->connectShop(expiresAt: now()->addMinutes(30));
|
|
|
|
Http::fake();
|
|
|
|
$this->artisan('etsy:refresh-token')->assertExitCode(0);
|
|
|
|
Http::assertNothingSent();
|
|
}
|
|
|
|
private function connectShop(?Carbon $expiresAt = null): EtsyConnection
|
|
{
|
|
EtsyAppCredential::create([
|
|
'id' => 1,
|
|
'keystring' => 'key',
|
|
'shared_secret' => 'secret',
|
|
'shop_name' => 'HickLab3D',
|
|
]);
|
|
|
|
return EtsyConnection::create([
|
|
'shop_id' => 123,
|
|
'shop_name' => 'HickLab3D',
|
|
'access_token' => 'token',
|
|
'refresh_token' => 'refresh',
|
|
'expires_at' => $expiresAt ?? now()->addHour(),
|
|
'scopes' => 'listings_r listings_w transactions_r transactions_w shops_r',
|
|
]);
|
|
}
|
|
}
|