SystemControllerTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace Tests\Feature\Http;
  3. use App\Http\Controllers\SystemController;
  4. use App\Models\User;
  5. use App\Notifications\TestEmailSettingNotification;
  6. use App\Services\ReleaseRadarService;
  7. use Exception;
  8. use Illuminate\Contracts\Notifications\Dispatcher;
  9. use Illuminate\Support\Facades\Notification;
  10. use PHPUnit\Framework\Attributes\CoversClass;
  11. use PHPUnit\Framework\Attributes\Test;
  12. use Tests\FeatureTestCase;
  13. /**
  14. * SystemControllerTest test class
  15. */
  16. #[CoversClass(SystemController::class)]
  17. #[CoversClass(TestEmailSettingNotification::class)]
  18. class SystemControllerTest extends FeatureTestCase
  19. {
  20. /**
  21. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  22. */
  23. protected $user;
  24. protected $admin;
  25. public function setUp() : void
  26. {
  27. parent::setUp();
  28. $this->user = User::factory()->create();
  29. $this->admin = User::factory()->administrator()->create();
  30. }
  31. #[Test]
  32. public function test_infos_returns_unauthorized()
  33. {
  34. $response = $this->json('GET', '/system/infos')
  35. ->assertUnauthorized();
  36. }
  37. #[Test]
  38. public function test_infos_returns_forbidden()
  39. {
  40. $response = $this->actingAs($this->user, 'api-guard')
  41. ->json('GET', '/system/infos')
  42. ->assertForbidden();
  43. }
  44. #[Test]
  45. public function test_infos_returns_only_base_collection()
  46. {
  47. $response = $this->actingAs($this->admin, 'api-guard')
  48. ->json('GET', '/system/infos')
  49. ->assertOk()
  50. ->assertJsonStructure([
  51. 'common' => [
  52. 'Date',
  53. 'userAgent',
  54. 'Version',
  55. 'Environment',
  56. 'Install path',
  57. 'Debug',
  58. 'Cache driver',
  59. 'Log channel',
  60. 'Log level',
  61. 'DB driver',
  62. 'PHP version',
  63. 'Operating system',
  64. 'interface',
  65. 'Auth guard',
  66. 'webauthn user verification',
  67. 'Trusted proxies',
  68. 'lastRadarScan',
  69. ],
  70. ]);
  71. }
  72. #[Test]
  73. public function test_infos_returns_proxy_collection_when_signed_in_behind_proxy()
  74. {
  75. $response = $this->actingAs($this->admin, 'reverse-proxy-guard')
  76. ->json('GET', '/system/infos')
  77. ->assertOk()
  78. ->assertJsonStructure([
  79. 'common' => [
  80. 'Auth proxy logout url',
  81. 'Auth proxy header for user',
  82. 'Auth proxy header for email',
  83. ],
  84. ]);
  85. }
  86. #[Test]
  87. public function test_latestrelease_runs_manual_scan()
  88. {
  89. $releaseRadarService = $this->mock(ReleaseRadarService::class)->makePartial();
  90. $releaseRadarService->shouldReceive('manualScan')
  91. ->once()
  92. ->andReturn('new_release');
  93. $response = $this->json('GET', '/system/latestRelease')
  94. ->assertOk()
  95. ->assertJson([
  96. 'newRelease' => 'new_release',
  97. ]);
  98. }
  99. #[Test]
  100. public function test_testEmail_sends_a_notification()
  101. {
  102. Notification::fake();
  103. $response = $this->actingAs($this->admin, 'web-guard')
  104. ->json('POST', '/system/test-email', []);
  105. $response->assertStatus(200);
  106. Notification::assertSentTo($this->admin, TestEmailSettingNotification::class);
  107. }
  108. #[Test]
  109. public function test_testEmail_returns_unauthorized()
  110. {
  111. $response = $this->json('GET', '/system/infos')
  112. ->assertUnauthorized();
  113. }
  114. #[Test]
  115. public function test_testEmail_returns_forbidden()
  116. {
  117. $response = $this->actingAs($this->user, 'api-guard')
  118. ->json('GET', '/system/infos')
  119. ->assertForbidden();
  120. }
  121. #[Test]
  122. public function test_testEmail_returns_success_even_if_sending_fails()
  123. {
  124. Notification::fake();
  125. $this->mock(Dispatcher::class)->shouldReceive('send')->andThrow(new Exception);
  126. $response = $this->actingAs($this->admin, 'web-guard')
  127. ->json('POST', '/system/test-email', []);
  128. $response->assertStatus(200);
  129. Notification::assertNothingSentTo($this->admin);
  130. }
  131. #[Test]
  132. public function test_clearCache_returns_success()
  133. {
  134. $response = $this->json('GET', '/system/clear-cache');
  135. $response->assertStatus(200);
  136. }
  137. #[Test]
  138. public function test_optimize_returns_success()
  139. {
  140. $response = $this->json('GET', '/system/optimize');
  141. $response->assertStatus(200);
  142. }
  143. }