summaryrefslogtreecommitdiff
path: root/mailman/tests/test_security_mgr.py
blob: 8355304969f1c92328ffa2e694e0dee2c5c0dc1b (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# Copyright (C) 2001-2008 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.

"""Unit tests for the SecurityManager module."""

import os
import errno
import Cookie
import unittest

# Don't use cStringIO because we're going to inherit
from StringIO import StringIO

from mailman import Utils
from mailman import passwords
from mailman.configuration import config



def password(cleartext):
    return passwords.make_secret(cleartext, passwords.Schemes.ssha)



class TestSecurityManager(unittest.TestCase):
    def test_init_vars(self):
        eq = self.assertEqual
        eq(self._mlist.mod_password, None)
        eq(self._mlist.passwords, {})

    def test_auth_context_info_authuser(self):
        mlist = self._mlist
        self.assertRaises(TypeError, mlist.AuthContextInfo, config.AuthUser)
        # Add a member
        mlist.addNewMember('aperson@dom.ain', password='xxXXxx')
        self.assertEqual(
            mlist.AuthContextInfo(config.AuthUser, 'aperson@dom.ain'),
            ('_xtest%40example.com+user+aperson--at--dom.ain', 'xxXXxx'))

    def test_auth_context_moderator(self):
        mlist = self._mlist
        mlist.mod_password = 'yyYYyy'
        self.assertEqual(
            mlist.AuthContextInfo(config.AuthListModerator),
            ('_xtest%40example.com+moderator', 'yyYYyy'))

    def test_auth_context_admin(self):
        mlist = self._mlist
        mlist.password = 'zzZZzz'
        self.assertEqual(
            mlist.AuthContextInfo(config.AuthListAdmin),
            ('_xtest%40example.com+admin', 'zzZZzz'))

    def test_auth_context_site(self):
        mlist = self._mlist
        mlist.password = 'aaAAaa'
        self.assertEqual(
            mlist.AuthContextInfo(config.AuthSiteAdmin),
            ('_xtest%40example.com+admin', 'aaAAaa'))

    def test_auth_context_huh(self):
        self.assertEqual(
            self._mlist.AuthContextInfo('foo'),
            (None, None))



class TestAuthenticate(unittest.TestCase):
    def setUp(self):
        Utils.set_global_password('bbBBbb', siteadmin=True)
        Utils.set_global_password('ccCCcc', siteadmin=False)

    def tearDown(self):
        try:
            os.unlink(config.SITE_PW_FILE)
        except OSError, e:
            if e.errno <> errno.ENOENT: raise
        try:
            os.unlink(config.LISTCREATOR_PW_FILE)
        except OSError, e:
            if e.errno <> errno.ENOENT: raise

    def test_auth_creator(self):
        self.assertEqual(self._mlist.Authenticate(
            [config.AuthCreator], 'ccCCcc'), config.AuthCreator)

    def test_auth_creator_unauth(self):
        self.assertEqual(self._mlist.Authenticate(
            [config.AuthCreator], 'xxxxxx'), config.UnAuthorized)

    def test_auth_site_admin(self):
        self.assertEqual(self._mlist.Authenticate(
            [config.AuthSiteAdmin], 'bbBBbb'), config.AuthSiteAdmin)

    def test_auth_site_admin_unauth(self):
        self.assertEqual(self._mlist.Authenticate(
            [config.AuthSiteAdmin], 'xxxxxx'), config.UnAuthorized)

    def test_list_admin(self):
        self._mlist.password = password('ttTTtt')
        self.assertEqual(self._mlist.Authenticate(
            [config.AuthListAdmin], 'ttTTtt'), config.AuthListAdmin)

    def test_list_admin_unauth(self):
        self._mlist.password = password('ttTTtt')
        self.assertEqual(self._mlist.Authenticate(
            [config.AuthListAdmin], 'xxxxxx'), config.UnAuthorized)

    def test_list_moderator(self):
        self._mlist.mod_password = password('mmMMmm')
        self.assertEqual(self._mlist.Authenticate(
            [config.AuthListModerator], 'mmMMmm'), config.AuthListModerator)

    def test_user(self):
        mlist = self._mlist
        mlist.addNewMember('aperson@dom.ain', password=password('nosrepa'))
        self.assertEqual(mlist.Authenticate(
            [config.AuthUser], 'nosrepa', 'aperson@dom.ain'), config.AuthUser)

    def test_wrong_user(self):
        mlist = self._mlist
        mlist.addNewMember('aperson@dom.ain', password='nosrepa')
        self.assertEqual(
            mlist.Authenticate([config.AuthUser], 'nosrepa', 'bperson@dom.ain'),
            config.UnAuthorized)

    def test_no_user(self):
        mlist = self._mlist
        mlist.addNewMember('aperson@dom.ain', password='nosrepa')
        self.assertEqual(mlist.Authenticate([config.AuthUser], 'norespa'),
                         config.UnAuthorized)

    def test_user_unauth(self):
        mlist = self._mlist
        mlist.addNewMember('aperson@dom.ain', password='nosrepa')
        self.assertEqual(mlist.Authenticate(
            [config.AuthUser], 'xxxxxx', 'aperson@dom.ain'),
                         config.UnAuthorized)

    def test_value_error(self):
        self.assertRaises(ValueError, self._mlist.Authenticate,
                          ['spooge'], 'xxxxxx', 'zperson@dom.ain')



class StripperIO(StringIO):
    HEAD = 'Set-Cookie: '
    def write(self, s):
        if s.startswith(self.HEAD):
            s = s[len(self.HEAD):]
        StringIO.write(self, s)


class TestWebAuthenticate(unittest.TestCase):
    def setUp(self):
        Utils.set_global_password('bbBBbb', siteadmin=True)
        Utils.set_global_password('ccCCcc', siteadmin=False)
        mlist = self._mlist
        mlist.mod_password = password('abcdefg')
        mlist.addNewMember('aperson@dom.ain', password='qqQQqq')
        # Set up the cookie data
        sfp = StripperIO()
        print >> sfp, mlist.MakeCookie(config.AuthSiteAdmin)
        # AuthCreator isn't handled in AuthContextInfo()
        print >> sfp, mlist.MakeCookie(config.AuthListAdmin)
        print >> sfp, mlist.MakeCookie(config.AuthListModerator)
        print >> sfp, mlist.MakeCookie(config.AuthUser, 'aperson@dom.ain')
        # Strip off the "Set-Cookie: " prefix
        cookie = sfp.getvalue()
        os.environ['HTTP_COOKIE'] = cookie

    def tearDown(self):
        try:
            os.unlink(config.SITE_PW_FILE)
        except OSError, e:
            if e.errno <> errno.ENOENT: raise
        try:
            os.unlink(config.LISTCREATOR_PW_FILE)
        except OSError, e:
            if e.errno <> errno.ENOENT: raise
        del os.environ['HTTP_COOKIE']

    def test_auth_site_admin(self):
        self.failUnless(self._mlist.WebAuthenticate(
            [config.AuthSiteAdmin], 'does not matter'))

    def test_list_admin(self):
        self.failUnless(self._mlist.WebAuthenticate(
            [config.AuthListAdmin], 'does not matter'))

    def test_list_moderator(self):
        self.failUnless(self._mlist.WebAuthenticate(
            [config.AuthListModerator], 'does not matter'))

    def test_user(self):
        self.failUnless(self._mlist.WebAuthenticate(
            [config.AuthUser], 'does not matter'))

    def test_not_a_user(self):
        self._mlist.removeMember('aperson@dom.ain')
        self.failIf(self._mlist.WebAuthenticate(
            [config.AuthUser], 'does not matter', 'aperson@dom.ain'))



# TBD: Tests for MakeCookie(), ZapCookie(), CheckCookie() -- although the
# latter is implicitly tested by testing WebAuthenticate() above.



def test_suite():
    suite = unittest.TestSuite()
##     suite.addTest(unittest.makeSuite(TestSecurityManager))
##     suite.addTest(unittest.makeSuite(TestAuthenticate))
##     suite.addTest(unittest.makeSuite(TestWebAuthenticate))
    return suite