View source
<?php
namespace Drupal\Tests\farm_api\Functional;
use Drupal\Component\Serialization\Json;
class ConsumerClientIdTest extends OauthTestBase {
public function testValidClientId() {
$valid_payload = [
'grant_type' => 'password',
'client_id' => $this->client
->uuid(),
'username' => $this->user
->getAccountName(),
'password' => $this->user->pass_raw,
'scope' => $this->scope,
];
$response = $this
->post($this->url, $valid_payload);
$this
->assertValidTokenResponse($response, TRUE);
$parsed = Json::decode((string) $response
->getBody());
$response = $this
->post($this->url, $valid_payload, [
'headers' => [
'Authorization' => 'Bearer ' . $parsed['access_token'],
],
]);
$this
->assertValidTokenResponse($response, TRUE);
$payload_client_id = $valid_payload;
$payload_client_id['client_id'] = $this->client
->get('client_id')->value;
$response = $this
->post($this->url, $payload_client_id);
$this
->assertValidTokenResponse($response, TRUE);
$payload_no_scope = $valid_payload;
unset($payload_no_scope['scope']);
$response = $this
->post($this->url, $payload_no_scope);
$this
->assertValidTokenResponse($response, TRUE);
$payload_no_client = $valid_payload;
unset($payload_no_client['client_id']);
$response = $this
->post($this->url, $payload_no_scope, [
'auth' => [
$this->client
->get('client_id')->value,
'',
],
]);
$this
->assertValidTokenResponse($response, TRUE);
}
public function testInvalidClientId() {
$valid_payload = [
'grant_type' => 'password',
'client_id' => $this->client
->get('client_id')->value,
'username' => $this->user
->getAccountName(),
'password' => $this->user->pass_raw,
'scope' => $this->scope,
];
$invalid_payload = $valid_payload;
$invalid_payload['client_id'] = $this
->getRandomGenerator()
->string();
$response = $this
->post($this->url, $invalid_payload);
$parsed_response = Json::decode((string) $response
->getBody());
$this
->assertSame('invalid_client', $parsed_response['error']);
$this
->assertSame(401, $response
->getStatusCode());
$invalid_payload = $valid_payload;
unset($invalid_payload['client_id']);
$response = $this
->post($this->url, $invalid_payload);
$parsed_response = Json::decode((string) $response
->getBody());
$this
->assertSame('invalid_request', $parsed_response['error']);
$this
->assertSame(400, $response
->getStatusCode());
}
}