You are here

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

Same name and namespace in other branches
  1. 8.4 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

Class SimpleOauthAuthenticationTest.

@package Drupal\Tests\simple_oauth\Unit\Authentication\Provider

@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 19

Namespace

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

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

  /**
   * @covers ::getTokenValue
   * @covers ::applies
   *
   * @dataProvider getTokenValueProvider
   */
  public function testGetTokenValue(Request $request, $token) {
    $this
      ->assertSame($token, $this->provider
      ->getTokenValue($request));
  }
  public function getTokenValueProvider() {
    $data = [];

    // 1. Authentication header.
    $token = $this
      ->getRandomGenerator()
      ->name();
    $request = new Request();
    $request->headers
      ->set('Authorization', 'Bearer ' . $token);
    $data[] = [
      $request,
      $token,
    ];

    // 2. Authentication header. Fail: wrong token.
    $token = $this
      ->getRandomGenerator()
      ->name();
    $request = new Request();
    $request->headers
      ->set('Authorization', 'Bearer fail--' . $token);
    $data[] = [
      $request,
      'fail--' . $token,
    ];

    // 3. Authentication header. Fail: no token.
    $token = $this
      ->getRandomGenerator()
      ->name();
    $request = new Request();
    $data[] = [
      $request,
      NULL,
    ];

    // 4. Form encoded parameter.
    $token = $this
      ->getRandomGenerator()
      ->name();
    $request = new Request();
    $request
      ->setMethod(Request::METHOD_POST);
    $request->headers
      ->set('Content-Type', 'application/x-www-form-urlencoded');
    $request->request
      ->set('access_token', $token);
    $data[] = [
      $request,
      $token,
    ];

    // 5. Form encoded parameter. Fail: missing content type.
    $token = $this
      ->getRandomGenerator()
      ->name();
    $request = new Request();
    $request
      ->setMethod(Request::METHOD_POST);
    $request->request
      ->set('access_token', $token);
    $data[] = [
      $request,
      NULL,
    ];

    // 6. Form encoded parameter. Fail: missing token.
    $request = new Request();
    $request
      ->setMethod(Request::METHOD_POST);
    $request->headers
      ->set('Content-Type', 'application/x-www-form-urlencoded');
    $data[] = [
      $request,
      NULL,
    ];

    // 7. Form encoded parameter. Fail: wrong method.
    $token = $this
      ->getRandomGenerator()
      ->name();
    $request = new Request();
    $request
      ->setMethod(Request::METHOD_GET);
    $request->headers
      ->set('Content-Type', 'application/x-www-form-urlencoded');
    $request->request
      ->set('access_token', $token);
    $data[] = [
      $request,
      NULL,
    ];
    return $data;
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $config_factory = $this
      ->prophesize(ConfigFactoryInterface::class);
    $entity_manager = $this
      ->prophesize(EntityManagerInterface::class);
    $this->provider = new SimpleOauthAuthenticationProvider($config_factory
      ->reveal(), $entity_manager
      ->reveal());
  }

}

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::$provider protected property The authentication provider.
SimpleOauthAuthenticationTest::getTokenValueProvider public function
SimpleOauthAuthenticationTest::setUp protected function Overrides UnitTestCase::setUp
SimpleOauthAuthenticationTest::testGetTokenValue public function @covers ::getTokenValue @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.