woocommerce-paypal-payments/tests/PHPUnit/ApiClient/Authentication/PayPalBearerTest.php

253 lines
9.1 KiB
PHP
Raw Normal View History

2020-08-31 13:38:54 +03:00
<?php
declare(strict_types=1);
2020-09-14 07:51:45 +03:00
namespace WooCommerce\PayPalCommerce\ApiClient\Authentication;
2020-08-31 13:38:54 +03:00
2021-09-14 17:46:33 +02:00
use Requests_Utility_CaseInsensitiveDictionary;
2020-09-14 07:51:45 +03:00
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\ApiClient\TestCase;
2020-08-31 13:38:54 +03:00
use Psr\Log\LoggerInterface;
use Mockery;
2021-07-27 14:19:23 +02:00
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
2020-08-31 13:38:54 +03:00
use function Brain\Monkey\Functions\expect;
class PayPalBearerTest extends TestCase
{
2020-09-01 09:47:36 +03:00
public function testDefault()
2020-08-31 13:38:54 +03:00
{
2020-09-01 09:47:36 +03:00
expect('wp_json_encode')->andReturnUsing('json_encode');
2020-08-31 13:38:54 +03:00
$json = '{"access_token":"abc","expires_in":100, "created":' . time() . '}';
2020-09-14 07:51:45 +03:00
$cache = Mockery::mock(Cache::class);
2020-08-31 13:38:54 +03:00
$cache
->expects('get')
->andReturn('{"access_token":"abc","expires_in":100, "created":100}');
$cache
->expects('set');
$host = 'https://example.com';
$key = 'key';
$secret = 'secret';
$logger = Mockery::mock(LoggerInterface::class);
2021-09-14 17:46:33 +02:00
$logger->shouldReceive('debug');
2021-07-27 14:19:23 +02:00
$settings = Mockery::mock(Settings::class);
$settings->shouldReceive('has')->andReturn(true);
$settings->shouldReceive('get')->andReturn('');
2021-09-14 17:46:33 +02:00
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
2020-08-31 13:38:54 +03:00
2021-07-27 14:19:23 +02:00
$bearer = new PayPalBearer($cache, $host, $key, $secret, $logger, $settings);
2020-08-31 13:38:54 +03:00
expect('trailingslashit')
->with($host)
->andReturn($host . '/');
expect('wp_remote_get')
->andReturnUsing(
2021-09-14 17:46:33 +02:00
function ($url, $args) use ($json, $key, $secret, $host, $headers) {
2020-08-31 13:38:54 +03:00
if ($url !== $host . '/v1/oauth2/token?grant_type=client_credentials') {
return false;
}
if ($args['method'] !== 'POST') {
return false;
}
if ($args['headers']['Authorization'] !== 'Basic ' . base64_encode($key . ':' . $secret)) {
return false;
}
return [
'body' => $json,
2021-09-14 17:46:33 +02:00
'headers' => $headers
2020-08-31 13:38:54 +03:00
];
}
);
expect('is_wp_error')
->andReturn(false);
expect('wp_remote_retrieve_response_code')
->andReturn(200);
$token = $bearer->bearer();
$this->assertEquals("abc", $token->token());
2020-09-01 09:47:36 +03:00
$this->assertTrue($token->is_valid());
2020-08-31 13:38:54 +03:00
}
public function testNoTokenCached()
{
2020-09-01 09:47:36 +03:00
expect('wp_json_encode')->andReturnUsing('json_encode');
2020-08-31 13:38:54 +03:00
$json = '{"access_token":"abc","expires_in":100, "created":' . time() . '}';
2020-09-14 07:51:45 +03:00
$cache = Mockery::mock(Cache::class);
2020-08-31 13:38:54 +03:00
$cache
->expects('get')
->andReturn('');
$cache
->expects('set');
$host = 'https://example.com';
$key = 'key';
$secret = 'secret';
$logger = Mockery::mock(LoggerInterface::class);
2021-09-14 17:46:33 +02:00
$logger->shouldReceive('debug');
2021-07-27 14:19:23 +02:00
$settings = Mockery::mock(Settings::class);
$settings->shouldReceive('has')->andReturn(true);
$settings->shouldReceive('get')->andReturn('');
2021-09-14 17:46:33 +02:00
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
2020-08-31 13:38:54 +03:00
2021-07-27 14:19:23 +02:00
$bearer = new PayPalBearer($cache, $host, $key, $secret, $logger, $settings);
2020-08-31 13:38:54 +03:00
expect('trailingslashit')
->with($host)
->andReturn($host . '/');
expect('wp_remote_get')
->andReturnUsing(
2021-09-14 17:46:33 +02:00
function ($url, $args) use ($json, $key, $secret, $host, $headers) {
2020-08-31 13:38:54 +03:00
if ($url !== $host . '/v1/oauth2/token?grant_type=client_credentials') {
return false;
}
if ($args['method'] !== 'POST') {
return false;
}
if ($args['headers']['Authorization'] !== 'Basic ' . base64_encode($key . ':' . $secret)) {
return false;
}
return [
'body' => $json,
2021-09-14 17:46:33 +02:00
'headers' => $headers,
2020-08-31 13:38:54 +03:00
];
}
);
expect('is_wp_error')
->andReturn(false);
expect('wp_remote_retrieve_response_code')
->andReturn(200);
$token = $bearer->bearer();
$this->assertEquals("abc", $token->token());
2020-09-01 09:47:36 +03:00
$this->assertTrue($token->is_valid());
2020-08-31 13:38:54 +03:00
}
public function testCachedTokenIsStillValid()
{
$json = '{"access_token":"abc","expires_in":100, "created":' . time() . '}';
2020-09-14 07:51:45 +03:00
$cache = Mockery::mock(Cache::class);
2020-08-31 13:38:54 +03:00
$cache
->expects('get')
->andReturn($json);
$host = 'https://example.com';
$key = 'key';
$secret = 'secret';
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldNotReceive('log');
2021-07-27 14:19:23 +02:00
$settings = Mockery::mock(Settings::class);
$settings->shouldReceive('has')->andReturn(true);
$settings->shouldReceive('get')->andReturn('');
2020-08-31 13:38:54 +03:00
2021-07-27 14:19:23 +02:00
$bearer = new PayPalBearer($cache, $host, $key, $secret, $logger, $settings);
2020-08-31 13:38:54 +03:00
$token = $bearer->bearer();
$this->assertEquals("abc", $token->token());
2020-09-01 09:47:36 +03:00
$this->assertTrue($token->is_valid());
2020-08-31 13:38:54 +03:00
}
public function testExceptionThrownOnError()
{
$json = '{"access_token":"abc","expires_in":100, "created":' . time() . '}';
2020-09-14 07:51:45 +03:00
$cache = Mockery::mock(Cache::class);
2020-08-31 13:38:54 +03:00
$cache
->expects('get')
->andReturn('');
$host = 'https://example.com';
$key = 'key';
$secret = 'secret';
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log');
2021-09-14 17:46:33 +02:00
$logger->shouldReceive('debug');
2021-07-27 14:19:23 +02:00
$settings = Mockery::mock(Settings::class);
$settings->shouldReceive('has')->andReturn(true);
$settings->shouldReceive('get')->andReturn('');
2021-09-14 17:46:33 +02:00
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
2020-08-31 13:38:54 +03:00
2021-07-27 14:19:23 +02:00
$bearer = new PayPalBearer($cache, $host, $key, $secret, $logger, $settings);
2020-08-31 13:38:54 +03:00
expect('trailingslashit')
->with($host)
->andReturn($host . '/');
expect('wp_remote_get')
->andReturnUsing(
2021-09-14 17:46:33 +02:00
function ($url, $args) use ($json, $key, $secret, $host, $headers) {
2020-08-31 13:38:54 +03:00
if ($url !== $host . '/v1/oauth2/token?grant_type=client_credentials') {
return false;
}
if ($args['method'] !== 'POST') {
return false;
}
if ($args['headers']['Authorization'] !== 'Basic ' . base64_encode($key . ':' . $secret)) {
return false;
}
return [
'body' => $json,
2021-09-14 17:46:33 +02:00
'headers' => $headers,
2020-08-31 13:38:54 +03:00
];
}
);
expect('is_wp_error')
->andReturn(true);
$this->expectException(RuntimeException::class);
$bearer->bearer();
}
public function testExceptionThrownBecauseOfHttpStatusCode()
{
$json = '{"access_token":"abc","expires_in":100, "created":' . time() . '}';
2020-09-14 07:51:45 +03:00
$cache = Mockery::mock(Cache::class);
2020-08-31 13:38:54 +03:00
$cache
->expects('get')
->andReturn('');
$host = 'https://example.com';
$key = 'key';
$secret = 'secret';
$logger = Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('log');
2021-09-14 17:46:33 +02:00
$logger->shouldReceive('debug');
2021-07-27 14:19:23 +02:00
$settings = Mockery::mock(Settings::class);
$settings->shouldReceive('has')->andReturn(true);
$settings->shouldReceive('get')->andReturn('');
2021-09-14 17:46:33 +02:00
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll');
2020-08-31 13:38:54 +03:00
2021-07-27 14:19:23 +02:00
$bearer = new PayPalBearer($cache, $host, $key, $secret, $logger, $settings);
2020-08-31 13:38:54 +03:00
expect('trailingslashit')
->with($host)
->andReturn($host . '/');
expect('wp_remote_get')
->andReturnUsing(
2021-09-14 17:46:33 +02:00
function ($url, $args) use ($json, $key, $secret, $host, $headers) {
2020-08-31 13:38:54 +03:00
if ($url !== $host . '/v1/oauth2/token?grant_type=client_credentials') {
return false;
}
if ($args['method'] !== 'POST') {
return false;
}
if ($args['headers']['Authorization'] !== 'Basic ' . base64_encode($key . ':' . $secret)) {
return false;
}
return [
'body' => $json,
2021-09-14 17:46:33 +02:00
'headers' => $headers,
2020-08-31 13:38:54 +03:00
];
}
);
expect('is_wp_error')
->andReturn(false);
expect('wp_remote_retrieve_response_code')
->andReturn(500);
$this->expectException(RuntimeException::class);
$bearer->bearer();
}
}