Exemple #1
0
    def test_has_subject_ReturnsFalse_WhenSubjectIsEmpty(self):
        message = BasicMessage()
        message.subject = None

        actual = has_subject(message)

        self.assertFalse(actual)
Exemple #2
0
    def test_has_subject_ReturnsTrue_WhenSubjectIsNotEmpty(self):
        message = BasicMessage()
        message.subject = self.random_helper.random_string()

        actual = has_subject(message)

        self.assertTrue(actual)
Exemple #3
0
    def test_has_from_email_address_ReturnsTrue_WhenFromEmailIsNotEmpty(self):
        # Arrange
        message = BasicMessage()
        message.from_email_address = self.random_helper.random_email_address()

        # Act
        actual = has_from_email_address(message)

        # Assert
        self.assertTrue(actual)
Exemple #4
0
    def test_has_api_template_ReturnsTrue_WhenApiTemplateIsNotMinValue(self):
        # Arrange
        message = BasicMessage()
        message.api_template = self.random_helper.random_int(1, 10)

        # Act
        actual = has_api_template(message)

        # Assert
        self.assertTrue(actual)
Exemple #5
0
    def test_has_valid_reply_to_email_address_ReturnsFalse_WhenReplyToEmailIsEmpty(self):
        # Arrange
        message = BasicMessage()
        message.reply_to_email_address = EmailAddress('')

        # Act
        actual = has_valid_reply_to_email_address(message)

        # Assert
        self.assertFalse(actual)
Exemple #6
0
    def test_get_full_recipient_count_BasicMessage_ReturnsGT0_WhenOnlyBccRecipientsHasOneValue(self):
        # Arrange
        message = BasicMessage()
        message.bcc_email_address = self.random_helper.random_list_of_email_addresses(1)

        # Act
        actual = get_full_recipient_count(message)

        # Assert
        self.assertTrue(actual > 0)
Exemple #7
0
    def test_has_valid_reply_to_email_address_ReturnsTrue_WhenReplyToEmailIsNotEmpty(self):
        # Arrange
        message = BasicMessage()
        message.reply_to_email_address = self.random_helper.random_email_address()

        # Act
        actual = has_valid_reply_to_email_address(message)

        # Assert
        self.assertTrue(actual)
Exemple #8
0
    def test_validate_email_addresses_BasicMessage_ReturnsSuccess_WhenBccIsNotEmpty(self):
        #  Arrange
        message = BasicMessage()
        message.bcc_email_address = [self.random_helper.random_email_address()]

        #  Act
        actual = validate_email_addresses(message)

        #  Assert
        self.assertEqual(SendResult.Success, actual.result)
Exemple #9
0
    def test_has_invalid_email_addresses_BasicMessage_ReturnsListOfOne_WhenBccHasOneInvalid(self):
        # Arrange
        message = BasicMessage()
        message.bcc_email_address = [EmailAddress(self.random_helper.random_string())]

        # Act
        actual = has_invalid_email_addresses(message)

        # Assert
        self.assertEqual(1, len(actual))
Exemple #10
0
    def test_has_api_template_ReturnsFalse_WhenApiTemplateIsLessThanZero(self):
        # Arrange
        message = BasicMessage()
        message.api_template = -1

        # Act
        actual = has_api_template(message)

        # Assert
        self.assertFalse(actual)
Exemple #11
0
    def test_has_valid_reply_to_email_address_ReturnsTrue_WhenReplyToEmailIsNone(self):
        # Arrange
        message = BasicMessage()
        message.reply_to_email_address = None

        # Act
        actual = has_valid_reply_to_email_address(message)

        # Assert
        self.assertTrue(actual)
Exemple #12
0
    def test_has_from_email_address_ReturnsFalse_WhenFromEmailIsEmpty(self):
        # Arrange
        message = BasicMessage()
        message.from_email_address = EmailAddress('')

        # Act
        actual = has_from_email_address(message)

        # Assert
        self.assertFalse(actual)
Exemple #13
0
    def test_validate_email_addresses_BasicMessage_ReturnsTooManyRecipients_WhenToHasToManyRecipients(self):
        #  Arrange
        num_in_list = int(maximumRecipientsPerMessage + 1)
        message = BasicMessage()
        message.to_email_address = self.random_helper.random_list_of_email_addresses(num_in_list)

        #  Act
        actual = validate_email_addresses(message)

        #  Assert
        self.assertEqual(SendResult.RecipientValidationMaxExceeded, actual.result)
Exemple #14
0
    def test_validate_base_message_ReturnsMessageValidationEmptySubject_WhenSubjectIsEmpty(self):
        
        #  Arrange
        message = BasicMessage()
        message.subject = None

        #  Act
        actual = validate_base_message(message)

        #  Assert
        self.assertEqual(SendResult.MessageValidationEmptySubject, actual)
Exemple #15
0
    def test_validate_base_message_ReturnsEmailAddressValidationInvalidFrom_WhenFromRecipientIsInvalid(self):
        #  Arrange
        message = BasicMessage()
        message.subject = self.random_helper.random_string()
        message.from_email_address = EmailAddress("$$##%%")

        #  Act
        actual = validate_base_message(message)

        #  Assert
        self.assertEqual(SendResult.EmailAddressValidationInvalidFrom, actual)
Exemple #16
0
    def test_has_invalid_email_addresses_BasicMessage_ReturnsNull_WhenNoInvalidRecipientsFound(self):
        # Arrange
        message = BasicMessage()
        message.to_email_address = self.random_helper.random_list_of_email_addresses(1)
        message.cc_email_address = self.random_helper.random_list_of_email_addresses(1)
        message.bcc_email_address = self.random_helper.random_list_of_email_addresses(1)

        # Act
        actual = has_invalid_email_addresses(message)

        # Assert
        self.assertIsNone(actual)
Exemple #17
0
    def test_has_message_body_ReturnsTrue_WhenApiTemplateIsNotEmptyAndPlainTextBodyAndHtmlBodyIsEmpty(self):
        # Arrange
        message = BasicMessage()
        message.html_body = None
        message.plain_text_body = None
        message.api_template = self.random_helper.random_int()

        # Act
        actual = has_message_body(message)

        # Assert
        self.assertTrue(actual)
Exemple #18
0
    def test_has_message_body_ReturnsFalse_WhenHtmlBodyAndPlainTextBodyAndApiTemplateIsEmpty(self):
        # Arrange
        message = BasicMessage()
        message.html_body = None
        message.plain_text_body = None
        message.api_template = None

        # Act
        actual = has_message_body(message)

        # Assert
        self.assertFalse(actual)
Exemple #19
0
    def test_validate_base_message_ReturnsSuccess_WhenSubjectAndFromRecipientAndApiTemplateIsNotEmpty(self):
        #  Arrange
        message = BasicMessage()
        message.subject = self.random_helper.random_string()
        message.from_email_address = self.random_helper.random_email_address()
        message.api_template = self.random_helper.random_int()

        #  Act
        actual = validate_base_message(message)

        #  Assert
        self.assertEqual(SendResult.Success, actual)
Exemple #20
0
    def test_validate_email_addresses_BasicMessage_ReturnsNoRecipients_WhenToAndCcAndBccIsNone(self):
        #  Arrange
        message = BasicMessage()

        #  Act
        actual = validate_email_addresses(message)

        #  Assert
        self.assertEqual(SendResult.RecipientValidationNoneInMessage, actual.result)
Exemple #21
0
    def test_get_full_recipient_count_BasicMessage_Returns0_WhenNoRecipientsAdded(self):
        # Arrange
        message = BasicMessage()

        # Act
        actual = get_full_recipient_count(message)

        # Assert
        self.assertEqual(0, actual)
Exemple #22
0
def _inbound_logic(json_body):
    message_data = json_body['Message']
    email_to = parseaddr(message_data['To'][0]['EmailAddress'])[1]
    local_portion = email_to.split('@')[0]
    from_address = parseaddr(message_data['From']['EmailAddress'])[1]
    subject = message_data.get('Subject')
    text = message_data.get('TextBody')
    html = message_data.get('HtmlBody')

    # TODO: do we need this in SocketLabs?
    # 404s make sendgrid retry the email, so respond with 200 even if
    # the address isn't found
    try:
        relay_address = RelayAddress.objects.get(address=local_portion)
    except RelayAddress.DoesNotExist as e:
        print(e)
        return HttpResponse("Address does not exist")

    # Forward to real email address
    sl_message = BasicMessage()
    sl_message.subject = subject
    sl_message.html_body = html
    sl_message.plain_text_body = text
    relay_from_address, relay_from_display = _generate_relay_From(from_address)
    sl_message.from_email_address = EmailAddress(
        relay_from_address, relay_from_display
    )
    sl_message.to_email_address.append(EmailAddress(relay_address.user.email))
    socketlabs_client = SocketLabsClient(
        settings.SOCKETLABS_SERVER_ID, settings.SOCKETLABS_API_KEY
    )
    response = socketlabs_client.send(sl_message)
    print(response)
Exemple #23
0
    def test_validate_base_message_ReturnsMessageValidationInvalidCustomHeaders_WhenCustomHeadersAreInvalid(self):
        #  Arrange
        message = BasicMessage()
        message.subject = self.random_helper.random_string()
        message.from_email_address = self.random_helper.random_email_address()
        message.html_body = self.random_helper.random_string()
        message.add_custom_header("", "")

        #  Act
        actual = validate_base_message(message)

        #  Assert
        self.assertEqual(SendResult.MessageValidationInvalidCustomHeaders, actual)
Exemple #24
0
def _inbound_logic(json_body):
    message_data = json_body['Message']
    email_to = parseaddr(message_data['To'][0]['EmailAddress'])[1]
    local_portion = email_to.split('@')[0]
    from_address = parseaddr(message_data['From']['EmailAddress'])[1]
    subject = message_data.get('Subject')
    text = message_data.get('TextBody')
    html = message_data.get('HtmlBody')

    # TODO: do we need this in SocketLabs?
    # 404s make sendgrid retry the email, so respond with 200 even if
    # the address isn't found
    try:
        relay_address = RelayAddress.objects.get(address=local_portion)
        if not relay_address.enabled:
            relay_address.num_blocked += 1
            relay_address.save(update_fields=['num_blocked'])
            return HttpResponse("Address does not exist")
    except RelayAddress.DoesNotExist as e:
        # TODO?: if sha256 of the address is in DeletedAddresses,
        # create a hard bounce receipt rule
        print(e)
        return HttpResponse("Address does not exist")

    logger.info(
        'email_relay',
        extra={
            'fxa_uid': (relay_address.user.socialaccount_set.first().uid),
            'relay_address_id':
            relay_address.id,
            'relay_address':
            sha256(local_portion.encode('utf-8')).hexdigest(),
            'real_address':
            sha256(relay_address.user.email.encode('utf-8')).hexdigest(),
        })
    # Forward to real email address
    sl_message = BasicMessage()
    sl_message.subject = subject
    sl_message.html_body = html
    sl_message.plain_text_body = text
    relay_from_address, relay_from_display = _generate_relay_From(from_address)
    sl_message.from_email_address = EmailAddress(relay_from_address,
                                                 relay_from_display)
    sl_message.to_email_address.append(EmailAddress(relay_address.user.email))
    sl_client = _get_socketlabs_client()
    response = _socketlabs_send(sl_client, sl_message)
    # if _socketlabs_send returns a django HttpResponse return it immediately
    if type(response) == HttpResponse:
        return response
    if not response.result.name == 'Success':
        logger.error('socketlabs_error', extra=response.to_json())
        return HttpResponse("Internal Server Error", status=500)
    relay_address.num_forwarded += 1
    relay_address.last_used_at = datetime.now()
    relay_address.save(update_fields=['num_forwarded', 'last_used_at'])
    return HttpResponse("Created", status=201)
Exemple #25
0
    def test_validate_base_message_ReturnsMessageValidationEmptyMessage_WhenAllBodyFieldsAreEmpty(self):
        #  Arrange
        message = BasicMessage()
        message.subject = self.random_helper.random_string()
        message.from_email_address = self.random_helper.random_email_address()
        message.html_body = None
        message.plain_text_body = None
        message.api_template = None

        #  Act
        actual = validate_base_message(message)

        #  Assert
        self.assertEqual(SendResult.MessageValidationEmptyMessage, actual)
def email_invited_user(invitee, invitation):
    context = {
        'email': invitee.primary_email,
        'current_domain': settings.SITE_ORIGIN,
    }

    # Send email invite
    sl_message = BasicMessage()
    sl_message.subject = (
        "Firefox Relay beta: Protect your real email address from hackers and trackers"
    )

    sl_message.html_body = render_to_string(
        'emails/beta_invite_html_email.html',
        context,
    )
    sl_message.plain_text_body = render_to_string(
        'emails/beta_invite_text_email.txt',
        context,
    )

    relay_display_name, relay_from_address = parseaddr(
        settings.RELAY_FROM_ADDRESS)
    sl_message.from_email_address = EmailAddress(relay_from_address)
    sl_message.to_email_address.append(EmailAddress(invitee.primary_email))

    sl_client = get_socketlabs_client()
    response = socketlabs_send(sl_client, sl_message)

    if not response.result.name == 'Success':
        logger.error('socketlabs_error', extra=response.to_json())
        return response

    invitation.date_sent = datetime.now(timezone.utc)
    invitation.save(update_fields=['date_sent'])

    invitee.waitlists_joined['email_relay']['notified'] = True
    invitee.save(update_fields=['waitlists_joined'])

    return response
from socketlabs.injectionapi import SocketLabsClient
from socketlabs.injectionapi.message.basicmessage import BasicMessage
from socketlabs.injectionapi.message.emailaddress import EmailAddress

serverId = #######
injectionApiKey = "????????????????"

client = SocketLabsClient(serverId, injectionApiKey);

message = BasicMessage()

message.subject = "Prove of Concept - Socketlab Email API"
message.html_body = "<html>This is a test email sent through a .py file testing the email API. Text me if you get this email. Thanks - Ron</html>"
message.plain_text_body = "This is a test email sent through a .py file testing the email API. Text me if you get this email. Thanks - Ron";

message.from_email_address = EmailAddress("no-reply@pencil_it_in.com")
message.to_email_address.append(EmailAddress("*****@*****.**", "Ron Villena"))
message.to_email_address.append(EmailAddress("*****@*****.**", "Zachary Lake"))
message.to_email_address.append(EmailAddress("*****@*****.**", "Jeannie Halvorson"))
message.to_email_address.append(EmailAddress("*****@*****.**", "Depinder Kaur"))

response = client.send(message)

# Instructions for creating email
# https://cp.socketlabs.com/servers/32586/launch/languageselection/codesample?language=json