[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/OFS/Services/AuthenticationService/tests - __init__.py:1.1 testAuthenticationService.py:1.1 testUser.py:1.1

Stephan Richter srichter@cbu.edu
Sat, 13 Jul 2002 12:52:59 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Services/AuthenticationService/tests
In directory cvs.zope.org:/tmp/cvs-serv9375/AuthenticationService/tests

Added Files:
	__init__.py testAuthenticationService.py testUser.py 
Log Message:
Okay, this is a truely cheesy Authentication Service. You are not able to
specify multiple or different data sources, there are no groups and I
cannot specify roles on the user yet, since Issue 19 has not been
implemented.


=== Added File Zope3/lib/python/Zope/App/OFS/Services/AuthenticationService/tests/__init__.py ===


=== Added File Zope3/lib/python/Zope/App/OFS/Services/AuthenticationService/tests/testAuthenticationService.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""
$Id: testAuthenticationService.py,v 1.1 2002/07/13 16:52:58 srichter Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from Zope.App.OFS.Services.AuthenticationService.AuthenticationService \
     import AuthenticationService, DuplicateLogin, DuplicateId
from Zope.App.OFS.Services.AuthenticationService.User import User
from Zope.App.OFS.Services.AuthenticationService.IUser import IUser

from Zope.Exceptions import NotFoundError
from Zope.Publisher.HTTP.IHTTPCredentials import IHTTPCredentials
from Zope.App.OFS.Services.ServiceManager.tests.PlacefulSetup \
           import PlacefulSetup

class Request:

    __implements__ = IHTTPCredentials

    def __init__(self, lpw):
        self.__lpw = lpw

    def _authUserPW(self):
        return self.__lpw

    challenge = None
    def unauthorized(self, challenge):
        self.challenge = challenge


class AuthServiceTest(TestCase, PlacefulSetup):

    def setUp(self):
        PlacefulSetup.setUp(self)

        from Zope.ComponentArchitecture import getService
        from Zope.App.Security.BasicAuthAdapter import BasicAuthAdapter
        from Zope.App.Security.ILoginPassword import ILoginPassword
        getService(None, "Adapters").provideAdapter(
            IHTTPCredentials, ILoginPassword, BasicAuthAdapter)

        auth = AuthenticationService()
        auth.setObject('srichter', User('srichter', 'Stephan', 'Richter',
                                        'srichter', 'hello'))
        auth.setObject('jim', User('jim', 'Jim', 'Foulton',
                                        'jim', 'hello2'))
        auth.setObject('stevea', User('stevea', 'Steve', 'Alexander',
                                        'stevea', 'hello3'))
        self._auth = auth
        

    def testGetPrincipalByLogin(self):
        auth = self._auth
        self.assertEqual(auth['srichter'],
                         auth.getPrincipalByLogin('srichter'))

    def testAuthenticate(self):
        auth = self._auth
        req = Request(('srichter', 'hello'))
        pid = auth.authenticate(req)
        self.assertEquals(pid, 'srichter')
        req = Request(('srichter', 'hello2'))
        pid = auth.authenticate(req)
        self.assertEquals(pid, None)
        req = Request(('strichter', 'hello'))
        pid = auth.authenticate(req)
        self.assertEquals(pid, None)

    def testUnauthenticatedPrincipal(self):
        auth = self._auth
        self.assertEqual(None, auth.unauthenticatedPrincipal())

    def testUnauthorized(self):
        auth = self._auth
        request = Request(None)
        auth.unauthorized(auth.unauthenticatedPrincipal(), request)
        self.assertEquals(request.challenge, "basic realm=zope")
        request = Request(None)
        auth.unauthorized(None, request)
        self.assertEquals(request.challenge, "basic realm=zope")
        request = Request(None)
        auth.unauthorized("srichter", request)
        self.assertEquals(request.challenge, None)

    def testGetPrincipal(self):
        auth = self._auth
        self.assertEqual(auth['srichter'], auth.getPrincipal('srichter'))
        self.assertEqual(None, auth.getPrincipal('srichter2'))

    def testGetPrincipals(self):
        auth = self._auth
        self.assertEqual([auth['srichter']], auth.getPrincipals('srichter'))

    def testIsAddable(self):
        auth = self._auth
        self.assertEqual(1, auth.isAddable(IUser))
        self.assertEqual(0, auth.isAddable(IHTTPCredentials))


def test_suite():
    return TestSuite((
        makeSuite(AuthServiceTest),
        ))

if __name__=='__main__':
    main(defaultTest='test_suite')


=== Added File Zope3/lib/python/Zope/App/OFS/Services/AuthenticationService/tests/testUser.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""
$Id: testUser.py,v 1.1 2002/07/13 16:52:58 srichter Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from Zope.App.OFS.Services.AuthenticationService.User import User

class UserTest(TestCase):

    def setUp(self):
        self._user = User('srichter', 'Stephan Richter',
                          'Local Authentication Service Developer',
                          'srichter', 'hello')

    def testGetLogin(self):
        user = self._user
        self.assertEqual('srichter', user.getLogin())
        
    def testValidate(self):
        user = self._user
        self.assertEqual(1, user.validate('hello'))

    def testGetId(self):
        user = self._user
        self.assertEqual('srichter', user.getId())

    def testGetTitle(self):
        user = self._user
        self.assertEqual('Stephan Richter', user.getTitle())

    def testGetDescription(self):
        user = self._user
        self.assertEqual('Local Authentication Service Developer',
                         user.getDescription())

    def testSetTitle(self):
        user = self._user
        user.setTitle('Stephan 2')
        self.assertEqual('Stephan 2', user.getTitle())

    def testSetDescription(self):
        user = self._user
        user.setDescription('Programmer')
        self.assertEqual('Programmer', user.getDescription())

    def testSetLogin(self):
        user = self._user
        user.setLogin('srichter2')
        self.assertEqual('srichter', user.getLogin())

    def testSetPassword(self):
        user = self._user
        user.setPassword('hello2')
        self.assertEqual(1, user.validate('hello2'))


def test_suite():
    return TestSuite((
        makeSuite(UserTest),
        ))

if __name__=='__main__':
    main(defaultTest='test_suite')