Integration tests: added 'createUser' method, portal support

This commit is contained in:
Taras Machyshyn
2016-11-03 18:00:03 +02:00
parent 988388ed16
commit 43a49e3c31
5 changed files with 242 additions and 29 deletions
+21 -5
View File
@@ -37,7 +37,11 @@ class ApiClient
private $password;
protected $urlPath = '/api/v1/';
private $portalId;
protected $apiPath = '/api/v1/';
protected $portalApiPath = '/api/v1/portal-access/{PORTAL_ID}/';
protected $userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36';
@@ -55,7 +59,7 @@ class ApiClient
*/
private $lastResponse;
public function __construct($url = null, $userName = null, $password = null)
public function __construct($url = null, $userName = null, $password = null, $portalId = null)
{
if (isset($url)) {
$this->url = $url;
@@ -68,6 +72,10 @@ class ApiClient
if (isset($password)) {
$this->password = $password;
}
if (isset($portalId)) {
$this->portalId = $portalId;
}
}
public function setUrl($url)
@@ -85,6 +93,11 @@ class ApiClient
$this->password = $password;
}
public function setPortalId($portalId)
{
$this->portalId = $portalId;
}
/**
* Send request to EspoCRM
*
@@ -142,10 +155,11 @@ class ApiClient
}
$header = $this->normalizeHeader($parsedResponse['header']);
$errorMessage = !empty($header['X-Status-Reason']) ? $header['X-Status-Reason'] : 'Unknown Error';
$errorCode = $this->getResponseHttpCode();
$errorMessage = !empty($header['X-Status-Reason']) ? $header['X-Status-Reason'] : $errorCode;
curl_close($ch);
throw new \Exception($errorMessage, $this->getResponseHttpCode());
throw new \Exception($errorMessage, $errorCode);
}
public function getResponseContentType()
@@ -165,7 +179,9 @@ class ApiClient
protected function normalizeUrl($action)
{
return $this->url . $this->urlPath . $action;
$apiPath = $this->portalId ? str_replace('{PORTAL_ID}', $this->portalId, $this->portalApiPath) : $this->apiPath;
return $this->url . $apiPath . $action;
}
protected function checkParams()
+10 -23
View File
@@ -63,18 +63,21 @@ abstract class BaseTestCase extends \PHPUnit_Framework_TestCase
*/
protected $password = null;
protected $portalId = null;
protected function createApplication($clearCache = true)
{
return $this->espoTester->getApplication(true, $clearCache);
}
protected function auth($userName, $password = null)
protected function auth($userName, $password = null, $portalId = null)
{
$this->userName = $userName;
$this->password = $password;
$this->portalId = $portalId;
if (isset($this->espoTester)) {
$this->espoTester->auth($userName, $password);
$this->espoTester->auth($userName, $password, $portalId);
}
}
@@ -127,6 +130,11 @@ abstract class BaseTestCase extends \PHPUnit_Framework_TestCase
$this->espoApplication = NULL;
}
protected function createUser($userData, array $role = null, $isPortal = false)
{
return $this->espoTester->createUser($userData, $role, $isPortal);
}
protected function beforeStartApplication()
{
@@ -136,25 +144,4 @@ abstract class BaseTestCase extends \PHPUnit_Framework_TestCase
{
}
/**
* Create a user with roles
*
* @param string|array $userData - If $userData is a string, then it's a userName with default password
* @param array $roles
*
* @return \Espo\Entities\User
*/
/*protected function createUser($userData, array $roles)
{
if (!is_array($userData)) {
$userData = array(
'userName' => $userData
);
}
}*/
}
+84 -1
View File
@@ -61,6 +61,8 @@ class Tester
*/
protected $password = null;
protected $portalId = null;
protected $defaultUserPassword = '1';
public function __construct(array $params)
@@ -103,10 +105,11 @@ class Tester
return $returns;
}
public function auth($userName, $password = null)
public function auth($userName, $password = null, $portalId = null)
{
$this->userName = $userName;
$this->password = $password;
$this->portalId = $portalId;
}
public function getApplication($reload = false, $clearCache = true)
@@ -218,7 +221,87 @@ class Tester
$apiClient = $this->getApiClient();
$apiClient->setUserName($this->userName);
$apiClient->setPassword(isset($this->password) ? $this->password : $this->defaultUserPassword);
$apiClient->setPortalId($this->portalId);
return $apiClient->request($method, $action, $data);
}
/**
* Create a user with roles
*
* @param string|array $userData - If $userData is a string, then it's a userName with default password
* @param array $role
*
* @return \Espo\Entities\User
*/
public function createUser($userData, array $roleData = null, $isPortal = false)
{
if (!is_array($userData)) {
$userData = array(
'userName' => $userData,
'lastName' => $userData,
);
}
//create a role
if (!empty($roleData)) {
if (!isset($roleData['name'])) {
$roleData['name'] = $userData['userName'] . 'Role';
}
$role = $this->createRole($roleData, $isPortal);
if (isset($role)) {
$fieldName = $isPortal ? 'portalRolesIds' : 'rolesIds';
if (!isset($userData[$fieldName])) {
$userData[$fieldName] = array();
}
$userData[$fieldName][] = $role->id;
}
}
$application = $this->getApplication();
$entityManager = $application->getContainer()->get('entityManager');
$config = $application->getContainer()->get('config');
if (!isset($userData['password'])) {
$userData['password'] = $this->defaultUserPassword;
}
$passwordHash = new \Espo\Core\Utils\PasswordHash($config);
$userData['password'] = $passwordHash->hash($userData['password']);
if ($isPortal) {
$userData['isPortalUser'] = true;
}
$user = $entityManager->getEntity('User');
$user->set($userData);
$entityManager->saveEntity($user);
return $user;
}
protected function createRole(array $roleData, $isPortal = false)
{
$entityName = $isPortal ? 'PortalRole' : 'Role';
if (is_array($roleData['data'])) {
$roleData['data'] = json_encode($roleData['data']);
}
if (is_array($roleData['fieldData'])) {
$roleData['fieldData'] = json_encode($roleData['fieldData']);
}
$application = $this->getApplication();
$entityManager = $application->getContainer()->get('entityManager');
$role = $entityManager->getEntity($entityName);
$role->set($roleData);
$entityManager->saveEntity($role);
return $role;
}
}
+119
View File
@@ -50,4 +50,123 @@ class LoginTest extends \tests\integration\Core\BaseTestCase
$this->assertNull($application->getContainer()->get('user'));
}
public function testCreateUser()
{
$newUser = $this->createUser('tester');
$this->assertInstanceOf('\\Espo\\Orm\\Entity', $newUser);
$this->assertTrue(!empty($newUser->id));
$this->assertEquals('tester', $newUser->get('userName'));
}
public function testCreateUserWithAttributes()
{
$newUser = $this->createUser(array(
'userName' => 'tester',
'firstName' => 'Test',
'lastName' => 'Tester',
'emailAddress' => 'test@tester.com',
));
$this->assertInstanceOf('\\Espo\\Orm\\Entity', $newUser);
$this->assertTrue(!empty($newUser->id));
$this->assertEquals('tester', $newUser->get('userName'));
$this->assertEquals('Test', $newUser->get('firstName'));
$this->assertEquals('Tester', $newUser->get('lastName'));
$this->assertEquals('test@tester.com', $newUser->get('emailAddress'));
}
public function testCreateUserWithRole()
{
$newUser = $this->createUser('tester', array(
'assignmentPermission' => 'team',
'userPermission' => 'team',
'portalPermission' => 'not-set',
'data' =>
array (
'Account' => false,
'Call' =>
array (
'create' => 'yes',
'read' => 'team',
'edit' => 'team',
'delete' => 'no',
),
),
'fieldData' =>
array (
'Call' =>
array (
'direction' =>
array (
'read' => 'yes',
'edit' => 'no',
),
),
),
));
$this->assertInstanceOf('\\Espo\\Orm\\Entity', $newUser);
$this->assertTrue(!empty($newUser->id));
$this->assertEquals('tester', $newUser->get('userName'));
}
/**
* @expectedException \Exception
* @expectedExceptionCode 403
*/
public function testAccessUser()
{
$this->testCreateUserWithRole();
$this->auth('tester');
$this->sendRequest('POST', 'Account', array(
'name' => 'Test Account',
));
}
public function testCreatePortalUserWithRole()
{
$newUser = $this->createUser(array(
'userName' => 'tester',
'lastName' => 'tester',
'portalsIds' => array(
'testPortalId',
),
), array(
'assignmentPermission' => 'team',
'userPermission' => 'team',
'portalPermission' => 'not-set',
'data' => array (
'Account' => false,
),
'fieldData' => array (
'Call' => array (
'direction' => array (
'read' => 'yes',
'edit' => 'no',
),
),
),
), true);
$this->assertInstanceOf('\\Espo\\Orm\\Entity', $newUser);
$this->assertTrue(!empty($newUser->id));
$this->assertEquals('tester', $newUser->get('userName'));
}
/**
* @expectedException \Exception
* @expectedExceptionCode 403
*/
public function testAccessPortalUser()
{
$this->testCreatePortalUserWithRole();
$this->auth('tester', null, 'testPortalId');
$this->sendRequest('POST', 'Account', array(
'name' => 'Test Account',
));
}
}
@@ -49,5 +49,13 @@ return array(
),
),
],
'Portal' => [
array(
'id' => 'testPortalId',
'isActive' => true,
'name' => 'Test portal',
'customId' => 'test',
),
],
),
);