2FAuth/tests/Feature/Services/ReleaseRadarServiceTest.php

163 lines
4.2 KiB
PHP
Raw Normal View History

2022-12-09 10:52:17 +01:00
<?php
namespace Tests\Feature\Services;
use App\Facades\Settings;
2023-03-10 16:03:42 +01:00
// use App\Services\ReleaseRadarService;
use Facades\App\Services\ReleaseRadarService;
2022-12-09 10:52:17 +01:00
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Support\Facades\Http;
2023-08-01 11:28:27 +02:00
use PHPUnit\Framework\Attributes\CoversClass;
2022-12-09 10:52:17 +01:00
use Tests\Data\HttpRequestTestData;
2022-12-13 12:07:29 +01:00
use Tests\FeatureTestCase;
2022-12-09 10:52:17 +01:00
/**
2023-08-01 11:28:27 +02:00
* ReleaseRadarServiceTest test class
2022-12-09 10:52:17 +01:00
*/
2023-09-17 22:08:47 +02:00
#[CoversClass(\App\Services\ReleaseRadarService::class)]
2022-12-09 10:52:17 +01:00
class ReleaseRadarServiceTest extends FeatureTestCase
{
use WithoutMiddleware;
/**
* @test
*/
public function test_manualScan_returns_no_new_release()
{
$url = config('2fauth.latestReleaseUrl');
Http::preventStrayRequests();
Http::fake([
$url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NO_NEW_RELEASE, 200),
]);
2023-03-10 16:03:42 +01:00
$this->assertFalse(ReleaseRadarService::manualScan());
2022-12-09 10:52:17 +01:00
$this->assertDatabaseHas('options', [
2022-12-13 12:07:29 +01:00
'key' => 'lastRadarScan',
2022-12-09 10:52:17 +01:00
]);
$this->assertDatabaseMissing('options', [
2022-12-13 12:07:29 +01:00
'key' => 'latestRelease',
'value' => HttpRequestTestData::TAG_NAME,
2022-12-09 10:52:17 +01:00
]);
}
/**
* @test
*/
public function test_manualScan_returns_new_release()
{
$url = config('2fauth.latestReleaseUrl');
Http::preventStrayRequests();
Http::fake([
$url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NEW_RELEASE, 200),
]);
2023-03-10 16:03:42 +01:00
$this->assertEquals(HttpRequestTestData::NEW_TAG_NAME, ReleaseRadarService::manualScan());
2022-12-09 10:52:17 +01:00
$this->assertDatabaseHas('options', [
'key' => 'latestRelease',
2022-12-13 12:07:29 +01:00
'value' => HttpRequestTestData::NEW_TAG_NAME,
2022-12-09 10:52:17 +01:00
]);
$this->assertDatabaseHas('options', [
2022-12-13 12:07:29 +01:00
'key' => 'lastRadarScan',
2022-12-09 10:52:17 +01:00
]);
}
/**
* @test
*/
2023-03-10 16:03:42 +01:00
public function test_manualScan_complete_when_http_call_fails()
2022-12-09 10:52:17 +01:00
{
// We do not fake the http request so an exception will be thrown
Http::preventStrayRequests();
$this->assertNull(ReleaseRadarService::manualScan());
2022-12-09 10:52:17 +01:00
}
/**
* @test
*/
public function test_manualScan_succeed_when_github_is_unreachable()
{
$url = config('2fauth.latestReleaseUrl');
Http::preventStrayRequests();
Http::fake([
$url => Http::response(null, 400),
]);
$this->assertNull(ReleaseRadarService::manualScan());
2022-12-09 10:52:17 +01:00
}
/**
* @test
*/
public function test_scheduleScan_runs_after_one_week()
{
$url = config('2fauth.latestReleaseUrl');
Http::preventStrayRequests();
Http::fake([
$url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NEW_RELEASE, 200),
]);
2023-03-10 16:03:42 +01:00
$time = time() - (60 * 60 * 24 * 7) - 1;
Settings::set('lastRadarScan', $time);
Settings::delete('latestRelease');
2022-12-09 10:52:17 +01:00
2023-03-10 16:03:42 +01:00
ReleaseRadarService::scheduledScan();
2022-12-09 10:52:17 +01:00
2023-03-10 16:03:42 +01:00
$this->assertDatabaseHas('options', [
2023-03-10 22:59:46 +01:00
'key' => 'latestRelease',
2023-03-10 16:03:42 +01:00
'value' => HttpRequestTestData::NEW_TAG_NAME,
]);
$this->assertDatabaseMissing('options', [
2023-03-10 22:59:46 +01:00
'key' => 'lastRadarScan',
2023-03-10 16:03:42 +01:00
'value' => $time,
]);
2022-12-09 10:52:17 +01:00
}
/**
* @test
*/
public function test_scheduleScan_does_not_run_before_one_week()
{
2023-03-10 16:03:42 +01:00
$url = config('2fauth.latestReleaseUrl');
2022-12-09 10:52:17 +01:00
2023-03-10 16:03:42 +01:00
Http::preventStrayRequests();
Http::fake([
$url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NEW_RELEASE, 200),
]);
$time = time() - (60 * 60 * 24 * 7) + 1;
Settings::set('latestRelease', 'v1');
Settings::set('lastRadarScan', $time);
ReleaseRadarService::scheduledScan();
$this->assertDatabaseHas('options', [
2023-03-10 22:59:46 +01:00
'key' => 'latestRelease',
2023-03-10 16:03:42 +01:00
'value' => 'v1',
]);
$this->assertDatabaseHas('options', [
2023-03-10 22:59:46 +01:00
'key' => 'lastRadarScan',
2023-03-10 16:03:42 +01:00
'value' => $time,
]);
}
/**
* @test
*/
public function test_scheduleScan_complete_when_http_call_fails()
{
// We do not fake the http request so an exception will be thrown
Http::preventStrayRequests();
2022-12-09 10:52:17 +01:00
2023-03-10 16:03:42 +01:00
$this->assertNull(ReleaseRadarService::scheduledScan());
2022-12-09 10:52:17 +01:00
}
}