OidcClientTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /*
  3. * Copyright 2026 Alessandro Chiapparini <sprint_planer_web@chiapparini.org>
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * See the LICENSE file in the project root for the full license text.
  9. */
  10. declare(strict_types=1);
  11. namespace App\Tests\Auth;
  12. use App\Auth\OidcClient;
  13. use App\Tests\TestCase;
  14. /**
  15. * Pins the OIDC env-driven gating: `isConfigured()` is true only when every
  16. * ENTRA_* + APP_BASE_URL var is set AND `OIDC_ENABLED` isn't explicitly false.
  17. * The `OIDC_ENABLED=false` path is the dev/testing kill-switch that lets
  18. * operators run on the LOCAL_ADMIN_* fallback alone without unsetting the
  19. * Entra creds in .env.
  20. */
  21. final class OidcClientTest extends TestCase
  22. {
  23. /** @var array<string, string|false> */
  24. private array $envBackup = [];
  25. /** @var string[] */
  26. private array $envKeys = [
  27. 'ENTRA_TENANT_ID',
  28. 'ENTRA_CLIENT_ID',
  29. 'ENTRA_CLIENT_SECRET',
  30. 'APP_BASE_URL',
  31. 'OIDC_ENABLED',
  32. ];
  33. protected function setUp(): void
  34. {
  35. parent::setUp();
  36. foreach ($this->envKeys as $k) {
  37. $this->envBackup[$k] = getenv($k);
  38. putenv($k);
  39. }
  40. }
  41. protected function tearDown(): void
  42. {
  43. foreach ($this->envKeys as $k) {
  44. $prev = $this->envBackup[$k] ?? false;
  45. if ($prev === false) {
  46. putenv($k);
  47. } else {
  48. putenv("{$k}={$prev}");
  49. }
  50. }
  51. parent::tearDown();
  52. }
  53. private function setEntraVars(): void
  54. {
  55. putenv('ENTRA_TENANT_ID=tenant-guid');
  56. putenv('ENTRA_CLIENT_ID=client-guid');
  57. putenv('ENTRA_CLIENT_SECRET=secret');
  58. putenv('APP_BASE_URL=https://example.com');
  59. }
  60. public function testNotConfiguredWithoutEntraVars(): void
  61. {
  62. self::assertFalse(OidcClient::isConfigured());
  63. self::assertFalse(OidcClient::isExplicitlyDisabled());
  64. }
  65. public function testConfiguredWhenAllEntraVarsSet(): void
  66. {
  67. $this->setEntraVars();
  68. self::assertTrue(OidcClient::isConfigured());
  69. self::assertFalse(OidcClient::isExplicitlyDisabled());
  70. }
  71. public function testConfiguredStaysTrueWhenFlagBlankOrUnset(): void
  72. {
  73. $this->setEntraVars();
  74. // Unset (default of putenv with no =).
  75. putenv('OIDC_ENABLED');
  76. self::assertTrue(OidcClient::isConfigured());
  77. // Explicit blank.
  78. putenv('OIDC_ENABLED=');
  79. self::assertTrue(OidcClient::isConfigured());
  80. self::assertFalse(OidcClient::isExplicitlyDisabled());
  81. }
  82. public function testConfiguredStaysTrueWhenFlagAnyTruthyValue(): void
  83. {
  84. $this->setEntraVars();
  85. foreach (['true', '1', 'yes', 'on', 'TRUE', 'enabled', 'whatever'] as $v) {
  86. putenv("OIDC_ENABLED={$v}");
  87. self::assertTrue(
  88. OidcClient::isConfigured(),
  89. "isConfigured() should be true with OIDC_ENABLED={$v}",
  90. );
  91. self::assertFalse(
  92. OidcClient::isExplicitlyDisabled(),
  93. "isExplicitlyDisabled() should be false with OIDC_ENABLED={$v}",
  94. );
  95. }
  96. }
  97. public function testFalseyFlagDisablesOidcEvenWithEntraVarsSet(): void
  98. {
  99. $this->setEntraVars();
  100. foreach (['false', '0', 'no', 'off', 'FALSE', 'No', ' off '] as $v) {
  101. putenv("OIDC_ENABLED={$v}");
  102. self::assertFalse(
  103. OidcClient::isConfigured(),
  104. "isConfigured() should be false with OIDC_ENABLED={$v}",
  105. );
  106. self::assertTrue(
  107. OidcClient::isExplicitlyDisabled(),
  108. "isExplicitlyDisabled() should be true with OIDC_ENABLED={$v}",
  109. );
  110. }
  111. }
  112. public function testFalseyFlagWithoutEntraVarsStillReportsDisabled(): void
  113. {
  114. // No ENTRA_*. Flag set false. isConfigured stays false (already was);
  115. // isExplicitlyDisabled tells the operator-facing page which message
  116. // to show ("disabled" vs. "not configured").
  117. putenv('OIDC_ENABLED=false');
  118. self::assertFalse(OidcClient::isConfigured());
  119. self::assertTrue(OidcClient::isExplicitlyDisabled());
  120. }
  121. }