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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\UsersManager\tests\System;
use Piwik\Date;
use Piwik\API\Request;
use Piwik\Piwik;
use Piwik\Plugins\UsersManager\API;
use Piwik\Plugins\UsersManager\Model;
use Piwik\Plugins\UsersManager\tests\Fixtures\ManyUsers;
use Piwik\Tests\Framework\TestCase\SystemTestCase;
/**
* @group UsersManager
* @group ApiTest
* @group Plugins
*/
class ApiTest extends SystemTestCase
{
/**
* @var ManyUsers
*/
public static $fixture = null; // initialized below class definition
/**
* @var API
*/
private $api;
/**
* @var Model
*/
private $model;
public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$this->api = API::getInstance();
$this->model = new Model();
}
/**
* @dataProvider getApiForTesting
*/
public function testApi($api, $params = [])
{
$apiId = implode('_', $params);
$logins = [
'login1' => 'when_superuseraccess',
'login2' => 'when_adminaccess',
'login4' => 'when_viewaccess',
];
// login1 = super user, login2 = some admin access, login4 = only view access
foreach ($logins as $login => $appendix) {
$params['token_auth'] = self::$fixture->users[$login]['token'];
$xmlFieldsToRemove = [
'date_registered',
'invite_token',
'invite_accept_at',
'invite_expired_at',
'last_seen',
'last_seen_ago',
'password',
'token_auth',
'ts_password_modified',
'idchange_last_viewed',
'invite_status',
'ts_changes_shown',
'ts_last_seen',
];
$this->runAnyApiTest($api, $apiId . '_' . $appendix, $params, array('xmlFieldsToRemove' => $xmlFieldsToRemove));
}
}
public function testGetUserPreferenceLoginIsOptional()
{
$response = Request::processRequest('UsersManager.getUserPreference', array(
'preferenceName' => API::PREFERENCE_DEFAULT_REPORT,
));
$this->assertEquals('1', $response);
$response = Request::processRequest('UsersManager.getUserPreference', array(
'preferenceName' => API::PREFERENCE_DEFAULT_REPORT_DATE,
));
$this->assertEquals('yesterday', $response);
}
public function testGetUserPreferenceLoginCanBeSet()
{
$response = Request::processRequest('UsersManager.getUserPreference', array(
'userLogin' => Piwik::getCurrentUserLogin(),
'preferenceName' => API::PREFERENCE_DEFAULT_REPORT_DATE,
));
$this->assertEquals('yesterday', $response);
// user not exists
$response = Request::processRequest('UsersManager.getUserPreference', array(
'userLogin' => 'foo',
'preferenceName' => API::PREFERENCE_DEFAULT_REPORT_DATE,
));
$this->assertEquals('yesterday', $response);
}
public function getApiForTesting()
{
$apiToTest = array(
array('UsersManager.getUsers'),
array('UsersManager.getUsersLogin'),
array('UsersManager.getUsersAccessFromSite', array('idSite' => 6)), // admin user has admin access for this
array('UsersManager.getUsersAccessFromSite', array('idSite' => 3)), // admin user has only view access for this, should not see anything
array('UsersManager.getUsersSitesFromAccess', array('access' => 'admin')),
array('UsersManager.getUsersWithSiteAccess', array('idSite' => 3, 'access' => 'admin')),
array('UsersManager.getUser', array('userLogin' => 'login1')),
array('UsersManager.getUser', array('userLogin' => 'login2')),
array('UsersManager.getUser', array('userLogin' => 'login4')),
array('UsersManager.getUser', array('userLogin' => 'login6')),
);
return $apiToTest;
}
public function testCreateAppSpecificTokenAuthWithCrypticPassword()
{
$password = 'p§$%"@&<~#\'\\/+ >*^!°p';
API::$UPDATE_USER_REQUIRE_PASSWORD_CONFIRMATION = false;
$this->api->updateUser('login6', $password);
API::$UPDATE_USER_REQUIRE_PASSWORD_CONFIRMATION = true;
$this->model->deleteAllTokensForUser('login6');
$token = $this->api->createAppSpecificTokenAuth('login6', $password, 'test');
$this->assertMd5($token);
$user = $this->model->getUserByTokenAuth($token);
$this->assertSame('login6', $user['login']);
}
public function testCreateAppSpecificTokenAuth()
{
$this->model->deleteAllTokensForUser('login1');
$token = $this->api->createAppSpecificTokenAuth('login1', 'password', 'test');
$this->assertMd5($token);
$user = $this->model->getUserByTokenAuth($token);
$this->assertSame('login1', $user['login']);
}
public function testCreateAppSpecificTokenAuthCanLoginByEmail()
{
$this->model->deleteAllTokensForUser('login1');
$token = $this->api->createAppSpecificTokenAuth('login1@example.com', 'password', 'test');
$this->assertMd5($token);
$user = $this->model->getUserByTokenAuth($token);
$this->assertSame('login1', $user['login']);
}
public function testCreateAppSpecificTokenAuthFailsWhenPasswordNotValid()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The current password you entered is not correct.');
$this->model->deleteAllTokensForUser('login1');
$this->api->createAppSpecificTokenAuth('login1', 'foooooo', 'test');
}
public function testCreateAppSpecificTokenAuthWithExpireDate()
{
$this->model->deleteAllTokensForUser('login1');
$expiryDate = (new \DateTime())->modify('+1 day');
$token = $this->api->createAppSpecificTokenAuth('login1', 'password', 'test', $expiryDate->format('Y-m-d H:i:s'));
$this->assertMd5($token);
$tokens = $this->model->getAllNonSystemTokensForLogin('login1');
$this->assertEquals($this->model->hashTokenAuth($token), $tokens[0]['password']);
$this->assertEquals('login1', $tokens[0]['login']);
$this->assertEquals('test', $tokens[0]['description']);
$this->assertEquals($expiryDate->format('Y-m-d H:i:s'), $tokens[0]['date_expired']);
}
public function testCreateAppSpecificTokenAuthWithExpireHours()
{
$expireInHours = 48;
$this->model->deleteAllTokensForUser('login1');
$token = $this->api->createAppSpecificTokenAuth('login1', 'password', 'test', null, $expireInHours);
$this->assertMd5($token);
$tokens = $this->model->getAllNonSystemTokensForLogin('login1');
$this->assertEquals($this->model->hashTokenAuth($token), $tokens[0]['password']);
$this->assertEquals('login1', $tokens[0]['login']);
$this->assertNotEmpty($tokens[0]['date_expired']);
$dateExpired = Date::factory($tokens[0]['date_expired']);
$dateExpired->isLater(Date::now()->addHour($expireInHours - 1));
$dateExpired->isEarlier(Date::now()->addHour($expireInHours + 1));
}
private function assertMd5($string)
{
$this->assertSame(32, strlen($string));
$this->assertTrue(ctype_xdigit($string));
}
public static function getOutputPrefix()
{
return '';
}
public static function getPathToTestDirectory()
{
return dirname(__FILE__);
}
}
ApiTest::$fixture = new ManyUsers(1, 1, false);
|