CredentialsValidationTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Config\Backend;
  6. use Magento\Framework\Exception\AuthenticationException;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. use Temando\Shipping\Model\Config\Backend\Active\ApiConnection;
  9. use Temando\Shipping\Model\Config\Backend\Active\CredentialsValidator;
  10. use Temando\Shipping\Rest\Authentication;
  11. /**
  12. * Temando Carrier Enabled Config Backend Model Test
  13. *
  14. * @package Temando\Shipping\Test\Integration
  15. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  16. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  17. * @link http://www.temando.com/
  18. */
  19. class CredentialsValidationTest extends \PHPUnit\Framework\TestCase
  20. {
  21. const ENDPOINT = 'https://auth.temando.io/v1/';
  22. const ENDPOINT_INVALID = 'gopher://127.0.0.1';
  23. const CHECKOUT_DISABLED = '0';
  24. const CHECKOUT_ENABLED = '1';
  25. const USER_VALID = 'alice';
  26. const USER_INVALID = 'malice';
  27. const PASSWORD_VALID = 'openSesame';
  28. const PASSWORD_INVALID = 'computerSaysNo';
  29. /**
  30. * @return CredentialsValidator
  31. */
  32. private function getValidator()
  33. {
  34. $authTest = $this->getMockBuilder(ApiConnection::class)
  35. ->setMethods(['test'])
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $authTest
  39. ->expects($this->any())
  40. ->method('test')
  41. ->willReturnMap([
  42. [self::ENDPOINT, '', self::PASSWORD_INVALID, false],
  43. [self::ENDPOINT, self::USER_INVALID, '', false],
  44. [self::ENDPOINT, self::USER_INVALID, self::PASSWORD_INVALID, false],
  45. [self::ENDPOINT, '', self::PASSWORD_VALID, false],
  46. [self::ENDPOINT, self::USER_VALID, '', false],
  47. [self::ENDPOINT, self::USER_VALID, self::PASSWORD_VALID, true],
  48. ]);
  49. $validator = Bootstrap::getObjectManager()->create(CredentialsValidator::class, [
  50. 'connection' => $authTest,
  51. ]);
  52. return $validator;
  53. }
  54. /**
  55. * @test
  56. */
  57. public function canSaveWithCheckoutDisabledAndApiEndpointEmpty()
  58. {
  59. $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
  60. 'validationRules' => $this->getValidator(),
  61. 'data' => [
  62. 'value' => self::CHECKOUT_DISABLED,
  63. 'fieldset_data' => [
  64. 'session_endpoint' => '',
  65. 'account_id' => '',
  66. 'bearer_token' => '',
  67. ]
  68. ]
  69. ]);
  70. // just assert no exception is thrown
  71. $this->assertSame($backendModel, $backendModel->validateBeforeSave());
  72. }
  73. /**
  74. * @test
  75. */
  76. public function canSaveWithCheckoutDisabledAndCredentialsEmpty()
  77. {
  78. $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
  79. 'validationRules' => $this->getValidator(),
  80. 'data' => [
  81. 'value' => self::CHECKOUT_DISABLED,
  82. 'fieldset_data' => [
  83. 'session_endpoint' => self::ENDPOINT,
  84. 'account_id' => '',
  85. 'bearer_token' => '',
  86. ]
  87. ]
  88. ]);
  89. // just assert no exception is thrown
  90. $this->assertSame($backendModel, $backendModel->validateBeforeSave());
  91. }
  92. /**
  93. * @test
  94. */
  95. public function canSaveWithCheckoutDisabledAndCredentialsValid()
  96. {
  97. $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
  98. 'validationRules' => $this->getValidator(),
  99. 'data' => [
  100. 'value' => self::CHECKOUT_DISABLED,
  101. 'fieldset_data' => [
  102. 'session_endpoint' => self::ENDPOINT,
  103. 'account_id' => self::USER_VALID,
  104. 'bearer_token' => self::PASSWORD_VALID,
  105. ]
  106. ]
  107. ]);
  108. // just assert no exception is thrown
  109. $this->assertSame($backendModel, $backendModel->validateBeforeSave());
  110. }
  111. /**
  112. * @test
  113. */
  114. public function canSaveWithCheckoutEnabledAndCredentialsValid()
  115. {
  116. $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
  117. 'validationRules' => $this->getValidator(),
  118. 'data' => [
  119. 'value' => self::CHECKOUT_ENABLED,
  120. 'fieldset_data' => [
  121. 'session_endpoint' => self::ENDPOINT,
  122. 'account_id' => self::USER_VALID,
  123. 'bearer_token' => self::PASSWORD_VALID,
  124. ]
  125. ]
  126. ]);
  127. // just assert no exception is thrown
  128. $this->assertSame($backendModel, $backendModel->validateBeforeSave());
  129. }
  130. /**
  131. * @test
  132. */
  133. public function cannotSaveWithCheckoutDisabledAndPasswordInvalid()
  134. {
  135. $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
  136. 'validationRules' => $this->getValidator(),
  137. 'data' => [
  138. 'value' => self::CHECKOUT_DISABLED,
  139. 'fieldset_data' => [
  140. 'session_endpoint' => self::ENDPOINT,
  141. 'account_id' => self::USER_VALID,
  142. 'bearer_token' => self::PASSWORD_INVALID,
  143. ]
  144. ]
  145. ]);
  146. $this->expectException(\Magento\Framework\Validator\Exception::class);
  147. $this->expectExceptionMessage('Magento Shipping authentication failed. Please check your credentials.');
  148. $backendModel->validateBeforeSave();
  149. }
  150. /**
  151. * @test
  152. */
  153. public function cannotSaveWithCheckoutDisabledAndUserInvalid()
  154. {
  155. $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
  156. 'validationRules' => $this->getValidator(),
  157. 'data' => [
  158. 'value' => self::CHECKOUT_DISABLED,
  159. 'fieldset_data' => [
  160. 'session_endpoint' => self::ENDPOINT,
  161. 'account_id' => self::USER_INVALID,
  162. 'bearer_token' => self::PASSWORD_VALID,
  163. ]
  164. ]
  165. ]);
  166. $this->expectException(\Magento\Framework\Validator\Exception::class);
  167. $this->expectExceptionMessage('Magento Shipping authentication failed. Please check your credentials.');
  168. $backendModel->validateBeforeSave();
  169. }
  170. /**
  171. * @test
  172. */
  173. public function cannotSaveWithCheckoutDisabledAndCredentialsInvalid()
  174. {
  175. $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
  176. 'validationRules' => $this->getValidator(),
  177. 'data' => [
  178. 'value' => self::CHECKOUT_DISABLED,
  179. 'fieldset_data' => [
  180. 'session_endpoint' => self::ENDPOINT,
  181. 'account_id' => self::USER_INVALID,
  182. 'bearer_token' => self::PASSWORD_INVALID,
  183. ]
  184. ]
  185. ]);
  186. $this->expectException(\Magento\Framework\Validator\Exception::class);
  187. $this->expectExceptionMessage('Magento Shipping authentication failed. Please check your credentials.');
  188. $backendModel->validateBeforeSave();
  189. }
  190. /**
  191. * @test
  192. */
  193. public function cannotSaveWithCheckoutEnabledAndCredentialsEmpty()
  194. {
  195. $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
  196. 'validationRules' => $this->getValidator(),
  197. 'data' => [
  198. 'value' => self::CHECKOUT_ENABLED,
  199. 'fieldset_data' => [
  200. 'session_endpoint' => self::ENDPOINT,
  201. 'account_id' => '',
  202. 'bearer_token' => '',
  203. ]
  204. ]
  205. ]);
  206. $this->expectException(\Magento\Framework\Validator\Exception::class);
  207. $this->expectExceptionMessage('Please set API credentials before enabling Magento Shipping.');
  208. $backendModel->validateBeforeSave();
  209. }
  210. /**
  211. * @test
  212. */
  213. public function cannotSaveWithCheckoutEnabledAndPasswordInvalid()
  214. {
  215. $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
  216. 'validationRules' => $this->getValidator(),
  217. 'data' => [
  218. 'value' => self::CHECKOUT_ENABLED,
  219. 'fieldset_data' => [
  220. 'session_endpoint' => self::ENDPOINT,
  221. 'account_id' => self::USER_VALID,
  222. 'bearer_token' => self::PASSWORD_INVALID,
  223. ]
  224. ]
  225. ]);
  226. $this->expectException(\Magento\Framework\Validator\Exception::class);
  227. $this->expectExceptionMessage('Magento Shipping authentication failed. Please check your credentials.');
  228. $backendModel->validateBeforeSave();
  229. }
  230. /**
  231. * @test
  232. */
  233. public function cannotSaveWithCheckoutEnabledAndUserInvalid()
  234. {
  235. $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
  236. 'validationRules' => $this->getValidator(),
  237. 'data' => [
  238. 'value' => self::CHECKOUT_ENABLED,
  239. 'fieldset_data' => [
  240. 'session_endpoint' => self::ENDPOINT,
  241. 'account_id' => self::USER_INVALID,
  242. 'bearer_token' => self::PASSWORD_VALID,
  243. ]
  244. ]
  245. ]);
  246. $this->expectException(\Magento\Framework\Validator\Exception::class);
  247. $this->expectExceptionMessage('Magento Shipping authentication failed. Please check your credentials.');
  248. $backendModel->validateBeforeSave();
  249. }
  250. /**
  251. * @test
  252. */
  253. public function cannotSaveWithCheckoutEnabledAndCredentialsInvalid()
  254. {
  255. $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
  256. 'validationRules' => $this->getValidator(),
  257. 'data' => [
  258. 'value' => self::CHECKOUT_ENABLED,
  259. 'fieldset_data' => [
  260. 'session_endpoint' => self::ENDPOINT,
  261. 'account_id' => self::USER_INVALID,
  262. 'bearer_token' => self::PASSWORD_INVALID,
  263. ]
  264. ]
  265. ]);
  266. $this->expectException(\Magento\Framework\Validator\Exception::class);
  267. $this->expectExceptionMessage('Magento Shipping authentication failed. Please check your credentials.');
  268. $backendModel->validateBeforeSave();
  269. }
  270. /**
  271. * @test
  272. */
  273. public function canSaveWithValidEndpointProtocol()
  274. {
  275. $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
  276. 'validationRules' => $this->getValidator(),
  277. 'data' => [
  278. 'value' => self::CHECKOUT_ENABLED,
  279. 'fieldset_data' => [
  280. 'session_endpoint' => self::ENDPOINT,
  281. 'account_id' => self::USER_VALID,
  282. 'bearer_token' => self::PASSWORD_VALID,
  283. ]
  284. ]
  285. ]);
  286. // just assert no exception is thrown
  287. $this->assertSame($backendModel, $backendModel->validateBeforeSave());
  288. }
  289. /**
  290. * @test
  291. */
  292. public function cannotSaveWithInvalidEndpointProtocol()
  293. {
  294. $backendModel = Bootstrap::getObjectManager()->create(Active::class, [
  295. 'validationRules' => $this->getValidator(),
  296. 'data' => [
  297. 'value' => self::CHECKOUT_ENABLED,
  298. 'fieldset_data' => [
  299. 'session_endpoint' => self::ENDPOINT_INVALID,
  300. 'account_id' => self::USER_INVALID,
  301. 'bearer_token' => self::PASSWORD_INVALID,
  302. ]
  303. ]
  304. ]);
  305. $this->expectException(\Magento\Framework\Validator\Exception::class);
  306. $this->expectExceptionMessage('Please enter a valid URL. Protocol (http://, https://) is required.');
  307. $backendModel->validateBeforeSave();
  308. }
  309. }