~mvo/software-center/fix-index-update-terms-bug

2389 by Michael Vogt
add license-key backend test
1
import os
2
import unittest
3
3020.1.1 by Natalia B. Bidart
- Initial test cleanup:
4
from gi.repository import GObject
2389 by Michael Vogt
add license-key backend test
5
from mock import Mock
6
3020.1.1 by Natalia B. Bidart
- Initial test cleanup:
7
from tests.utils import (
8
    do_events_with_sleep,
9
    setup_test_env,
10
)
2653.1.2 by Michael Vogt
further cleanup to avoid fiddling with sys.path in every test file
11
setup_test_env()
2389 by Michael Vogt
add license-key backend test
12
from softwarecenter.db.application import Application
13
from softwarecenter.backend import get_install_backend
14
15
16
class TestPurchaseBackend(unittest.TestCase):
3020.1.1 by Natalia B. Bidart
- Initial test cleanup:
17
2389 by Michael Vogt
add license-key backend test
18
    PKGNAME = "hello-license-key-x"
19
    LICENSE_KEY = "license-key-data"
20
    # this must match the binary deb (XB-LicenseKeyPath)
21
    LICENSE_KEY_PATH = "/opt/hello-license-key-x/mykey.txt"
22
23
    def test_add_license_key_backend(self):
24
        self._finished = False
25
        # add repo
26
        deb_line = "deb https://mvo:nopassyet@private-ppa.launchpad.net/canonical-isd-hackers/internal-qa/ubuntu oneiric main"
27
        signing_key_id = "F5410BE0"
28
        app = Application("Test app1", self.PKGNAME)
29
        # install only when runnig as root, as we require polkit promtps
30
        # otherwise
31
        # FIXME: provide InstallBackendSimulate()
32
        if os.getuid() == 0:
33
            backend = get_install_backend()
34
            backend.ui = Mock()
3020.1.1 by Natalia B. Bidart
- Initial test cleanup:
35
            backend.connect("transaction-finished",
2389 by Michael Vogt
add license-key backend test
36
                            self._on_transaction_finished)
37
            # simulate repos becomes available for the public 20 s later
38
            GObject.timeout_add_seconds(20, self._add_pw_to_commercial_repo)
39
            # run it
40
            backend.add_repo_add_key_and_install_app(deb_line,
41
                                                     signing_key_id,
42
                                                     app,
43
                                                     "icon",
44
                                                     self.LICENSE_KEY)
45
            # wait until the pkg is installed
46
            while not self._finished:
3020.1.1 by Natalia B. Bidart
- Initial test cleanup:
47
                do_events_with_sleep()
48
2543 by Michael Vogt
test fixes, remove unused/broken ReviewsJsonLoader code, some pyflakes fixes
49
        if os.getuid() == 0:
50
            self.assertTrue(os.path.exists(self.LICENSE_KEY_PATH))
51
            self.assertEqual(open(self.LICENSE_KEY_PATH).read(), self.LICENSE_KEY)
2389 by Michael Vogt
add license-key backend test
52
        #time.sleep(10)
3020.1.1 by Natalia B. Bidart
- Initial test cleanup:
53
2389 by Michael Vogt
add license-key backend test
54
    def _add_pw_to_commercial_repo(self):
55
        print "making pw available now"
56
        path="/etc/apt/sources.list.d/private-ppa.launchpad.net_canonical-isd-hackers_internal-qa_ubuntu.list"
57
        content= open(path).read()
58
        passw = os.environ.get("SC_PASS") or "pass"
59
        content = content.replace("nopassyet", passw)
60
        open(path, "w").write(content)
61
62
    def _on_transaction_finished(self, backend, result):
63
        print "_on_transaction_finished", result.pkgname, result.success
64
        if not result.pkgname:
65
            return
66
        print "done", result.pkgname
67
        self._finished = True
68
        self.assertTrue(result.success)
69
3020.1.1 by Natalia B. Bidart
- Initial test cleanup:
70
2389 by Michael Vogt
add license-key backend test
71
if __name__ == "__main__":
72
    unittest.main()