~ubuntu-branches/ubuntu/maverick/gallery2/maverick

« back to all changes in this revision

Viewing changes to modules/core/test/phpunit/UserRecoverPasswordConfirmControllerTest.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
 
/**
22
 
 * UserRecoverPassword controller tests
23
 
 * @package GalleryCore
24
 
 * @subpackage PHPUnit
25
 
 * @author Bharat Mediratta <bharat@menalto.com>
26
 
 * @version $Revision: 15513 $
27
 
 */
28
 
class UserRecoverPasswordConfirmControllerTest extends GalleryControllerTestCase {
29
 
 
30
 
    function UserRecoverPasswordConfirmControllerTest($methodName) {
31
 
        $this->GalleryControllerTestCase($methodName, 'core.UserRecoverPasswordConfirm');
32
 
    }
33
 
 
34
 
    function setUp() {
35
 
        global $gallery;
36
 
        parent::setUp();
37
 
 
38
 
        /* Create a new user */
39
 
        list ($ret, $this->_user) = $this->_createRandomUser();
40
 
        if ($ret) {
41
 
            print $ret->getAsHtml();
42
 
            return $this->failWithStatus($ret);
43
 
        }
44
 
        $this->_markForCleanup($this->_user);
45
 
 
46
 
        $this->_hashedPassword = $this->_user->getHashedPassword();
47
 
    }
48
 
 
49
 
    function testRecoverPassword() {
50
 
        global $gallery;
51
 
 
52
 
        $ret = GalleryCoreApi::addMapEntry(
53
 
            'GalleryRecoverPasswordMap',
54
 
                array('userName' => $this->_user->getUserName(),
55
 
                      'authString' => md5('12345'),
56
 
                      'requestExpires' => time() + 60));
57
 
        if ($ret) {
58
 
            return $this->failWithStatus($ret);
59
 
        }
60
 
 
61
 
        GalleryUtilities::putRequestVariable('form[action][submit]', 1);
62
 
        GalleryUtilities::putRequestVariable('form[userName]', $this->_user->getUserName());
63
 
        GalleryUtilities::putRequestVariable('form[authString]', md5('12345'));
64
 
        GalleryUtilities::putRequestVariable('form[password1]', 'recoverConfirmPass');
65
 
        GalleryUtilities::putRequestVariable('form[password2]', 'recoverConfirmPass');
66
 
 
67
 
        $results = $this->handleRequest();
68
 
        $this->assertEquals(array('redirect' => array('view' => 'core.UserAdmin',
69
 
                                                      'subView' => 'core.UserLogin'),
70
 
                                  'return' => 0,
71
 
                                  'status' => array('passwordRecovered' => 1),
72
 
                                  'error' => array()),
73
 
                            $results);
74
 
 
75
 
        list ($ret, $this->_user) = $this->_user->refresh();
76
 
        if ($ret) {
77
 
            return $this->failWithStatus($ret);
78
 
        }
79
 
 
80
 
        $this->assertEquals(true, $this->_user->isCorrectPassword('recoverConfirmPass'),
81
 
                      'Password not changed');
82
 
 
83
 
        $ret = $this->_verifyMapEntryRemoved();
84
 
        if ($ret) {
85
 
            return $this->failWithStatus($ret);
86
 
        }
87
 
    }
88
 
 
89
 
    function testRecoverPasswordClearsFailedLogins() {
90
 
        global $gallery;
91
 
 
92
 
        $ret = GalleryCoreApi::addMapEntry(
93
 
            'GalleryRecoverPasswordMap',
94
 
                array('userName' => $this->_user->getUserName(),
95
 
                      'authString' => md5('12345'),
96
 
                      'requestExpires' => time() + 60));
97
 
        if ($ret) {
98
 
            return $this->failWithStatus($ret);
99
 
        }
100
 
 
101
 
        $ret = GalleryCoreApi::addMapEntry(
102
 
            'FailedLoginsMap',
103
 
                array('userName' => $this->_user->getUserName(),
104
 
                      'count' => 10,
105
 
                      'lastAttempt' => time()));
106
 
        if ($ret) {
107
 
            return $this->failWithStatus($ret);
108
 
        }
109
 
 
110
 
        GalleryUtilities::putRequestVariable('form[action][submit]', 1);
111
 
        GalleryUtilities::putRequestVariable('form[userName]', $this->_user->getUserName());
112
 
        GalleryUtilities::putRequestVariable('form[authString]', md5('12345'));
113
 
        GalleryUtilities::putRequestVariable('form[password1]', 'recoverConfirmPass');
114
 
        GalleryUtilities::putRequestVariable('form[password2]', 'recoverConfirmPass');
115
 
 
116
 
        $results = $this->handleRequest();
117
 
        /* We verified that password recovery works in another test */
118
 
 
119
 
        list ($ret, $searchResults) = GalleryCoreApi::getMapEntry(
120
 
            'FailedLoginsMap',
121
 
            array('userName'),
122
 
            array('userName' => $this->_user->getUserName()));
123
 
        if ($ret) {
124
 
            return $this->failWithStatus($ret);
125
 
        }
126
 
 
127
 
        $this->assertEquals(0, $searchResults->resultCount());
128
 
    }
129
 
 
130
 
    function testRequestTooOld() {
131
 
        global $gallery;
132
 
 
133
 
        $ret = GalleryCoreApi::addMapEntry(
134
 
            'GalleryRecoverPasswordMap',
135
 
            array('userName' => $this->_user->getUserName(),
136
 
                  'authString' => md5('12345'),
137
 
                  'requestExpires' => mktime(0,0,0,1,4,2005)));
138
 
        if ($ret) {
139
 
            return $this->failWithStatus($ret);
140
 
        }
141
 
 
142
 
        GalleryUtilities::putRequestVariable('form[action][submit]', 1);
143
 
        GalleryUtilities::putRequestVariable('form[userName]', $this->_user->getUserName());
144
 
        GalleryUtilities::putRequestVariable('form[authString]', md5('12345'));
145
 
        GalleryUtilities::putRequestVariable('form[password1]', 'password');
146
 
        GalleryUtilities::putRequestVariable('form[password2]', 'password');
147
 
 
148
 
        $results = $this->handleRequest();
149
 
        $this->assertEquals(
150
 
            array('delegate' => array('view' => 'core.UserAdmin',
151
 
                                      'subView' => 'core.UserRecoverPasswordConfirm'),
152
 
                  'status' => array(),
153
 
                  'error' => array('form[error][request][tooOld]')),
154
 
            $results);
155
 
 
156
 
        $ret = $this->_verifyMapEntryRemoved();
157
 
        if ($ret) {
158
 
            return $this->failWithStatus($ret);
159
 
        }
160
 
 
161
 
        $this->_verifyNoPasswordChange();
162
 
    }
163
 
 
164
 
    function testRequestMissing() {
165
 
        GalleryUtilities::putRequestVariable('form[action][submit]', 1);
166
 
        GalleryUtilities::putRequestVariable('form[userName]', 'recoverTest12345');
167
 
        GalleryUtilities::putRequestVariable('form[authString]', 1);
168
 
        GalleryUtilities::putRequestVariable('form[password1]', 'password');
169
 
        GalleryUtilities::putRequestVariable('form[password2]', 'password');
170
 
 
171
 
        $results = $this->handleRequest();
172
 
 
173
 
        $this->assertEquals(
174
 
            array('delegate' => array('view' => 'core.UserAdmin',
175
 
                                      'subView' => 'core.UserRecoverPasswordConfirm'),
176
 
                  'status' => array(),
177
 
                  'error' => array('form[error][request][missing]')),
178
 
            $results);
179
 
 
180
 
        $this->_verifyNoPasswordChange();
181
 
    }
182
 
 
183
 
    function testPasswordMismatch() {
184
 
        GalleryUtilities::putRequestVariable('form[action][submit]', 1);
185
 
        GalleryUtilities::putRequestVariable('form[userName]', 'recoverTest12345');
186
 
        GalleryUtilities::putRequestVariable('form[authString]', 1);
187
 
        GalleryUtilities::putRequestVariable('form[password1]', 'password');
188
 
        GalleryUtilities::putRequestVariable('form[password2]', 'mismatch');
189
 
 
190
 
        $results = $this->handleRequest();
191
 
 
192
 
        $this->assertEquals(
193
 
            array('delegate' => array('view' => 'core.UserAdmin',
194
 
                                      'subView' => 'core.UserRecoverPasswordConfirm'),
195
 
                  'status' => array(),
196
 
                  'error' => array('form[error][password][mismatch]')),
197
 
            $results);
198
 
 
199
 
        $this->_verifyNoPasswordChange();
200
 
    }
201
 
 
202
 
    function testMissingInputs() {
203
 
        GalleryUtilities::putRequestVariable('form[action][submit]', 1);
204
 
 
205
 
        $results = $this->handleRequest();
206
 
        $this->assertEquals(
207
 
            array('delegate' => array('view' => 'core.UserAdmin',
208
 
                                      'subView' => 'core.UserRecoverPasswordConfirm'),
209
 
                  'status' => array(),
210
 
                  'error' => array('form[error][userName][missing]',
211
 
                                   'form[error][authString][missing]',
212
 
                                   'form[error][password][missing]')),
213
 
            $results);
214
 
 
215
 
        $this->_verifyNoPasswordChange();
216
 
    }
217
 
 
218
 
    function testCancel() {
219
 
        GalleryUtilities::putRequestVariable('form[action][cancel]', 1);
220
 
 
221
 
        $results = $this->handleRequest();
222
 
        $this->assertEquals(array('return' => 1,
223
 
                                  'delegate' => array('view' => 'core.UserAdmin',
224
 
                                                    'subView' => 'core.UserRecoverPasswordConfirm'),
225
 
                                  'status' => array(),
226
 
                                  'error' => array()),
227
 
                            $results);
228
 
 
229
 
        $this->_verifyNoPasswordChange();
230
 
    }
231
 
 
232
 
    function _verifyNoPasswordChange() {
233
 
        $this->assertEquals($this->_hashedPassword, $this->_user->getHashedPassword(),
234
 
                            'Passwords changed');
235
 
    }
236
 
 
237
 
    function _verifyMapEntryRemoved() {
238
 
        global $gallery;
239
 
 
240
 
        $query = '
241
 
            SELECT
242
 
                [GalleryRecoverPasswordMap::userName]
243
 
            FROM
244
 
                [GalleryRecoverPasswordMap]
245
 
            WHERE
246
 
                [GalleryRecoverPasswordMap::userName] = ?
247
 
        ';
248
 
 
249
 
        $data = array($this->_user->getUserName());
250
 
 
251
 
        list ($ret, $searchResults) = $gallery->search($query, $data);
252
 
        if ($ret) {
253
 
            return $this->failWithStatus($ret);
254
 
        }
255
 
 
256
 
        if ($results = $searchResults->nextResult()) {
257
 
            $this->assert($results[0], 'RecoverMap entry was not purged during password reset');
258
 
        }
259
 
 
260
 
        return null;
261
 
    }
262
 
}
263
 
?>