File: test_tools.py

package info (click to toggle)
pwman3 0.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 636 kB
  • sloc: python: 3,421; makefile: 215; sh: 61; javascript: 6
file content (140 lines) | stat: -rw-r--r-- 3,805 bytes parent folder | download
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
import os
import os.path
import shutil
import sys

if sys.version_info.major > 2:  # pragma: no cover
    from urllib.parse import urlparse
else:
    from urlparse import urlparse

from pwman.data import factory
from pwman.util import config
from pwman.util.callback import Callback


def get_ui_platform(platform):  # pragma: no cover
    if 'darwin' in platform:
        from pwman.ui.mac import PwmanCliMac as PwmanCli
        OSX = True
    elif 'win' in platform:
        from pwman.ui.win import PwmanCliWin as PwmanCli
        OSX = False
    else:
        from pwman.ui.cli import PwmanCli
        OSX = False

    return PwmanCli, OSX

PwmanCliNew, OSX = get_ui_platform(sys.platform)


class DummyCallback(Callback):

    def getinput(self, question):
        return b'12345'

    def getsecret(self, question):
        return b'12345'


class DummyCallback2(Callback):

    def getinput(self, question):
        return b'newsecret'

    def getsecret(self, question):
        return b'wrong'


class DummyCallback3(Callback):

    def getinput(self, question):
        return b'newsecret'

    def getsecret(self, question):
        ans = b'12345'
        return ans


class DummyCallback4(Callback):

    def getinput(self, question):
        return b'newsecret'

    def getsecret(self, question):
        return b'newsecret'

db =  ".".join(("pwman","test", sys.version.split(" " ,1)[0], "db"))
testdb = os.path.abspath(os.path.join(os.path.dirname(__file__), db))

config.default_config['Database'] = {'type': 'sqlite',
                                     'filename': testdb,
                                     'dburi': os.path.join(
                                         'sqlite:///', testdb)
                                     }

dc = """
[Global]
xsel = /usr/bin/xsel
colors = yes
umask = 0100
cls_timeout = 5

[Database]
type = SQLite
"""


class SetupTester(object):

    def __init__(self, dbver=None, filename=None, dburi=None):

        f = open(os.path.join(os.path.dirname(__file__), 'test.conf'), 'w')
        f.write(dc)
        f.close()
        self.configp = config.Config(os.path.join(os.path.dirname(__file__),
                                                  "test.conf"),
                                     config.default_config)

        self.configp.set_value('Database', 'dburi',
                               'sqlite://' + testdb
                               )

        self.configp.set_value('Global', 'cls_timeout', "-1")

        if not OSX:
            self.xselpath = shutil.which("xsel") or ""
            self.configp.set_value("Global", "xsel", self.xselpath)
        else:
            self.xselpath = "xsel"

        self.dbver = dbver
        self.dburi = self.configp.get_value('Database', 'dburi')

    def clean(self):
        dbfile = self.configp.get_value('Database', 'filename')
        dburi = urlparse(self.configp.get_value('Database', 'dburi')).path
        try:
            if os.path.exists(dbfile):
                os.remove(dbfile)

            if os.path.exists(dburi):
                os.remove(dburi)
        except PermissionError:
            pass

        if os.path.exists(os.path.join(os.path.dirname(__file__),
                                       'testing_config')):
            os.remove(os.path.join(os.path.dirname(__file__),
                                   'testing_config'))

        if os.path.exists(os.path.join(os.path.dirname(__file__),
                                       'test.conf')):
            os.remove(os.path.join(os.path.dirname(__file__),
                                   'test.conf'))

    def create(self):
        db = factory.createdb(self.dburi, self.dbver)
        self.cli = PwmanCliNew(db, self.xselpath, DummyCallback,
                               config_parser=self.configp)