123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- <?php
- /**
- * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
- */
- namespace Temando\Shipping\Model\Config\Backend;
- use Magento\Framework\Exception\AuthenticationException;
- use Magento\TestFramework\Helper\Bootstrap;
- use Temando\Shipping\Model\Config\Backend\Active\ApiConnection;
- use Temando\Shipping\Model\Config\Backend\Active\CredentialsValidator;
- use Temando\Shipping\Rest\Authentication;
- /**
- * Temando Carrier Enabled Config Backend Model Test
- *
- * @package Temando\Shipping\Test\Integration
- * @author Christoph Aßmann <christoph.assmann@netresearch.de>
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
- * @link http://www.temando.com/
- */
- class CredentialsValidationTest extends \PHPUnit\Framework\TestCase
- {
- const ENDPOINT = 'https://auth.temando.io/v1/';
- const ENDPOINT_INVALID = 'gopher://127.0.0.1';
- const CHECKOUT_DISABLED = '0';
- const CHECKOUT_ENABLED = '1';
- const USER_VALID = 'alice';
- const USER_INVALID = 'malice';
- const PASSWORD_VALID = 'openSesame';
- const PASSWORD_INVALID = 'computerSaysNo';
- /**
- * @return CredentialsValidator
- */
- private function getValidator()
- {
- $authTest = $this->getMockBuilder(ApiConnection::class)
- ->setMethods(['test'])
- ->disableOriginalConstructor()
- ->getMock();
- $authTest
- ->expects($this->any())
- ->method('test')
- ->willReturnMap([
- [self::ENDPOINT, '', self::PASSWORD_INVALID, false],
- [self::ENDPOINT, self::USER_INVALID, '', false],
- [self::ENDPOINT, self::USER_INVALID, self::PASSWORD_INVALID, false],
- [self::ENDPOINT, '', self::PASSWORD_VALID, false],
- [self::ENDPOINT, self::USER_VALID, '', false],
- [self::ENDPOINT, self::USER_VALID, self::PASSWORD_VALID, true],
- ]);
- $validator = Bootstrap::getObjectManager()->create(CredentialsValidator::class, [
- 'connection' => $authTest,
- ]);
- return $validator;
- }
- /**
- * @test
- */
- public function canSaveWithCheckoutDisabledAndApiEndpointEmpty()
- {
- $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
- 'validationRules' => $this->getValidator(),
- 'data' => [
- 'value' => self::CHECKOUT_DISABLED,
- 'fieldset_data' => [
- 'session_endpoint' => '',
- 'account_id' => '',
- 'bearer_token' => '',
- ]
- ]
- ]);
- // just assert no exception is thrown
- $this->assertSame($backendModel, $backendModel->validateBeforeSave());
- }
- /**
- * @test
- */
- public function canSaveWithCheckoutDisabledAndCredentialsEmpty()
- {
- $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
- 'validationRules' => $this->getValidator(),
- 'data' => [
- 'value' => self::CHECKOUT_DISABLED,
- 'fieldset_data' => [
- 'session_endpoint' => self::ENDPOINT,
- 'account_id' => '',
- 'bearer_token' => '',
- ]
- ]
- ]);
- // just assert no exception is thrown
- $this->assertSame($backendModel, $backendModel->validateBeforeSave());
- }
- /**
- * @test
- */
- public function canSaveWithCheckoutDisabledAndCredentialsValid()
- {
- $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
- 'validationRules' => $this->getValidator(),
- 'data' => [
- 'value' => self::CHECKOUT_DISABLED,
- 'fieldset_data' => [
- 'session_endpoint' => self::ENDPOINT,
- 'account_id' => self::USER_VALID,
- 'bearer_token' => self::PASSWORD_VALID,
- ]
- ]
- ]);
- // just assert no exception is thrown
- $this->assertSame($backendModel, $backendModel->validateBeforeSave());
- }
- /**
- * @test
- */
- public function canSaveWithCheckoutEnabledAndCredentialsValid()
- {
- $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
- 'validationRules' => $this->getValidator(),
- 'data' => [
- 'value' => self::CHECKOUT_ENABLED,
- 'fieldset_data' => [
- 'session_endpoint' => self::ENDPOINT,
- 'account_id' => self::USER_VALID,
- 'bearer_token' => self::PASSWORD_VALID,
- ]
- ]
- ]);
- // just assert no exception is thrown
- $this->assertSame($backendModel, $backendModel->validateBeforeSave());
- }
- /**
- * @test
- */
- public function cannotSaveWithCheckoutDisabledAndPasswordInvalid()
- {
- $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
- 'validationRules' => $this->getValidator(),
- 'data' => [
- 'value' => self::CHECKOUT_DISABLED,
- 'fieldset_data' => [
- 'session_endpoint' => self::ENDPOINT,
- 'account_id' => self::USER_VALID,
- 'bearer_token' => self::PASSWORD_INVALID,
- ]
- ]
- ]);
- $this->expectException(\Magento\Framework\Validator\Exception::class);
- $this->expectExceptionMessage('Magento Shipping authentication failed. Please check your credentials.');
- $backendModel->validateBeforeSave();
- }
- /**
- * @test
- */
- public function cannotSaveWithCheckoutDisabledAndUserInvalid()
- {
- $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
- 'validationRules' => $this->getValidator(),
- 'data' => [
- 'value' => self::CHECKOUT_DISABLED,
- 'fieldset_data' => [
- 'session_endpoint' => self::ENDPOINT,
- 'account_id' => self::USER_INVALID,
- 'bearer_token' => self::PASSWORD_VALID,
- ]
- ]
- ]);
- $this->expectException(\Magento\Framework\Validator\Exception::class);
- $this->expectExceptionMessage('Magento Shipping authentication failed. Please check your credentials.');
- $backendModel->validateBeforeSave();
- }
- /**
- * @test
- */
- public function cannotSaveWithCheckoutDisabledAndCredentialsInvalid()
- {
- $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
- 'validationRules' => $this->getValidator(),
- 'data' => [
- 'value' => self::CHECKOUT_DISABLED,
- 'fieldset_data' => [
- 'session_endpoint' => self::ENDPOINT,
- 'account_id' => self::USER_INVALID,
- 'bearer_token' => self::PASSWORD_INVALID,
- ]
- ]
- ]);
- $this->expectException(\Magento\Framework\Validator\Exception::class);
- $this->expectExceptionMessage('Magento Shipping authentication failed. Please check your credentials.');
- $backendModel->validateBeforeSave();
- }
- /**
- * @test
- */
- public function cannotSaveWithCheckoutEnabledAndCredentialsEmpty()
- {
- $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
- 'validationRules' => $this->getValidator(),
- 'data' => [
- 'value' => self::CHECKOUT_ENABLED,
- 'fieldset_data' => [
- 'session_endpoint' => self::ENDPOINT,
- 'account_id' => '',
- 'bearer_token' => '',
- ]
- ]
- ]);
- $this->expectException(\Magento\Framework\Validator\Exception::class);
- $this->expectExceptionMessage('Please set API credentials before enabling Magento Shipping.');
- $backendModel->validateBeforeSave();
- }
- /**
- * @test
- */
- public function cannotSaveWithCheckoutEnabledAndPasswordInvalid()
- {
- $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
- 'validationRules' => $this->getValidator(),
- 'data' => [
- 'value' => self::CHECKOUT_ENABLED,
- 'fieldset_data' => [
- 'session_endpoint' => self::ENDPOINT,
- 'account_id' => self::USER_VALID,
- 'bearer_token' => self::PASSWORD_INVALID,
- ]
- ]
- ]);
- $this->expectException(\Magento\Framework\Validator\Exception::class);
- $this->expectExceptionMessage('Magento Shipping authentication failed. Please check your credentials.');
- $backendModel->validateBeforeSave();
- }
- /**
- * @test
- */
- public function cannotSaveWithCheckoutEnabledAndUserInvalid()
- {
- $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
- 'validationRules' => $this->getValidator(),
- 'data' => [
- 'value' => self::CHECKOUT_ENABLED,
- 'fieldset_data' => [
- 'session_endpoint' => self::ENDPOINT,
- 'account_id' => self::USER_INVALID,
- 'bearer_token' => self::PASSWORD_VALID,
- ]
- ]
- ]);
- $this->expectException(\Magento\Framework\Validator\Exception::class);
- $this->expectExceptionMessage('Magento Shipping authentication failed. Please check your credentials.');
- $backendModel->validateBeforeSave();
- }
- /**
- * @test
- */
- public function cannotSaveWithCheckoutEnabledAndCredentialsInvalid()
- {
- $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
- 'validationRules' => $this->getValidator(),
- 'data' => [
- 'value' => self::CHECKOUT_ENABLED,
- 'fieldset_data' => [
- 'session_endpoint' => self::ENDPOINT,
- 'account_id' => self::USER_INVALID,
- 'bearer_token' => self::PASSWORD_INVALID,
- ]
- ]
- ]);
- $this->expectException(\Magento\Framework\Validator\Exception::class);
- $this->expectExceptionMessage('Magento Shipping authentication failed. Please check your credentials.');
- $backendModel->validateBeforeSave();
- }
- /**
- * @test
- */
- public function canSaveWithValidEndpointProtocol()
- {
- $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
- 'validationRules' => $this->getValidator(),
- 'data' => [
- 'value' => self::CHECKOUT_ENABLED,
- 'fieldset_data' => [
- 'session_endpoint' => self::ENDPOINT,
- 'account_id' => self::USER_VALID,
- 'bearer_token' => self::PASSWORD_VALID,
- ]
- ]
- ]);
- // just assert no exception is thrown
- $this->assertSame($backendModel, $backendModel->validateBeforeSave());
- }
- /**
- * @test
- */
- public function cannotSaveWithInvalidEndpointProtocol()
- {
- $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
- 'validationRules' => $this->getValidator(),
- 'data' => [
- 'value' => self::CHECKOUT_ENABLED,
- 'fieldset_data' => [
- 'session_endpoint' => self::ENDPOINT_INVALID,
- 'account_id' => self::USER_INVALID,
- 'bearer_token' => self::PASSWORD_INVALID,
- ]
- ]
- ]);
- $this->expectException(\Magento\Framework\Validator\Exception::class);
- $this->expectExceptionMessage('Please enter a valid URL. Protocol (http://, https://) is required.');
- $backendModel->validateBeforeSave();
- }
- }
|