summaryrefslogtreecommitdiffstats
path: root/tests/unit/test-020-auth_cache.py
blob: 1332ab341e82ebbe3aefabfd0badd8654cba8fe6 (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
# -*- coding: utf-8 -*-

import unittest

import datetime
import os

from pykolab.auth.ldap import auth_cache
import pykolab
conf = pykolab.getConf()
conf.finalize_conf()

import sqlalchemy

from sqlalchemy import Column
from sqlalchemy import DateTime
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy import String
from sqlalchemy import Table
from sqlalchemy import Text

from sqlalchemy import desc
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import mapper

try:
    from sqlalchemy.orm import relationship
except:
    from sqlalchemy.orm import relation as relationship

try:
    from sqlalchemy.orm import sessionmaker
except:
    from sqlalchemy.orm import create_session

metadata = MetaData()

DeclarativeBase = declarative_base()


class Entry(DeclarativeBase):
    __tablename__ = 'entries'

    id = Column(Integer, primary_key=True)
    domain = Column(String(256), index=True, nullable=True)
    key = Column(Text, index=True, nullable=False)
    value = Column(Text, nullable=False)
    last_change = Column(DateTime, nullable=False, default=datetime.datetime.now())

    def __init__(self, key, value):
        self.key = key
        if not isinstance(value, unicode):
            self.value = unicode(value, 'utf-8')
        else:
            self.value = value

engine = create_engine('sqlite:////tmp/%s.db' % (os.getpid()), echo=False, echo_pool=False)
DeclarativeBase.metadata.create_all(engine)

Session = sessionmaker(bind=engine)
db = Session()

auth_cache.db = db


class TestAuthCache(unittest.TestCase):
    def test_001_plain_insert(self):
        auth_cache.set_entry(
                'somekey',
                'ou=People,dc=example,dc=org'
            )

        result = auth_cache.get_entry('somekey')
        self.assertEqual(result, 'ou=People,dc=example,dc=org')

    def test_002_plain_encoding_insert(self):
        auth_cache.set_entry(
                'somekey2',
                'ou=Geschäftsbereich,ou=People,dc=example,dc=org'
            )

        result = auth_cache.get_entry('somekey2')
        self.assertEqual(result, 'ou=Gesch\xc3\xa4ftsbereich,ou=People,dc=example,dc=org')

    def test_003_unicode_insert(self):
        auth_cache.set_entry(
                'somekey3',
                u'ou=Geschäftsbereich,ou=People,dc=example,dc=org'
            )

        result = auth_cache.get_entry('somekey3')
        self.assertEqual(result, 'ou=Gesch\xc3\xa4ftsbereich,ou=People,dc=example,dc=org')

    @unittest.skip("Double encoding or decoding")
    def test_004_unicode_escape(self):
        auth_cache.set_entry(
                'somekey4',
                u'ou=Gesch\xc3\xa4ftsbereich,ou=People,dc=example,dc=org'
            )

        result = auth_cache.get_entry('somekey4')
        self.assertEqual(result, u'ou=Gesch\xc3\xa4ftsbereich,ou=People,dc=example,dc=org')

    def test_005_longkey(self):
        auth_cache.set_entry(
                'v' + 'e'*512 + 'rylongkey',
                'v' + 'e'*512 + 'rylongvalue'
            )

        result = auth_cache.get_entry('v' + 'e'*512 + 'rylongkey')
        self.assertEqual(result, 'v' + 'e'*512 + 'rylongvalue')

    def test_006_plain_update(self):
        auth_cache.set_entry(
                'somekey',
                'ou=People,dc=example,dc=org2'
            )

        result = auth_cache.get_entry('somekey')
        self.assertEqual(result, 'ou=People,dc=example,dc=org2')

    def test_007_plain_encoding_update(self):
        auth_cache.set_entry(
                'somekey2',
                'ou=Geschäftsbereich,ou=People,dc=example,dc=org2'
            )

        result = auth_cache.get_entry('somekey2')
        self.assertEqual(result, 'ou=Gesch\xc3\xa4ftsbereich,ou=People,dc=example,dc=org2')

    def test_008_unicode_update(self):
        auth_cache.set_entry(
                'somekey3',
                u'ou=Geschäftsbereich,ou=People,dc=example,dc=org2'
            )

        result = auth_cache.get_entry('somekey3')
        self.assertEqual(result, 'ou=Gesch\xc3\xa4ftsbereich,ou=People,dc=example,dc=org2')

    @unittest.skip("Double encoding or decoding")
    def test_009_unicode_escape_update(self):
        auth_cache.set_entry(
                'somekey4',
                u'ou=Gesch\xc3\xa4ftsbereich,ou=People,dc=example,dc=org2'
            )

        result = auth_cache.get_entry('somekey4')
        self.assertEqual(result, u'ou=Gesch\xc3\xa4ftsbereich,ou=People,dc=example,dc=org2')

    def test_010_longkey_update(self):
        auth_cache.set_entry(
                'v' + 'e'*512 + 'rylongkey',
                'v' + 'e'*512 + 'rylongvalue2'
            )

        result = auth_cache.get_entry('v' + 'e'*512 + 'rylongkey')
        self.assertEqual(result, 'v' + 'e'*512 + 'rylongvalue2')