Merge lp:~nskaggs/reminders-app/setup-ap-env into lp:reminders-app

Proposed by Nicholas Skaggs
Status: Superseded
Proposed branch: lp:~nskaggs/reminders-app/setup-ap-env
Merge into: lp:reminders-app
Diff against target: 286 lines (+206/-12)
3 files modified
tests/autopilot/reminders/credentials.py (+131/-0)
tests/autopilot/reminders/tests/__init__.py (+54/-10)
tests/autopilot/reminders/tests/test_reminders.py (+21/-2)
To merge this branch: bzr merge lp:~nskaggs/reminders-app/setup-ap-env
Reviewer Review Type Date Requested Status
Ubuntu Notes app developers Pending
Review via email: mp+218843@code.launchpad.net

Commit message

setup safe AP env for testing

Description of the change

Properly setup environment for autopilot testing by using a faked /home directory on phablet and desktop

To post a comment you must log in.
125. By Nicholas Skaggs

add _copy_xauthority_file rename setup env

Unmerged revisions

125. By Nicholas Skaggs

add _copy_xauthority_file rename setup env

124. By Nicholas Skaggs

switch to fixtures

123. By Nicholas Skaggs

setup env properly for test run

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'tests/autopilot/reminders/credentials.py'
--- tests/autopilot/reminders/credentials.py 1970-01-01 00:00:00 +0000
+++ tests/autopilot/reminders/credentials.py 2014-05-08 16:40:22 +0000
@@ -0,0 +1,131 @@
1# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2#
3# Copyright (C) 2014 Canonical Ltd.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License version 3, as published
7# by the Free Software Foundation.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
18import threading
19
20from gi.repository import Accounts, GLib, Signon
21
22
23class CredentialsException(Exception):
24 """Exception for credentials problems."""
25
26
27class AccountManager(object):
28
29 def __init__(self):
30 self._manager = Accounts.Manager()
31
32 def _start_main_loop(self):
33 self.error = None
34 self._main_loop = GLib.MainLoop()
35 self._main_loop_thread = threading.Thread(
36 target=self._main_loop.run)
37 self._main_loop_thread.start()
38
39 def _join_main_loop(self):
40 self._main_loop_thread.join()
41 if self.error is not None:
42 raise CredentialsException(self.error.message)
43
44 def add_evernote_credentials(self, user_name, password):
45 self._start_main_loop()
46
47 account = self._create_account()
48
49 info = self._get_identity_info(user_name, password)
50
51 identity = Signon.Identity.new()
52 identity.store_credentials_with_info(
53 info, self._set_credentials_id_to_account, account)
54
55 self._join_main_loop()
56
57 self._enable_evernote_service(account)
58
59 return account
60
61 def _create_account(self):
62 account = self._manager.create_account('evernote')
63 account.set_enabled(True)
64 account.store(self._on_account_created, None)
65 return account
66
67 def _on_account_created(self, account, error, _):
68 if error:
69 self.error = error
70 self._main_loop.quit()
71
72 def _get_identity_info(self, user_name, password):
73 info = Signon.IdentityInfo.new()
74 info.set_username(user_name)
75 info.set_caption(user_name)
76 info.set_secret(password, True)
77 return info
78
79 def _set_credentials_id_to_account(self, identity, id, error, account):
80 if error:
81 self.error = error
82 self._main_loop.quit()
83
84 account.set_variant('CredentialsId', GLib.Variant('u', id))
85 account.store(self._process_session, None)
86
87 def _process_session(self, account, error, _):
88 if error:
89 self.error = error
90 self._main_loop.quit()
91
92 account_service = Accounts.AccountService.new(account, None)
93 auth_data = account_service.get_auth_data()
94 identity = auth_data.get_credentials_id()
95 method = auth_data.get_method()
96 mechanism = auth_data.get_mechanism()
97 session_data = auth_data.get_parameters()
98 oauth_token = (
99 'S=s1:U=8e6bf:E=14d08e375ff:C=145b1324a03:P=1cd:A=en-devtoken:'
100 'V=2:H=79b946c32b4515ee52b387f7b68baa69')
101 session_data['ProvidedTokens'] = GLib.Variant('a{sv}', {
102 'TokenSecret': GLib.Variant('s', 'dummy'),
103 'AccessToken': GLib.Variant('s', oauth_token),
104 })
105 session = Signon.AuthSession.new(identity, method)
106 session.process(
107 session_data, mechanism, self._on_login_processed, None)
108
109 def _on_login_processed(self, session, reply, error, userdata):
110 if error:
111 self.error = error
112
113 self._main_loop.quit()
114
115 def _enable_evernote_service(self, account):
116 service = self._manager.get_service('evernote')
117 account.select_service(service)
118 account.set_enabled(True)
119 account.store(self._on_account_created, None)
120
121 def delete_account(self, account):
122 self._start_main_loop()
123 account.delete()
124 account.store(self._on_account_deleted, None)
125 self._join_main_loop()
126
127 def _on_account_deleted(self, account, error, userdata):
128 if error:
129 self.error = error
130
131 self._main_loop.quit()
0132
=== modified file 'tests/autopilot/reminders/tests/__init__.py'
--- tests/autopilot/reminders/tests/__init__.py 2014-04-25 15:57:50 +0000
+++ tests/autopilot/reminders/tests/__init__.py 2014-05-08 16:40:22 +0000
@@ -17,7 +17,7 @@
17"""Reminders app autopilot tests."""17"""Reminders app autopilot tests."""
1818
19import os19import os
20import os.path20import shutil
21import logging21import logging
2222
23import fixtures23import fixtures
@@ -25,7 +25,10 @@
25from autopilot.input import Mouse, Touch, Pointer25from autopilot.input import Mouse, Touch, Pointer
26from autopilot.platform import model26from autopilot.platform import model
27from autopilot.testcase import AutopilotTestCase27from autopilot.testcase import AutopilotTestCase
28from ubuntuuitoolkit import emulators as toolkit_emulators28from ubuntuuitoolkit import (
29 emulators as toolkit_emulators,
30 fixture_setup as toolkit_fixtures
31)
2932
30import reminders33import reminders
3134
@@ -41,22 +44,62 @@
41 else:44 else:
42 scenarios = [('with touch', dict(input_device_class=Touch))]45 scenarios = [('with touch', dict(input_device_class=Touch))]
4346
44 local_location_binary = '../../src/app/reminders'47 local_location = os.path.dirname(os.path.dirname(os.getcwd()))
48 local_location_qml = local_location + "/reminders.qml"
49 local_location_binary = os.path.join(local_location, 'src/app/reminders')
45 installed_location_binary = '/usr/bin/reminders'50 installed_location_binary = '/usr/bin/reminders'
46 installed_location_qml = '/usr/share/reminders/qml/reminders.qml'51 installed_location_qml = '/usr/share/reminders/qml/reminders.qml'
4752
53 def setup_environment(self):
54 if os.path.exists(self.local_location_qml):
55 launch = self.launch_test_local
56 test_type = 'local'
57 elif os.path.exists(self.installed_location_qml):
58 launch = self.launch_test_installed
59 test_type = 'deb'
60 else:
61 launch = self.launch_test_click
62 test_type = 'click'
63 return launch, test_type
64
48 def setUp(self):65 def setUp(self):
66 launch, self.test_type = self.setup_environment()
67 self.home_dir = self._patch_home()
49 self.pointing_device = Pointer(self.input_device_class.create())68 self.pointing_device = Pointer(self.input_device_class.create())
50 super(RemindersAppTestCase, self).setUp()69 super(RemindersAppTestCase, self).setUp()
5170
52 if os.path.exists(self.local_location_binary):71 self.app = reminders.RemindersApp(launch())
53 app_proxy = self.launch_test_local()72
54 elif os.path.exists(self.installed_location_binary):73 def _patch_home(self):
55 app_proxy = self.launch_test_installed()74 """ mock /home for testing purposes to preserve user data
75 """
76 temp_dir_fixture = fixtures.TempDir()
77 self.useFixture(temp_dir_fixture)
78 temp_dir = temp_dir_fixture.path
79
80 #click requires using initctl env (upstart), but the desktop can set
81 #an environment variable instead
82 if self.test_type == 'click':
83 self.useFixture(toolkit_fixtures.InitctlEnvironmentVariable(
84 HOME=temp_dir))
56 else:85 else:
57 app_proxy = self.launch_test_click()86 self.useFixture(fixtures.EnvironmentVariable('HOME',
5887 newvalue=temp_dir))
59 self.app = reminders.RemindersApp(app_proxy)88
89 #If running under xvfb, as jenkins does,
90 #xsession will fail to start without xauthority file
91 #Thus if the Xauthority file is in home directory
92 #make sure we copy it to temp home, otherwise do nothing
93 xauth = os.path.expanduser(os.path.join('~', '.Xauthority'))
94 if os.path.isfile(xauth):
95 logger.debug("Copying .Xauthority to fake home " + temp_dir)
96 shutil.copyfile(
97 os.path.expanduser(os.path.join('~', '.Xauthority')),
98 os.path.join(temp_dir, '.Xauthority'))
99
100 logger.debug("Patched home to fake home directory " + temp_dir)
101
102 return temp_dir
60103
61 @autopilot_logging.log_action(logger.info)104 @autopilot_logging.log_action(logger.info)
62 def launch_test_local(self):105 def launch_test_local(self):
@@ -64,6 +107,7 @@
64 'QML2_IMPORT_PATH', newvalue='../../src/plugin'))107 'QML2_IMPORT_PATH', newvalue='../../src/plugin'))
65 return self.launch_test_application(108 return self.launch_test_application(
66 self.local_location_binary,109 self.local_location_binary,
110 '-q', self.local_location_qml,
67 app_type='qt',111 app_type='qt',
68 emulator_base=toolkit_emulators.UbuntuUIToolkitEmulatorBase)112 emulator_base=toolkit_emulators.UbuntuUIToolkitEmulatorBase)
69113
70114
=== modified file 'tests/autopilot/reminders/tests/test_reminders.py'
--- tests/autopilot/reminders/tests/test_reminders.py 2014-04-24 19:33:26 +0000
+++ tests/autopilot/reminders/tests/test_reminders.py 2014-05-08 16:40:22 +0000
@@ -23,9 +23,10 @@
23from autopilot import platform23from autopilot import platform
24from autopilot.matchers import Eventually24from autopilot.matchers import Eventually
25from testtools.matchers import Equals25from testtools.matchers import Equals
26from testtools import ExpectedException
2627
27import reminders28import reminders
28from reminders import fixture_setup, tests29from reminders import credentials, fixture_setup, tests
2930
3031
31logger = logging.getLogger(__name__)32logger = logging.getLogger(__name__)
@@ -40,7 +41,7 @@
40 def test_go_to_account_settings(self):41 def test_go_to_account_settings(self):
41 """Test that the Go to account settings button calls url-dispatcher."""42 """Test that the Go to account settings button calls url-dispatcher."""
42 if platform.model() == 'Desktop':43 if platform.model() == 'Desktop':
43 self.skipTest("URL dispatcher doesn't work on the desktop.")44 self.skipTest("URL dispatcher doesn't work on the desktop.")
44 url_dispatcher = fixture_setup.FakeURLDispatcher()45 url_dispatcher = fixture_setup.FakeURLDispatcher()
45 self.useFixture(url_dispatcher)46 self.useFixture(url_dispatcher)
4647
@@ -56,3 +57,21 @@
56 self.assertThat(57 self.assertThat(
57 get_last_dispatch_url_call_parameter,58 get_last_dispatch_url_call_parameter,
58 Eventually(Equals('settings:///system/online-accounts')))59 Eventually(Equals('settings:///system/online-accounts')))
60
61
62class RemindersTestCaseWithAccount(tests.RemindersAppTestCase):
63
64 def setUp(self):
65 self.add_evernote_credentials()
66 super(RemindersTestCaseWithAccount, self).setUp()
67
68 def add_evernote_credentials(self):
69 account_manager = credentials.AccountManager()
70 account = account_manager.add_evernote_credentials(
71 'u1test@canonical.com', 'password')
72 self.addCleanup(account_manager.delete_account, account)
73
74 def test_open_application_with_account(self):
75 """Test that the No account dialog is not visible."""
76 with ExpectedException(reminders.RemindersAppException):
77 self.app.main_view.no_account_dialog

Subscribers

People subscribed via source and target branches