Update Feature tests

This commit is contained in:
Bubka
2023-03-10 16:03:42 +01:00
parent 4753401827
commit 80723d94ba
14 changed files with 366 additions and 128 deletions

View File

@ -103,6 +103,9 @@ class ForgotPasswordControllerTest extends FeatureTestCase
*/ */
public function test_submit_email_password_request_when_authenticated_returns_bad_request() public function test_submit_email_password_request_when_authenticated_returns_bad_request()
{ {
/**
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
*/
$user = User::factory()->create(); $user = User::factory()->create();
$this->actingAs($user, 'web-guard') $this->actingAs($user, 'web-guard')

View File

@ -191,7 +191,8 @@ class LoginTest extends FeatureTestCase
public function test_user_logout_after_inactivity_returns_teapot() public function test_user_logout_after_inactivity_returns_teapot()
{ {
// Set the autolock period to 1 minute // Set the autolock period to 1 minute
Settings::set('kickUserAfter', 1); $this->user['preferences->kickUserAfter'] = 1;
$this->user->save();
$response = $this->json('POST', '/user/login', [ $response = $this->json('POST', '/user/login', [
'email' => $this->user->email, 'email' => $this->user->email,

View File

@ -11,7 +11,7 @@ use Tests\FeatureTestCase;
class PasswordControllerTest extends FeatureTestCase class PasswordControllerTest extends FeatureTestCase
{ {
/** /**
* @var \App\Models\User * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
*/ */
protected $user; protected $user;

View File

@ -45,6 +45,39 @@ class RegisterControllerTest extends FeatureTestCase
->assertJsonFragment([ ->assertJsonFragment([
'name' => self::USERNAME, 'name' => self::USERNAME,
]); ]);
$this->assertDatabaseHas('users', [
'name' => self::USERNAME,
'email' => self::EMAIL,
]);
}
/**
* @test
*/
public function test_register_with_uppercased_email_returns_success()
{
DB::table('users')->delete();
$response = $this->json('POST', '/user', [
'name' => self::USERNAME,
'email' => strtoupper(self::EMAIL),
'password' => self::PASSWORD,
'password_confirmation' => self::PASSWORD,
])
->assertCreated()
->assertJsonStructure([
'message',
'name',
])
->assertJsonFragment([
'name' => self::USERNAME,
]);
$this->assertDatabaseHas('users', [
'name' => self::USERNAME,
'email' => self::EMAIL,
]);
} }
/** /**

View File

@ -15,7 +15,7 @@ use Tests\FeatureTestCase;
class ResetPasswordControllerTest extends FeatureTestCase class ResetPasswordControllerTest extends FeatureTestCase
{ {
/** /**
* @var \App\Models\User * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
*/ */
protected $user; protected $user;

View File

@ -3,6 +3,8 @@
namespace Tests\Feature\Http\Auth; namespace Tests\Feature\Http\Auth;
use App\Facades\Settings; use App\Facades\Settings;
use App\Models\Group;
use App\Models\TwoFAccount;
use App\Models\User; use App\Models\User;
use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Config;
use Tests\FeatureTestCase; use Tests\FeatureTestCase;
@ -14,7 +16,7 @@ use Tests\FeatureTestCase;
class UserControllerTest extends FeatureTestCase class UserControllerTest extends FeatureTestCase
{ {
/** /**
* @var \App\Models\User * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
*/ */
protected $user; protected $user;
@ -50,6 +52,14 @@ class UserControllerTest extends FeatureTestCase
'name' => self::NEW_USERNAME, 'name' => self::NEW_USERNAME,
'id' => $this->user->id, 'id' => $this->user->id,
'email' => self::NEW_EMAIL, 'email' => self::NEW_EMAIL,
'is_admin' => false,
]);
$this->assertDatabaseHas('users', [
'name' => self::NEW_USERNAME,
'id' => $this->user->id,
'email' => self::NEW_EMAIL,
'is_admin' => false,
]); ]);
} }
@ -71,6 +81,14 @@ class UserControllerTest extends FeatureTestCase
'name' => $this->user->name, 'name' => $this->user->name,
'id' => $this->user->id, 'id' => $this->user->id,
'email' => $this->user->email, 'email' => $this->user->email,
'is_admin' => $this->user->is_admin,
]);
$this->assertDatabaseHas('users', [
'name' => $this->user->name,
'id' => $this->user->id,
'email' => $this->user->email,
'is_admin' => $this->user->is_admin,
]); ]);
} }
@ -107,11 +125,43 @@ class UserControllerTest extends FeatureTestCase
*/ */
public function test_delete_user_returns_success() public function test_delete_user_returns_success()
{ {
$response = $this->actingAs($this->user, 'web-guard') TwoFAccount::factory()->for($this->user)->create();
Group::factory()->for($this->user)->create();
$admin = User::factory()->administrator()->create();
$this->assertDatabaseCount('users', 2);
$this->actingAs($this->user, 'web-guard')
->json('DELETE', '/user', [ ->json('DELETE', '/user', [
'password' => self::PASSWORD, 'password' => self::PASSWORD,
]) ])
->assertNoContent(); ->assertNoContent();
$this->assertDatabaseMissing('users', [
'id' => $this->user->id
]);
$this->assertDatabaseHas('users', [
'id' => $admin->id
]);
$this->assertDatabaseCount('users', 1);
$this->assertDatabaseMissing('twofaccounts', [
'user_id' => $this->user->id
]);
$this->assertDatabaseMissing('groups', [
'user_id' => $this->user->id
]);
$this->assertDatabaseMissing('webauthn_credentials', [
'authenticatable_id' => $this->user->id
]);
$this->assertDatabaseMissing('webauthn_recoveries', [
'email' => $this->user->email
]);
$this->assertDatabaseMissing('oauth_access_tokens', [
'user_id' => $this->user->id
]);
$this->assertDatabaseMissing('password_resets', [
'email' => $this->user->email
]);
} }
/** /**
@ -130,6 +180,10 @@ class UserControllerTest extends FeatureTestCase
->assertJsonStructure([ ->assertJsonStructure([
'message', 'message',
]); ]);
$this->assertDatabaseHas('users', [
'id' => $this->user->id
]);
} }
/** /**
@ -142,5 +196,33 @@ class UserControllerTest extends FeatureTestCase
'password' => 'wrongPassword', 'password' => 'wrongPassword',
]) ])
->assertStatus(400); ->assertStatus(400);
$this->assertDatabaseHas('users', [
'id' => $this->user->id
]);
}
/**
* @test
*/
public function test_delete_the_only_admin_returns_bad_request()
{
/**
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
*/
$admin = User::factory()->administrator()->create();
$this->assertDatabaseCount('users', 2);
$this->assertEquals(1, User::admins()->count());
$response = $this->actingAs($admin, 'web-guard')
->json('DELETE', '/user', [
'password' => self::PASSWORD,
])
->assertStatus(400);
$this->assertDatabaseHas('users', [
'id' => $admin->id
]);
} }
} }

View File

@ -194,7 +194,9 @@ class WebAuthnLoginControllerTest extends FeatureTestCase
'created_at' => now(), 'created_at' => now(),
]); ]);
$response = $this->json('POST', '/webauthn/login/options') $response = $this->json('POST', '/webauthn/login/options', [
'email' => $this->user->email,
])
->assertOk() ->assertOk()
->assertJsonStructure([ ->assertJsonStructure([
'challenge', 'challenge',
@ -234,7 +236,9 @@ class WebAuthnLoginControllerTest extends FeatureTestCase
'created_at' => now(), 'created_at' => now(),
]); ]);
$response = $this->json('POST', '/webauthn/login/options') $response = $this->json('POST', '/webauthn/login/options', [
'email' => $this->user->email,
])
->assertOk() ->assertOk()
->assertJsonStructure([ ->assertJsonStructure([
'challenge', 'challenge',
@ -253,12 +257,55 @@ class WebAuthnLoginControllerTest extends FeatureTestCase
/** /**
* @test * @test
*/ */
public function test_get_options_with_no_registred_user_returns_error() public function test_get_options_with_capitalized_email_returns_success()
{ {
$this->json('POST', '/webauthn/login/options') $this->user = User::factory()->create();
->assertStatus(400)
->assertJsonStructure([ $this->json('POST', '/webauthn/login/options', [
'message', 'email' => strtoupper($this->user->email),
])
->assertOk();
}
/**
* @test
*/
public function test_get_options_with_missing_email_returns_validation_errors()
{
$this->json('POST', '/webauthn/login/options', [
'email' => null,
])
->assertStatus(422)
->assertJsonValidationErrors([
'email',
]);
}
/**
* @test
*/
public function test_get_options_with_invalid_email_returns_validation_errors()
{
$this->json('POST', '/webauthn/login/options', [
'email' => 'invalid',
])
->assertStatus(422)
->assertJsonValidationErrors([
'email',
]);
}
/**
* @test
*/
public function test_get_options_with_unknown_email_returns_validation_errors()
{
$this->json('POST', '/webauthn/login/options', [
'email' => 'john@example.com',
])
->assertStatus(422)
->assertJsonValidationErrors([
'email',
]); ]);
} }
} }

View File

@ -17,7 +17,7 @@ class WebAuthnManageControllerTest extends FeatureTestCase
// use WithoutMiddleware; // use WithoutMiddleware;
/** /**
* @var \App\Models\User * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
*/ */
protected $user; protected $user;

View File

@ -16,7 +16,7 @@ use Tests\FeatureTestCase;
class WebAuthnRegisterControllerTest extends FeatureTestCase class WebAuthnRegisterControllerTest extends FeatureTestCase
{ {
/** /**
* @var \App\Models\User * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
*/ */
protected $user; protected $user;

View File

@ -3,6 +3,7 @@
namespace Tests\Feature\Http\Requests; namespace Tests\Feature\Http\Requests;
use App\Http\Requests\UserStoreRequest; use App\Http\Requests\UserStoreRequest;
use App\Models\User;
use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Validator;
use Tests\FeatureTestCase; use Tests\FeatureTestCase;
@ -29,6 +30,11 @@ class UserStoreRequestTest extends FeatureTestCase
*/ */
public function test_valid_data(array $data) : void public function test_valid_data(array $data) : void
{ {
User::factory()->create([
'name' => 'Jane',
'email' => 'jane@example.com',
]);
$request = new UserStoreRequest(); $request = new UserStoreRequest();
$validator = Validator::make($data, $request->rules()); $validator = Validator::make($data, $request->rules());
@ -47,6 +53,12 @@ class UserStoreRequestTest extends FeatureTestCase
'password' => 'MyPassword', 'password' => 'MyPassword',
'password_confirmation' => 'MyPassword', 'password_confirmation' => 'MyPassword',
]], ]],
[[
'name' => 'John',
'email' => 'JOHN@example.com',
'password' => 'MyPassword',
'password_confirmation' => 'MyPassword',
]],
]; ];
} }
@ -55,15 +67,10 @@ class UserStoreRequestTest extends FeatureTestCase
*/ */
public function test_invalid_data(array $data) : void public function test_invalid_data(array $data) : void
{ {
$user = new \App\Models\User( User::factory()->create([
[
'name' => 'John', 'name' => 'John',
'email' => 'john@example.com', 'email' => 'john@example.com',
'password' => 'MyPassword', ]);
'password_confirmation' => 'MyPassword',
]
);
$user->save();
$request = new UserStoreRequest(); $request = new UserStoreRequest();
$validator = Validator::make($data, $request->rules()); $validator = Validator::make($data, $request->rules());
@ -78,8 +85,8 @@ class UserStoreRequestTest extends FeatureTestCase
{ {
return [ return [
[[ [[
'name' => 'John', // unique 'name' => 'John',
'email' => 'john@example.com', 'email' => 'john@example.com', // unique
'password' => 'MyPassword', 'password' => 'MyPassword',
'password_confirmation' => 'MyPassword', 'password_confirmation' => 'MyPassword',
]], ]],

View File

@ -3,15 +3,16 @@
namespace Tests\Feature\Http\Requests; namespace Tests\Feature\Http\Requests;
use App\Http\Requests\UserUpdateRequest; use App\Http\Requests\UserUpdateRequest;
use App\Models\User;
use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Validator;
use Tests\TestCase; use Tests\FeatureTestCase;
/** /**
* @covers \App\Http\Requests\UserUpdateRequest * @covers \App\Http\Requests\UserUpdateRequest
*/ */
class UserUpdateRequestTest extends TestCase class UserUpdateRequestTest extends FeatureTestCase
{ {
use WithoutMiddleware; use WithoutMiddleware;
@ -34,6 +35,11 @@ class UserUpdateRequestTest extends TestCase
*/ */
public function test_valid_data(array $data) : void public function test_valid_data(array $data) : void
{ {
User::factory()->create([
'name' => 'Jane',
'email' => 'jane@example.com',
]);
$request = new UserUpdateRequest(); $request = new UserUpdateRequest();
$validator = Validator::make($data, $request->rules()); $validator = Validator::make($data, $request->rules());
@ -59,6 +65,11 @@ class UserUpdateRequestTest extends TestCase
*/ */
public function test_invalid_data(array $data) : void public function test_invalid_data(array $data) : void
{ {
User::factory()->create([
'name' => 'Jane',
'email' => 'jane@example.com',
]);
$request = new UserUpdateRequest(); $request = new UserUpdateRequest();
$validator = Validator::make($data, $request->rules()); $validator = Validator::make($data, $request->rules());
@ -71,6 +82,11 @@ class UserUpdateRequestTest extends TestCase
public function provideInvalidData() : array public function provideInvalidData() : array
{ {
return [ return [
[[
'name' => 'John',
'email' => 'jane@example.com', // unique
'password' => 'MyPassword',
]],
[[ [[
'name' => '', // required 'name' => '', // required
'email' => 'john@example.com', 'email' => 'john@example.com',

View File

@ -15,7 +15,7 @@ class SystemControllerTest extends FeatureTestCase
use WithoutMiddleware; use WithoutMiddleware;
/** /**
* @var \App\Models\User * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
*/ */
protected $user; protected $user;
@ -37,10 +37,12 @@ class SystemControllerTest extends FeatureTestCase
$response = $this->json('GET', '/infos') $response = $this->json('GET', '/infos')
->assertOk() ->assertOk()
->assertJsonStructure([ ->assertJsonStructure([
'common' => [
'Date', 'Date',
'userAgent', 'userAgent',
'Version', 'Version',
'Environment', 'Environment',
'Install path',
'Debug', 'Debug',
'Cache driver', 'Cache driver',
'Log channel', 'Log channel',
@ -49,22 +51,24 @@ class SystemControllerTest extends FeatureTestCase
'PHP version', 'PHP version',
'Operating system', 'Operating system',
'interface', 'interface',
]
])
->assertJsonMissing([
'user_preferences',
'admin_settings',
]); ]);
} }
/** /**
* @test * @test
*/ */
public function test_infos_returns_full_collection_when_signed_in() public function test_infos_returns_user_preferences_when_signed_in()
{ {
$response = $this->actingAs($this->user, 'api-guard') $response = $this->actingAs($this->user, 'api-guard')
->json('GET', '/infos') ->json('GET', '/infos')
->assertOk() ->assertOk()
->assertJsonStructure([ ->assertJsonStructure([
'Auth guard', 'user_preferences' => [
'webauthn user verification',
'Trusted proxies',
'options' => [
'showTokenAsDot', 'showTokenAsDot',
'closeOtpOnCopy', 'closeOtpOnCopy',
'copyOtpOnDisplay', 'copyOtpOnDisplay',
@ -75,15 +79,11 @@ class SystemControllerTest extends FeatureTestCase
'activeGroup', 'activeGroup',
'rememberActiveGroup', 'rememberActiveGroup',
'defaultGroup', 'defaultGroup',
'useEncryption',
'defaultCaptureMode', 'defaultCaptureMode',
'useDirectCapture', 'useDirectCapture',
'useWebauthnAsDefault', 'useWebauthnAsDefault',
'useWebauthnOnly', 'useWebauthnOnly',
'getOfficialIcons', 'getOfficialIcons',
'checkForUpdate',
'lastRadarScan',
'latestRelease',
'lang', 'lang',
], ],
]); ]);
@ -92,14 +92,38 @@ class SystemControllerTest extends FeatureTestCase
/** /**
* @test * @test
*/ */
public function test_infos_returns_full_collection_when_signed_in_behind_proxy() public function test_infos_returns_admin_settings_when_signed_in_as_admin()
{
/**
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
*/
$admin = User::factory()->administrator()->create();
$response = $this->actingAs($admin, 'api-guard')
->json('GET', '/infos')
->assertOk()
->assertJsonStructure([
'admin_settings' => [
'useEncryption',
'lastRadarScan',
'checkForUpdate',
]
]);
}
/**
* @test
*/
public function test_infos_returns_proxy_collection_when_signed_in_behind_proxy()
{ {
$response = $this->actingAs($this->user, 'reverse-proxy-guard') $response = $this->actingAs($this->user, 'reverse-proxy-guard')
->json('GET', '/infos') ->json('GET', '/infos')
->assertOk() ->assertOk()
->assertJsonStructure([ ->assertJsonStructure([
'common' => [
'Auth proxy header for user', 'Auth proxy header for user',
'Auth proxy header for email', 'Auth proxy header for email',
]
]); ]);
} }

View File

@ -3,6 +3,7 @@
namespace Tests\Feature\Models; namespace Tests\Feature\Models;
use App\Models\TwoFAccount; use App\Models\TwoFAccount;
use App\Models\User;
use Illuminate\Http\Testing\FileFactory; use Illuminate\Http\Testing\FileFactory;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
@ -16,14 +17,14 @@ use Tests\FeatureTestCase;
class TwoFAccountModelTest extends FeatureTestCase class TwoFAccountModelTest extends FeatureTestCase
{ {
/** /**
* App\Models\TwoFAccount $customTotpTwofaccount * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
*/ */
protected $customTotpTwofaccount; protected $user, $anotherUser;
/** /**
* App\Models\TwoFAccount $customTotpTwofaccount * @var \App\Models\TwoFAccount
*/ */
protected $customHotpTwofaccount; protected $customTotpTwofaccount, $customHotpTwofaccount, $customSteamTotpTwofaccount;
/** /**
* Helpers $helpers; * Helpers $helpers;
@ -37,43 +38,45 @@ class TwoFAccountModelTest extends FeatureTestCase
{ {
parent::setUp(); parent::setUp();
$this->customTotpTwofaccount = new TwoFAccount; $this->user = User::factory()->create();
$this->customTotpTwofaccount->legacy_uri = OtpTestData::TOTP_FULL_CUSTOM_URI;
$this->customTotpTwofaccount->service = OtpTestData::SERVICE;
$this->customTotpTwofaccount->account = OtpTestData::ACCOUNT;
$this->customTotpTwofaccount->icon = OtpTestData::ICON_PNG;
$this->customTotpTwofaccount->otp_type = 'totp';
$this->customTotpTwofaccount->secret = OtpTestData::SECRET;
$this->customTotpTwofaccount->digits = OtpTestData::DIGITS_CUSTOM;
$this->customTotpTwofaccount->algorithm = OtpTestData::ALGORITHM_CUSTOM;
$this->customTotpTwofaccount->period = OtpTestData::PERIOD_CUSTOM;
$this->customTotpTwofaccount->counter = null;
$this->customTotpTwofaccount->save();
$this->customHotpTwofaccount = new TwoFAccount; $this->customTotpTwofaccount = TwoFAccount::factory()->for($this->user)->create([
$this->customHotpTwofaccount->legacy_uri = OtpTestData::HOTP_FULL_CUSTOM_URI; 'legacy_uri' => OtpTestData::TOTP_FULL_CUSTOM_URI,
$this->customHotpTwofaccount->service = OtpTestData::SERVICE; 'service' => OtpTestData::SERVICE,
$this->customHotpTwofaccount->account = OtpTestData::ACCOUNT; 'account' => OtpTestData::ACCOUNT,
$this->customHotpTwofaccount->icon = OtpTestData::ICON_PNG; 'icon' => OtpTestData::ICON_PNG,
$this->customHotpTwofaccount->otp_type = 'hotp'; 'otp_type' => 'totp',
$this->customHotpTwofaccount->secret = OtpTestData::SECRET; 'secret' => OtpTestData::SECRET,
$this->customHotpTwofaccount->digits = OtpTestData::DIGITS_CUSTOM; 'digits' => OtpTestData::DIGITS_CUSTOM,
$this->customHotpTwofaccount->algorithm = OtpTestData::ALGORITHM_CUSTOM; 'algorithm' => OtpTestData::ALGORITHM_CUSTOM,
$this->customHotpTwofaccount->period = null; 'period' => OtpTestData::PERIOD_CUSTOM,
$this->customHotpTwofaccount->counter = OtpTestData::COUNTER_CUSTOM; 'counter' => null,
$this->customHotpTwofaccount->save(); ]);
$this->customSteamTotpTwofaccount = new TwoFAccount; $this->customHotpTwofaccount = TwoFAccount::factory()->for($this->user)->create([
$this->customSteamTotpTwofaccount->legacy_uri = OtpTestData::STEAM_TOTP_URI; 'legacy_uri' => OtpTestData::HOTP_FULL_CUSTOM_URI,
$this->customSteamTotpTwofaccount->service = OtpTestData::STEAM; 'service' => OtpTestData::SERVICE,
$this->customSteamTotpTwofaccount->account = OtpTestData::ACCOUNT; 'account' => OtpTestData::ACCOUNT,
$this->customSteamTotpTwofaccount->otp_type = 'steamtotp'; 'icon' => OtpTestData::ICON_PNG,
$this->customSteamTotpTwofaccount->secret = OtpTestData::STEAM_SECRET; 'otp_type' => 'hotp',
$this->customSteamTotpTwofaccount->digits = OtpTestData::DIGITS_STEAM; 'secret' => OtpTestData::SECRET,
$this->customSteamTotpTwofaccount->algorithm = OtpTestData::ALGORITHM_DEFAULT; 'digits' => OtpTestData::DIGITS_CUSTOM,
$this->customSteamTotpTwofaccount->period = OtpTestData::PERIOD_DEFAULT; 'algorithm' => OtpTestData::ALGORITHM_CUSTOM,
$this->customSteamTotpTwofaccount->counter = null; 'period' => null,
$this->customSteamTotpTwofaccount->save(); 'counter' => OtpTestData::COUNTER_CUSTOM,
]);
$this->customSteamTotpTwofaccount = TwoFAccount::factory()->for($this->user)->create([
'legacy_uri' => OtpTestData::STEAM_TOTP_URI,
'service' => OtpTestData::STEAM,
'account' => OtpTestData::ACCOUNT,
'otp_type' => 'steamtotp',
'secret' => OtpTestData::STEAM_SECRET,
'digits' => OtpTestData::DIGITS_STEAM,
'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
'period' => OtpTestData::PERIOD_DEFAULT,
'counter' => null,
]);
} }
/** /**

View File

@ -3,7 +3,8 @@
namespace Tests\Feature\Services; namespace Tests\Feature\Services;
use App\Facades\Settings; use App\Facades\Settings;
use App\Services\ReleaseRadarService; // use App\Services\ReleaseRadarService;
use Facades\App\Services\ReleaseRadarService;
use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Tests\Data\HttpRequestTestData; use Tests\Data\HttpRequestTestData;
@ -28,10 +29,7 @@ class ReleaseRadarServiceTest extends FeatureTestCase
$url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NO_NEW_RELEASE, 200), $url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NO_NEW_RELEASE, 200),
]); ]);
$releaseRadarService = new ReleaseRadarService(); $this->assertFalse(ReleaseRadarService::manualScan());
$release = $releaseRadarService->manualScan();
$this->assertFalse($release);
$this->assertDatabaseHas('options', [ $this->assertDatabaseHas('options', [
'key' => 'lastRadarScan', 'key' => 'lastRadarScan',
]); ]);
@ -53,10 +51,7 @@ class ReleaseRadarServiceTest extends FeatureTestCase
$url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NEW_RELEASE, 200), $url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NEW_RELEASE, 200),
]); ]);
$releaseRadarService = new ReleaseRadarService(); $this->assertEquals(HttpRequestTestData::NEW_TAG_NAME, ReleaseRadarService::manualScan());
$release = $releaseRadarService->manualScan();
$this->assertEquals(HttpRequestTestData::NEW_TAG_NAME, $release);
$this->assertDatabaseHas('options', [ $this->assertDatabaseHas('options', [
'key' => 'latestRelease', 'key' => 'latestRelease',
'value' => HttpRequestTestData::NEW_TAG_NAME, 'value' => HttpRequestTestData::NEW_TAG_NAME,
@ -69,17 +64,12 @@ class ReleaseRadarServiceTest extends FeatureTestCase
/** /**
* @test * @test
*/ */
public function test_manualScan_succeed_when_something_fails() public function test_manualScan_complete_when_http_call_fails()
{ {
$url = config('2fauth.latestReleaseUrl');
// We do not fake the http request so an exception will be thrown // We do not fake the http request so an exception will be thrown
Http::preventStrayRequests(); Http::preventStrayRequests();
$releaseRadarService = new ReleaseRadarService(); $this->assertFalse(ReleaseRadarService::manualScan());
$release = $releaseRadarService->manualScan();
$this->assertFalse($release);
} }
/** /**
@ -94,10 +84,7 @@ class ReleaseRadarServiceTest extends FeatureTestCase
$url => Http::response(null, 400), $url => Http::response(null, 400),
]); ]);
$releaseRadarService = new ReleaseRadarService(); $this->assertFalse(ReleaseRadarService::manualScan());
$release = $releaseRadarService->manualScan();
$this->assertFalse($release);
} }
/** /**
@ -112,14 +99,22 @@ class ReleaseRadarServiceTest extends FeatureTestCase
$url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NEW_RELEASE, 200), $url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NEW_RELEASE, 200),
]); ]);
Settings::set('lastRadarScan', time() - (60 * 60 * 24 * 7) - 1); $time = time() - (60 * 60 * 24 * 7) - 1;
$releaseRadarService = $this->mock(ReleaseRadarService::class)->makePartial(); Settings::set('lastRadarScan', $time);
$releaseRadarService->shouldAllowMockingProtectedMethods() Settings::delete('latestRelease');
->shouldReceive('newRelease')
->once();
$releaseRadarService->scheduledScan(); ReleaseRadarService::scheduledScan();
$this->assertDatabaseHas('options', [
'key' => 'latestRelease',
'value' => HttpRequestTestData::NEW_TAG_NAME,
]);
$this->assertDatabaseMissing('options', [
'key' => 'lastRadarScan',
'value' => $time,
]);
} }
/** /**
@ -127,12 +122,39 @@ class ReleaseRadarServiceTest extends FeatureTestCase
*/ */
public function test_scheduleScan_does_not_run_before_one_week() public function test_scheduleScan_does_not_run_before_one_week()
{ {
Settings::set('lastRadarScan', time() - (60 * 60 * 24 * 7) + 2); $url = config('2fauth.latestReleaseUrl');
$releaseRadarService = $this->mock(ReleaseRadarService::class)->makePartial(); Http::preventStrayRequests();
$releaseRadarService->shouldAllowMockingProtectedMethods() Http::fake([
->shouldNotReceive('newRelease'); $url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NEW_RELEASE, 200),
]);
$releaseRadarService->scheduledScan(); $time = time() - (60 * 60 * 24 * 7) + 1;
Settings::set('latestRelease', 'v1');
Settings::set('lastRadarScan', $time);
ReleaseRadarService::scheduledScan();
$this->assertDatabaseHas('options', [
'key' => 'latestRelease',
'value' => 'v1',
]);
$this->assertDatabaseHas('options', [
'key' => 'lastRadarScan',
'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();
$this->assertNull(ReleaseRadarService::scheduledScan());
} }
} }