AllFlowsTest - If `Authorization:` support is disabled, then it's ignored completely.
authorTim Otten <totten@civicrm.org>
Sat, 26 Feb 2022 00:46:26 +0000 (16:46 -0800)
committerTim Otten <totten@civicrm.org>
Mon, 25 Apr 2022 22:21:55 +0000 (15:21 -0700)
This means that the request is effectively anonymous -- the same as it would be if you had not sent an `Authorization:` header.

ext/authx/tests/phpunit/Civi/Authx/AllFlowsTest.php

index 1c374fc05ebbb5e2a575504e677333b09e2ede28..026a05aada81885ce6b42aa23bf478d05c606eac 100644 (file)
@@ -128,7 +128,7 @@ class AllFlowsTest extends \PHPUnit\Framework\TestCase implements EndToEndInterf
     // Phase 1: Request fails if this credential type is not enabled
     \Civi::settings()->set("authx_{$flowType}_cred", []);
     $response = $http->send($request);
-    $this->assertFailedDueToProhibition($response);
+    $this->assertNotAuthenticated($flowType === 'header' ? 'anon' : 'prohibit', $response);
 
     // Phase 2: Request succeeds if this credential type is enabled
     \Civi::settings()->set("authx_{$flowType}_cred", [$credType]);
@@ -159,7 +159,7 @@ class AllFlowsTest extends \PHPUnit\Framework\TestCase implements EndToEndInterf
     // Phase 1: Request fails if this credential type is not enabled
     \Civi::settings()->set("authx_{$flowType}_cred", []);
     $response = $http->send($request);
-    $this->assertFailedDueToProhibition($response);
+    $this->assertNotAuthenticated($flowType === 'header' ? 'anon' : 'prohibit', $response);
 
     // Phase 2: Request succeeds if this credential type is enabled
     \Civi::settings()->set("authx_{$flowType}_cred", [$credType]);
@@ -793,6 +793,28 @@ class AllFlowsTest extends \PHPUnit\Framework\TestCase implements EndToEndInterf
     return NULL;
   }
 
+  /**
+   * Assert that a request was not authenticated.
+   *
+   * @param string $mode
+   *   Expect that the  'prohibited' or 'anon'
+   * @param \Psr\Http\Message\ResponseInterface $response
+   */
+  private function assertNotAuthenticated(string $mode, $response) {
+    switch ($mode) {
+      case 'anon':
+        $this->assertAnonymousContact($response);
+        break;
+
+      case 'prohibit':
+        $this->assertFailedDueToProhibition($response);
+        break;
+
+      default:
+        throw new \RuntimeException("Invalid option: mode=$mode");
+    }
+  }
+
   /**
    * @param \Psr\Http\Message\ResponseInterface $response
    */