CustomerTokenServiceTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Model;
  7. use Magento\Customer\Api\AccountManagementInterface;
  8. use Magento\Framework\Exception\InputException;
  9. use Magento\Integration\Model\Oauth\Token as TokenModel;
  10. use Magento\TestFramework\Helper\Bootstrap;
  11. /**
  12. * Test class for \Magento\Integration\Model\CustomerTokenService.
  13. */
  14. class CustomerTokenServiceTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var CustomerTokenServiceInterface
  18. */
  19. private $tokenService;
  20. /**
  21. * @var AccountManagementInterface
  22. */
  23. private $accountManagement;
  24. /**
  25. * @var TokenModel
  26. */
  27. private $tokenModel;
  28. /**
  29. * Setup CustomerTokenService
  30. */
  31. public function setUp()
  32. {
  33. $this->tokenService = Bootstrap::getObjectManager()->get(
  34. \Magento\Integration\Model\CustomerTokenService::class
  35. );
  36. $this->accountManagement = Bootstrap::getObjectManager()->get(
  37. \Magento\Customer\Api\AccountManagementInterface::class
  38. );
  39. $this->tokenModel = Bootstrap::getObjectManager()->get(\Magento\Integration\Model\Oauth\Token::class);
  40. }
  41. /**
  42. * @magentoDataFixture Magento/Customer/_files/customer.php
  43. */
  44. public function testCreateCustomerAccessToken()
  45. {
  46. $customerUserName = 'customer@example.com';
  47. $password = 'password';
  48. $accessToken = $this->tokenService->createCustomerAccessToken($customerUserName, $password);
  49. $customerData = $this->accountManagement->authenticate($customerUserName, $password);
  50. /** @var $token TokenModel */
  51. $token = $this->tokenModel->loadByCustomerId($customerData->getId())->getToken();
  52. $this->assertEquals($accessToken, $token);
  53. }
  54. /**
  55. * @dataProvider validationDataProvider
  56. */
  57. public function testCreateCustomerAccessTokenEmptyOrNullCredentials($username, $password)
  58. {
  59. try {
  60. $this->tokenService->createCustomerAccessToken($username, $password);
  61. } catch (InputException $e) {
  62. $this->assertInputExceptionMessages($e);
  63. }
  64. }
  65. /**
  66. * @expectedException \Magento\Framework\Exception\AuthenticationException
  67. */
  68. public function testCreateCustomerAccessTokenInvalidCustomer()
  69. {
  70. $customerUserName = 'invalid';
  71. $password = 'invalid';
  72. $this->tokenService->createCustomerAccessToken($customerUserName, $password);
  73. $this->expectExceptionMessage(
  74. 'The account sign-in was incorrect or your account is disabled temporarily. '
  75. . 'Please wait and try again later.'
  76. );
  77. }
  78. /**
  79. * Provider to test input validation
  80. *
  81. * @return array
  82. */
  83. public function validationDataProvider()
  84. {
  85. return [
  86. 'Check for empty credentials' => ['', ''],
  87. 'Check for null credentials' => [null, null]
  88. ];
  89. }
  90. /**
  91. * Assert for presence of Input exception messages
  92. *
  93. * @param InputException $e
  94. */
  95. private function assertInputExceptionMessages($e)
  96. {
  97. $this->assertEquals('One or more input exceptions have occurred.', $e->getMessage());
  98. $errors = $e->getErrors();
  99. $this->assertCount(2, $errors);
  100. $this->assertEquals('"username" is required. Enter and try again.', $errors[0]->getLogMessage());
  101. $this->assertEquals('"password" is required. Enter and try again.', $errors[1]->getLogMessage());
  102. }
  103. }