unittests.py :  » Network » Pyzor » pyzor-0.5.0 » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Pyzor 
Pyzor » pyzor 0.5.0 » unittests.py
import sys
import unittest
import StringIO

sys.path.insert(0, './lib')
from pyzor import *
from pyzor.server import *
from pyzor.client import *

__revision__ = "$Id: unittests.py,v 1.8 2002-09-07 22:45:03 ftobin Exp $"


class ACLTest(unittest.TestCase):
    def setUp(self):
        access_file = AccessFile(StringIO.StringIO("""check : alice : allow
        # comment
        check : bob : deny
        report check : all : allow
        ping : all : deny
        all : charlie : allow
        all : all : allow
        all : all : deny
        """))

        self.acl = ACL()
        access_file.feed_into(self.acl)

    def test_basic_allow(self):
        self.assert_(self.acl.allows(Username('alice'), Opname('check')))

    def test_basic_deny(self):
        self.assert_(not self.acl.allows(Username('bob'), Opname('check')))

    def test_all_user_allow(self):
        self.assert_(self.acl.allows(Username('dennis'), Opname('report')))
        self.assert_(self.acl.allows(Username('bob'), Opname('report')))

    def test_all_user_deny(self):
        self.assert_(not self.acl.allows(Username('alice'), Opname('ping')))
        self.assert_(not self.acl.allows(Username('frank'), Opname('ping')))

    def test_allow_user_all_ops(self):
        self.assert_(self.acl.allows(Username('charlie'), Opname('check')))
        self.assert_(self.acl.allows(Username('charlie'), Opname('foobar')))
        
    def test_all_allowed_all(self):
        self.assert_(self.acl.allows(Username('giggles'), Opname('report')))
        self.assert_(self.acl.allows(Username('zoe'), Opname('foobar')))



class PasswdTest(unittest.TestCase):
    def setUp(self):
        passwd_file = PasswdFile(StringIO.StringIO("""alice:5
        bob:b
        charlie:cc
        """))

        self.passwd = Passwd()
        for u,k in passwd_file:
            self.passwd[u] = k
        
    def test_keys(self):
        self.assertEquals(self.passwd[Username('alice')], 5L)
        self.assertEquals(self.passwd[Username('bob')], 11L)
        self.assertEquals(self.passwd[Username('charlie')], 204L)

    def test_no_user(self):
        self.assert_(not self.passwd.has_key(Username('foobar')))


class AcountInfoTest(unittest.TestCase):
    def setUp(self):
        account_file = AccountsFile(StringIO.StringIO("""127.0.0.0 : 3333 : alice : 5,a
        # comment
        127.0.0.1 : 4444 : bob : ,18
        """))
        
##        # For testing in the future
##        127.0.0.1 : 4445 : charlie : c,
##        127.0.0.1 : 4446 : david : ,

        self.accounts = AccountsDict()

        for addr, acc in account_file:
            self.accounts[addr] = acc

    def test_full_key(self):
        self.assertEquals(self.accounts[Address(('127.0.0.0', 3333))],
                          Account((Username('alice'),
                                   Keystuff((5L, 10L)))))

    def test_only_key(self):
        self.assertEquals(self.accounts[Address(('127.0.0.1', 4444))],
                          Account((Username('bob'),
                                   Keystuff((None, 24L)))))

##    def test_only_salt(self):
##       self.assertEquals(self.accounts[Address(('127.0.0.1', 4445))],
##                         Account((Username('charlie'),
##                                  Keystuff((None, 12)))))
        

##    def test_neither(self):
##        self.assertEquals(self.accounts[Address(('127.0.0.1', 4446))],
##                          Account((Username('david'),
##                                   Keystuff((None, 24)))))



class KeystuffTest(unittest.TestCase):
    def test_full_stuff(self):
        self.assertEquals(Keystuff.from_hexstr("10,ab"),
                          Keystuff((16L, 171L)))

    def test_only_key(self):
        self.assertEquals(Keystuff.from_hexstr(",ab"),
                          Keystuff((None, 171L)))



class ServerListTest(unittest.TestCase):
    def setUp(self):
        self.sl = ServerList()

        self.sl.read(StringIO.StringIO("""127.0.0.1:4444
        # comment
        127.0.0.2:1234
        """))

    def test_sl_length(self):
        self.assertEquals(len(self.sl), 2)

    def test_entries(self):
        self.assert_(Address(('127.0.0.1', 4444)) in self.sl)
        self.assert_(Address(('127.0.0.2', 1234)) in self.sl)



class DataDigestTest(unittest.TestCase):
    def test_ptrns(self):
        norm = DataDigester.normalize
        self.assertEqual(norm('aaa me@example.com bbb'), 'aaabbb')
        self.assertEqual(norm('aaa http://www.example.com/ bbb'), 'aaabbb')
        self.assertEqual(norm('aaa Supercalifragilisticexpialidocious bbb'),
                         'aaabbb')
        self.assertEqual(norm('aaa  bbb  ccc\n'), 'aaabbbccc')
        self.assertEqual(norm('aaa <! random tag > bbb'), 'aaabbb')

    def test_should_handle_line(self):
        min_len = int(DataDigester.min_line_length)
        self.assert_(DataDigester.should_handle_line('a' * min_len))
        self.assert_(not DataDigester.should_handle_line('a' * (min_len-1)))


    def test_atomicness(self):
        self.assert_(DataDigester(StringIO.StringIO("""
        It is contrary to reasoning to say that there is a vacuum or space in
        which there is absolutely nothing.
                -- Descartes
        """),
                                  ExecCall.digest_spec,
                                  seekable=True).is_atomic())

    def test_non_atomicness(self):
        self.assert_(not DataDigester(StringIO.StringIO("""
        If builders built buildings the way programmers write programs,
        Jolt Cola would be a Fortune-500 company.

        If builders built buildings the way programmers write programs,
        you'd be able to buy a nice little colonial split-level at
        Babbages for $34.95.

        If programmers wrote programs the way builders build buildings,
        we'd still be using autocoder and running compile decks.

        ---

        Love is always open arms.  With arms open you allow love to come and
        go as it wills, freely, for it will do so anyway.  If you close your
        arms about love you'll find you are left only holding yourself.
        """),
                                      ExecCall.digest_spec,
                                      seekable=True).is_atomic())


class rfc822BodyCleanerTest(unittest.TestCase):
    def test_cleaning(self):
        expected = open('t/multipart.expected')
        for line in rfc822BodyCleaner(open('t/multipart')):
            self.assertEqual(line, expected.readline())



if __name__ == "__main__":
    unittest.main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.