Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update length subdomain names validation #256

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/"],
];
}
}
Loading