~rpadovani/reminders-app/fixForReload

« back to all changes in this revision

Viewing changes to tests/autopilot/reminders/credentials.py

  • Committer: Tarmac
  • Author(s): Leo Arias
  • Date: 2014-07-02 15:14:42 UTC
  • mfrom: (164.1.25 fix_with_account)
  • Revision ID: tarmac-20140702151442-ii7yu2u4v24l9a9q
Added logging messages and tests to the credentials autopilot tests helpers.

Approved by Ubuntu Phone Apps Jenkins Bot, David Planella.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
 
 
 
17
import logging
 
18
import time
18
19
import threading
19
20
 
20
21
from gi.repository import Accounts, GLib, Signon
21
22
 
22
23
 
 
24
logger = logging.getLogger(__name__)
 
25
 
 
26
 
23
27
class CredentialsException(Exception):
24
28
 
25
29
    """Exception for credentials problems."""
52
56
        :param oauth_token: The oauth token of the account.
53
57
 
54
58
        """
 
59
        logger.debug('Add an evernote account.')
55
60
        self._start_main_loop()
56
61
 
57
62
        account = self._create_account()
65
70
 
66
71
        self._join_main_loop()
67
72
 
 
73
        # XXX Sometimes, the account fails to be enabled. This sleep seems to
 
74
        # fix it but we haven't yet found the reason. --elopio - 2014-06-25
 
75
        time.sleep(10)
68
76
        self._enable_evernote_service(account)
69
77
 
 
78
        logger.info('Created the account with id: {}.'.format(account.id))
 
79
        self._log_accounts_info()
70
80
        return account
71
81
 
72
82
    def _create_account(self):
 
83
        logger.debug('Creating the Evernote account.')
73
84
        account = self._manager.create_account('evernote')
74
85
        account.set_enabled(True)
75
86
        account.store(self._on_account_created, None)
77
88
 
78
89
    def _on_account_created(self, account, error, _):
79
90
        if error:
80
 
            self.error = error
81
 
            self._main_loop.quit()
 
91
            self._quit_main_loop_on_error(error, 'storing account')
 
92
 
 
93
    def _quit_main_loop_on_error(self, error, step):
 
94
        logger.error('Error {}.'.format(step))
 
95
        self.error = error
 
96
        self._main_loop.quit()
82
97
 
83
98
    def _get_identity_info(self, user_name, password):
84
99
        info = Signon.IdentityInfo.new()
90
105
    def _set_credentials_id_to_account(
91
106
            self, identity, id_, error, account_dict):
92
107
        if error:
93
 
            self.error = error
94
 
            self._main_loop.quit()
 
108
            self._quit_main_loop_on_error(
 
109
                error, 'storing credentials with info')
95
110
 
 
111
        logger.debug('Setting credentials to account.')
96
112
        account = account_dict.get('account')
97
113
        oauth_token = account_dict.get('oauth_token')
98
114
        account.set_variant('CredentialsId', GLib.Variant('u', id_))
100
116
 
101
117
    def _process_session(self, account, error, oauth_token):
102
118
        if error:
103
 
            self.error = error
104
 
            self._main_loop.quit()
 
119
            self._quit_main_loop_on_error(
 
120
                error, 'setting credentials id to account')
105
121
 
 
122
        logger.debug('Processing session.')
106
123
        account_service = Accounts.AccountService.new(account, None)
107
124
        auth_data = account_service.get_auth_data()
108
125
        identity = auth_data.get_credentials_id()
119
136
 
120
137
    def _on_login_processed(self, session, reply, error, userdata):
121
138
        if error:
122
 
            self.error = error
 
139
            self._quit_main_loop_on_error(error, 'processing session')
123
140
 
124
 
        self._main_loop.quit()
 
141
        else:
 
142
            self._main_loop.quit()
125
143
 
126
144
    def _enable_evernote_service(self, account):
 
145
        logger.debug('Enabling evernote service.')
127
146
        service = self._manager.get_service('evernote')
128
147
        account.select_service(service)
129
148
        account.set_enabled(True)
130
 
        account.store(self._on_account_created, None)
 
149
        account.store(self._on_service_enabled, None)
 
150
 
 
151
    def _on_service_enabled(self, account, error, _):
 
152
        if error:
 
153
            self._quit_main_loop_on_error(error, 'enabling service')
 
154
 
 
155
    def _log_accounts_info(self):
 
156
        account_ids = self._manager.list()
 
157
        logger.debug('Existing accounts: {}.'.format(account_ids))
 
158
        for id_ in account_ids:
 
159
            account = self._manager.get_account(id_)
 
160
            self._log_account_info(account)
 
161
 
 
162
    def _log_account_info(self, account):
 
163
        logger.debug('Account info:')
 
164
        logger.debug('id: {}'.format(account.id))
 
165
        logger.debug('provider: {}'.format(account.get_provider_name()))
 
166
        logger.debug('enabled: {}'.format(account.get_enabled()))
 
167
        logger.debug('Account services:')
 
168
        for service in account.list_services():
 
169
            logger.debug('name: {}'.format(service.get_name()))
 
170
            account_service = Accounts.AccountService.new(account, service)
 
171
            logger.debug('enabled: {}'.format(account_service.get_enabled()))
131
172
 
132
173
    def delete_account(self, account):
133
174
        """Delete an account.
135
176
        :param account: The account to delete.
136
177
 
137
178
        """
 
179
        logger.info('Deleting the account with id {}.'.format(account.id))
138
180
        self._start_main_loop()
139
181
        account.delete()
140
182
        account.store(self._on_account_deleted, None)
141
183
        self._join_main_loop()
142
184
 
143
 
    def _on_account_deleted(self, account, error, userdata):
 
185
    def _on_account_deleted(self, account, error, _):
144
186
        if error:
145
 
            self.error = error
146
 
 
147
 
        self._main_loop.quit()
 
187
            self._quit_main_loop_on_error(error, 'deleting account')
 
188
        else:
 
189
            self._main_loop.quit()