Skip to content

Commit

Permalink
Update length subdomain names validation (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
armando-rodriguez-cko authored Jun 6, 2024
1 parent 2946028 commit c5528fe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 43 deletions.
2 changes: 1 addition & 1 deletion lib/Checkout/EnvironmentSubdomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private function addSubdomainToApiUrlEnvironment($environment, $subdomain)
$apiUrl = $environment->getBaseUri();
$newEnvironment = $apiUrl;

$regex = '/^[0-9a-z]{8}$/';
$regex = '/^[0-9a-z]{8,11}$/';
if (preg_match($regex, $subdomain)) {
$urlParts = parse_url($apiUrl);
$newHost = $subdomain . '.' . $urlParts['host'];
Expand Down
69 changes: 27 additions & 42 deletions test/Checkout/Tests/CheckoutConfigurationTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
class CheckoutConfigurationTests extends MockeryTestCase
{
/**
* @test
* @dataProvider validSubdomainProvider
*/
public function shouldCreateConfiguration()
public function testShouldCreateConfigurationWithSubdomain($subdomain, $expectedUrl)
{
$credentials = new StaticKeysSdkCredentials(
getenv("CHECKOUT_DEFAULT_SECRET_KEY"),
getenv("CHECKOUT_DEFAULT_PUBLIC_KEY")
);
$httpClient = $this->createMock(HttpClientBuilderInterface::class);

$environmentSubdomain = new EnvironmentSubdomain(Environment::sandbox(), $subdomain);

$checkoutLog = new Logger("checkout-sdk-test-php");
$checkoutLog->pushHandler(new StreamHandler("php://stderr"));
$checkoutLog->pushHandler(new StreamHandler("checkout-sdk-test-php.log"));
Expand All @@ -35,25 +37,24 @@ public function shouldCreateConfiguration()
$checkoutLog
);

$configuration->setEnvironmentSubdomain($environmentSubdomain);

$this->assertEquals(Environment::sandbox(), $configuration->getEnvironment());
$this->assertEquals(
"https://api.sandbox.checkout.com/",
$configuration->getEnvironment()->getBaseUri()
);
$this->assertEquals($expectedUrl, $configuration->getEnvironmentSubdomain()->getBaseUri());
}

/**
* @test
* @dataProvider invalidSubdomainProvider
*/
public function shouldCreateConfigurationWithSubdomain()
public function testShouldCreateConfigurationWithBadSubdomain($subdomain, $expectedUrl)
{
$credentials = new StaticKeysSdkCredentials(
getenv("CHECKOUT_DEFAULT_SECRET_KEY"),
getenv("CHECKOUT_DEFAULT_PUBLIC_KEY")
);
$httpClient = $this->createMock(HttpClientBuilderInterface::class);

$environmentSubdomain = new EnvironmentSubdomain(Environment::sandbox(), "123dmain");
$environmentSubdomain = new EnvironmentSubdomain(Environment::sandbox(), $subdomain);

$checkoutLog = new Logger("checkout-sdk-test-php");
$checkoutLog->pushHandler(new StreamHandler("php://stderr"));
Expand All @@ -69,42 +70,26 @@ public function shouldCreateConfigurationWithSubdomain()
$configuration->setEnvironmentSubdomain($environmentSubdomain);

$this->assertEquals(Environment::sandbox(), $configuration->getEnvironment());
$this->assertEquals(
"https://123dmain.api.sandbox.checkout.com/",
$configuration->getEnvironmentSubdomain()->getBaseUri()
);
$this->assertEquals($expectedUrl, $configuration->getEnvironmentSubdomain()->getBaseUri());
}

/**
* @test
*/
public function shouldCreateConfigurationWithBadSubdomain()
public function validSubdomainProvider()
{
$credentials = new StaticKeysSdkCredentials(
getenv("CHECKOUT_DEFAULT_SECRET_KEY"),
getenv("CHECKOUT_DEFAULT_PUBLIC_KEY")
);
$httpClient = $this->createMock(HttpClientBuilderInterface::class);

$environmentSubdomain = new EnvironmentSubdomain(Environment::sandbox(), "subdomain");

$checkoutLog = new Logger("checkout-sdk-test-php");
$checkoutLog->pushHandler(new StreamHandler("php://stderr"));
$checkoutLog->pushHandler(new StreamHandler("checkout-sdk-test-php.log"));

$configuration = new CheckoutConfiguration(
$credentials,
Environment::sandbox(),
$httpClient,
$checkoutLog
);

$configuration->setEnvironmentSubdomain($environmentSubdomain);
return [
["123dmain", "https://123dmain.api.sandbox.checkout.com/"],
["123domain", "https://123domain.api.sandbox.checkout.com/"],
["1234domain", "https://1234domain.api.sandbox.checkout.com/"],
["12345domain", "https://12345domain.api.sandbox.checkout.com/"],
];
}

$this->assertEquals(Environment::sandbox(), $configuration->getEnvironment());
$this->assertEquals(
"https://api.sandbox.checkout.com/",
$configuration->getEnvironmentSubdomain()->getBaseUri()
);
public function invalidSubdomainProvider()
{
return [
["", "https://api.sandbox.checkout.com/"],
["123", "https://api.sandbox.checkout.com/"],
["123bad", "https://api.sandbox.checkout.com/"],
["12345domainBad", "https://api.sandbox.checkout.com/"],
];
}
}

0 comments on commit c5528fe

Please sign in to comment.