~divmod-dev/divmod.org/1304710-storeless-adapter

« back to all changes in this revision

Viewing changes to Axiom/axiom/test/test_userbase.py

  • Committer: glyph
  • Date: 2005-07-28 22:09:16 UTC
  • Revision ID: svn-v4:866e43f7-fbfc-0310-8f2a-ec88d1da2979:trunk:2
move this repository to a more official-looking URL

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
from zope.interface import Interface, implements
 
3
from twisted.trial import unittest
 
4
 
 
5
from twisted.cred.portal import Portal, IRealm
 
6
from twisted.cred.checkers import ICredentialsChecker
 
7
from twisted.cred.credentials import UsernamePassword
 
8
 
 
9
from axiom.store import Store
 
10
from axiom.userbase import LoginAccount, LoginSystem
 
11
from axiom.item import Item
 
12
from axiom.attributes import integer
 
13
from axiom.scripts import axiomatic
 
14
 
 
15
 
 
16
class IGarbage(Interface):
 
17
    pass
 
18
 
 
19
class GarbageProtocolHandler(Item):
 
20
    schemaVersion = 1
 
21
    typeName = 'test_login_garbage'
 
22
 
 
23
    garbage = integer()
 
24
 
 
25
    implements(IGarbage)
 
26
 
 
27
    def install(self):
 
28
        self.store.powerUp(self, IGarbage)
 
29
 
 
30
SECRET = 'bananas'
 
31
 
 
32
class UserBaseTest(unittest.TestCase):
 
33
 
 
34
    def logInAndCheck(self, username, domain='localhost'):
 
35
        s = Store(self.mktemp())
 
36
        def _speedup():
 
37
            l = LoginSystem(store=s)
 
38
            l.install()
 
39
            s.checkpoint()
 
40
            p = Portal(IRealm(s),
 
41
                       [ICredentialsChecker(s)])
 
42
 
 
43
            a = l.addAccount(username, 'localhost', SECRET)
 
44
            gph = GarbageProtocolHandler(store=a.avatars.open(),
 
45
                                         garbage=0)
 
46
            gph.install()
 
47
            return p, gph
 
48
 
 
49
        p, gph = s.transact(_speedup)
 
50
 
 
51
        def wasItGph((interface, avatar, logout)):
 
52
            self.assertEquals(interface, IGarbage)
 
53
            self.assertEquals(avatar, gph)
 
54
            logout()
 
55
 
 
56
        return p.login(UsernamePassword('bob@localhost', SECRET), None, IGarbage
 
57
                       ).addCallback(wasItGph)
 
58
 
 
59
    def testBasicLogin(self):
 
60
        self.logInAndCheck('bob')
 
61
 
 
62
    def testUppercaseLogin(self):
 
63
        self.logInAndCheck('BOB')
 
64
 
 
65
    def testMixedCaseLogin(self):
 
66
        self.logInAndCheck('BoB')
 
67
 
 
68
 
 
69
class CommandTestCase(unittest.TestCase):
 
70
    def testUserBaseInstall(self):
 
71
        dbdir = self.mktemp()
 
72
        axiomatic.main([
 
73
                '-d', dbdir, 'userbase'])
 
74
 
 
75
        s = Store(dbdir)
 
76
        IRealm(s)
 
77
        ICredentialsChecker(s)
 
78
        s.close()
 
79
 
 
80
    def testUserCreation(self):
 
81
        dbdir = self.mktemp()
 
82
        axiomatic.main([
 
83
                '-d', dbdir, 'userbase',
 
84
                '-u', 'alice',
 
85
                '-d', 'localhost',
 
86
                '-p', SECRET])
 
87
 
 
88
        s = Store(dbdir)
 
89
        cc = ICredentialsChecker(s)
 
90
        p = Portal(IRealm(s), [cc])
 
91
 
 
92
        def cb((interface, avatar, logout)):
 
93
            logout()
 
94
 
 
95
        return p.login(UsernamePassword('alice@localhost', SECRET), None, lambda orig, default: orig
 
96
                       ).addCallback(cb)