View source
<?php
namespace Druapl\tests\farm_login\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Tests\farm_api\Functional\OauthTestBase;
class OauthPasswordTest extends OauthTestBase {
protected static $modules = [
'image',
'node',
'serialization',
'simple_oauth',
'text',
'user',
'farm_api',
'farm_login',
];
public function testPasswordGrant() {
$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,
];
$response = $this
->post($this->url, $valid_payload);
$this
->assertValidTokenResponse($response, TRUE);
$payload_client_id = $valid_payload;
$payload_client_id['username'] = $this->user
->getEmail();
$response = $this
->post($this->url, $payload_client_id);
$this
->assertValidTokenResponse($response, TRUE);
}
public function testInvalidPasswordGrant() {
$valid_payload = [
'grant_type' => 'password',
'client_id' => $this->client
->get('client_id')->value,
'client_secret' => $this->clientSecret,
'username' => $this->user
->getAccountName(),
'password' => $this->user->pass_raw,
'scope' => $this->scope,
];
$invalid_payload = $valid_payload;
$invalid_payload['username'] = $this
->getRandomGenerator()
->string();
$response = $this
->post($this->url, $invalid_payload);
$parsed_response = Json::decode((string) $response
->getBody());
$this
->assertSame('invalid_credentials', $parsed_response['error']);
$this
->assertSame(401, $response
->getStatusCode());
$invalid_payload = $valid_payload;
$invalid_payload['password'] = $this
->getRandomGenerator()
->string();
$response = $this
->post($this->url, $invalid_payload);
$parsed_response = Json::decode((string) $response
->getBody());
$this
->assertSame('invalid_credentials', $parsed_response['error']);
$this
->assertSame(401, $response
->getStatusCode());
}
}