Skip to content

33378 GraphQL: create an entry in 'customer_log' after generate customer token #33409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/code/Magento/CustomerGraphQl/etc/graphql/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="customer_login">
<observer name="customer_log_login" instance="Magento\Customer\Observer\LogLastLoginAtObserver" />
</event>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,40 @@

namespace Magento\GraphQl\Customer;

use Magento\Customer\Model\Log;
use Magento\Customer\Model\Logger;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* API-functional tests cases for generateCustomerToken mutation
*/
class GenerateCustomerTokenTest extends GraphQlAbstract
{
/**
* @var Logger
*/
private $logger;

/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->logger = Bootstrap::getObjectManager()->get(Logger::class);
}

/**
* Verify customer token with valid credentials
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testGenerateCustomerValidToken()
public function testGenerateCustomerValidToken(): void
{
$email = '[email protected]';
$password = 'password';

$mutation = $this->getQuery($email, $password);
$mutation = $this->getQuery();

$response = $this->graphQlMutation($mutation);
$this->assertArrayHasKey('generateCustomerToken', $response);
Expand All @@ -41,7 +57,7 @@ public function testGenerateCustomerValidToken()
* @param string $password
* @param string $message
*/
public function testGenerateCustomerTokenInvalidData(string $email, string $password, string $message)
public function testGenerateCustomerTokenInvalidData(string $email, string $password, string $message): void
{
$this->expectException(\Exception::class);

Expand All @@ -55,12 +71,9 @@ public function testGenerateCustomerTokenInvalidData(string $email, string $pass
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testRegenerateCustomerToken()
public function testRegenerateCustomerToken(): void
{
$email = '[email protected]';
$password = 'password';

$mutation = $this->getQuery($email, $password);
$mutation = $this->getQuery();

$response1 = $this->graphQlMutation($mutation);
$token1 = $response1['generateCustomerToken']['token'];
Expand Down Expand Up @@ -110,7 +123,7 @@ public function dataProviderInvalidCustomerInfo(): array
* @param string $password
* @return string
*/
private function getQuery(string $email, string $password) : string
private function getQuery(string $email = '[email protected]', string $password = 'password'): string
{
return <<<MUTATION
mutation {
Expand All @@ -127,22 +140,12 @@ private function getQuery(string $email, string $password) : string
/**
* Verify customer with empty email
*/
public function testGenerateCustomerTokenWithEmptyEmail()
public function testGenerateCustomerTokenWithEmptyEmail(): void
{
$email = '';
$password = 'bad-password';

$mutation
= <<<MUTATION
mutation {
generateCustomerToken(
email: "{$email}"
password: "{$password}"
) {
token
}
}
MUTATION;
$mutation = $this->getQuery($email, $password);

$this->expectException(\Exception::class);
$this->expectExceptionMessage('GraphQL response contains errors: Specify the "email" value.');
Expand All @@ -152,25 +155,51 @@ public function testGenerateCustomerTokenWithEmptyEmail()
/**
* Verify customer with empty password
*/
public function testGenerateCustomerTokenWithEmptyPassword()
public function testGenerateCustomerTokenWithEmptyPassword(): void
{
$email = '[email protected]';
$password = '';

$mutation
= <<<MUTATION
mutation {
generateCustomerToken(
email: "{$email}"
password: "{$password}"
) {
token
}
}
MUTATION;
$mutation = $this->getQuery($email, $password);

$this->expectException(\Exception::class);
$this->expectExceptionMessage('GraphQL response contains errors: Specify the "password" value.');
$this->graphQlMutation($mutation);
}

/**
* Verify customer log after generate customer token
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testCustomerLogAfterGenerateCustomerToken(): void
{
$response = $this->graphQlMutation($this->getQuery());
$this->assertArrayHasKey('generateCustomerToken', $response);
$this->assertIsArray($response['generateCustomerToken']);

/** @var Log $log */
$log = $this->logger->get(1);
$this->assertNotEmpty($log->getLastLoginAt());
}

/**
* Ensure that customer log record is deleted.
*
* @return void
*/
protected function tearDown(): void
{
if ($this->logger->get(1)->getLastLoginAt()) {
/** @var ResourceConnection $resource */
$resource = Bootstrap::getObjectManager()->get(ResourceConnection::class);
/** @var AdapterInterface $connection */
$connection = $resource->getConnection(ResourceConnection::DEFAULT_CONNECTION);
$connection->delete(
$resource->getTableName('customer_log'),
['customer_id' => 1]
);
}
parent::tearDown();
}
}