Beispiel #1
0
 def test_get_name_email_tuples(self):
     result = MailerUtils.get_name_email_tuples(['*****@*****.**','Amit Ruparel <*****@*****.**>'])
     assert len(result) == 2
     assert result[0] == (None, '*****@*****.**')
     assert result[1] == ('Amit Ruparel', '*****@*****.**')
     
     result = MailerUtils.get_name_email_tuples([None,None])
     assert result == [None,None]
     
     result = MailerUtils.get_name_email_tuples(None)
     assert result == []
Beispiel #2
0
def validate_send_message_input(input_dict):
    """
        Validates the input supplied for the POST call on the message resource.

        Args:
            input_dict - JSON input in dictionary form

        Throws:
            InvalidInputException when input is malformed or doesn't match schema
    """

    ## Validate against JSON schema
    try:
        validate(input_dict, send_input_schema_dict)
    except ValidationError as e:
        raise InvalidInputException(e.message)

    ## Validate email addresses
    invalid_emails = []

    # Validate from
    from_email = input_dict.get('from')
    if (not MailerUtils.is_email_valid(from_email)):
        invalid_emails.append(from_email)

    # Validate to
    for to_email in input_dict.get('to'):
        if (not MailerUtils.is_email_valid(to_email)):
            invalid_emails.append(to_email)

    # Validate cc
    for to_email in input_dict.get('cc', []):
        if (not MailerUtils.is_email_valid(to_email)):
            invalid_emails.append(to_email)

    # Validate bcc
    for to_email in input_dict.get('bcc', []):
        if (not MailerUtils.is_email_valid(to_email)):
            invalid_emails.append(to_email)

    if (len(invalid_emails) != 0):
        payload = {"invalid_emails": invalid_emails}
        raise InvalidInputException(message="Input contains invalid email(s)",
                                    payload=payload)
Beispiel #3
0
def get_status():
    """
        Calls should be made to this resource to get the status of a request earlier enqueued.
        The user should supply the ID of a request made previously, with the email address for which
        the status is desired.

        The input is validated against the JSON schema.

        All email fields can bear values with the name as specified in RFC-822, i.e. First Last <*****@*****.**>
    """
    # Only accept JSON
    if not request.json:
        resp = create_response(
            "Input should be specified in valid JSON format only", 400)
        return resp

    validate_get_status_input(request.json)

    name, email_address = MailerUtils.get_name_email_tuple(
        request.json.get('email'))

    # Get the job associated with the given ID from the Queue
    job_id = request.json['id']
    job = q.fetch_job(job_id)

    if (job is None):
        resp = create_response("Cannot find result for supplied ID and email",
                               404)
        return resp

    # Get relevant metadata from the job
    mailer_name = job.meta['handled_by']  # Which mailer was used
    messages_info = job.meta[
        'messages_info']  # Info about all recepients and underlying provider specific ID for the request

    # Get info about the relevant message
    single_message_info = next(
        message_info for message_info in messages_info
        if message_info.get('email_address') == email_address)
    if (single_message_info is None):
        resp = create_response(
            "Cannot find message sent to {0} during request with ID {1}".
            format(email_address, job_id), 404)
        return resp

    relevant_mailer = available_mailers[mailer_name]
    status_info = relevant_mailer.get_message_status(single_message_info)

    if (status_info is None):
        # Must have timed out
        resp = create_response(
            "This request cannot be served right now. Please try again.", 503)
        return resp

    resp = create_response(None, 200, status_info)
    return resp
Beispiel #4
0
def validate_send_message_input(input_dict):
    """
        Validates the input supplied for the POST call on the message resource.

        Args:
            input_dict - JSON input in dictionary form

        Throws:
            InvalidInputException when input is malformed or doesn't match schema
    """

    ## Validate against JSON schema
    try:
        validate(input_dict, send_input_schema_dict)
    except ValidationError as e:
        raise InvalidInputException(e.message)

    ## Validate email addresses
    invalid_emails = []
    
    # Validate from
    from_email = input_dict.get('from')
    if(not MailerUtils.is_email_valid(from_email)):
        invalid_emails.append(from_email)

    # Validate to
    for to_email in input_dict.get('to'):
        if(not MailerUtils.is_email_valid(to_email)):
            invalid_emails.append(to_email)

    # Validate cc
    for to_email in input_dict.get('cc', []):
        if(not MailerUtils.is_email_valid(to_email)):
            invalid_emails.append(to_email)

    # Validate bcc
    for to_email in input_dict.get('bcc',[]):
        if(not MailerUtils.is_email_valid(to_email)):
            invalid_emails.append(to_email)
    
    if(len(invalid_emails)!=0):
        payload = {"invalid_emails":invalid_emails}
        raise InvalidInputException(message = "Input contains invalid email(s)", payload = payload)
Beispiel #5
0
def get_status():
    """
        Calls should be made to this resource to get the status of a request earlier enqueued.
        The user should supply the ID of a request made previously, with the email address for which
        the status is desired.

        The input is validated against the JSON schema.

        All email fields can bear values with the name as specified in RFC-822, i.e. First Last <*****@*****.**>
    """
    # Only accept JSON
    if not request.json:
        resp = create_response("Input should be specified in valid JSON format only",400)
        return resp
            
    validate_get_status_input(request.json)

    name, email_address = MailerUtils.get_name_email_tuple(request.json.get('email'))

    # Get the job associated with the given ID from the Queue
    job_id = request.json['id']
    job = q.fetch_job(job_id)
    
    if(job is None):
        resp = create_response("Cannot find result for supplied ID and email", 404)
        return resp

    # Get relevant metadata from the job
    mailer_name = job.meta['handled_by'] # Which mailer was used
    messages_info = job.meta['messages_info'] # Info about all recepients and underlying provider specific ID for the request

    # Get info about the relevant message
    single_message_info = next(message_info for message_info in messages_info if message_info.get('email_address') == email_address)
    if(single_message_info is None):
        resp = create_response("Cannot find message sent to {0} during request with ID {1}".format(email_address,job_id),404)
        return resp

    relevant_mailer = available_mailers[mailer_name]
    status_info = relevant_mailer.get_message_status(single_message_info)
    
    if(status_info is None):
        # Must have timed out
        resp = create_response("This request cannot be served right now. Please try again.", 503)
        return resp

    resp = create_response(None, 200, status_info)
    return resp
Beispiel #6
0
def validate_get_status_input(input_dict):
    """
        Validates the input supplied for the POST call on the info resource.

        Args:
            input_dict (dict) - JSON input in dictionary form

        Throws:
            InvalidInputException when input is malformed or doesn't match schema for this call.
    """

    # Validate against JSON schema
    try:
        validate(input_dict, info_input_schema_dict)
    except ValidationError as e:
        raise InvalidInputException(e.message)

    # Validate email address
    email = input_dict.get('email')
    if(not MailerUtils.is_email_valid(email)):
        raise InvalidInputException(message = "Input contains invalid email: "+email)
Beispiel #7
0
def validate_get_status_input(input_dict):
    """
        Validates the input supplied for the POST call on the info resource.

        Args:
            input_dict (dict) - JSON input in dictionary form

        Throws:
            InvalidInputException when input is malformed or doesn't match schema for this call.
    """

    # Validate against JSON schema
    try:
        validate(input_dict, info_input_schema_dict)
    except ValidationError as e:
        raise InvalidInputException(e.message)

    # Validate email address
    email = input_dict.get('email')
    if (not MailerUtils.is_email_valid(email)):
        raise InvalidInputException(message="Input contains invalid email: " +
                                    email)
Beispiel #8
0
 def test_is_email_valid(self):
     assert MailerUtils.is_email_valid("Amit Ruparel <*****@*****.**>") == True
     assert MailerUtils.is_email_valid("*****@*****.**") == True
     assert MailerUtils.is_email_valid("blah") == False
     assert MailerUtils.is_email_valid("") == False
     assert MailerUtils.is_email_valid(None) == False