def test_login_logout(self): credential = conf.get_credential('test') username = credential['username'] password = credential['password'] if self.api.has_logged_in: assert self.api.logout() assert self.api.login(username, password)
def login(self, username=None, password=None, section='default'): """ Created the passport with ``username`` and ``password`` and log in. If either ``username`` or ``password`` is None or omitted, the credentials file will be parsed. :param str username: username to login (email, phone number or user ID) :param str password: password :param str section: section name in the credential file :raise: raises :class:`.AuthenticationError` if failed to login """ if self.has_logged_in: return True if username is None or password is None: credential = conf.get_credential(section) username = credential['username'] password = credential['password'] passport = Passport(username, password) r = self.http.post(passport.login_url, passport.form) if r.state is True: # Bind this passport to API self.passport = passport passport.data = r.content['data'] passport.user_id = r.content['data']['USER_ID'] passport.status = 'LOGGED_IN' return True else: msg = None if 'err_name' in r.content: if r.content['err_name'] == 'account': msg = 'Account does not exist.' elif r.content['err_name'] == 'passwd': msg = 'Password is incorrect.' passport.status = 'FAILED' raise AuthenticationError(msg)