|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Customer; |
| 9 | + |
| 10 | +use Magento\Customer\Api\AccountManagementInterface; |
| 11 | +use Magento\Framework\Exception\LocalizedException; |
| 12 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 13 | +use Magento\TestFramework\ObjectManager; |
| 14 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 15 | + |
| 16 | +class CustomerChangePasswordTest extends GraphQlAbstract |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var ObjectManager |
| 20 | + */ |
| 21 | + private $objectManager; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var AccountManagementInterface |
| 25 | + */ |
| 26 | + private $accountManagement; |
| 27 | + |
| 28 | + /** |
| 29 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 30 | + */ |
| 31 | + public function testCustomerChangeValidPassword() |
| 32 | + { |
| 33 | + $customerEmail = 'customer@example.com'; |
| 34 | + $oldCustomerPassword = 'password'; |
| 35 | + $newCustomerPassword = 'anotherPassword1'; |
| 36 | + |
| 37 | + $query = <<<QUERY |
| 38 | +mutation { |
| 39 | + changePassword( |
| 40 | + currentPassword: "$oldCustomerPassword", |
| 41 | + newPassword: "$newCustomerPassword" |
| 42 | + ) { |
| 43 | + id |
| 44 | + email |
| 45 | + firstname |
| 46 | + lastname |
| 47 | + } |
| 48 | +} |
| 49 | +QUERY; |
| 50 | + |
| 51 | + /** @var CustomerTokenServiceInterface $customerTokenService */ |
| 52 | + $customerTokenService = $this->objectManager->create(CustomerTokenServiceInterface::class); |
| 53 | + $customerToken = $customerTokenService->createCustomerAccessToken($customerEmail, $oldCustomerPassword); |
| 54 | + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; |
| 55 | + $response = $this->graphQlQuery($query, [], '', $headerMap); |
| 56 | + $this->assertEquals($customerEmail, $response['changePassword']['email']); |
| 57 | + |
| 58 | + try { |
| 59 | + // registry contains the old password hash so needs to be reset |
| 60 | + $this->objectManager->get(\Magento\Customer\Model\CustomerRegistry::class) |
| 61 | + ->removeByEmail($customerEmail); |
| 62 | + $this->accountManagement->authenticate($customerEmail, $newCustomerPassword); |
| 63 | + } catch (LocalizedException $e) { |
| 64 | + $this->fail('Password was not changed: ' . $e->getMessage()); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public function testGuestUserCannotChangePassword() |
| 69 | + { |
| 70 | + $query = <<<QUERY |
| 71 | +mutation { |
| 72 | + changePassword( |
| 73 | + currentPassword: "currentpassword", |
| 74 | + newPassword: "newpassword" |
| 75 | + ) { |
| 76 | + id |
| 77 | + email |
| 78 | + firstname |
| 79 | + lastname |
| 80 | + } |
| 81 | +} |
| 82 | +QUERY; |
| 83 | + $this->expectException(\Exception::class); |
| 84 | + $this->expectExceptionMessage('GraphQL response contains errors: Current customer' . ' ' . |
| 85 | + 'does not have access to the resource "customer"'); |
| 86 | + $this->graphQlQuery($query); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 91 | + */ |
| 92 | + public function testChangeWeakPassword() |
| 93 | + { |
| 94 | + $customerEmail = 'customer@example.com'; |
| 95 | + $oldCustomerPassword = 'password'; |
| 96 | + $newCustomerPassword = 'weakpass'; |
| 97 | + |
| 98 | + $query = <<<QUERY |
| 99 | +mutation { |
| 100 | + changePassword( |
| 101 | + currentPassword: "$oldCustomerPassword", |
| 102 | + newPassword: "$newCustomerPassword" |
| 103 | + ) { |
| 104 | + id |
| 105 | + email |
| 106 | + firstname |
| 107 | + lastname |
| 108 | + } |
| 109 | +} |
| 110 | +QUERY; |
| 111 | + |
| 112 | + /** @var CustomerTokenServiceInterface $customerTokenService */ |
| 113 | + $customerTokenService = $this->objectManager->create(CustomerTokenServiceInterface::class); |
| 114 | + $customerToken = $customerTokenService->createCustomerAccessToken($customerEmail, $oldCustomerPassword); |
| 115 | + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; |
| 116 | + |
| 117 | + $this->expectException(\Exception::class); |
| 118 | + $this->expectExceptionMessageRegExp('/Minimum of different classes of characters in password is.*/'); |
| 119 | + |
| 120 | + $this->graphQlQuery($query, [], '', $headerMap); |
| 121 | + } |
| 122 | + |
| 123 | + protected function setUp() |
| 124 | + { |
| 125 | + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 126 | + $this->accountManagement = $this->objectManager->get(AccountManagementInterface::class); |
| 127 | + } |
| 128 | +} |
0 commit comments