ChangeCustomerPasswordTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\GraphQl\Customer;
  8. use Magento\Customer\Api\AccountManagementInterface;
  9. use Magento\Customer\Model\CustomerRegistry;
  10. use Magento\Framework\Exception\LocalizedException;
  11. use Magento\Integration\Api\CustomerTokenServiceInterface;
  12. use Magento\TestFramework\Helper\Bootstrap;
  13. use Magento\TestFramework\TestCase\GraphQlAbstract;
  14. class ChangeCustomerPasswordTest extends GraphQlAbstract
  15. {
  16. /**
  17. * @var AccountManagementInterface
  18. */
  19. private $accountManagement;
  20. /**
  21. * @var CustomerTokenServiceInterface
  22. */
  23. private $customerTokenService;
  24. /**
  25. * @var CustomerRegistry
  26. */
  27. private $customerRegistry;
  28. protected function setUp()
  29. {
  30. $this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
  31. $this->accountManagement = Bootstrap::getObjectManager()->get(AccountManagementInterface::class);
  32. $this->customerRegistry = Bootstrap::getObjectManager()->get(CustomerRegistry::class);
  33. }
  34. /**
  35. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  36. */
  37. public function testChangePassword()
  38. {
  39. $customerEmail = 'customer@example.com';
  40. $oldCustomerPassword = 'password';
  41. $newCustomerPassword = 'anotherPassword1';
  42. $query = $this->getChangePassQuery($oldCustomerPassword, $newCustomerPassword);
  43. $headerMap = $this->getCustomerAuthHeaders($customerEmail, $oldCustomerPassword);
  44. $response = $this->graphQlQuery($query, [], '', $headerMap);
  45. $this->assertEquals($customerEmail, $response['changeCustomerPassword']['email']);
  46. try {
  47. // registry contains the old password hash so needs to be reset
  48. $this->customerRegistry->removeByEmail($customerEmail);
  49. $this->accountManagement->authenticate($customerEmail, $newCustomerPassword);
  50. } catch (LocalizedException $e) {
  51. $this->fail('Password was not changed: ' . $e->getMessage());
  52. }
  53. }
  54. /**
  55. * @expectedException \Exception
  56. * @expectedExceptionMessage The current customer isn't authorized.
  57. */
  58. public function testChangePasswordIfUserIsNotAuthorizedTest()
  59. {
  60. $query = $this->getChangePassQuery('currentpassword', 'newpassword');
  61. $this->graphQlQuery($query);
  62. }
  63. /**
  64. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  65. */
  66. public function testChangeWeakPassword()
  67. {
  68. $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/190');
  69. $customerEmail = 'customer@example.com';
  70. $oldCustomerPassword = 'password';
  71. $newCustomerPassword = 'weakpass';
  72. $query = $this->getChangePassQuery($oldCustomerPassword, $newCustomerPassword);
  73. $headerMap = $this->getCustomerAuthHeaders($customerEmail, $oldCustomerPassword);
  74. $this->expectException(\Exception::class);
  75. $this->expectExceptionMessageRegExp('/Minimum of different classes of characters in password is.*/');
  76. $this->graphQlQuery($query, [], '', $headerMap);
  77. }
  78. /**
  79. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  80. * @expectedException \Exception
  81. * @expectedExceptionMessage The password doesn't match this account. Verify the password and try again.
  82. */
  83. public function testChangePasswordIfPasswordIsInvalid()
  84. {
  85. $customerEmail = 'customer@example.com';
  86. $oldCustomerPassword = 'password';
  87. $newCustomerPassword = 'anotherPassword1';
  88. $incorrectPassword = 'password-incorrect';
  89. $query = $this->getChangePassQuery($incorrectPassword, $newCustomerPassword);
  90. $headerMap = $this->getCustomerAuthHeaders($customerEmail, $oldCustomerPassword);
  91. $this->graphQlQuery($query, [], '', $headerMap);
  92. }
  93. private function getChangePassQuery($currentPassword, $newPassword)
  94. {
  95. $query = <<<QUERY
  96. mutation {
  97. changeCustomerPassword(
  98. currentPassword: "$currentPassword",
  99. newPassword: "$newPassword"
  100. ) {
  101. id
  102. email
  103. firstname
  104. lastname
  105. }
  106. }
  107. QUERY;
  108. return $query;
  109. }
  110. /**
  111. * @param string $email
  112. * @param string $password
  113. * @return array
  114. */
  115. private function getCustomerAuthHeaders(string $email, string $password): array
  116. {
  117. $customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
  118. return ['Authorization' => 'Bearer ' . $customerToken];
  119. }
  120. }