RejectIfSsoOnlyAndNotForAdminMiddlewareTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace Tests\Feature\Http\Middlewares;
  3. use App\Facades\Settings;
  4. use App\Http\Middleware\RejectIfSsoOnlyAndNotForAdmin;
  5. use App\Models\User;
  6. use Illuminate\Http\Response;
  7. use PHPUnit\Framework\Attributes\CoversClass;
  8. use PHPUnit\Framework\Attributes\CoversNothing;
  9. use PHPUnit\Framework\Attributes\DataProvider;
  10. use PHPUnit\Framework\Attributes\Test;
  11. use Tests\FeatureTestCase;
  12. /**
  13. * RejectIfSsoOnlyAndNotForAdminMiddlewareTest test class
  14. */
  15. #[CoversClass(RejectIfSsoOnlyAndNotForAdmin::class)]
  16. class RejectIfSsoOnlyAndNotForAdminMiddlewareTest extends FeatureTestCase
  17. {
  18. /**
  19. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  20. */
  21. protected $user;
  22. /**
  23. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  24. */
  25. protected $admin;
  26. private const PASSWORD = 'password';
  27. protected function setUp() : void
  28. {
  29. parent::setUp();
  30. $this->user = User::factory()->create();
  31. $this->admin = User::factory()->administrator()->create([
  32. 'password' => self::PASSWORD,
  33. ]);
  34. Settings::set('useSsoOnly', true);
  35. }
  36. #[Test]
  37. public function test_admin_login_with_password_returns_success()
  38. {
  39. $this->json('POST', '/user/login', [
  40. 'email' => $this->admin->email,
  41. 'password' => self::PASSWORD,
  42. ])
  43. ->assertOk();
  44. }
  45. #[Test]
  46. #[CoversNothing]
  47. public function test_admin_login_with_webauthn_returns_success()
  48. {
  49. // See WebAuthnLoginControllerTest->test_webauthn_login_of_admin_returns_success_even_with_sso_only_enabled()
  50. }
  51. #[Test]
  52. public function test_login_of_missing_account_returns_NOT_ALLOWED()
  53. {
  54. $this->json('POST', '/user/login', [
  55. 'email' => 'missing@user.com',
  56. 'password' => self::PASSWORD,
  57. ])
  58. ->assertMethodNotAllowed();
  59. }
  60. #[Test]
  61. #[DataProvider('providePublicEndPoints')]
  62. public function test_public_endpoint_does_not_return_NOT_ALLOWED_if_requested_for_an_admin(string $method, string $url)
  63. {
  64. $expectedResponseCodes = [
  65. Response::HTTP_OK,
  66. Response::HTTP_UNPROCESSABLE_ENTITY,
  67. ];
  68. $response = $this->json($method, $url, [
  69. 'email' => $this->admin->email,
  70. ]);
  71. $this->assertContains($response->getStatusCode(), $expectedResponseCodes);
  72. }
  73. #[Test]
  74. #[DataProvider('providePublicEndPoints')]
  75. public function test_public_endpoint_returns_NOT_ALLOWED_if_requested_for_regular_user(string $method, string $url)
  76. {
  77. $this->json($method, $url)
  78. ->assertMethodNotAllowed();
  79. }
  80. /**
  81. * Provide Valid data for validation test
  82. */
  83. public static function providePublicEndPoints() : array
  84. {
  85. return [
  86. 'PWD_REGISTER' => [
  87. 'method' => 'POST',
  88. 'url' => '/user',
  89. ],
  90. 'PWD_LOGIN' => [
  91. 'method' => 'POST',
  92. 'url' => '/user/login',
  93. ],
  94. 'PWD_LOST' => [
  95. 'method' => 'POST',
  96. 'url' => '/user/password/lost',
  97. ],
  98. 'PWD_RESET' => [
  99. 'method' => 'POST',
  100. 'url' => '/user/password/reset',
  101. ],
  102. 'WEBAUTHN_LOGIN' => [
  103. 'method' => 'POST',
  104. 'url' => '/webauthn/login',
  105. ],
  106. 'WEBAUTHN_LOGIN_OPTIONS' => [
  107. 'method' => 'POST',
  108. 'url' => '/webauthn/login/options',
  109. ],
  110. 'WEBAUTHN_LOST' => [
  111. 'method' => 'POST',
  112. 'url' => '/webauthn/lost',
  113. ],
  114. 'WEBAUTHN_RECOVER' => [
  115. 'method' => 'POST',
  116. 'url' => '/webauthn/recover',
  117. ],
  118. ];
  119. }
  120. }