View source
<?php
namespace Drupal\Tests\simple_oauth\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\consumers\Entity\Consumer;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
class RolesNegotiationFunctionalTest extends BrowserTestBase {
use RequestHelperTrait;
use SimpleOauthTestTrait;
protected $defaultTheme = 'stark';
public static $modules = [
'node',
'serialization',
'simple_oauth',
'image',
'text',
'user',
];
protected $url;
protected $tokenTestUrl;
protected $client;
protected $user;
protected $httpClient;
protected $clientSecret;
public function setUp() {
parent::setUp();
$this->htmlOutputEnabled = FALSE;
$this->tokenTestUrl = Url::fromRoute('oauth2_token.user_debug');
$this->url = Url::fromRoute('oauth2_token.token');
$this->user = $this
->drupalCreateUser();
$this->httpClient = $this->container
->get('http_client_factory')
->fromOptions([
'base_uri' => $this->baseUrl,
]);
$this->clientSecret = $this
->getRandomGenerator()
->string();
$role = Role::create([
'id' => 'foo',
'label' => 'Foo',
'is_admin' => FALSE,
]);
$this
->grantPermissions(Role::load(RoleInterface::ANONYMOUS_ID), [
'debug simple_oauth tokens',
]);
$this
->grantPermissions(Role::load(RoleInterface::AUTHENTICATED_ID), [
'debug simple_oauth tokens',
]);
$role
->grantPermission('view own simple_oauth entities');
$role
->save();
$role = Role::create([
'id' => 'bar',
'label' => 'Bar',
'is_admin' => FALSE,
]);
$role
->grantPermission('administer simple_oauth entities');
$role
->save();
$role = Role::create([
'id' => 'oof',
'label' => 'Oof',
'is_admin' => FALSE,
]);
$role
->grantPermission('delete own simple_oauth entities');
$role
->save();
$this->user
->addRole('foo');
$this->user
->addRole('bar');
$this->user
->save();
$this->client = Consumer::create([
'owner_id' => 1,
'user_id' => $this->user
->id(),
'label' => $this
->getRandomGenerator()
->name(),
'secret' => $this->clientSecret,
'confidential' => TRUE,
'roles' => [
[
'target_id' => 'oof',
],
],
]);
$this->client
->save();
$this
->setUpKeys();
}
public function testRequestWithRoleRemovedFromUser() {
$access_token = $this
->getAccessToken([
'foo',
'bar',
]);
$response = $this
->get($this->tokenTestUrl, [
'query' => [
'_format' => 'json',
],
'headers' => [
'Authorization' => 'Bearer ' . $access_token,
],
]);
$parsed_response = Json::decode((string) $response
->getBody());
$this
->assertEquals($this->user
->id(), $parsed_response['id']);
$this
->assertEquals([
'foo',
'bar',
'authenticated',
'oof',
], $parsed_response['roles']);
$this
->assertTrue($parsed_response['permissions']['view own simple_oauth entities']['access']);
$this
->assertTrue($parsed_response['permissions']['administer simple_oauth entities']['access']);
$this->user
->removeRole('bar');
$this->user
->save();
$response = $this
->get($this->tokenTestUrl, [
'query' => [
'_format' => 'json',
],
'headers' => [
'Authorization' => 'Bearer ' . $access_token,
],
]);
$this
->assertEquals(401, $response
->getStatusCode());
$access_token = $this
->getAccessToken([
'foo',
'bar',
]);
$response = $this
->get($this->tokenTestUrl, [
'query' => [
'_format' => 'json',
],
'headers' => [
'Authorization' => 'Bearer ' . $access_token,
],
]);
$parsed_response = Json::decode((string) $response
->getBody());
$this
->assertEquals($this->user
->id(), $parsed_response['id']);
$this
->assertEquals([
'foo',
'authenticated',
'oof',
], $parsed_response['roles']);
$this
->assertTrue($parsed_response['permissions']['view own simple_oauth entities']['access']);
$this
->assertFalse($parsed_response['permissions']['administer simple_oauth entities']['access']);
}
public function testRequestWithRoleRemovedFromClient() {
$access_token = $this
->getAccessToken([
'oof',
]);
$response = $this
->get($this->tokenTestUrl, [
'query' => [
'_format' => 'json',
],
'headers' => [
'Authorization' => 'Bearer ' . $access_token,
],
]);
$parsed_response = Json::decode((string) $response
->getBody());
$this
->assertEquals($this->user
->id(), $parsed_response['id']);
$this
->assertEquals([
'authenticated',
'oof',
], $parsed_response['roles']);
$this
->assertTrue($parsed_response['permissions']['delete own simple_oauth entities']['access']);
$this->client
->set('roles', []);
$this->client
->save();
$response = $this
->get($this->tokenTestUrl, [
'query' => [
'_format' => 'json',
],
'headers' => [
'Authorization' => 'Bearer ' . $access_token,
],
]);
$this
->assertEquals(401, $response
->getStatusCode());
$access_token = $this
->getAccessToken([
'oof',
]);
$response = $this
->get($this->tokenTestUrl, [
'query' => [
'_format' => 'json',
],
'headers' => [
'Authorization' => 'Bearer ' . $access_token,
],
]);
$parsed_response = Json::decode((string) $response
->getBody());
$this
->assertEquals($this->user
->id(), $parsed_response['id']);
$this
->assertEquals([
'authenticated',
], $parsed_response['roles']);
$this
->assertFalse($parsed_response['permissions']['delete own simple_oauth entities']['access']);
}
public function testRequestWithMissingScope() {
$access_token = $this
->getAccessToken();
$response = $this
->get($this->tokenTestUrl, [
'query' => [
'_format' => 'json',
],
'headers' => [
'Authorization' => 'Bearer ' . $access_token,
],
]);
$parsed_response = Json::decode((string) $response
->getBody());
$this
->assertEquals($this->user
->id(), $parsed_response['id']);
$this
->assertEquals([
'authenticated',
'oof',
], $parsed_response['roles']);
$this
->assertFalse($parsed_response['permissions']['view own simple_oauth entities']['access']);
}
private function getAccessToken(array $scopes = []) {
$valid_payload = [
'grant_type' => 'client_credentials',
'client_id' => $this->client
->uuid(),
'client_secret' => $this->clientSecret,
];
if (!empty($scopes)) {
$valid_payload['scope'] = implode(' ', $scopes);
}
$response = $this
->post($this->url, $valid_payload);
$parsed_response = Json::decode((string) $response
->getBody());
return isset($parsed_response['access_token']) ? $parsed_response['access_token'] : NULL;
}
}