File: test_cache.py

package info (click to toggle)
udiskie 2.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 800 kB
  • sloc: python: 3,346; makefile: 16
file content (76 lines) | stat: -rw-r--r-- 2,176 bytes parent folder | download | duplicates (4)
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
"""
Tests for the udiskie.cache module.
"""

import unittest
import time

from udiskie.cache import PasswordCache


class TestDev:

    def __init__(self, id_uuid):
        self.id_uuid = id_uuid


class TestPasswordCache(unittest.TestCase):

    """
    Tests for the udiskie.cache.PasswordCache class.
    """

    # NOTE: The device names are different in each test so that they do not
    # interfere accidentally.

    def test_timeout(self):
        """The cached password expires after the specified timeout."""
        device = TestDev('ALPHA')
        password = '{<}hëllo ωορλδ!{>}'
        cache = PasswordCache(1)
        cache[device] = password
        self.assertEqual(cache[device], password.encode('utf-8'))
        time.sleep(1.5)
        with self.assertRaises(KeyError):
            cache[device]

    def test_touch(self):
        """Key access refreshes the timeout."""
        device = TestDev('BETA')
        password = '{<}hëllo ωορλδ!{>}'
        cache = PasswordCache(3)
        cache[device] = password
        time.sleep(2)
        self.assertEqual(cache[device], password.encode('utf-8'))
        time.sleep(2)
        self.assertEqual(cache[device], password.encode('utf-8'))
        time.sleep(4)
        with self.assertRaises(KeyError):
            cache[device]

    def test_revoke(self):
        """A key can be deleted manually."""
        device = TestDev('GAMMA')
        password = '{<}hëllo ωορλδ!{>}'
        cache = PasswordCache(0)
        cache[device] = password
        self.assertEqual(cache[device], password.encode('utf-8'))
        del cache[device]
        with self.assertRaises(KeyError):
            cache[device]

    def test_update(self):
        device = TestDev('DELTA')
        password = '{<}hëllo ωορλδ!{>}'
        cache = PasswordCache(0)
        cache[device] = password
        self.assertEqual(cache[device], password.encode('utf-8'))
        cache[device] = password * 2
        self.assertEqual(cache[device], password.encode('utf-8')*2)
        del cache[device]
        with self.assertRaises(KeyError):
            cache[device]


if __name__ == '__main__':
    unittest.main()