def test_update_rotating_tables_month_end(self): today = datetime.date.today() next_month = today.month + 1 next_year = today.year if next_month > 12: # pragma: nocover next_month = 1 next_year += 1 tomorrow = datetime.datetime(year=next_year, month=next_month, day=1) AutopushSettings._tomorrow = Mock() AutopushSettings._tomorrow.return_value = tomorrow settings = AutopushSettings( hostname="example.com", resolve_hostname=True) # shift off tomorrow's table. tomorrow_table = sorted(settings.message_tables.keys())[-1] settings.message_tables.pop(tomorrow_table) # Get the deferred back d = settings.update_rotating_tables() def check_tables(result): eq_(len(settings.message_tables), 3) eq_(sorted(settings.message_tables.keys())[-1], tomorrow_table) d.addCallback(check_tables) return d
def setUp(self): from autopush.web.webpush import WebPushHandler settings = AutopushSettings( hostname="localhost", statsd_host=None, ) self.fernet_mock = settings.fernet = Mock(spec=Fernet) self.ap_settings = settings self.router_mock = settings.router = Mock(spec=Router) self.request_mock = Mock(body=b'', arguments={}, headers={"ttl": "0"}, host='example.com:8080') self.wp = WebPushHandler(Application(), self.request_mock, ap_settings=settings) self.wp.path_kwargs = {} self.status_mock = self.wp.set_status = Mock() self.write_mock = self.wp.write = Mock() self.wp.log = Mock(spec=Logger) d = self.finish_deferred = Deferred() self.wp.finish = lambda: d.callback(True) settings.routers["webpush"] = Mock(spec=IRouter) self.wp_router_mock = settings.routers["webpush"] self.message_mock = settings.message = Mock() self.message_mock.all_channels.return_value = (True, [dummy_chid])
def setUp(self): settings = AutopushSettings( hostname="localhost", statsd_host=None, ) self.headers = headers = { "content-encoding": "aes128", "encryption": "awesomecrypto", "crypto-key": "niftykey" } self.router = WebPushRouter(settings, {}) self.notif = WebPushNotification( uaid=uuid.UUID(dummy_uaid), channel_id=uuid.UUID(dummy_chid), data="data", headers=headers, ttl=20, message_id=uuid.uuid4().hex, ) self.notif.cleanup_headers() mock_result = Mock(spec=gcmclient.gcm.Result) mock_result.canonical = dict() mock_result.failed = dict() mock_result.not_registered = dict() mock_result.needs_retry.return_value = False self.router_mock = settings.router = Mock(spec=Router) self.message_mock = settings.message = Mock(spec=Message) self.agent_mock = Mock(spec=settings.agent) settings.agent = self.agent_mock self.router.metrics = Mock() self.settings = settings
def setUp(self): self.timeout = 0.5 twisted.internet.base.DelayedCall.debug = True settings = AutopushSettings( hostname="localhost", statsd_host=None, ) self.fernet_mock = settings.fernet = Mock(spec=Fernet) self.metrics_mock = settings.metrics = Mock(spec=Metrics) self.agent_mock = settings.agent = Mock(spec=Agent) self.response_mock = Mock(spec=Response) self.router_mock = settings.router = Mock(spec=Router) self.storage_mock = settings.storage = Mock(spec=Storage) self.senderIDs_mock = settings.senderIDs = Mock(spec=SenderIDs) self.senderIDs_mock.get_ID.return_value = "test_senderid" self.request_mock = Mock(body=b'', arguments={}, headers={}) self.endpoint = endpoint.EndpointHandler(Application(), self.request_mock, ap_settings=settings) self.settings = settings settings.routers["simplepush"] = Mock(spec=IRouter) settings.routers["webpush"] = Mock(spec=IRouter) self.sp_router_mock = settings.routers["simplepush"] self.wp_router_mock = settings.routers["webpush"] self.status_mock = self.endpoint.set_status = Mock() self.write_mock = self.endpoint.write = Mock() d = self.finish_deferred = Deferred() self.endpoint.finish = lambda: d.callback(True) self.endpoint.start_time = time.time()
def setUp(self): settings = AutopushSettings( hostname="localhost", statsd_host=None, ) self.headers = headers = { "content-encoding": "aes128", "encryption": "awesomecrypto", "crypto-key": "niftykey" } self.router = WebPushRouter(settings, {}) self.notif = Notification("EncMessageId", "data", dummy_chid, headers, 20) mock_result = Mock(spec=gcmclient.gcm.Result) mock_result.canonical = dict() mock_result.failed = dict() mock_result.not_registered = dict() mock_result.needs_retry.return_value = False self.router_mock = settings.router = Mock(spec=Router) self.message_mock = settings.message = Mock(spec=Message) self.agent_mock = Mock(spec=settings.agent) settings.agent = self.agent_mock self.router.metrics = Mock() self.settings = settings
def test_update_rotating_tables(self): from autopush.db import get_month settings = AutopushSettings(hostname="example.com", resolve_hostname=True) # Erase the tables it has on init, and move current month back one last_month = get_month(-1) settings.current_month = last_month.month settings.message_tables = {} # Create the next month's table, just in case today is the day before # a new month, in which case the lack of keys will cause an error in # update_rotating_tables next_month = get_month(1) settings.message_tables[next_month.month] = None # Get the deferred back e = Deferred() d = settings.update_rotating_tables() def check_tables(result): eq_(len(settings.message_tables), 2) eq_(settings.current_month, get_month().month) d.addCallback(check_tables) d.addBoth(lambda x: e.callback(True)) return e
def setUp(self): twisted.internet.base.DelayedCall.debug = True settings = AutopushSettings( hostname="localhost", statsd_host=None, bear_hash_key='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB=', ) self.fernet_mock = settings.fernet = Mock(spec=Fernet) self.metrics_mock = settings.metrics = Mock(spec=Metrics) self.router_mock = settings.router = Mock(spec=Router) self.storage_mock = settings.storage = Mock(spec=Storage) self.router_mock.register_user = Mock() self.router_mock.register_user.return_value = (True, {}, {}) settings.routers["test"] = Mock(spec=IRouter) settings.router.get_uaid.return_value = { "router_type": "test", "router_data": dict() } self.request_mock = Mock(body=b'', arguments={}, headers={}) self.reg = RegistrationHandler(Application(), self.request_mock, ap_settings=settings) self.reg.request.uri = '/v1/xxx/yyy/register' self.status_mock = self.reg.set_status = Mock() self.write_mock = self.reg.write = Mock() self.auth = ("WebPush %s" % generate_hash(self.reg.ap_settings.bear_hash_key[0], dummy_uaid.hex)) d = self.finish_deferred = Deferred() self.reg.finish = lambda: d.callback(True) self.settings = settings
def test_update_rotating_tables_month_end(self): today = datetime.date.today() next_month = today.month + 1 next_year = today.year if next_month > 12: # pragma: nocover next_month = 1 next_year += 1 tomorrow = datetime.datetime(year=next_year, month=next_month, day=1) AutopushSettings._tomorrow = Mock() AutopushSettings._tomorrow.return_value = tomorrow settings = AutopushSettings(hostname="example.com", resolve_hostname=True) # shift off tomorrow's table. tomorrow_table = sorted(settings.message_tables.keys())[-1] settings.message_tables.pop(tomorrow_table) # Get the deferred back d = settings.update_rotating_tables() def check_tables(result): eq_(len(settings.message_tables), 3) eq_(sorted(settings.message_tables.keys())[-1], tomorrow_table) d.addCallback(check_tables) return d
def __init__(self, sysargs, use_files=True): args = self._load_args(sysargs, use_files) self._settings = AutopushSettings( crypto_key=args.crypto_key, router_tablename=args.router_tablename, storage_tablename=args.storage_tablename, message_tablename=args.message_tablename, ) self._endpoint = args.endpoint self._pp = pprint.PrettyPrinter(indent=4)
def test_bad_fcm_senders(self): old_auth = self.TestArg.fcm_auth old_senderid = self.TestArg.fcm_senderid self.TestArg.fcm_auth = "" with assert_raises(InvalidSettings): AutopushSettings.from_argparse(self.TestArg) self.TestArg.fcm_auth = old_auth self.TestArg.fcm_senderid = "" with assert_raises(InvalidSettings): AutopushSettings.from_argparse(self.TestArg) self.TestArg.fcm_senderid = old_senderid
def test_settings_crypto_key(self): fake = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=' settings = AutopushSettings(crypto_key=fake) eq_(settings.fernet._fernets[0]._encryption_key, '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') fake2 = 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB=' settings = AutopushSettings(crypto_key=[fake, fake2]) eq_(settings.fernet._fernets[0]._encryption_key, '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') eq_(settings.fernet._fernets[1]._encryption_key, '\x10A\x04\x10A\x04\x10A\x04\x10A\x04\x10A\x04\x10')
def setUp(self): twisted.internet.base.DelayedCall.debug = True self.proto = SimplePushServerProtocol() settings = AutopushSettings( hostname="localhost", statsd_host=None, ) self.proto.ap_settings = settings self.proto.sendMessage = self.send_mock = Mock() self.orig_close = self.proto.sendClose self.proto.sendClose = self.close_mock = Mock() self.proto.transport = self.transport_mock = Mock() self.proto.closeHandshakeTimeout = 0 settings.metrics = Mock(spec=Metrics)
def test_update_not_needed(self): settings = AutopushSettings( hostname="google.com", resolve_hostname=True) # Erase the tables it has on init, and move current month back one settings.message_tables = {} # Get the deferred back d = settings.update_rotating_tables() def check_tables(result): eq_(len(settings.message_tables), 0) d.addCallback(check_tables) return d
def test_update_not_needed(self): from autopush.db import get_month settings = AutopushSettings(hostname="google.com", resolve_hostname=True) db = DatabaseManager.from_settings(settings) db.create_initial_message_tables() # Erase the tables it has on init, and move current month back one db.message_tables = {} # Create the next month's table, just in case today is the day before # a new month, in which case the lack of keys will cause an error in # update_rotating_tables next_month = get_month(1) db.message_tables[next_month.month] = None # Get the deferred back e = Deferred() d = db.update_rotating_tables() def check_tables(result): eq_(len(db.message_tables), 1) d.addCallback(check_tables) d.addBoth(lambda x: e.callback(True)) return e
def setUp(self): twisted.internet.base.DelayedCall.debug = True settings = AutopushSettings( hostname="localhost", statsd_host=None, bear_hash_key='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB=', ) self.fernet_mock = settings.fernet = Mock(spec=Fernet) self.db = db = test_db() db.router.register_user.return_value = (True, {}, {}) db.router.get_uaid.return_value = { "router_type": "test", "router_data": dict() } db.create_initial_message_tables() self.routers = routers = routers_from_settings(settings, db, Mock()) routers["test"] = Mock(spec=IRouter) app = EndpointHTTPFactory(settings, db=db, routers=routers) self.client = Client(app) self.request_mock = Mock(body=b'', arguments={}, headers={}) self.reg = NewRegistrationHandler(app, self.request_mock) self.auth = ("WebPush %s" % generate_hash(settings.bear_hash_key[0], dummy_uaid.hex)) self.settings = settings
def setUp(self, fgcm): settings = AutopushSettings( hostname="localhost", statsd_host=None, ) self.gcm_config = {'s3_bucket': 'None', 'max_data': 32, 'ttl': 60, 'senderid_list': {'test123': {"auth": "12345678abcdefg"}}} self.gcm = fgcm self.router = GCMRouter(settings, self.gcm_config) self.headers = {"content-encoding": "aesgcm", "encryption": "test", "encryption-key": "test"} # Payloads are Base64-encoded. self.notif = Notification(10, "q60d6g", dummy_chid, self.headers, 200) self.router_data = dict( router_data=dict( token="connect_data", creds=dict(senderID="test123", auth="12345678abcdefg"))) mock_result = Mock(spec=gcmclient.gcm.Result) mock_result.canonical = dict() mock_result.failed = dict() mock_result.not_registered = dict() mock_result.needs_retry.return_value = False self.mock_result = mock_result fgcm.send.return_value = mock_result
def test_init(self): settings = AutopushSettings( hostname="localhost", statsd_host=None, ) with assert_raises(IOError): GCMRouter(settings, {"senderIDs": {}})
def setUp(self): from autopush.web.base import BaseWebHandler settings = AutopushSettings( hostname="localhost", statsd_host=None, ) self.request_mock = Mock(body=b'', arguments={}, headers={"ttl": "0"}, host='example.com:8080') self.base = BaseWebHandler( EndpointHTTPFactory(settings, db=None, routers=None), self.request_mock) self.status_mock = self.base.set_status = Mock() self.write_mock = self.base.write = Mock() self.base.log = Mock(spec=Logger) d = self.finish_deferred = Deferred() self.base.finish = lambda: d.callback(True) # Attach some common cors stuff for testing self.base.cors_methods = "POST,PUT" self.base.cors_request_headers = [ "content-encoding", "encryption", "crypto-key", "ttl", "encryption-key", "content-type", "authorization" ] self.base.cors_response_headers = ["location", "www-authenticate"]
def test_update_rotating_tables_month_end(self): """Test that rotating adds next months table This test is intended to ensure that if the next day is a new month, then update_rotating_tables realizes this and add's the new table to the message_tables. A pre-requisite is that today cannot be the last day of the current month. Therefore, we first sub in _tomorrow to ensure it always appears as next month, and then remove the new table create_initial_tables made so we can observe update_rotating_tables add the new one. Note that sorting message table keys to find the last month does *not work* since the month digit is not zero-padded. """ today = datetime.date.today() next_month = today.month + 1 next_year = today.year if next_month > 12: # pragma: nocover next_month = 1 next_year += 1 tomorrow = datetime.datetime(year=next_year, month=next_month, day=1) AutopushSettings._tomorrow = Mock() AutopushSettings._tomorrow.return_value = tomorrow settings = AutopushSettings(hostname="example.com", resolve_hostname=True) # We should have 3 tables, one for next/this/last month eq_(len(settings.message_tables), 3) # Grab next month's table name and remove it next_month = get_rotating_message_table(settings._message_prefix, delta=1) settings.message_tables.pop(next_month.table_name) # Get the deferred back d = settings.update_rotating_tables() def check_tables(result): eq_(len(settings.message_tables), 3) ok_(next_month.table_name in settings.message_tables) d.addCallback(check_tables) return d
def test_aws_ami_id(self, request_mock, mt, mc): class MockReply: content = "ami_123" request_mock.return_value = MockReply self.TestArg.no_aws = False settings = AutopushSettings.from_argparse(self.TestArg) eq_(settings.ami_id, "ami_123")
def test_gcmclient_fail(self, fgcm): fgcm.side_effect = Exception settings = AutopushSettings( hostname="localhost", statsd_host=None, ) with assert_raises(IOError): GCMRouter(settings, {"senderIDs": {"test123": {"auth": "abcd"}}})
def test_settings(self, *args): settings = AutopushSettings.from_argparse(self.TestArg) app = EndpointApplication(settings) # verify that the hostname is what we said. eq_(settings.hostname, self.TestArg.hostname) eq_(app.routers["gcm"].config['collapsekey'], "collapse") eq_(app.routers["apns"]._config['firefox']['cert'], "cert.file") eq_(app.routers["apns"]._config['firefox']['key'], "key.file") eq_(settings.wake_timeout, 10)
def test_update_rotating_tables(self): from autopush.db import get_month settings = AutopushSettings( hostname="example.com", resolve_hostname=True) # Erase the tables it has on init, and move current month back one last_month = get_month(-1) settings.current_month = last_month.month settings.message_tables = {} # Get the deferred back d = settings.update_rotating_tables() def check_tables(result): eq_(len(settings.message_tables), 1) d.addCallback(check_tables) return d
def setUp(self): settings = AutopushSettings( hostname="localhost", statsd_host=None, ) self.router = SimpleRouter(settings, {}) self.notif = Notification(10, "data", dummy_chid, None, 200) mock_result = Mock(spec=gcmclient.gcm.Result) mock_result.canonical = dict() mock_result.failed = dict() mock_result.not_registered = dict() mock_result.needs_retry.return_value = False self.router_mock = settings.router = Mock(spec=Router) self.storage_mock = settings.storage = Mock(spec=Storage) self.agent_mock = Mock(spec=settings.agent) settings.agent = self.agent_mock self.router.metrics = Mock()
class EndpointDiagnosticCLI(object): log = Logger() def __init__(self, sysargs, use_files=True): args = self._load_args(sysargs, use_files) self._settings = AutopushSettings( crypto_key=args.crypto_key, router_tablename=args.router_tablename, storage_tablename=args.storage_tablename, message_tablename=args.message_tablename, ) self._endpoint = args.endpoint self._pp = pprint.PrettyPrinter(indent=4) def _load_args(self, sysargs, use_files): if use_files: config_files = shared_config_files + [ # pragma: nocover '/etc/autopush_endpoint.ini', '~/.autopush_endpoint.ini', '.autopush_endpoint.ini' ] else: config_files = [] # pragma: nocover parser = configargparse.ArgumentParser( description='Runs endpoint diagnostics.', default_config_files=config_files) parser.add_argument('endpoint', help="Endpoint to parse") add_shared_args(parser) return parser.parse_args(sysargs) def run(self): match = PUSH_RE.search(self._endpoint) if not match: return "Not a valid endpoint" md = match.groupdict() api_ver, token = md.get("api_ver", "v1"), md["token"] parsed = self._settings.parse_endpoint( token=token, version=api_ver, ) uaid, chid = parsed["uaid"], parsed["chid"] print("UAID: {}\nCHID: {}\n".format(uaid, chid)) rec = self._settings.router.get_uaid(uaid) print("Router record:") self._pp.pprint(rec._data) print("\n") mess_table = rec["current_month"] chans = self._settings.message_tables[mess_table].all_channels(uaid) print("Channels in message table:") self._pp.pprint(chans)
def _from_argparse(cls, ns, **kwargs): # type: (Namespace, **Any) -> AutopushMultiService """Create an instance from argparse/additional kwargs""" # Add some entropy to prevent potential conflicts. postfix = os.urandom(4).encode('hex').ljust(8, '0') settings = AutopushSettings.from_argparse( ns, debug=ns.debug, preflight_uaid="deadbeef000000000deadbeef" + postfix, **kwargs) return cls(settings)
def setUp(self): settings = AutopushSettings( hostname="localhost", statsd_host=None, ) apns_config = {'cert_file': 'fake.cert', 'key_file': 'fake.key'} self.mock_apns = Mock(spec=apns.APNs) self.router = APNSRouter(settings, apns_config) self.router.apns = self.mock_apns self.notif = Notification(10, "data", dummy_chid, None, 200) self.router_data = dict(router_data=dict(token="connect_data"))
def setUp(self): from twisted.logger import Logger settings = AutopushSettings( hostname="localhost", statsd_host=None, ) self.metrics = metrics = Mock(spec=SinkMetrics) db = test_db(metrics=metrics) self.router = SimpleRouter(settings, {}, db) self.router.log = Mock(spec=Logger) self.notif = Notification(10, "data", dummy_chid) mock_result = Mock(spec=gcmclient.gcm.Result) mock_result.canonical = dict() mock_result.failed = dict() mock_result.not_registered = dict() mock_result.needs_retry.return_value = False self.router_mock = db.router self.storage_mock = db.storage self.agent_mock = Mock(spec=settings.agent) settings.agent = self.agent_mock
def test_init(self, ffcm): settings = AutopushSettings( hostname="localhost", statsd_host=None, ) def throw_auth(*args, **kwargs): raise Exception("oopsy") ffcm.side_effect = throw_auth with assert_raises(IOError): FCMRouter(settings, {})
def test_new_month(self): today = datetime.date.today() next_month = today.month + 1 next_year = today.year if next_month > 12: # pragma: nocover next_month = 1 next_year += 1 tomorrow = datetime.datetime(year=next_year, month=next_month, day=1) AutopushSettings._tomorrow = Mock() AutopushSettings._tomorrow.return_value = tomorrow settings = AutopushSettings() eq_(len(settings.message_tables), 3)
def test_update_rotating_tables_no_such_table(self, mock_date): from autopush.db import get_month mock_date.date.today.return_value = get_month(-7) settings = AutopushSettings( hostname="google.com", resolve_hostname=True) # Drop the table created during the creation of autopush settings settings.message.table.delete() # Erase the tables it has on init, and move current month back one last_month = get_month(-1) settings.current_month = last_month.month settings.message_tables = {} # Get the deferred back d = settings.update_rotating_tables() def check_tables(result): eq_(len(settings.message_tables), 0) d.addCallback(check_tables) return d
def setUp(self): twisted.internet.base.DelayedCall.debug = True settings = AutopushSettings( hostname="localhost", statsd_host=None, crypto_key='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=', ) db = test_db() self.message_mock = db.message = Mock(spec=Message) self.fernet_mock = settings.fernet = Mock(spec=Fernet) app = EndpointHTTPFactory.for_handler(MessageHandler, settings, db=db) self.client = Client(app)
def __init__(self, sysargs, use_files=True): args = self._load_args(sysargs, use_files) self._settings = settings = AutopushSettings( crypto_key=args.crypto_key, router_tablename=args.router_tablename, storage_tablename=args.storage_tablename, message_tablename=args.message_tablename, statsd_host=None, ) self.db = DatabaseManager.from_settings(settings) self.db.setup(settings.preflight_uaid) self._endpoint = args.endpoint self._pp = pprint.PrettyPrinter(indent=4)
def setUp(self): import cyclone.web from autobahn.twisted.websocket import WebSocketServerFactory from autobahn.twisted.resource import WebSocketResource from autopush.main import skip_request_logging from autopush.endpoint import EndpointHandler, MessageHandler, RegistrationHandler from autopush.settings import AutopushSettings from autopush.websocket import ( PushServerProtocol, RouterHandler, NotificationHandler, DefaultResource, StatusResource, ) from twisted.web.server import Site router_table = os.environ.get("ROUTER_TABLE", "router_int_test") storage_table = os.environ.get("STORAGE_TABLE", "storage_int_test") message_table = os.environ.get("MESSAGE_TABLE", "message_int_test") settings = AutopushSettings( hostname="localhost", statsd_host=None, endpoint_port="9020", router_port="9030", router_tablename=router_table, storage_tablename=storage_table, message_tablename=message_table, ) # Websocket server self._ws_url = "ws://localhost:9010/" factory = WebSocketServerFactory(self._ws_url, debug=False) factory.protocol = PushServerProtocol factory.protocol.ap_settings = settings factory.setProtocolOptions(webStatus=False, openHandshakeTimeout=5) settings.factory = factory resource = DefaultResource(WebSocketResource(factory)) resource.putChild("status", StatusResource()) self.websocket = reactor.listenTCP(9010, Site(resource)) # Websocket HTTP router # Internal HTTP notification router r = RouterHandler r.ap_settings = settings n = NotificationHandler n.ap_settings = settings ws_site = cyclone.web.Application( [(r"/push/([^\/]+)", r), (r"/notif/([^\/]+)(/([^\/]+))?", n)], default_host=settings.router_hostname, log_function=skip_request_logging, debug=False, ) self.ws_website = reactor.listenTCP(9030, ws_site) # Endpoint HTTP router site = cyclone.web.Application( [ (r"/push/([^\/]+)", EndpointHandler, dict(ap_settings=settings)), (r"/m/([^\/]+)", MessageHandler, dict(ap_settings=settings)), # PUT /register/ => connect info # GET /register/uaid => chid + endpoint (r"/register(?:/(.+))?", RegistrationHandler, dict(ap_settings=settings)), ], default_host=settings.hostname, log_function=skip_request_logging, debug=False, ) self.website = reactor.listenTCP(9020, site) self._settings = settings