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
|
<?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();
// valid login & hashed token auth
$auth->setLogin($user['login']);
$hash = $auth->getHashTokenAuth($user['login'], $tokenAuth);
$auth->setTokenAuth($hash);
$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 );
// valid login & hashed token auth
$auth->setLogin($user['login']);
$hash = $auth->getHashTokenAuth($user['login'], $tokenAuth);
$auth->setTokenAuth($hash);
$rc = $auth->authenticate();
$this->assertEqual( $rc->getCode(), Piwik_Auth_Result::SUCCESS_SUPERUSER_AUTH_CODE );
}
}
|