FINALLY
[appstream:software-center.git] / test / gtk3 / test_purchase.py
1 #!/usr/bin/python
2
3 import time
4 import unittest
5
6 from mock import Mock, patch
7
8 from testutils import setup_test_env
9 setup_test_env()
10
11 from softwarecenter.testutils import do_events, get_mock_options
12 from softwarecenter.ui.gtk3.app import SoftwareCenterAppGtk3
13 from softwarecenter.ui.gtk3.panes.availablepane import AvailablePane
14 import softwarecenter.paths
15
16 class TestPurchase(unittest.TestCase):
17
18     def test_purchase_view_log_cleaner(self):
19         import softwarecenter.ui.gtk3.views.purchaseview
20         from softwarecenter.ui.gtk3.views.purchaseview import get_test_window_purchaseview
21         win = get_test_window_purchaseview()
22         self._p()
23         # get the view
24         view = win.get_data("view")
25         # install the mock
26         softwarecenter.ui.gtk3.views.purchaseview.LOG = mock = Mock()
27         # run a "harmless" log message and ensure its logged normally
28         view.wk.webkit.execute_script('console.log("foo")')
29         self.assertTrue("foo" in mock.debug.call_args[0][0])
30         mock.reset_mock()
31
32         # run a message that contains token info
33         s = 'http://sca.razorgirl.info/subscriptions/19077/checkout_complete/ @10: {"token_key": "hiddenXXXXXXXXXX", "consumer_secret": "hiddenXXXXXXXXXXXX", "api_version": 2.0, "subscription_id": 19077, "consumer_key": "rKhNPBw", "token_secret": "hiddenXXXXXXXXXXXXXXX"}'
34         view.wk.webkit.execute_script("console.log('%s')" % s)
35         self.assertTrue("skipping" in mock.debug.call_args[0][0])
36         self.assertFalse("consumer_secret" in mock.debug.call_args[0][0])
37         mock.reset_mock()
38
39         # run another one
40         win.destroy()
41
42     def test_purchase_view_tos(self):
43         from softwarecenter.ui.gtk3.views.purchaseview import get_test_window_purchaseview
44         win = get_test_window_purchaseview()
45         view = win.get_data("view")
46         # install the mock
47         mock_config = Mock()
48         mock_config.has_option.return_value = False
49         mock_config.getboolean.return_value = False
50         view.config = mock_config
51         func = "softwarecenter.ui.gtk3.views.purchaseview.show_accept_tos_dialog"
52         with patch(func) as mock_func:
53             mock_func.return_value = False
54             res = view.initiate_purchase(None, None)
55             self.assertFalse(res)
56             self.assertTrue(mock_func.called)
57         win.destroy()
58
59     def test_spinner_emits_signals(self):
60         from softwarecenter.ui.gtk3.views.purchaseview import get_test_window_purchaseview
61         win = get_test_window_purchaseview()
62         self._p()
63         # get the view
64         view = win.get_data("view")
65         # ensure "purchase-needs-spinner" signals are send
66         signal_mock = Mock()
67         view.connect("purchase-needs-spinner", signal_mock)
68         view.wk.webkit.load_uri("http://www.ubuntu.com/")
69         self._p()
70         self.assertTrue(signal_mock.called)
71         # run another one
72         win.destroy()
73
74     def test_reinstall_previous_purchase_display(self):
75         mock_options = get_mock_options()
76         xapiandb = "/var/cache/software-center/"
77         app = SoftwareCenterAppGtk3(
78             softwarecenter.paths.datadir, xapiandb, mock_options)
79         # real app opens cache async
80         app.cache.open()
81         # show it
82         app.window_main.show_all()
83         app.available_pane.init_view()
84         self._p()
85         app.on_menuitem_reinstall_purchases_activate(None)
86         # it can take a bit until the sso client is ready
87         for i in range(100):
88             if (app.available_pane.get_current_page() ==
89                 AvailablePane.Pages.LIST):
90                 break
91             self._p()
92         self.assertEqual(
93             app.available_pane.get_current_page(), AvailablePane.Pages.LIST)
94
95     def _p(self):
96         for i in range(5):
97             time.sleep(0.1)
98             do_events()
99
100 if __name__ == "__main__":
101     import logging
102     logging.basicConfig(level=logging.DEBUG)
103     unittest.main()