You are here

class SimpleOauthAuthenticationTest in Simple OAuth (OAuth2) & OpenID Connect 8.4

Same name and namespace in other branches
  1. 8 tests/src/Unit/Authentication/Provider/SimpleOauthAuthenticationTest.php \Drupal\Tests\simple_oauth\Unit\Authentication\Provider\SimpleOauthAuthenticationTest
  2. 8.2 tests/src/Unit/Authentication/Provider/SimpleOauthAuthenticationTest.php \Drupal\Tests\simple_oauth\Unit\Authentication\Provider\SimpleOauthAuthenticationTest
  3. 8.3 tests/src/Unit/Authentication/Provider/SimpleOauthAuthenticationTest.php \Drupal\Tests\simple_oauth\Unit\Authentication\Provider\SimpleOauthAuthenticationTest
  4. 5.x tests/src/Unit/Authentication/Provider/SimpleOauthAuthenticationTest.php \Drupal\Tests\simple_oauth\Unit\Authentication\Provider\SimpleOauthAuthenticationTest

@coversDefaultClass \Drupal\simple_oauth\Authentication\Provider\SimpleOauthAuthenticationProvider @group simple_oauth

Hierarchy

Expanded class hierarchy of SimpleOauthAuthenticationTest

File

tests/src/Unit/Authentication/Provider/SimpleOauthAuthenticationTest.php, line 17

Namespace

Drupal\Tests\simple_oauth\Unit\Authentication\Provider
View source
class SimpleOauthAuthenticationTest extends UnitTestCase {

  /**
   * The authentication provider.
   *
   * @var \Drupal\simple_oauth\Authentication\Provider\SimpleOauthAuthenticationProvider
   */
  protected $provider;

  /**
   * The OAuth page cache request policy.
   *
   * @var \Drupal\simple_oauth\PageCache\SimpleOauthRequestPolicyInterface
   */
  protected $oauthPageCacheRequestPolicy;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $resource_server = $this
      ->prophesize(ResourceServerInterface::class);
    $entity_type_manager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $this->oauthPageCacheRequestPolicy = new DisallowSimpleOauthRequests();
    $this->provider = new SimpleOauthAuthenticationProvider($resource_server
      ->reveal(), $entity_type_manager
      ->reveal(), $this->oauthPageCacheRequestPolicy);
  }

  /**
   * @covers ::applies
   *
   * @dataProvider hasTokenValueProvider
   */
  public function testHasTokenValue($authorization, $has_token) {
    $request = new Request();
    if ($authorization !== NULL) {
      $request->headers
        ->set('Authorization', $authorization);
    }
    $this
      ->assertSame($has_token, $this->provider
      ->applies($request));
    $this
      ->assertSame($has_token ? RequestPolicyInterface::DENY : NULL, $this->oauthPageCacheRequestPolicy
      ->check($request));
  }
  public function hasTokenValueProvider() {
    $token = $this
      ->getRandomGenerator()
      ->name();
    $data = [];

    // 1. Authentication header.
    $data[] = [
      'Bearer ' . $token,
      TRUE,
    ];

    // 2. Authentication header. Trailing white spaces.
    $data[] = [
      '  Bearer ' . $token,
      TRUE,
    ];

    // 3. Authentication header. No white spaces.
    $data[] = [
      'Foo' . $token,
      FALSE,
    ];

    // 4. Authentication header. Empty value.
    $data[] = [
      '',
      FALSE,
    ];

    // 5. Authentication header. Fail: no token.
    $data[] = [
      NULL,
      FALSE,
    ];
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
SimpleOauthAuthenticationTest::$oauthPageCacheRequestPolicy protected property The OAuth page cache request policy.
SimpleOauthAuthenticationTest::$provider protected property The authentication provider.
SimpleOauthAuthenticationTest::hasTokenValueProvider public function
SimpleOauthAuthenticationTest::setUp protected function Overrides UnitTestCase::setUp
SimpleOauthAuthenticationTest::testHasTokenValue public function @covers ::applies
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.