def test_insert(self): with self.app.test_request_context(): with patch.object(Interactions, 'insert', return_value={}): response = insert(DEFAULT_SUBSCRIPTIONS_TABLE, **self.param_kwargs) self.assertEqual(response.status_code, client.CREATED)
def test_insert_type_error(self): with self.app.test_request_context(): with patch.object(Interactions, 'insert', side_effect=TypeError): response = insert(DEFAULT_SUBSCRIPTIONS_TABLE, **self.param_kwargs) self.assertRaises(TypeError) self.assertEqual(response.status_code, client.BAD_REQUEST)
def test_insert_rql_driver_error(self): with self.app.test_request_context(): with patch.object(Interactions, 'insert', side_effect=RqlDriverError(None)): response = insert(DEFAULT_SUBSCRIPTIONS_TABLE, **self.param_kwargs) self.assertRaises(RqlDriverError) self.assertEqual(response.status_code, client.INTERNAL_SERVER_ERROR)
def post(self, registration_id): """ Creates new triggered webhook event """ registration = Interactions.query(DEFAULT_REGISTRATIONS_TABLE, filters={'id': registration_id}) if not registration: return make_response( jsonify({'Error': 'Registration id not found'}), client.NOT_FOUND) # Other users cannot trigger webhooks they didn't create calling_account_id = lookup_account_id(request.headers['username']) if not lookup_registration_id(calling_account_id, registration_id): return make_response( jsonify({ 'Error': 'You don\'t have access ' 'to this registration record or it no ' 'longer exists' }), client.UNAUTHORIZED) # Notify subscribed endpoints (send the webhooks out) subscriptions = Interactions.list_all( DEFAULT_SUBSCRIPTIONS_TABLE, order_by='epoch', filters={'registration_id': registration_id}) if subscriptions: for record in subscriptions: account = Interactions.get(DEFAULT_ACCOUNTS_TABLE, record['account_id']) # Only hit the endpoint if their failed count is low enough if int(account['failed_count']) < MAX_FAILED_COUNT: # This import is required to be here so the flask-restful # piece works properly with Celery from pywebhooks.tasks.webhook_notification import \ notify_subscribed_accounts notify_subscribed_accounts.delay( event=registration[0]['event'], event_data=registration[0]['event_data'], secret_key=account['secret_key'], endpoint=account['endpoint'], account_id=record['account_id']) return insert(DEFAULT_TRIGGERED_TABLE, **{'registration_id': registration_id})
def post(self): """ Creates a new registration """ json_data = request.get_json() # Look up account id based on username, username will be valid since # the api_key_restricted_resource decorator runs first account_id = lookup_account_id(request.headers['username']) return insert(DEFAULT_REGISTRATIONS_TABLE, **{'account_id': account_id, 'event': json_data['event'], 'description': json_data['description'], 'event_data': json_data['event_data']})
def post(self, registration_id): """ Creates new triggered webhook event """ registration = Interactions.query( DEFAULT_REGISTRATIONS_TABLE, filters={'id': registration_id}) if not registration: return make_response( jsonify( {'Error': 'Registration id not found'} ), client.NOT_FOUND) # Other users cannot trigger webhooks they didn't create calling_account_id = lookup_account_id(request.headers['username']) if not lookup_registration_id(calling_account_id, registration_id): return make_response( jsonify({'Error': 'You don\'t have access ' 'to this registration record or it no ' 'longer exists'}), client.UNAUTHORIZED) # Notify subscribed endpoints (send the webhooks out) subscriptions = Interactions.list_all( DEFAULT_SUBSCRIPTIONS_TABLE, order_by='epoch', filters={'registration_id': registration_id}) if subscriptions: for record in subscriptions: account = Interactions.get(DEFAULT_ACCOUNTS_TABLE, record['account_id']) # Only hit the endpoint if their failed count is low enough if int(account['failed_count']) < MAX_FAILED_COUNT: # This import is required to be here so the flask-restful # piece works properly with Celery from pywebhooks.tasks.webhook_notification import \ notify_subscribed_accounts notify_subscribed_accounts.delay( event=registration[0]['event'], event_data=registration[0]['event_data'], secret_key=account['secret_key'], endpoint=account['endpoint'], account_id=record['account_id']) return insert(DEFAULT_TRIGGERED_TABLE, **{'registration_id': registration_id})
def post(self): """ Creates a new registration """ json_data = request.get_json() # Look up account id based on username, username will be valid since # the api_key_restricted_resource decorator runs first account_id = lookup_account_id(request.headers['username']) return insert( DEFAULT_REGISTRATIONS_TABLE, **{ 'account_id': account_id, 'event': json_data['event'], 'description': json_data['description'], 'event_data': json_data['event_data'] })
def post(self, subscription_id): """ Creates new subscription """ # subscription_id is actually the registration_id registration_id = subscription_id account_id = lookup_account_id(request.headers['username']) if not registration_id_exists(registration_id): return make_response( jsonify({'Error': 'The registration id does not exist'}), client.NOT_FOUND) return insert(DEFAULT_SUBSCRIPTIONS_TABLE, **{'account_id': account_id, 'registration_id': registration_id})
def post(self, subscription_id): """ Creates new subscription """ # subscription_id is actually the registration_id registration_id = subscription_id account_id = lookup_account_id(request.headers['username']) if not registration_id_exists(registration_id): return make_response( jsonify({'Error': 'The registration id does not exist'}), client.NOT_FOUND) return insert( DEFAULT_SUBSCRIPTIONS_TABLE, **{ 'account_id': account_id, 'registration_id': registration_id })