Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrobocoder <anthon.pang@gmail.com>2010-08-27 04:32:31 +0400
committerrobocoder <anthon.pang@gmail.com>2010-08-27 04:32:31 +0400
commitebe648c5b4230b77808a9e1696b462c29ab235c6 (patch)
tree11b81fb94b3d02ff9d1c6d4571c7ab5a7b6a2768 /plugins/Login/tests
parentcd1ea3f5c87d5a8b619ad7672e7b28b5565e6b31 (diff)
fixes #1597 - add unit test
git-svn-id: http://dev.piwik.org/svn/trunk@2998 59fd770c-687e-43c8-a1e3-f5a4ff64c105
Diffstat (limited to 'plugins/Login/tests')
-rw-r--r--plugins/Login/tests/Login.test.php189
1 files changed, 189 insertions, 0 deletions
diff --git a/plugins/Login/tests/Login.test.php b/plugins/Login/tests/Login.test.php
new file mode 100644
index 0000000000..24b5a4277b
--- /dev/null
+++ b/plugins/Login/tests/Login.test.php
@@ -0,0 +1,189 @@
+<?php
+if(!defined("PIWIK_PATH_TEST_TO_ROOT")) {
+ define('PIWIK_PATH_TEST_TO_ROOT', getcwd().'/../../..');
+}
+if(!defined('PIWIK_CONFIG_TEST_INCLUDED'))
+{
+ require_once PIWIK_PATH_TEST_TO_ROOT . "/tests/config_test.php";
+}
+
+if(!class_exists('Piwik_Login_Auth', false))
+{
+ require_once 'Login/Auth.php';
+}
+require_once 'Database.test.php';
+
+class Test_Piwik_Login extends Test_Database
+{
+ function setUp()
+ {
+ parent::setUp();
+
+ // setup the access layer
+ $pseudoMockAccess = new FakeAccess;
+ FakeAccess::setIdSitesView( array(1,2));
+ FakeAccess::setIdSitesAdmin( array(3,4));
+
+ //finally we set the user as a super user by default
+ FakeAccess::$superUser = true;
+ Zend_Registry::set('access', $pseudoMockAccess);
+
+ // we make sure the tests don't depend on the config file content
+ Zend_Registry::get('config')->superuser = array(
+ 'login'=>'superusertest',
+ 'password'=>md5('passwordsuperusertest'),
+ 'email'=>'superuser@example.com'
+ );
+ }
+
+ public function test_authenticate()
+ {
+ // no login; no token auth
+ $auth = new Piwik_Login_Auth();
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::FAILURE );
+
+ // empty login; no token auth
+ $auth->setLogin('');
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::FAILURE );
+
+ // non-existent user
+ $auth->setLogin('nobody');
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::FAILURE );
+
+ // anonymous user doesn't exist yet
+ $auth->setLogin('anonymous');
+ $auth->setTokenAuth('');
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::FAILURE );
+
+ // empty login; anonymous user doesn't exist yet
+ $auth->setLogin('');
+ $auth->setTokenAuth('anonymous');
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::FAILURE );
+
+ // API authentication; anonymous user doesn't exist yet
+ $auth->setLogin(null);
+ $auth->setTokenAuth('anonymous');
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::FAILURE );
+
+ // anonymous user doesn't exist yet
+ $auth->setLogin('anonymous');
+ $auth->setTokenAuth('anonymous');
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::FAILURE );
+
+ Piwik::createAnonymousUser();
+
+ // missing token_auth
+ $auth->setLogin('anonymous');
+ $auth->setTokenAuth('');
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::FAILURE );
+
+ // empty login
+ $auth->setLogin('');
+ $auth->setTokenAuth('anonymous');
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::FAILURE );
+
+ // API authentication
+ $auth->setLogin(null);
+ $auth->setTokenAuth('anonymous');
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::SUCCESS );
+
+ // valid login & token auth
+ $auth->setLogin('anonymous');
+ $auth->setTokenAuth('anonymous');
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::SUCCESS );
+
+ $user = array( 'login'=>'user',
+ 'password'=>"geqgeagae",
+ 'email'=>"test@test.com",
+ 'alias'=>"alias");
+ Piwik_UsersManager_API::getInstance()->addUser($user['login'],$user['password'] ,$user['email'] ,$user['alias'] );
+ $password = md5($user['password']);
+ $tokenAuth = Piwik_UsersManager_API::getInstance()->getTokenAuth($user['login'], $password);
+
+ // empty token auth
+ $auth->setLogin($user['login']);
+ $auth->setTokenAuth('');
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::FAILURE );
+
+ // not a token auth
+ $auth->setLogin($user['login']);
+ $auth->setTokenAuth($user['password']);
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::FAILURE );
+
+ // not a token auth
+ $auth->setLogin($user['login']);
+ $auth->setTokenAuth($password);
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::FAILURE );
+
+ // empty login
+ $auth->setLogin('');
+ $auth->setTokenAuth($tokenAuth);
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::FAILURE );
+
+ // API authentication
+ $auth->setLogin(null);
+ $auth->setTokenAuth($tokenAuth);
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::SUCCESS );
+
+ // valid login & token auth
+ $auth->setLogin($user['login']);
+ $auth->setTokenAuth($tokenAuth);
+ $rc = $auth->authenticate();
+
+ $user = Zend_Registry::get('config')->superuser->toArray();
+ $password = $user['password'];
+ $tokenAuth = Piwik_UsersManager_API::getInstance()->getTokenAuth($user['login'], $password);
+
+ // empty token auth
+ $auth->setLogin($user['login']);
+ $auth->setTokenAuth('');
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::FAILURE );
+
+ // not a token auth
+ $auth->setLogin($user['login']);
+ $auth->setTokenAuth($user['password']);
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::FAILURE );
+
+ // not a token auth
+ $auth->setLogin($user['login']);
+ $auth->setTokenAuth($password);
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::FAILURE );
+
+ // empty login
+ $auth->setLogin('');
+ $auth->setTokenAuth($tokenAuth);
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::FAILURE );
+
+ // API authentication
+ $auth->setLogin(null);
+ $auth->setTokenAuth($tokenAuth);
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::SUCCESS_SUPERUSER_AUTH_CODE );
+
+ // valid login & token auth
+ $auth->setLogin($user['login']);
+ $auth->setTokenAuth($tokenAuth);
+ $rc = $auth->authenticate();
+ $this->assertEqual( $rc->getCode(), Piwik_Auth_Result::SUCCESS_SUPERUSER_AUTH_CODE );
+ }
+}