def test_post_notifications_saves_email_normally_if_save_email_to_queue_fails(client, notify_db_session, mocker):
    save_email_task = mocker.patch(
        "app.celery.tasks.save_api_email.apply_async",
        side_effect=SQSError({'some': 'json'}, 'some opname')
    )
    mock_send_task = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')

    service = create_service(
        service_id=current_app.config['HIGH_VOLUME_SERVICE'][1],
        service_name='high volume service',
    )
    template = create_template(service=service, content='((message))', template_type=EMAIL_TYPE)
    data = {
        "email_address": "*****@*****.**",
        "template_id": template.id,
        "personalisation": {"message": "Dear citizen, have a nice day"}
    }
    response = client.post(
        path='/v2/notifications/email',
        data=json.dumps(data),
        headers=[('Content-Type', 'application/json'), create_authorization_header(service_id=service.id)]
    )

    json_resp = response.get_json()

    assert response.status_code == 201
    assert json_resp['id']
    assert json_resp['content']['body'] == "Dear citizen, have a nice day"
    assert json_resp['template']['id'] == str(template.id)
    # save email
    save_email_task.assert_called_once_with([mock.ANY], queue='save-api-email-tasks')
    mock_send_task.assert_called_once_with([json_resp['id']], queue='send-email-tasks')
    assert Notification.query.count() == 1
Esempio n. 2
0
 def delete_queue(self, queue, force_deletion=False):
     """
     Delete an SQS Queue.
     Inputs:
         queue - a Queue object representing the SQS queue to be deleted.
         force_deletion - (Optional) Normally, SQS will not delete a
                          queue that contains messages.  However, if
                          the force_deletion argument is True, the
                          queue will be deleted regardless of whether
                          there are messages in the queue or not.
                          USE WITH CAUTION.  This will delete all
                          messages in the queue as well.
     Returns:
         An empty ResultSet object.  Not sure why, actually.  It
         should probably return a Boolean indicating success or
         failure.
     """
     method = 'DELETE'
     path = queue.id
     if force_deletion:
         path = path + '?ForceDeletion=true'
     response = self.make_request(method, path)
     body = response.read()
     if response.status >= 300:
         raise SQSError(response.status, response.reason, body)
     rs = ResultSet()
     h = handler.XmlHandler(rs, self)
     xml.sax.parseString(body, h)
     return rs
Esempio n. 3
0
 def receive_message(self,
                     queue_url,
                     number_messages=1,
                     visibility_timeout=None,
                     message_class=Message):
     """
     This provides the same functionality as the read and get_messages methods
     of the queue object.  The only reason this is included here is that there is
     currently a bug in SQS that makes it impossible to read a message from a queue
     owned by someone else (even if you have been granted appropriate permissions)
     via the REST interface.  As it turns out, I need to be able to do this so until
     the REST interface gets fixed this is the workaround.
     """
     params = {'NumberOfMessages': number_messages}
     if visibility_timeout:
         params['VisibilityTimeout'] = visibility_timeout
     response = self.make_request('ReceiveMessage', params, queue_url)
     body = response.read()
     if response.status == 200:
         rs = ResultSet([('Message', message_class)])
         h = handler.XmlHandler(rs, queue_url)
         xml.sax.parseString(body, h)
         if len(rs) == 1:
             return rs[0]
         else:
             return rs
     else:
         raise SQSError(response.status, response.reason, body)
Esempio n. 4
0
 def delete_message(self, message):
     path = '%s/%s' % (self.id, message.id)
     response = self.connection.make_request('DELETE', path)
     body = response.read()
     if response.status >= 300:
         raise SQSError(response.status, response.reason, body)
     rs = ResultSet()
     h = XmlHandler(rs, self.connection)
     xml.sax.parseString(body, h)
     return rs
Esempio n. 5
0
 def set_queue_attribute(self, queue_url, attribute, value):
     params = {'Attribute': attribute, 'Value': value}
     response = self.make_request('SetQueueAttributes', params, queue_url)
     body = response.read()
     if response.status == 200:
         rs = ResultSet()
         h = handler.XmlHandler(rs, self)
         xml.sax.parseString(body, h)
         return rs.status
     else:
         raise SQSError(response.status, response.reason, body)
Esempio n. 6
0
 def get_queue_attributes(self, queue_url, attribute='All'):
     params = {'Attribute': attribute}
     response = self.make_request('GetQueueAttributes', params, queue_url)
     body = response.read()
     if response.status == 200:
         attrs = Attributes()
         h = handler.XmlHandler(attrs, self)
         xml.sax.parseString(body, h)
         return attrs
     else:
         raise SQSError(response.status, response.reason, body)
Esempio n. 7
0
 def change_message_visibility(self, queue_url, message_id, vtimeout):
     params = {'MessageId': message_id, 'VisibilityTimeout': vtimeout}
     response = self.make_request('ChangeMessageVisibility', params,
                                  queue_url)
     body = response.read()
     if response.status == 200:
         rs = ResultSet()
         h = handler.XmlHandler(rs, self)
         xml.sax.parseString(body, h)
         return rs.status
     else:
         raise SQSError(response.status, response.reason, body)
Esempio n. 8
0
 def get_messages(self, num_messages=1, visibility_timeout=None):
     path = '%s/front?NumberOfMessages=%d' % (self.id, num_messages)
     if visibility_timeout:
         path = '%s&VisibilityTimeout=%d' % (path, visibility_timeout)
     response = self.connection.make_request('GET', path)
     body = response.read()
     if response.status >= 300:
         raise SQSError(response.status, response.reason, body)
     rs = ResultSet([('Message', self.message_class)])
     h = XmlHandler(rs, self)
     xml.sax.parseString(body, h)
     return rs
Esempio n. 9
0
 def get_all_queues(self, prefix=''):
     if prefix:
         path = '/?QueueNamePrefix=%s' % prefix
     else:
         path = '/'
     response = self.make_request('GET', path)
     body = response.read()
     if response.status >= 300:
         raise SQSError(response.status, response.reason, body)
     rs = ResultSet([('QueueUrl', Queue)])
     h = handler.XmlHandler(rs, self)
     xml.sax.parseString(body, h)
     return rs
Esempio n. 10
0
 def delete_message(self, queue_url, message_id):
     """
     Because we have to use the Query interface to read messages from queues that
     we don't own, we also have to provide a way to delete those messages via Query.
     """
     params = {'MessageId': message_id}
     response = self.make_request('DeleteMessage', params, queue_url)
     body = response.read()
     if response.status == 200:
         rs = ResultSet()
         h = handler.XmlHandler(rs, self)
         xml.sax.parseString(body, h)
         return rs.status
     else:
         raise SQSError(response.status, response.reason, body)
Esempio n. 11
0
 def list_grants(self,
                 queue_url,
                 permission=None,
                 email_address=None,
                 user_id=None):
     params = {}
     if user_id:
         params['Grantee.ID'] = user_id
     if email_address:
         params['Grantee.EmailAddress'] = email_address
     if permission:
         params['Permission'] = permission
     response = self.make_request('ListGrants', params, queue_url)
     body = response.read()
     if response.status == 200:
         return body
     else:
         raise SQSError(response.status, response.reason, body)
Esempio n. 12
0
 def write(self, message):
     """
     Add a single message to the queue.
     Inputs:
         message - The message to be written to the queue
     Returns:
         None
     """
     path = '%s/back' % self.id
     message.queue = self
     response = self.connection.make_request('PUT', path, None,
                                             message.get_body_encoded())
     body = response.read()
     if response.status >= 300:
         raise SQSError(response.status, response.reason, body)
     handler = XmlHandler(message, self.connection)
     xml.sax.parseString(body, handler)
     return None
Esempio n. 13
0
 def remove_grant(self,
                  queue_url,
                  permission,
                  email_address=None,
                  user_id=None):
     params = {'Permission': permission}
     if user_id:
         params['Grantee.ID'] = user_id
     if email_address:
         params['Grantee.EmailAddress'] = email_address
     response = self.make_request('RemoveGrant', params, queue_url)
     body = response.read()
     if response.status == 200:
         rs = ResultSet()
         h = handler.XmlHandler(rs, self)
         xml.sax.parseString(body, h)
         return rs.status
     else:
         raise SQSError(response.status, response.reason, body)
Esempio n. 14
0
 def create_queue(self, queue_name, visibility_timeout=None):
     """
     Create a new queue.
     Inputs:
         queue_name - The name of the new queue
         visibility_timeout - (Optional) The default visibility
                              timeout for the new queue.
     Returns:
         A new Queue object representing the newly created queue.
     """
     path = '/?QueueName=%s' % queue_name
     if visibility_timeout:
         path = path + '&DefaultVisibilityTimeout=%d' % visibility_timeout
     response = self.make_request('POST', path)
     body = response.read()
     if response.status >= 300:
         raise SQSError(response.status, response.reason, body)
     q = Queue(self)
     h = handler.XmlHandler(q, self)
     xml.sax.parseString(body, h)
     return q
Esempio n. 15
0
def test_post_notifications_saves_email_or_sms_normally_if_saving_to_queue_fails(
    client, notify_db_session, mocker, notification_type
):
    save_task = mocker.patch(
        f"app.celery.tasks.save_api_{notification_type}.apply_async",
        side_effect=SQSError({'some': 'json'}, 'some opname')
    )
    mock_send_task = mocker.patch(f'app.celery.provider_tasks.deliver_{notification_type}.apply_async')

    service = create_service(
        service_name='high volume service',
    )
    with set_config_values(current_app, {
        'HIGH_VOLUME_SERVICE': [str(service.id)],

    }):
        template = create_template(service=service, content='((message))', template_type=notification_type)
        data = {
            "template_id": template.id,
            "personalisation": {"message": "Dear citizen, have a nice day"}
        }
        data.update({"email_address": "*****@*****.**"}) if notification_type == EMAIL_TYPE \
            else data.update({"phone_number": "+447700900855"})

        response = client.post(
            path=f'/v2/notifications/{notification_type}',
            data=json.dumps(data),
            headers=[('Content-Type', 'application/json'), create_authorization_header(service_id=service.id)]
        )

        json_resp = response.get_json()

        assert response.status_code == 201
        assert json_resp['id']
        assert json_resp['content']['body'] == "Dear citizen, have a nice day"
        assert json_resp['template']['id'] == str(template.id)
        save_task.assert_called_once_with([mock.ANY], queue=f'save-api-{notification_type}-tasks')
        mock_send_task.assert_called_once_with([json_resp['id']], queue=f'send-{notification_type}-tasks')
        assert Notification.query.count() == 1