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

« back to all changes in this revision

Viewing changes to modules/httpauth/test/phpunit/HttpAuthPluginTest.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/httpauth/classes/HttpAuthPlugin.class');
22
 
 
23
 
/**
24
 
 * HTTP auth tests.
25
 
 * @package HttpAuth
26
 
 * @subpackage PHPUnit
27
 
 * @author Jack Bates<ms419@freezone.co.uk>
28
 
 * @version $Revision: 15759 $
29
 
 */
30
 
class HttpAuthPluginTest extends GalleryTestCase {
31
 
 
32
 
    function HttpAuthPluginTest($methodName) {
33
 
        $this->GalleryTestCase($methodName);
34
 
    }
35
 
 
36
 
    function setUp() {
37
 
        global $gallery;
38
 
        parent::setUp();
39
 
 
40
 
        /* Create test user */
41
 
        list ($ret, $this->_user) = $this->_createRandomUser(null, 'password');
42
 
        if ($ret) {
43
 
            print $ret->getAsHtml();
44
 
            return $this->failWithStatus($ret);
45
 
        }
46
 
        $this->_markForCleanup($this->_user);
47
 
        $_SERVER['AUTH_TYPE'] = 'Test';
48
 
 
49
 
        $this->_plugin = new HttpAuthPlugin();
50
 
 
51
 
        $this->_saveSession = $gallery->_session;       
52
 
        $gallery->_session = new HttpAuthPluginTestSession();
53
 
 
54
 
        $this->_listener = new NullEventListener();
55
 
        $this->_registerTestEventListener('Gallery::FailedLogin', $this->_listener);
56
 
    }
57
 
 
58
 
    function tearDown() {
59
 
        global $gallery;
60
 
        $gallery->_session = $this->_saveSession;
61
 
 
62
 
        $ret = GalleryCoreApi::removeAllMapEntries('FailedLoginsMap');
63
 
        if ($ret) {
64
 
            $this->failWithStatus($ret);
65
 
        }
66
 
 
67
 
        parent::tearDown();
68
 
    }
69
 
 
70
 
    function testValidUser() {
71
 
        global $gallery;
72
 
        $session =& $gallery->getSession();
73
 
 
74
 
        $_SERVER['PHP_AUTH_USER'] = $this->_user->getUserName();
75
 
        $_SERVER['PHP_AUTH_PW'] = 'password';
76
 
 
77
 
        list ($ret, $user) = $this->_plugin->getUser();
78
 
        if ($ret) {
79
 
            return $this->failWithStatus($ret);
80
 
        }
81
 
 
82
 
        $this->assertEquals($this->_user, $user);
83
 
        $this->assert(!isset($this->_listener->_event), 'There was a FailedLogin event!');
84
 
        $this->assert($session->regenerateWasCalled(), 'Session has not been regenerated!');
85
 
    }
86
 
 
87
 
    function testInvalidUser() {
88
 
        $_SERVER['PHP_AUTH_USER'] = 'bogusUser-' . rand();
89
 
        $_SERVER['PHP_AUTH_PW'] = 'password';
90
 
 
91
 
        list ($ret, $user) = $this->_plugin->getUser();
92
 
        if ($ret) {
93
 
            return $this->failWithStatus($ret);
94
 
        }
95
 
 
96
 
        $this->assert(!isset($user));
97
 
 
98
 
        if (!isset($this->_listener->_event)) {
99
 
            $this->assert(false, 'Listener didn\'t receive event!');
100
 
        } else {
101
 
            $eventData = $this->_listener->_event->getData();
102
 
            $this->assertEquals($_SERVER['PHP_AUTH_USER'], $eventData['userName']);
103
 
        }
104
 
    }
105
 
 
106
 
    function testValidUserInvalidPassword() {
107
 
        $_SERVER['PHP_AUTH_USER'] = $this->_user->getUserName();
108
 
        $_SERVER['PHP_AUTH_PW'] = 'wrongPassword';
109
 
 
110
 
        list ($ret, $user) = $this->_plugin->getUser();
111
 
        if ($ret) {
112
 
            return $this->failWithStatus($ret);
113
 
        }
114
 
 
115
 
        $this->assert(!isset($user));
116
 
 
117
 
        if (!isset($this->_listener->_event)) {
118
 
            $this->assert(false, 'Listener didn\'t receive event!');
119
 
        } else {
120
 
            $eventData = $this->_listener->_event->getData();
121
 
            $this->assertEquals($this->_user->getUserName(), $eventData['userName']);
122
 
        }
123
 
    }
124
 
 
125
 
    function testNoFailedLoginEventForSpecialLogoutUsername() {
126
 
        $_SERVER['PHP_AUTH_USER'] = '__LOGOUT__' . rand();
127
 
        $_SERVER['PHP_AUTH_PW'] = 'somepassword';
128
 
 
129
 
        list ($ret, $user) = $this->_plugin->getUser();
130
 
        if ($ret) {
131
 
            return $this->failWithStatus($ret);
132
 
        }
133
 
 
134
 
        $this->assert(!isset($user));
135
 
        $this->assert(!isset($this->_listener->_event), 'There was a FailedLogin event!');
136
 
    }
137
 
}
138
 
 
139
 
/**
140
 
 * Mock Session
141
 
 */
142
 
class HttpAuthPluginTestSession {
143
 
    function HttpAuthPluginTestSession() {
144
 
        $this->_regenerateWasCalled = false;
145
 
    }
146
 
 
147
 
    function getUserId() {
148
 
        return null;
149
 
    }
150
 
 
151
 
    function regenerate() {
152
 
        $this->_regenerateWasCalled = true;
153
 
    }
154
 
 
155
 
    function regenerateWasCalled() {
156
 
        return $this->_regenerateWasCalled;
157
 
    }
158
 
}
159
 
?>