def test_only_subscriber_or_admin_can_unsubscribe(self): alert = self.factory.create_alert() other_user = self.factory.create_user() path = '/api/alerts/{}/subscriptions/{}'.format(alert.id, other_user.id) AlertSubscription.create(alert=alert, user=other_user) response = self.make_request('delete', path) self.assertEqual(response.status_code, 403) response = self.make_request('delete', path, user=self.factory.create_admin()) self.assertEqual(response.status_code, 200) AlertSubscription.create(alert=alert, user=other_user) response = self.make_request('delete', path, user=other_user) self.assertEqual(response.status_code, 200)
def get(self, alert_id): alert_id = int(alert_id) alert = Alert.get_by_id_and_org(alert_id, self.current_org) require_access(alert.groups, self.current_user, view_only) subscriptions = AlertSubscription.all(alert_id) return [s.to_dict() for s in subscriptions]
def test_only_subscriber_or_admin_can_unsubscribe(self): alert = self.factory.create_alert() other_user = self.factory.create_user() path = '/api/alerts/{}/subscriptions/{}'.format( alert.id, other_user.id) AlertSubscription.create(alert=alert, user=other_user) response = self.make_request('delete', path) self.assertEqual(response.status_code, 403) response = self.make_request('delete', path, user=self.factory.create_admin()) self.assertEqual(response.status_code, 200) AlertSubscription.create(alert=alert, user=other_user) response = self.make_request('delete', path, user=other_user) self.assertEqual(response.status_code, 200)
def post(self, alert_id): req = request.get_json(True) alert = Alert.get_by_id_and_org(alert_id, self.current_org) require_access(alert.groups, self.current_user, view_only) kwargs = {'alert': alert, 'user': self.current_user} if 'destination_id' in req: destination = NotificationDestination.get_by_id_and_org( req['destination_id'], self.current_org) kwargs['destination'] = destination # 用提供外键对应的列 # kwargs = {'alert': alert, 'user': self.current_user, 'destination': destination} # AlertSubscription. 一个alert有对应多个订阅者,一订阅者订阅多个alert,多对多 # alert # user # query # notification subscription = AlertSubscription(**kwargs) session.add(subscription) session.commit() self.record_event({ 'action': 'subscribe', 'timestamp': int(time.time()), 'object_id': alert_id, 'object_type': 'alert', 'destination': req.get('destination_id') }) d = subscription.to_dict() return d
def test_only_subscriber_or_admin_can_unsubscribe(self): subscription = self.factory.create_alert_subscription() alert = subscription.alert user = subscription.user path = '/api/alerts/{}/subscriptions/{}'.format( alert.id, subscription.id) other_user = self.factory.create_user() response = self.make_request('delete', path, user=other_user) self.assertEqual(response.status_code, 403) response = self.make_request('delete', path, user=user) self.assertEqual(response.status_code, 200) subscription_two = AlertSubscription(alert=alert, user=other_user) admin_user = self.factory.create_admin() db.session.add_all([subscription_two, admin_user]) db.session.commit() path = '/api/alerts/{}/subscriptions/{}'.format( alert.id, subscription_two.id) response = self.make_request('delete', path, user=admin_user) self.assertEqual(response.status_code, 200)
from redash.models import db, Alert, AlertSubscription if __name__ == '__main__': with db.database.transaction(): # There was an AWS/GCE image created without this table, to make sure this exists we run this migration. if not AlertSubscription.table_exists(): AlertSubscription.create_table() db.close_db(None)
conf['username'] = settings.WEBHOOK_USERNAME conf['password'] = settings.WEBHOOK_PASSWORD options = ConfigurationContainer(conf, schema) webhook = NotificationDestination.create( org=org, user=user, name="Webhook", type="webhook", options=options ) for alert in Alert.select(): AlertSubscription.create( user=user, destination=webhook, alert=alert ) if settings.HIPCHAT_API_TOKEN: # Have all existing alerts send to HipChat if already configured schema = get_configuration_schema_for_destination_type('hipchat') conf = {} if settings.HIPCHAT_API_URL: conf['url'] = '{url}/room/{room_id}/notification?auth_token={token}'.format( url=settings.HIPCHAT_API_URL, room_id=settings.HIPCHAT_ROOM_ID, token=settings.HIPCHAT_API_TOKEN) else: conf['url'] = 'https://hipchat.com/v2/room/{room_id}/notification?auth_token={token}'.format( room_id=settings.HIPCHAT_ROOM_ID, token=settings.HIPCHAT_API_TOKEN)
conf['username'] = WEBHOOK_USERNAME conf['password'] = WEBHOOK_PASSWORD options = ConfigurationContainer(conf, schema) webhook = NotificationDestination.create( org=org, user=user, name="Webhook", type="webhook", options=options ) for alert in Alert.select(): AlertSubscription.create( user=user, destination=webhook, alert=alert ) if HIPCHAT_API_TOKEN: # Have all existing alerts send to HipChat if already configured schema = get_configuration_schema_for_destination_type('hipchat') conf = {} if HIPCHAT_API_URL: conf['url'] = '{url}/room/{room_id}/notification?auth_token={token}'.format( url=HIPCHAT_API_URL, room_id=HIPCHAT_ROOM_ID, token=HIPCHAT_API_TOKEN) else: conf['url'] = 'https://hipchat.com/v2/room/{room_id}/notification?auth_token={token}'.format( room_id=HIPCHAT_ROOM_ID, token=HIPCHAT_API_TOKEN)
from redash.models import db, Alert, AlertSubscription if __name__ == '__main__': with db.database.transaction(): Alert.create_table() AlertSubscription.create_table() db.close_db(None)