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