SystemControllerTest.php 4.2 KB

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