def test_deny_unverified_by_default(httpsserver): """Ensure that we throw an error by default for self-signed servers.""" api = Api(domain=httpsserver.url) with pytest.raises(HttpCouldNotVerifyServerError): api.login('*****@*****.**', 'test') with pytest.raises(HttpCouldNotVerifyServerError): api.logout() with pytest.raises(HttpCouldNotVerifyServerError): api.refresh_token() # Also ensure that the RestResource works as well resource = api.event with pytest.raises(HttpCouldNotVerifyServerError): resource.get() with pytest.raises(HttpCouldNotVerifyServerError): resource.put() with pytest.raises(HttpCouldNotVerifyServerError): resource.patch() with pytest.raises(HttpCouldNotVerifyServerError): resource.post() with pytest.raises(HttpCouldNotVerifyServerError): resource.delete()
def test_allow_unverified_option(httpsserver): """Ensure that we allow unverified servers if the user passes a flag.""" api = Api(domain=httpsserver.url, verify=False) api.login('*****@*****.**', 'test') api.logout() api.refresh_token() # Also ensure that the RestResource works as well resource = api.event resource.get() resource.put() resource.patch() resource.post() resource.delete()
def test_logout(self, m): payload = {'jwt': 'big-token', 'username': '******'} # login works only if there is no Authorization header in the request def match_request_headers(request): return 'Authorization' not in request.headers m.post('http://iotile.test/api/v1/auth/login/', additional_matcher=match_request_headers, text=json.dumps(payload)) m.post('http://iotile.test/api/v1/auth/logout/', status_code=204) api = Api(domain='http://iotile.test') ok = api.login(email='*****@*****.**', password='******') self.assertTrue(ok) api.logout() self.assertEqual(api.username, None) self.assertEqual(api.token, None) # can log in again ok = api.login(email='*****@*****.**', password='******') self.assertTrue(ok)
class BaseMain(object): parser = None args = None api = None domain = 'https://iotile.cloud' def __init__(self): """ Initialize Logging configuration Initialize argument parsing Process any extra arguments Only hard codes one required argument: --user Additional arguments can be configured by overwriting the add_extra_args() method Logging configuration can be changed by overwritting the config_logging() method """ self.parser = argparse.ArgumentParser(description=__doc__) self.parser.add_argument('-u', '--user', dest='email', type=str, help='Email used for login') self.add_extra_args() self.args = self.parser.parse_args() self.config_logging() if not self.args.email: logger.error('User email is required: --user') sys.exit(1) def _critical_exit(self, msg): logger.error(msg) sys.exit(1) def main(self): """ Main function to call to initiate execution. 1. Get domain name and use to instantiate Api object 2. Call before_login to allow for work before logging in 3. Logging into the server 4. Call after_loging to do actual work with server data 5. Logout 6. Call after_logout to do work at end of script :return: Nothing """ self.domain = self.get_domain() self.api = Api(self.domain) self.before_login() ok = self.login() if ok: self.after_login() self.logout() self.after_logout() # Following functions can be overwritten if needed # ================================================ def config_logging(self): """ Overwrite to change the way the logging package is configured :return: Nothing """ logging.basicConfig( level=logging.INFO, format='[%(asctime)-15s] %(levelname)-6s %(message)s', datefmt='%d/%b/%Y %H:%M:%S') def add_extra_args(self): """ Overwrite to change the way extra arguments are added to the args parser :return: Nothing """ pass def get_domain(self): """ Overwrite to change the default domain :return: URL for server. e.g. 'https://iotile.cloud' """ return self.domain def login(self): """ Overwrite to change how to login to the server :return: True if successful """ password = getpass.getpass() ok = self.api.login(email=self.args.email, password=password) return ok def logout(self): """ Overwrite to change how to logout from server :return: Nothing """ self.api.logout() def before_login(self): """ Overwrite to do work after parsing, but before logging in to the server This is a good place to do additional custom argument checks :return: Nothing """ pass def after_login(self): """ This function MUST be overwritten to do actual work after logging into the Server :return: Nothing """ logger.warning('No actual work done') def after_logout(self): """ Overwrite if you want to do work after loging out of the server :return: Nothing """ pass
logger.error(e) sys.exit(1) if not args.t1: t1 = datetime.utcnow() else: try: t1 = dt_parse(args.t1) except Exception as e: logger.error(e) sys.exit(1) password = getpass.getpass() domain = PRODUCTION_DOMAIN_NAME logger.info('Using Server: {0}'.format(domain)) c = Api(domain) ok = c.login(email=args.email, password=password) if ok: logger.info('Welcome {0}'.format(args.email)) gen = AccumulationReportGenerator(c) stats =gen.compute_sum(sources=args.sources, start=t0, end=t1) pprint(stats) logger.info('Goodbye!!') c.logout()