Esempio n. 1
0
class TestNotifications(unittest.TestCase):
    def setUp(self):
        self.app = pp_tests.get_app()
        self.connection, self.db = pp_tests.connect_to_mongo()
        self.account = pp_tests.create_account_and_login(self.app, self.db, {
            'email': '*****@*****.**',
            'password': '******',
            'name': 'Daniel',
            'startup': 'Payparrot',
            'url': 'http://payparrot.com/',
            'callback_url': 'http://www.epistemonikos.org',
            'notification_url': 'http://www.epistemonikos.org',
        })
            
        self.notification = Notifications(self.db, {
            'suscription_id': ObjectId(),
            'account_id': self.account.id,
            'external_id': '238462834',
            'parrot_id': ObjectId(),
            'request_url': 'http://localhost:3000/notifications/echo',
            'status': 'pending',
            'type': 'subscription_activated'
        })
        self.notification.insert()

    def tearDown(self):
        pp_tests.tear_down(self.db, self.app)
        self.connection.close()

    def test_get_notification(self):
        response = self.app.get('/accounts/%s/notifications/%s' % (self.account.id, self.notification.id))
        expected = self.notification.JSON()
        self.assertEqual(200, response.status_int)
        self.assertEqual(expected, response.json)
Esempio n. 2
0
def _create_notification(db, account,parrot,subscription):
    notification = Notifications(db, {
        'account_id': account.id,
        'parrot_id': parrot.id,
        'type': 'subscription_activated',
        'subscription_id': subscription.id,
        'external_id': subscription.external_id,
        'request_url': account.notification_url
    })
    notification.insert()
    return notification.id
Esempio n. 3
0
def send_notification(db, payment, notification_type):
    subscription = Subscriptions.findOne(db, {'account_id': payment.account_id, 'parrot_id': payment.parrot_id})
    if subscription:
        account = Accounts.findOne(db, payment.account_id)
        if account:
          notification = Notifications(db, {
              'account_id': payment.account_id, 
              'parrot_id': payment.parrot_id, 
              'type': notification_type,
              'subscription_id': subscription.id,
              'external_id': subscription.external_id,
              'request_url': account.notification_url
          })
          notification.insert()
Esempio n. 4
0
def get_notification(account_id, notification_id, db, secure = True):
    notification = Notifications.findOne(db, {'_id': ObjectId(notification_id),'account_id': ObjectId(account_id)})
    if notification:
        # del notification.queue_message_id
        return notification.JSON()
    else:
        print "cagamos"
        response.status = 404
        return {}
Esempio n. 5
0
def notify(db, notification_raw, notification_message):
    notification = Notifications.findOne(db, notification_message.get('notification_id'))
    account = Accounts.findOne(db, notification.account_id)
    if notification:
        log('cron3', 'Notifying remote customer', notification_message.get('subscription_id'))
        if not notification_message.get('type') in VALID_NOTIFICATIONS:
            log('cron3', 'ERROR: Unknown notification', notification_message.get('subscription_id'))
            # TODO: Check what to do in this case
            return
        if notification.request_url:
            query_data = {
                'subscription_id': str(notification.subscription_id),
                'account_id': str(notification.account_id),
                'parrot_id': str(notification.parrot_id),
                'type': notification_message.get('type'),
                'external_id': str(notification.external_id),
                'notification_id': str(notification.id),
            }
            log('cron3', 'Notification URL: %s' % notification.request_url, notification_message.get('subscription_id'))
            utf8_query_data = dict([(key,val.encode('utf-8')) for key, val in query_data.items() if isinstance(val, basestring)])
            delete_message = False
            try:
                if account.notification_active:
                    http_client = Http()
                    headers, body = http_client.request(uri = notification.request_url, body = urlencode(utf8_query_data), method = 'POST')
                    if int(headers.status) >= 200 and int(headers.status) < 300:
                        notification.update({
                            'response_status': headers.status,
                            'response_headers': headers,
                            'response_body': body,
                            'status': 'sent'
                        })
                        log('cron3', 'Remote notification succeded', notification_message.get('subscription_id'))
                    else:
                        log('cron3', "Failed. Notification response not 2XX (received %s) from url %s" % (
                            headers.status,
                            notification.request_url
                        ), notification_message.get('subscription_id'))
                else:
                    delete_message = True
                    notification.update({
                            'status': 'off'
                    })
                if delete_message:
                    Queue.delete_message('notifications', notification_raw)

            except Exception, e:
                log('cron3', "Failed. Exception %specified" % e)
        else:
            log('cron3', 'ERROR: No remote url specified', notification_message.get('subscription_id'))
Esempio n. 6
0
 def setUp(self):
     self.app = pp_tests.get_app()
     self.connection, self.db = pp_tests.connect_to_mongo()
     self.account = pp_tests.create_account_and_login(self.app, self.db, {
         'email': '*****@*****.**',
         'password': '******',
         'name': 'Daniel',
         'startup': 'Payparrot',
         'url': 'http://payparrot.com/',
         'callback_url': 'http://www.epistemonikos.org',
         'notification_url': 'http://www.epistemonikos.org',
     })
         
     self.notification = Notifications(self.db, {
         'suscription_id': ObjectId(),
         'account_id': self.account.id,
         'external_id': '238462834',
         'parrot_id': ObjectId(),
         'request_url': 'http://localhost:3000/notifications/echo',
         'status': 'pending',
         'type': 'subscription_activated'
     })
     self.notification.insert()