PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/PHPUnit/Integration/Plugins/LoginTest.php

https://github.com/CodeYellowBV/piwik
PHP | 398 lines | 214 code | 64 blank | 120 comment | 0 complexity | 5485f0e78153b1c3a90f76e7a43445be MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. */
  8. use Piwik\Access;
  9. use Piwik\AuthResult;
  10. use Piwik\DbHelper;
  11. use Piwik\Plugins\Login\Auth;
  12. use Piwik\Plugins\UsersManager\API;
  13. require_once 'Login/Auth.php';
  14. /**
  15. * Class Plugins_LoginTest
  16. *
  17. * @group Plugins
  18. */
  19. class Plugins_LoginTest extends DatabaseTestCase
  20. {
  21. /**
  22. * @var Auth
  23. */
  24. private $auth;
  25. public function setUp()
  26. {
  27. parent::setUp();
  28. // setup the access layer
  29. $pseudoMockAccess = new FakeAccess;
  30. FakeAccess::setIdSitesView(array(1, 2));
  31. FakeAccess::setIdSitesAdmin(array(3, 4));
  32. //finally we set the user as a Super User by default
  33. FakeAccess::$superUser = true;
  34. Access::setSingletonInstance($pseudoMockAccess);
  35. $this->auth = new Auth();
  36. }
  37. /**
  38. * @group Plugins
  39. */
  40. public function testAuthenticateFailureNoLoginNoTokenAuth()
  41. {
  42. // no login; no token auth
  43. $rc = $this->auth->authenticate();
  44. $this->assertFailedLogin($rc);
  45. }
  46. /**
  47. * @group Plugins
  48. */
  49. public function testAuthenticateFailureEmptyLoginNoTokenAuth()
  50. {
  51. // empty login; no token auth
  52. $this->auth->setLogin('');
  53. $rc = $this->auth->authenticate();
  54. $this->assertFailedLogin($rc);
  55. }
  56. /**
  57. * @group Plugins
  58. */
  59. public function testAuthenticateFailureNonExistentUser()
  60. {
  61. // non-existent user
  62. $this->auth->setLogin('nobody');
  63. $rc = $this->auth->authenticate();
  64. $this->assertFailedLogin($rc);
  65. }
  66. /**
  67. * @group Plugins
  68. */
  69. public function testAuthenticateFailureAnonymousNotExisting()
  70. {
  71. // anonymous user doesn't exist yet
  72. $rc = $this->authenticate($login = 'anonymous', $authToken = '');
  73. $this->assertFailedLogin($rc);
  74. }
  75. /**
  76. * @group Plugins
  77. */
  78. public function testAuthenticateFailureAnonymousNotExistentEmptyLogin()
  79. {
  80. // empty login; anonymous user doesn't exist yet
  81. $rc = $this->authenticate($login = '', $authToken = 'anonymous');
  82. $this->assertFailedLogin($rc);
  83. }
  84. /**
  85. * @group Plugins
  86. */
  87. public function testAuthenticateFailureAnonymousNotExistentEmptyLoginWithTokenAuth()
  88. {
  89. // API authentication; anonymous user doesn't exist yet
  90. $rc = $this->authenticate($login = null, $authToken = 'anonymous');
  91. $this->assertFailedLogin($rc);
  92. }
  93. /**
  94. * @group Plugins
  95. */
  96. public function testAuthenticateFailureAnonymousNotExistentWithLoginAndTokenAuth()
  97. {
  98. // anonymous user doesn't exist yet
  99. $rc = $this->authenticate($login = 'anonymous', $authToken = 'anonymous');
  100. $this->assertFailedLogin($rc);
  101. }
  102. /**
  103. * @group Plugins
  104. */
  105. public function testAuthenticateFailureAnonymousWithLogin()
  106. {
  107. DbHelper::createAnonymousUser();
  108. // missing token_auth
  109. $rc = $this->authenticate($login = 'anonymous', $authToken = '');
  110. $this->assertFailedLogin($rc);
  111. }
  112. /**
  113. * @group Plugins
  114. */
  115. public function testAuthenticateFailureAnonymousEmptyLoginWithTokenAuth()
  116. {
  117. DbHelper::createAnonymousUser();
  118. // empty login
  119. $rc = $this->authenticate($login = '', $authToken = 'anonymous');
  120. $this->assertFailedLogin($rc);
  121. }
  122. /**
  123. * @group Plugins
  124. */
  125. public function testAuthenticateFailureAnonymousLoginTokenAuthMissmatch()
  126. {
  127. DbHelper::createAnonymousUser();
  128. // not equal
  129. $rc = $this->authenticate($login = 'anonymous', $authToken = 0);
  130. $this->assertFailedLogin($rc);
  131. }
  132. /**
  133. * @group Plugins
  134. */
  135. public function testAuthenticateSuccessAnonymousWithTokenAuth()
  136. {
  137. DbHelper::createAnonymousUser();
  138. // API authentication
  139. $rc = $this->authenticate($login = null, $authToken = 'anonymous');
  140. $this->assertUserLogin($rc, $login = 'anonymous', $tokenLength = 9);
  141. }
  142. /**
  143. * @group Plugins
  144. */
  145. public function testAuthenticateSuccessAnonymous()
  146. {
  147. DbHelper::createAnonymousUser();
  148. // valid login & token auth
  149. $rc = $this->authenticate($login = 'anonymous', $authToken = 'anonymous');
  150. $this->assertUserLogin($rc, $login = 'anonymous', $tokenLength = 9);
  151. }
  152. /**
  153. * @group Plugins
  154. */
  155. public function testAuthenticateFailureUserEmptyTokenAuth()
  156. {
  157. $user = $this->_setUpUser();
  158. // empty token auth
  159. $rc = $this->authenticate($login = $user['login'], $authToken = '');
  160. $this->assertFailedLogin($rc);
  161. }
  162. /**
  163. * @group Plugins
  164. */
  165. public function testAuthenticateFailureUserInvalidTokenAuth()
  166. {
  167. $user = $this->_setUpUser();
  168. // not a token auth
  169. $rc = $this->authenticate($login = $user['login'], $authToken = $user['password']);
  170. $this->assertFailedLogin($rc);
  171. }
  172. /**
  173. * @group Plugins
  174. */
  175. public function testAuthenticateFailureUserInvalidTokenAuth2()
  176. {
  177. $user = $this->_setUpUser();
  178. // not a token auth
  179. $rc = $this->authenticate($login = $user['login'], $authToken = md5($user['password']));
  180. $this->assertFailedLogin($rc);
  181. }
  182. /**
  183. * @group Plugins
  184. */
  185. public function testAuthenticateFailureUserEmptyLogin()
  186. {
  187. $user = $this->_setUpUser();
  188. // empty login
  189. $rc = $this->authenticate($login = '', $user['tokenAuth']);
  190. $this->assertFailedLogin($rc);
  191. }
  192. /**
  193. * @group Plugins
  194. */
  195. public function testAuthenticateFailureUserWithSuperUserAccessEmptyLogin()
  196. {
  197. $user = $this->_setUpUser();
  198. $this->_setUpSuperUserAccessViaDb();
  199. // empty login
  200. $rc = $this->authenticate($login = '', $user['tokenAuth']);
  201. $this->assertFailedLogin($rc);
  202. }
  203. /**
  204. * @group Plugins
  205. */
  206. public function testAuthenticateFailureUserLoginTokenAuthMissmatch()
  207. {
  208. $this->_setUpUser();
  209. // not equal
  210. $rc = $this->authenticate($login = 0, $authToken = 0);
  211. $this->assertFailedLogin($rc);
  212. }
  213. /**
  214. * @group Plugins
  215. */
  216. public function testAuthenticateFailureUserLoginTokenAuthMissmatch2()
  217. {
  218. $user = $this->_setUpUser();
  219. // not equal
  220. $rc = $this->authenticate($login = 0, $user['tokenAuth']);
  221. $this->assertFailedLogin($rc);
  222. }
  223. /**
  224. * @group Plugins
  225. */
  226. public function testAuthenticateFailureUserLoginTokenAuthMissmatch3()
  227. {
  228. $user = $this->_setUpUser();
  229. // not equal
  230. $rc = $this->authenticate($user['login'], $authToken = 0);
  231. $this->assertFailedLogin($rc);
  232. }
  233. /**
  234. * @group Plugins
  235. */
  236. public function testAuthenticateFailureUserWithSuperUserAccessLoginTokenAuthMissmatch()
  237. {
  238. $user = $this->_setUpUser();
  239. $this->_setUpSuperUserAccessViaDb();
  240. // not equal
  241. $rc = $this->authenticate($login = null, $authToken = $user['password']);
  242. $this->assertFailedLogin($rc);
  243. }
  244. /**
  245. * @group Plugins
  246. */
  247. public function testAuthenticateSuccessUserTokenAuth()
  248. {
  249. $user = $this->_setUpUser();
  250. // API authentication
  251. $rc = $this->authenticate($login = null, $user['tokenAuth']);
  252. $this->assertUserLogin($rc);
  253. }
  254. /**
  255. * @group Plugins
  256. */
  257. public function testAuthenticateSuccessUserWithSuperUserAccessByTokenAuth()
  258. {
  259. $user = $this->_setUpUser();
  260. $this->_setUpSuperUserAccessViaDb();
  261. // API authentication
  262. $rc = $this->authenticate($login = null, $user['tokenAuth']);
  263. $this->assertSuperUserLogin($rc, 'user');
  264. }
  265. /**
  266. * @group Plugins
  267. */
  268. public function testAuthenticateSuccessUserLoginAndTokenAuth()
  269. {
  270. $user = $this->_setUpUser();
  271. // valid login & token auth
  272. $rc = $this->authenticate($user['login'], $user['tokenAuth']);
  273. $this->assertUserLogin($rc);
  274. }
  275. /**
  276. * @group Plugins
  277. */
  278. public function testAuthenticateSuccessUserWithSuperUserAccessLoginAndTokenAuth()
  279. {
  280. $user = $this->_setUpUser();
  281. $this->_setUpSuperUserAccessViaDb();
  282. // valid login & token auth
  283. $rc = $this->authenticate($user['login'], $user['tokenAuth']);
  284. $this->assertSuperUserLogin($rc, 'user');
  285. }
  286. /**
  287. * @group Plugins
  288. */
  289. public function testAuthenticateSuccessLoginAndHashedTokenAuth()
  290. {
  291. $user = $this->_setUpUser();
  292. $hash = $this->auth->getHashTokenAuth($user['login'], $user['tokenAuth']);
  293. // valid login & hashed token auth
  294. $rc = $this->authenticate($user['login'], $tokenAuth = $hash);
  295. $this->assertUserLogin($rc);
  296. }
  297. protected function _setUpUser()
  298. {
  299. $user = array('login' => 'user',
  300. 'password' => 'geqgeagae',
  301. 'email' => 'test@test.com',
  302. 'alias' => 'alias',
  303. 'superuser_access' => 0);
  304. API::getInstance()->addUser($user['login'], $user['password'], $user['email'], $user['alias']);
  305. $user['tokenAuth'] = API::getInstance()->getTokenAuth($user['login'], md5($user['password']));
  306. return $user;
  307. }
  308. private function _setUpSuperUserAccessViaDb()
  309. {
  310. API::getInstance()->setSuperUserAccess('user', true);
  311. }
  312. private function authenticate($login, $tokenAuth)
  313. {
  314. $this->auth->setLogin($login);
  315. $this->auth->setTokenAuth($tokenAuth);
  316. return $this->auth->authenticate();
  317. }
  318. private function assertFailedLogin(AuthResult $authResult)
  319. {
  320. $this->assertEquals(AuthResult::FAILURE, $authResult->getCode());
  321. }
  322. private function assertSuperUserLogin(AuthResult $authResult, $login = 'superUserLogin', $tokenLength = 32)
  323. {
  324. $this->assertEquals(AuthResult::SUCCESS_SUPERUSER_AUTH_CODE, $authResult->getCode());
  325. $this->assertEquals($login, $authResult->getIdentity());
  326. $this->assertEquals($tokenLength, strlen($authResult->getTokenAuth()));
  327. }
  328. private function assertUserLogin(AuthResult $authResult, $login = 'user', $tokenLength = 32)
  329. {
  330. $this->assertEquals(AuthResult::SUCCESS, $authResult->getCode());
  331. $this->assertEquals($login, $authResult->getIdentity());
  332. $this->assertEquals($tokenLength, strlen($authResult->getTokenAuth()));
  333. }
  334. }