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
1=== added file 'tests/autopilot/reminders/credentials.py'
2--- tests/autopilot/reminders/credentials.py 1970-01-01 00:00:00 +0000
3+++ tests/autopilot/reminders/credentials.py 2014-05-08 16:40:22 +0000
4@@ -0,0 +1,131 @@
5+# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
6+#
7+# Copyright (C) 2014 Canonical Ltd.
8+#
9+# This program is free software; you can redistribute it and/or modify
10+# it under the terms of the GNU General Public License version 3, as published
11+# by the Free Software Foundation.
12+#
13+# This program is distributed in the hope that it will be useful,
14+# but WITHOUT ANY WARRANTY; without even the implied warranty of
15+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+# GNU General Public License for more details.
17+#
18+# You should have received a copy of the GNU General Public License
19+# along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
21+
22+import threading
23+
24+from gi.repository import Accounts, GLib, Signon
25+
26+
27+class CredentialsException(Exception):
28+ """Exception for credentials problems."""
29+
30+
31+class AccountManager(object):
32+
33+ def __init__(self):
34+ self._manager = Accounts.Manager()
35+
36+ def _start_main_loop(self):
37+ self.error = None
38+ self._main_loop = GLib.MainLoop()
39+ self._main_loop_thread = threading.Thread(
40+ target=self._main_loop.run)
41+ self._main_loop_thread.start()
42+
43+ def _join_main_loop(self):
44+ self._main_loop_thread.join()
45+ if self.error is not None:
46+ raise CredentialsException(self.error.message)
47+
48+ def add_evernote_credentials(self, user_name, password):
49+ self._start_main_loop()
50+
51+ account = self._create_account()
52+
53+ info = self._get_identity_info(user_name, password)
54+
55+ identity = Signon.Identity.new()
56+ identity.store_credentials_with_info(
57+ info, self._set_credentials_id_to_account, account)
58+
59+ self._join_main_loop()
60+
61+ self._enable_evernote_service(account)
62+
63+ return account
64+
65+ def _create_account(self):
66+ account = self._manager.create_account('evernote')
67+ account.set_enabled(True)
68+ account.store(self._on_account_created, None)
69+ return account
70+
71+ def _on_account_created(self, account, error, _):
72+ if error:
73+ self.error = error
74+ self._main_loop.quit()
75+
76+ def _get_identity_info(self, user_name, password):
77+ info = Signon.IdentityInfo.new()
78+ info.set_username(user_name)
79+ info.set_caption(user_name)
80+ info.set_secret(password, True)
81+ return info
82+
83+ def _set_credentials_id_to_account(self, identity, id, error, account):
84+ if error:
85+ self.error = error
86+ self._main_loop.quit()
87+
88+ account.set_variant('CredentialsId', GLib.Variant('u', id))
89+ account.store(self._process_session, None)
90+
91+ def _process_session(self, account, error, _):
92+ if error:
93+ self.error = error
94+ self._main_loop.quit()
95+
96+ account_service = Accounts.AccountService.new(account, None)
97+ auth_data = account_service.get_auth_data()
98+ identity = auth_data.get_credentials_id()
99+ method = auth_data.get_method()
100+ mechanism = auth_data.get_mechanism()
101+ session_data = auth_data.get_parameters()
102+ oauth_token = (
103+ 'S=s1:U=8e6bf:E=14d08e375ff:C=145b1324a03:P=1cd:A=en-devtoken:'
104+ 'V=2:H=79b946c32b4515ee52b387f7b68baa69')
105+ session_data['ProvidedTokens'] = GLib.Variant('a{sv}', {
106+ 'TokenSecret': GLib.Variant('s', 'dummy'),
107+ 'AccessToken': GLib.Variant('s', oauth_token),
108+ })
109+ session = Signon.AuthSession.new(identity, method)
110+ session.process(
111+ session_data, mechanism, self._on_login_processed, None)
112+
113+ def _on_login_processed(self, session, reply, error, userdata):
114+ if error:
115+ self.error = error
116+
117+ self._main_loop.quit()
118+
119+ def _enable_evernote_service(self, account):
120+ service = self._manager.get_service('evernote')
121+ account.select_service(service)
122+ account.set_enabled(True)
123+ account.store(self._on_account_created, None)
124+
125+ def delete_account(self, account):
126+ self._start_main_loop()
127+ account.delete()
128+ account.store(self._on_account_deleted, None)
129+ self._join_main_loop()
130+
131+ def _on_account_deleted(self, account, error, userdata):
132+ if error:
133+ self.error = error
134+
135+ self._main_loop.quit()
136
137=== modified file 'tests/autopilot/reminders/tests/__init__.py'
138--- tests/autopilot/reminders/tests/__init__.py 2014-04-25 15:57:50 +0000
139+++ tests/autopilot/reminders/tests/__init__.py 2014-05-08 16:40:22 +0000
140@@ -17,7 +17,7 @@
141 """Reminders app autopilot tests."""
142
143 import os
144-import os.path
145+import shutil
146 import logging
147
148 import fixtures
149@@ -25,7 +25,10 @@
150 from autopilot.input import Mouse, Touch, Pointer
151 from autopilot.platform import model
152 from autopilot.testcase import AutopilotTestCase
153-from ubuntuuitoolkit import emulators as toolkit_emulators
154+from ubuntuuitoolkit import (
155+ emulators as toolkit_emulators,
156+ fixture_setup as toolkit_fixtures
157+)
158
159 import reminders
160
161@@ -41,22 +44,62 @@
162 else:
163 scenarios = [('with touch', dict(input_device_class=Touch))]
164
165- local_location_binary = '../../src/app/reminders'
166+ local_location = os.path.dirname(os.path.dirname(os.getcwd()))
167+ local_location_qml = local_location + "/reminders.qml"
168+ local_location_binary = os.path.join(local_location, 'src/app/reminders')
169 installed_location_binary = '/usr/bin/reminders'
170 installed_location_qml = '/usr/share/reminders/qml/reminders.qml'
171
172+ def setup_environment(self):
173+ if os.path.exists(self.local_location_qml):
174+ launch = self.launch_test_local
175+ test_type = 'local'
176+ elif os.path.exists(self.installed_location_qml):
177+ launch = self.launch_test_installed
178+ test_type = 'deb'
179+ else:
180+ launch = self.launch_test_click
181+ test_type = 'click'
182+ return launch, test_type
183+
184 def setUp(self):
185+ launch, self.test_type = self.setup_environment()
186+ self.home_dir = self._patch_home()
187 self.pointing_device = Pointer(self.input_device_class.create())
188 super(RemindersAppTestCase, self).setUp()
189
190- if os.path.exists(self.local_location_binary):
191- app_proxy = self.launch_test_local()
192- elif os.path.exists(self.installed_location_binary):
193- app_proxy = self.launch_test_installed()
194+ self.app = reminders.RemindersApp(launch())
195+
196+ def _patch_home(self):
197+ """ mock /home for testing purposes to preserve user data
198+ """
199+ temp_dir_fixture = fixtures.TempDir()
200+ self.useFixture(temp_dir_fixture)
201+ temp_dir = temp_dir_fixture.path
202+
203+ #click requires using initctl env (upstart), but the desktop can set
204+ #an environment variable instead
205+ if self.test_type == 'click':
206+ self.useFixture(toolkit_fixtures.InitctlEnvironmentVariable(
207+ HOME=temp_dir))
208 else:
209- app_proxy = self.launch_test_click()
210-
211- self.app = reminders.RemindersApp(app_proxy)
212+ self.useFixture(fixtures.EnvironmentVariable('HOME',
213+ newvalue=temp_dir))
214+
215+ #If running under xvfb, as jenkins does,
216+ #xsession will fail to start without xauthority file
217+ #Thus if the Xauthority file is in home directory
218+ #make sure we copy it to temp home, otherwise do nothing
219+ xauth = os.path.expanduser(os.path.join('~', '.Xauthority'))
220+ if os.path.isfile(xauth):
221+ logger.debug("Copying .Xauthority to fake home " + temp_dir)
222+ shutil.copyfile(
223+ os.path.expanduser(os.path.join('~', '.Xauthority')),
224+ os.path.join(temp_dir, '.Xauthority'))
225+
226+ logger.debug("Patched home to fake home directory " + temp_dir)
227+
228+ return temp_dir
229
230 @autopilot_logging.log_action(logger.info)
231 def launch_test_local(self):
232@@ -64,6 +107,7 @@
233 'QML2_IMPORT_PATH', newvalue='../../src/plugin'))
234 return self.launch_test_application(
235 self.local_location_binary,
236+ '-q', self.local_location_qml,
237 app_type='qt',
238 emulator_base=toolkit_emulators.UbuntuUIToolkitEmulatorBase)
239
240
241=== modified file 'tests/autopilot/reminders/tests/test_reminders.py'
242--- tests/autopilot/reminders/tests/test_reminders.py 2014-04-24 19:33:26 +0000
243+++ tests/autopilot/reminders/tests/test_reminders.py 2014-05-08 16:40:22 +0000
244@@ -23,9 +23,10 @@
245 from autopilot import platform
246 from autopilot.matchers import Eventually
247 from testtools.matchers import Equals
248+from testtools import ExpectedException
249
250 import reminders
251-from reminders import fixture_setup, tests
252+from reminders import credentials, fixture_setup, tests
253
254
255 logger = logging.getLogger(__name__)
256@@ -40,7 +41,7 @@
257 def test_go_to_account_settings(self):
258 """Test that the Go to account settings button calls url-dispatcher."""
259 if platform.model() == 'Desktop':
260- self.skipTest("URL dispatcher doesn't work on the desktop.")
261+ self.skipTest("URL dispatcher doesn't work on the desktop.")
262 url_dispatcher = fixture_setup.FakeURLDispatcher()
263 self.useFixture(url_dispatcher)
264
265@@ -56,3 +57,21 @@
266 self.assertThat(
267 get_last_dispatch_url_call_parameter,
268 Eventually(Equals('settings:///system/online-accounts')))
269+
270+
271+class RemindersTestCaseWithAccount(tests.RemindersAppTestCase):
272+
273+ def setUp(self):
274+ self.add_evernote_credentials()
275+ super(RemindersTestCaseWithAccount, self).setUp()
276+
277+ def add_evernote_credentials(self):
278+ account_manager = credentials.AccountManager()
279+ account = account_manager.add_evernote_credentials(
280+ 'u1test@canonical.com', 'password')
281+ self.addCleanup(account_manager.delete_account, account)
282+
283+ def test_open_application_with_account(self):
284+ """Test that the No account dialog is not visible."""
285+ with ExpectedException(reminders.RemindersAppException):
286+ self.app.main_view.no_account_dialog

Subscribers

People subscribed via source and target branches