SimpleOauthAuthenticationTest.php in Simple OAuth (OAuth2) & OpenID Connect 8.4
File
tests/src/Unit/Authentication/Provider/SimpleOauthAuthenticationTest.php
View source
<?php
namespace Drupal\Tests\simple_oauth\Unit\Authentication\Provider;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\PageCache\RequestPolicyInterface;
use Drupal\simple_oauth\Authentication\Provider\SimpleOauthAuthenticationProvider;
use Drupal\simple_oauth\PageCache\DisallowSimpleOauthRequests;
use Drupal\simple_oauth\Server\ResourceServerInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
class SimpleOauthAuthenticationTest extends UnitTestCase {
protected $provider;
protected $oauthPageCacheRequestPolicy;
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);
}
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 = [];
$data[] = [
'Bearer ' . $token,
TRUE,
];
$data[] = [
' Bearer ' . $token,
TRUE,
];
$data[] = [
'Foo' . $token,
FALSE,
];
$data[] = [
'',
FALSE,
];
$data[] = [
NULL,
FALSE,
];
return $data;
}
}