~ubuntu-branches/ubuntu/quantal/gallery2/quantal

« back to all changes in this revision

Viewing changes to modules/core/test/phpunit/UserHelperTest.class

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2007-09-10 20:22:19 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070910202219-0jsuntvqge4ade6b
Tags: 2.2.3-2
Add Slovak translation of Debconf templates.  (Thanks to 
Ivan Masá.  Closes: #441671)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Gallery - a web based photo album viewer and editor
4
 
 * Copyright (C) 2000-2007 Bharat Mediratta
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or (at
9
 
 * your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful, but
12
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
 * General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
19
 
 */
20
 
 
21
 
GalleryCoreApi::requireOnce('modules/core/classes/helpers/GalleryUserHelper_medium.class');
22
 
 
23
 
/**
24
 
 * UserHelper tests
25
 
 *
26
 
 * @package GalleryCore
27
 
 * @subpackage PHPUnit
28
 
 * @author Bharat Mediratta <bharat@menalto.com>
29
 
 * @version $Revision: 15759 $
30
 
 */
31
 
class UserHelperTest extends GalleryTestCase {
32
 
 
33
 
    function setUp() {
34
 
        global $gallery;
35
 
        parent::setUp();
36
 
 
37
 
        $this->_phpVm = new UserHelperTestPhpVm();
38
 
        $this->_storage = new UserHelperTestMockStorage();
39
 
        $gallery->_storage =& $this->_storage;
40
 
        $gallery->_phpVm =& $this->_phpVm;
41
 
        $this->_saveSession = $gallery->_session;
42
 
    }
43
 
 
44
 
    function tearDown() {
45
 
        global $gallery;
46
 
        $gallery->_session = $this->_saveSession;
47
 
 
48
 
        parent::tearDown();
49
 
    }
50
 
 
51
 
    function testGalleryLoginEvent() {
52
 
        global $gallery;
53
 
 
54
 
        $gallery->_session = new UserHelperTestSession();
55
 
 
56
 
        $event = GalleryCoreApi::newEvent('Gallery::Login');
57
 
        $event->setEntity(new UserHelperTestMockUser());
58
 
        list ($ret, $ignored) = GalleryUserHelper_medium::handleEvent($event);
59
 
        if ($ret) {
60
 
            return $this->failWithStatus($ret);
61
 
        }
62
 
 
63
 
        $this->assertEquals(
64
 
            array(array('remove', 'FailedLoginsMap', array('userName' => 'MockUser'))),
65
 
            $this->_storage->_operations);
66
 
 
67
 
        $this->assert($gallery->_session->regenerateWasCalled(),
68
 
                      'Regenerate session was not called!');
69
 
    }
70
 
 
71
 
    function testGalleryFailedLoginEventFirstFailure() {
72
 
        $event = GalleryCoreApi::newEvent('Gallery::FailedLogin');
73
 
        $event->setData(array('userName' => 'MockUser'));
74
 
        $this->_phpVm->_time = 'NOW';
75
 
        list ($ret, $ignored) = GalleryUserHelper_medium::handleEvent($event);
76
 
        if ($ret) {
77
 
            return $this->failWithStatus($ret);
78
 
        }
79
 
 
80
 
        $this->assertEquals(
81
 
            array(
82
 
                array('get', 'FailedLoginsMap', array('count', 'lastAttempt'),
83
 
                      array('userName' => 'MockUser')),
84
 
                array('add', 'FailedLoginsMap',
85
 
                      array('userName' => 'MockUser', 'count' => 1, 'lastAttempt' => 'NOW'))),
86
 
            $this->_storage->_operations);
87
 
    }
88
 
 
89
 
    function testGalleryFailedLoginEventMissingUsername() {
90
 
        $event = GalleryCoreApi::newEvent('Gallery::FailedLogin');
91
 
        list ($ret, $ignored) = GalleryUserHelper_medium::handleEvent($event);
92
 
        if ($ret) {
93
 
            return $this->failWithStatus($ret);
94
 
        }
95
 
 
96
 
        $this->assertEquals(array(), $this->_storage->_operations);
97
 
    }
98
 
 
99
 
    function testGalleryFailedLoginEventSecondFailure() {
100
 
        global $gallery;
101
 
        $event = GalleryCoreApi::newEvent('Gallery::FailedLogin');
102
 
        $event->setData(array('userName' => 'MockUser'));
103
 
        $this->_storage->_count = 1;
104
 
        $this->_storage->_lastAttempt = 1;  /* way in the past */
105
 
        $this->_phpVm->_time = 'NOW';
106
 
        list ($ret, $ignored) = GalleryUserHelper_medium::handleEvent($event);
107
 
        if ($ret) {
108
 
            return $this->failWithStatus($ret);
109
 
        }
110
 
 
111
 
        $this->assertEquals(
112
 
            array(
113
 
                array('get', 'FailedLoginsMap',
114
 
                      array('count', 'lastAttempt'), array('userName' => 'MockUser')),
115
 
                array('remove', 'FailedLoginsMap', array('userName' => 'MockUser')),
116
 
                array('add', 'FailedLoginsMap',
117
 
                      array('userName' => 'MockUser', 'count' => 2, 'lastAttempt' => 'NOW'))),
118
 
            $this->_storage->_operations);
119
 
    }
120
 
 
121
 
    function testGalleryFailedLoginEventFailuresWhenDisabledAreIgnored() {
122
 
        global $gallery;
123
 
        $event = GalleryCoreApi::newEvent('Gallery::FailedLogin');
124
 
        $event->setData(array('userName' => 'MockUser'));
125
 
        $this->_storage->_count = 100;
126
 
        $this->_storage->_lastAttempt = time();
127
 
        $this->_phpVm->_time = time();
128
 
        list ($ret, $ignored) = GalleryUserHelper_medium::handleEvent($event);
129
 
        if ($ret) {
130
 
            return $this->failWithStatus($ret);
131
 
        }
132
 
 
133
 
        $this->assertEquals(
134
 
            array(array('get', 'FailedLoginsMap',
135
 
                        array('count', 'lastAttempt'), array('userName' => 'MockUser'))),
136
 
            $this->_storage->_operations);
137
 
    }
138
 
 
139
 
    function testIsDisabledUsernameNoRows() {
140
 
        list ($ret, $disabled) = GalleryUserHelper_medium::isDisabledUsername('SomeBogusUser');
141
 
        if ($ret) {
142
 
            return $this->failWithStatus($ret);
143
 
        }
144
 
 
145
 
        $this->assertEquals(
146
 
            array(array('get', 'FailedLoginsMap',
147
 
                        array('count', 'lastAttempt'), array('userName' => 'SomeBogusUser'))),
148
 
            $this->_storage->_operations);
149
 
 
150
 
        $this->assertEquals(false, $disabled);
151
 
    }
152
 
 
153
 
    function testIsDisabledUsernameBelowCutoff() {
154
 
        $this->_storage->_count = 9;
155
 
        $this->_storage->_lastAttempt = 10000;
156
 
        list ($ret, $disabled) = GalleryUserHelper_medium::isDisabledUsername('MockUser');
157
 
        if ($ret) {
158
 
            return $this->failWithStatus($ret);
159
 
        }
160
 
 
161
 
        $this->assertEquals(
162
 
            array(array('get', 'FailedLoginsMap',
163
 
                        array('count', 'lastAttempt'), array('userName' => 'MockUser'))),
164
 
            $this->_storage->_operations);
165
 
        $this->assertEquals(false, $disabled, 'should not be disabled');
166
 
    }
167
 
 
168
 
    function testIsDisabledUsernameAboveCutoffAndRecent() {
169
 
        $this->_storage->_count = 10;  /* disabled for an hour */
170
 
        $this->_storage->_lastAttempt = 10000;
171
 
        $this->_phpVm->_time = 10001;
172
 
        list ($ret, $disabled) = GalleryUserHelper_medium::isDisabledUsername('MockUser');
173
 
        if ($ret) {
174
 
            return $this->failWithStatus($ret);
175
 
        }
176
 
 
177
 
        $this->assertEquals(
178
 
            array(array('get', 'FailedLoginsMap',
179
 
                        array('count', 'lastAttempt'), array('userName' => 'MockUser'))),
180
 
            $this->_storage->_operations);
181
 
        $this->assertEquals(true, $disabled, 'should be disabled');
182
 
    }
183
 
 
184
 
    function testIsDisabledUsernameWayAboveCutoffAndTooRecent() {
185
 
        $this->_storage->_count = 100; /* disabled for 10 hours */
186
 
        $this->_storage->_lastAttempt = 10000;
187
 
        $this->_phpVm->_time = 10000 + 9 * 3600; /* 9 hours since last attempt */
188
 
        list ($ret, $disabled) = GalleryUserHelper_medium::isDisabledUsername('MockUser');
189
 
        if ($ret) {
190
 
            return $this->failWithStatus($ret);
191
 
        }
192
 
 
193
 
        $this->assertEquals(
194
 
            array(array('get', 'FailedLoginsMap',
195
 
                        array('count', 'lastAttempt'), array('userName' => 'MockUser'))),
196
 
            $this->_storage->_operations);
197
 
        $this->assertEquals(true, $disabled, 'should be disabled');
198
 
    }
199
 
 
200
 
    function testIsDisabledUsernameAboveCutoffButNotRecent() {
201
 
        $this->_storage->_count = 100; /* disabled for 10 hours */
202
 
        $this->_storage->_lastAttempt = 10000;
203
 
        $this->_phpVm->_time = 1000000; /* last attempt is in the distant past */
204
 
        list ($ret, $disabled) = GalleryUserHelper_medium::isDisabledUsername('MockUser');
205
 
        if ($ret) {
206
 
            return $this->failWithStatus($ret);
207
 
        }
208
 
 
209
 
        $this->assertEquals(
210
 
            array(array('get', 'FailedLoginsMap',
211
 
                        array('count', 'lastAttempt'), array('userName' => 'MockUser'))),
212
 
            $this->_storage->_operations);
213
 
        $this->assertEquals(false, $disabled, 'should not be disabled');
214
 
    }
215
 
}
216
 
 
217
 
/**
218
 
 * Fake storage class, pretends to extend GalleryStorage
219
 
 */
220
 
class UserHelperTestMockStorage {
221
 
    var $_operations;
222
 
    var $_count;
223
 
    var $_lastAttempt;
224
 
 
225
 
    function UserHelperTestMockStorage() {
226
 
        $this->_operations = array();
227
 
        $this->_count = null;
228
 
        $this->_lastAttempt = null;
229
 
    }
230
 
 
231
 
    function addMapEntry($mapName, $params) {
232
 
        $this->_operations[] = array('add', $mapName, $params);
233
 
        return null;
234
 
    }
235
 
 
236
 
    function getMapEntry($mapName, $members, $match) {
237
 
        $this->_operations[] = array('get', $mapName, $members, $match);
238
 
 
239
 
        if (count($members) == 1) {
240
 
            $results = new UserHelperTestMockStorageFakeResults($this->_count);
241
 
        } else {
242
 
            $results = new UserHelperTestMockStorageFakeResults($this->_count, $this->_lastAttempt);
243
 
        }
244
 
        return array(null, $results);
245
 
    }
246
 
 
247
 
    function removeMapEntry($mapName, $data) {
248
 
        $this->_operations[] = array('remove', $mapName, $data);
249
 
    }
250
 
}
251
 
 
252
 
/**
253
 
 * Fake search results class, pretends to extend GallerySearchResults
254
 
 */
255
 
class UserHelperTestMockStorageFakeResults {
256
 
    function UserHelperTestMockStorageFakeResults($count, $lastAttempt=null) {
257
 
        $this->_count = $count;
258
 
        $this->_lastAttempt = $lastAttempt;
259
 
    }
260
 
 
261
 
    function resultCount() {
262
 
        if (isset($this->_count)) {
263
 
            return 1;
264
 
        }
265
 
        return 0;
266
 
    }
267
 
 
268
 
    function nextResult() {
269
 
        if ($this->_lastAttempt) {
270
 
            $results = array($this->_count, $this->_lastAttempt);
271
 
        } else {
272
 
            $results = array($this->_count);
273
 
        }
274
 
        return $results;
275
 
    }
276
 
}
277
 
 
278
 
/**
279
 
 * Mock user, pretends to extend GalleryUser
280
 
 */
281
 
class UserHelperTestMockUser {
282
 
    function getUserName() {
283
 
        return 'MockUser';
284
 
    }
285
 
}
286
 
 
287
 
/**
288
 
 * Fake PHP vm, pretends to extend GalleryPhpVm
289
 
 */
290
 
class UserHelperTestPhpVm {
291
 
    var $_time;
292
 
 
293
 
    function time() {
294
 
        return $this->_time;
295
 
    }
296
 
}
297
 
 
298
 
/**
299
 
 * Mock Session
300
 
 */
301
 
class UserHelperTestSession {
302
 
    function UserHelperTestSession() {
303
 
        $this->_regenerateWasCalled = false;
304
 
    }
305
 
 
306
 
    function regenerate() {
307
 
        $this->_regenerateWasCalled = true;
308
 
    }
309
 
 
310
 
    function regenerateWasCalled() {
311
 
        return $this->_regenerateWasCalled;
312
 
    }
313
 
}
314
 
?>