Skip to content

Commit 63762d6

Browse files
committed
Respect allow send as guest configuration in resolver
Resolver throws `GraphQlAuthorizationException` when sending as guest if allow send as guest is disabled. Fixes #732
1 parent 9473b31 commit 63762d6

File tree

4 files changed

+177
-22
lines changed

4 files changed

+177
-22
lines changed

app/code/Magento/SendFriendGraphQl/Model/Resolver/SendEmailToFriend.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@
77

88
namespace Magento\SendFriendGraphQl\Model\Resolver;
99

10+
use Magento\Authorization\Model\UserContextInterface;
1011
use Magento\Catalog\Api\Data\ProductInterface;
1112
use Magento\Catalog\Api\ProductRepositoryInterface;
13+
use Magento\Framework\App\ObjectManager;
1214
use Magento\Framework\DataObjectFactory;
1315
use Magento\Framework\Event\ManagerInterface;
1416
use Magento\Framework\Exception\NoSuchEntityException;
1517
use Magento\Framework\GraphQl\Config\Element\Field;
18+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1619
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1720
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1821
use Magento\Framework\GraphQl\Query\ResolverInterface;
1922
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
2023
use Magento\SendFriend\Model\SendFriend;
2124
use Magento\SendFriend\Model\SendFriendFactory;
25+
use Magento\SendFriend\Helper\Data as SendFriendHelper;
2226

2327
/**
2428
* @inheritdoc
@@ -45,29 +49,41 @@ class SendEmailToFriend implements ResolverInterface
4549
*/
4650
private $eventManager;
4751

52+
/**
53+
* @var SendFriendHelper
54+
*/
55+
private $sendFriendHelper;
56+
4857
/**
4958
* @param SendFriendFactory $sendFriendFactory
5059
* @param ProductRepositoryInterface $productRepository
5160
* @param DataObjectFactory $dataObjectFactory
5261
* @param ManagerInterface $eventManager
62+
* @param SendFriendHelper|null $sendFriendHelper
5363
*/
5464
public function __construct(
5565
SendFriendFactory $sendFriendFactory,
5666
ProductRepositoryInterface $productRepository,
5767
DataObjectFactory $dataObjectFactory,
58-
ManagerInterface $eventManager
68+
ManagerInterface $eventManager,
69+
SendFriendHelper $sendFriendHelper = null
5970
) {
6071
$this->sendFriendFactory = $sendFriendFactory;
6172
$this->productRepository = $productRepository;
6273
$this->dataObjectFactory = $dataObjectFactory;
6374
$this->eventManager = $eventManager;
75+
$this->sendFriendHelper = $sendFriendHelper ?? ObjectManager::getInstance()->get(SendFriendHelper::class);
6476
}
6577

6678
/**
6779
* @inheritdoc
6880
*/
6981
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
7082
{
83+
if (!$this->sendFriendHelper->isAllowForGuest() && $this->isUserGuest($context->getUserId(), $context->getUserType())) {
84+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
85+
}
86+
7187
/** @var SendFriend $sendFriend */
7288
$sendFriend = $this->sendFriendFactory->create();
7389

@@ -195,4 +211,19 @@ private function extractSenderData(array $args): array
195211
],
196212
];
197213
}
214+
215+
/**
216+
* Checking if current customer is guest
217+
*
218+
* @param int|null $customerId
219+
* @param int|null $customerType
220+
* @return bool
221+
*/
222+
private function isUserGuest(?int $customerId, ?int $customerType): bool
223+
{
224+
if (null === $customerId || null === $customerType) {
225+
return true;
226+
}
227+
return 0 === (int)$customerId || (int)$customerType === UserContextInterface::USER_TYPE_GUEST;
228+
}
198229
}

dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php

Lines changed: 103 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
namespace Magento\GraphQl\SendFriend;
99

1010
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Framework\Exception\AuthenticationException;
12+
use Magento\Integration\Api\CustomerTokenServiceInterface;
1113
use Magento\SendFriend\Model\SendFriend;
1214
use Magento\SendFriend\Model\SendFriendFactory;
1315
use Magento\TestFramework\Helper\Bootstrap;
@@ -23,21 +25,29 @@ class SendFriendTest extends GraphQlAbstract
2325
* @var SendFriendFactory
2426
*/
2527
private $sendFriendFactory;
28+
2629
/**
2730
* @var ProductRepositoryInterface
2831
*/
2932
private $productRepository;
3033

34+
/**
35+
* @var CustomerTokenServiceInterface
36+
*/
37+
private $customerTokenService;
38+
3139
protected function setUp()
3240
{
3341
$this->sendFriendFactory = Bootstrap::getObjectManager()->get(SendFriendFactory::class);
3442
$this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
43+
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
3544
}
3645

3746
/**
3847
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
48+
* @magentoApiDataFixture Magento/GraphQl/SendFriend/_files/enable_send_friend_guest.php
3949
*/
40-
public function testSendFriend()
50+
public function testSendFriendGuestEnable()
4151
{
4252
$productId = (int)$this->productRepository->get('simple_product')->getId();
4353
$recipients = '{
@@ -51,15 +61,57 @@ public function testSendFriend()
5161
$query = $this->getQuery($productId, $recipients);
5262

5363
$response = $this->graphQlMutation($query);
54-
self::assertEquals('Name', $response['sendEmailToFriend']['sender']['name']);
55-
self::assertEquals('[email protected]', $response['sendEmailToFriend']['sender']['email']);
56-
self::assertEquals('Lorem Ipsum', $response['sendEmailToFriend']['sender']['message']);
57-
self::assertEquals('Recipient Name 1', $response['sendEmailToFriend']['recipients'][0]['name']);
58-
self::assertEquals('[email protected]', $response['sendEmailToFriend']['recipients'][0]['email']);
59-
self::assertEquals('Recipient Name 2', $response['sendEmailToFriend']['recipients'][1]['name']);
60-
self::assertEquals('[email protected]', $response['sendEmailToFriend']['recipients'][1]['email']);
64+
$this->assertResponse($response);
65+
}
66+
67+
/**
68+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
69+
* @magentoApiDataFixture Magento/GraphQl/SendFriend/_files/disable_send_friend_guest.php
70+
* @expectedException \Exception
71+
* @expectedExceptionMessage The current customer isn't authorized.
72+
*/
73+
public function testSendFriendGuestDisableAsGuest()
74+
{
75+
$productId = (int)$this->productRepository->get('simple_product')->getId();
76+
$recipients = '{
77+
name: "Recipient Name 1"
78+
79+
},
80+
{
81+
name: "Recipient Name 2"
82+
83+
}';
84+
$query = $this->getQuery($productId, $recipients);
85+
86+
$response = $this->graphQlMutation($query);
87+
$this->assertResponse($response);
88+
}
89+
90+
/**
91+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
92+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
93+
* @magentoApiDataFixture Magento/GraphQl/SendFriend/_files/disable_send_friend_guest.php
94+
*/
95+
public function testSendFriendGuestDisableAsCustomer()
96+
{
97+
$productId = (int)$this->productRepository->get('simple_product')->getId();
98+
$recipients = '{
99+
name: "Recipient Name 1"
100+
101+
},
102+
{
103+
name: "Recipient Name 2"
104+
105+
}';
106+
$query = $this->getQuery($productId, $recipients);
107+
108+
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
109+
$this->assertResponse($response);
61110
}
62111

112+
/**
113+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
114+
*/
63115
public function testSendWithoutExistProduct()
64116
{
65117
$productId = 2018;
@@ -77,10 +129,11 @@ public function testSendWithoutExistProduct()
77129
$this->expectExceptionMessage(
78130
'The product that was requested doesn\'t exist. Verify the product and try again.'
79131
);
80-
$this->graphQlMutation($query);
132+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
81133
}
82134

83135
/**
136+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
84137
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
85138
*/
86139
public function testMaxSendEmailToFriend()
@@ -118,10 +171,11 @@ public function testMaxSendEmailToFriend()
118171

119172
$this->expectException(\Exception::class);
120173
$this->expectExceptionMessage("No more than {$sendFriend->getMaxRecipients()} emails can be sent at a time.");
121-
$this->graphQlMutation($query);
174+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
122175
}
123176

124177
/**
178+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
125179
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
126180
* @dataProvider sendFriendsErrorsDataProvider
127181
* @param string $input
@@ -151,10 +205,11 @@ public function testErrors(string $input, string $errorMessage)
151205
QUERY;
152206
$this->expectException(\Exception::class);
153207
$this->expectExceptionMessage($errorMessage);
154-
$this->graphQlMutation($query);
208+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
155209
}
156210

157211
/**
212+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
158213
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
159214
* TODO: use magentoApiConfigFixture (to be merged https://github.com/magento/graphql-ce/pull/351)
160215
* @magentoApiDataFixture Magento/SendFriend/Fixtures/sendfriend_configuration.php
@@ -183,11 +238,12 @@ public function testLimitMessagesPerHour()
183238

184239
$maxSendToFriends = $sendFriend->getMaxSendsToFriend();
185240
for ($i = 0; $i <= $maxSendToFriends + 1; $i++) {
186-
$this->graphQlMutation($query);
241+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
187242
}
188243
}
189244

190245
/**
246+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
191247
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
192248
*/
193249
public function testSendProductWithoutSenderEmail()
@@ -201,10 +257,11 @@ public function testSendProductWithoutSenderEmail()
201257

202258
$this->expectException(\Exception::class);
203259
$this->expectExceptionMessage('GraphQL response contains errors: Please provide Email for all of recipients.');
204-
$this->graphQlMutation($query);
260+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
205261
}
206262

207263
/**
264+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
208265
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product_without_visibility.php
209266
*/
210267
public function testSendProductWithoutVisibility()
@@ -220,14 +277,8 @@ public function testSendProductWithoutVisibility()
220277
}';
221278
$query = $this->getQuery($productId, $recipients);
222279

223-
$response = $this->graphQlMutation($query);
224-
self::assertEquals('Name', $response['sendEmailToFriend']['sender']['name']);
225-
self::assertEquals('[email protected]', $response['sendEmailToFriend']['sender']['email']);
226-
self::assertEquals('Lorem Ipsum', $response['sendEmailToFriend']['sender']['message']);
227-
self::assertEquals('Recipient Name 1', $response['sendEmailToFriend']['recipients'][0]['name']);
228-
self::assertEquals('[email protected]', $response['sendEmailToFriend']['recipients'][0]['email']);
229-
self::assertEquals('Recipient Name 2', $response['sendEmailToFriend']['recipients'][1]['name']);
230-
self::assertEquals('[email protected]', $response['sendEmailToFriend']['recipients'][1]['email']);
280+
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
281+
$this->assertResponse($response);
231282
}
232283

233284
/**
@@ -311,6 +362,37 @@ public function sendFriendsErrorsDataProvider()
311362
];
312363
}
313364

365+
/**
366+
* Generic assertions for send a friend response
367+
*
368+
* @param array $response
369+
*/
370+
private function assertResponse(array $response): void
371+
{
372+
self::assertEquals('Name', $response['sendEmailToFriend']['sender']['name']);
373+
self::assertEquals('[email protected]', $response['sendEmailToFriend']['sender']['email']);
374+
self::assertEquals('Lorem Ipsum', $response['sendEmailToFriend']['sender']['message']);
375+
self::assertEquals('Recipient Name 1', $response['sendEmailToFriend']['recipients'][0]['name']);
376+
self::assertEquals('[email protected]', $response['sendEmailToFriend']['recipients'][0]['email']);
377+
self::assertEquals('Recipient Name 2', $response['sendEmailToFriend']['recipients'][1]['name']);
378+
self::assertEquals('[email protected]', $response['sendEmailToFriend']['recipients'][1]['email']);
379+
}
380+
381+
/**
382+
* Retrieve customer authorization headers
383+
*
384+
* @param string $username
385+
* @param string $password
386+
* @return array
387+
* @throws AuthenticationException
388+
*/
389+
private function getHeaderMap(string $username = '[email protected]', string $password = 'password'): array
390+
{
391+
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
392+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
393+
return $headerMap;
394+
}
395+
314396
/**
315397
* @param int $productId
316398
* @param string $recipients
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
// TODO: Should be removed in scope of https://github.com/magento/graphql-ce/issues/167
7+
declare(strict_types=1);
8+
9+
use Magento\Framework\App\Config\Storage\Writer;
10+
use Magento\Framework\App\Config\Storage\WriterInterface;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
14+
$objectManager = Bootstrap::getObjectManager();
15+
/** @var Writer $configWriter */
16+
$configWriter = $objectManager->get(WriterInterface::class);
17+
18+
$configWriter->save('sendfriend/email/allow_guest', '0');
19+
20+
$scopeConfig = $objectManager->get(ScopeConfigInterface::class);
21+
$scopeConfig->clean();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
// TODO: Should be removed in scope of https://github.com/magento/graphql-ce/issues/167
7+
declare(strict_types=1);
8+
9+
use Magento\Framework\App\Config\Storage\Writer;
10+
use Magento\Framework\App\Config\Storage\WriterInterface;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
14+
$objectManager = Bootstrap::getObjectManager();
15+
/** @var Writer $configWriter */
16+
$configWriter = $objectManager->get(WriterInterface::class);
17+
18+
$configWriter->save('sendfriend/email/allow_guest', '1');
19+
20+
$scopeConfig = $objectManager->get(ScopeConfigInterface::class);
21+
$scopeConfig->clean();

0 commit comments

Comments
 (0)