Ejemplo n.º 1
0
    def test_mailgun_key_or_domain_name_not_set_raises_exception(self) -> None:
        """Test that exceptions are raised when API key or domain name are
        unset.
        """
        # Testing no mailgun api key.
        mailgun_exception = self.assertRaisesRegex( # type: ignore[no-untyped-call]
            Exception, 'Mailgun API key is not available.')
        with mailgun_exception:
            mailgun_email_services.send_email_to_recipients(
                '*****@*****.**',
                ['*****@*****.**', '*****@*****.**', '*****@*****.**'],
                'Hola 😂 - invitation to collaborate',
                'plaintext_body 😂',
                'Hi abc,<br> 😂')

        # Testing no mailgun domain name.
        swap_api = self.swap(feconf, 'MAILGUN_API_KEY', 'key')
        mailgun_exception = self.assertRaisesRegex( # type: ignore[no-untyped-call]
            Exception, 'Mailgun domain name is not set.')
        with swap_api, mailgun_exception:
            mailgun_email_services.send_email_to_recipients(
                '*****@*****.**',
                ['*****@*****.**', '*****@*****.**', '*****@*****.**'],
                'Hola 😂 - invitation to collaborate',
                'plaintext_body 😂',
                'Hi abc,<br> 😂')
Ejemplo n.º 2
0
 def test_batch_send_to_mailgun(self) -> None:
     """Test for sending HTTP POST request."""
     expected_query_url: MailgunQueryType = (
         'https://api.mailgun.net/v3/domain/messages', {
             'from': ['*****@*****.**'],
             'text': ['plaintext_body 😂'],
             'recipient_variables': [{}],
             'to': ['[\'[email protected]\', \'[email protected]\', \'[email protected]\']'],
             'html': ['Hi abc,<br> 😂'],
             'subject': ['Hola 😂 - invitation to collaborate']
         }, {
             'Authorization': 'Basic YXBpOmtleQ=='
         })
     swapped_urlopen = lambda x: self.Response(x, expected_query_url)
     swapped_request = lambda *args: args
     swap_urlopen_context = self.swap(python_utils, 'url_open',
                                      swapped_urlopen)
     swap_request_context = self.swap(python_utils, 'url_request',
                                      swapped_request)
     swap_api = self.swap(feconf, 'MAILGUN_API_KEY', 'key')
     swap_domain = self.swap(feconf, 'MAILGUN_DOMAIN_NAME', 'domain')
     with swap_urlopen_context, swap_request_context, swap_api, swap_domain:
         resp = mailgun_email_services.send_email_to_recipients(
             '*****@*****.**', ['*****@*****.**', '*****@*****.**', '*****@*****.**'],
             'Hola 😂 - invitation to collaborate', 'plaintext_body 😂',
             'Hi abc,<br> 😂')
         self.assertTrue(resp)
Ejemplo n.º 3
0
 def test_invalid_status_code_returns_false(self) -> None:
     expected_query_url: MailgunQueryType = (
         'https://api.mailgun.net/v3/domain/messages', b'from=a%40a.com&'
         b'subject=Hola+%F0%9F%98%82+-+invitation+to+collaborate&'
         b'text=plaintext_body+%F0%9F%98%82&'
         b'html=Hi+abc%2C%3Cbr%3E+%F0%9F%98%82&'
         b'to=%5B%27b%40b.com%27%2C+%27c%40c.com%27%2C+%27d%40d.com%27%5D&'
         b'recipient_variables=%7B%7D', {
             'Authorization': 'Basic'
         })
     swapped_request = lambda *args: args
     swapped_urlopen = lambda x: self.Response(x, expected_query_url)
     swap_urlopen_context = self.swap(python_utils, 'url_open',
                                      swapped_urlopen)
     swap_request_context = self.swap(python_utils, 'url_request',
                                      swapped_request)
     swap_api = self.swap(feconf, 'MAILGUN_API_KEY', 'key')
     swap_domain = self.swap(feconf, 'MAILGUN_DOMAIN_NAME', 'domain')
     with swap_urlopen_context, swap_request_context, swap_api, swap_domain:
         resp = mailgun_email_services.send_email_to_recipients(
             '*****@*****.**', ['*****@*****.**'],
             'Hola 😂 - invitation to collaborate',
             'plaintext_body 😂',
             'Hi abc,<br> 😂',
             bcc=['*****@*****.**', '*****@*****.**'],
             reply_to='abc',
             recipient_variables=({
                 '*****@*****.**': {
                     'first': 'Bob',
                     'id': 1
                 }
             }))
         self.assertFalse(resp)
Ejemplo n.º 4
0
 def test_batch_send_to_mailgun(self) -> None:
     """Test for sending HTTP POST request."""
     expected_query_url: MailgunQueryType = (
         'https://api.mailgun.net/v3/domain/messages', b'from=a%40a.com&'
         b'subject=Hola+%F0%9F%98%82+-+invitation+to+collaborate&'
         b'text=plaintext_body+%F0%9F%98%82&'
         b'html=Hi+abc%2C%3Cbr%3E+%F0%9F%98%82&'
         b'to=%5B%27b%40b.com%27%2C+%27c%40c.com%27%2C+%27d%40d.com%27%5D&'
         b'recipient_variables=%7B%7D', {
             'Authorization': 'Basic YXBpOmtleQ=='
         })
     swapped_urlopen = lambda x: self.Response(x, expected_query_url)
     swapped_request = lambda *args: args
     swap_urlopen_context = self.swap(python_utils, 'url_open',
                                      swapped_urlopen)
     swap_request_context = self.swap(python_utils, 'url_request',
                                      swapped_request)
     swap_api = self.swap(feconf, 'MAILGUN_API_KEY', 'key')
     swap_domain = self.swap(feconf, 'MAILGUN_DOMAIN_NAME', 'domain')
     with swap_urlopen_context, swap_request_context, swap_api, swap_domain:
         resp = mailgun_email_services.send_email_to_recipients(
             '*****@*****.**', ['*****@*****.**', '*****@*****.**', '*****@*****.**'],
             'Hola 😂 - invitation to collaborate', 'plaintext_body 😂',
             'Hi abc,<br> 😂')
         self.assertTrue(resp)
 def test_invalid_status_code_returns_false(self):
     expected_query_url = (
         'https://api.mailgun.net/v3/domain/messages',
         (
             'from=a%40a.com&h%3AReply-To=abc&text=plaintext_body+%F0%9F' +
             '%98%82&bcc=%5Bu%27c%40c.com%27%2C+u%27d%40d.com%27%5D&' +
             'recipient_variables=%7Bu%27b%40b.com%27%3A+%7Bu%27id%27%3A+' +
             '1%2C+u%27first%27%3A+u%27Bob%27%7D%7D&to=b%40b.com&html=' +
             'Hi+abc%2C%3Cbr%3E+%F0%9F%98%82&subject=Hola+%F0%9F%98%82' +
             '+-+invitation+to+collaborate'),
         {'Authorization': 'Basic'})
     swapped_request = lambda *args: args
     swapped_urlopen = lambda x: self.Response(x, expected_query_url)
     swap_urlopen_context = self.swap(
         python_utils, 'url_open', swapped_urlopen)
     swap_request_context = self.swap(
         python_utils, 'url_request', swapped_request)
     swap_api = self.swap(feconf, 'MAILGUN_API_KEY', 'key')
     swap_domain = self.swap(feconf, 'MAILGUN_DOMAIN_NAME', 'domain')
     with swap_urlopen_context, swap_request_context, swap_api, swap_domain:
         resp = mailgun_email_services.send_email_to_recipients(
             '*****@*****.**',
             ['*****@*****.**'],
             (
                 'Hola 😂 - invitation to collaborate'
                 .encode(encoding='utf-8')),
             'plaintext_body 😂'.encode(encoding='utf-8'),
             'Hi abc,<br> 😂'.encode(encoding='utf-8'),
             bcc=['*****@*****.**', '*****@*****.**'],
             reply_to='abc',
             recipient_variables=({'*****@*****.**': {'first': 'Bob', 'id': 1}}))
         self.assertFalse(resp)
Ejemplo n.º 6
0
 def test_invalid_status_code_returns_false(self) -> None:
     expected_query_url: MailgunQueryType = (
         'https://api.mailgun.net/v3/domain/messages',
         {
             'from': ['*****@*****.**'],
             'h:Reply-To': ['abc'],
             'text': ['plaintext_body 😂'],
             'bcc': ['[\'[email protected]\', \'[email protected]\']'],
             'recipient_variables': [
                 {'*****@*****.**': {'id': 1, 'first': 'Bob'}}
             ],
             'to': ['*****@*****.**'],
             'html': ['Hi abc,<br> 😂'],
             'subject': ['Hola 😂 - invitation to collaborate']
         },
         {'Authorization': 'Basic'})
     swapped_request = lambda *args: args
     swapped_urlopen = lambda x: self.Response(x, expected_query_url)
     swap_urlopen_context = self.swap(
         python_utils, 'url_open', swapped_urlopen)
     swap_request_context = self.swap(
         python_utils, 'url_request', swapped_request)
     swap_api = self.swap(feconf, 'MAILGUN_API_KEY', 'key')
     swap_domain = self.swap(feconf, 'MAILGUN_DOMAIN_NAME', 'domain')
     with swap_urlopen_context, swap_request_context, swap_api, swap_domain:
         resp = mailgun_email_services.send_email_to_recipients(
             '*****@*****.**',
             ['*****@*****.**'],
             'Hola 😂 - invitation to collaborate',
             'plaintext_body 😂',
             'Hi abc,<br> 😂',
             bcc=['*****@*****.**', '*****@*****.**'],
             reply_to='abc',
             recipient_variables=({'*****@*****.**': {'first': 'Bob', 'id': 1}}))
         self.assertFalse(resp)
Ejemplo n.º 7
0
    def test_send_email_to_mailgun(self) -> None:
        """Test for sending HTTP POST request."""
        # Test sending email without bcc, reply_to or recipient_variables.
        expected_query_url: MailgunQueryType = (
            'https://api.mailgun.net/v3/domain/messages', {
                'from': ['*****@*****.**'],
                'text': ['plaintext_body 😂'],
                'recipient_variables': [{}],
                'to': ['*****@*****.**'],
                'html': ['Hi abc,<br> 😂'],
                'subject': ['Hola 😂 - invitation to collaborate']
            }, {
                'Authorization': 'Basic YXBpOmtleQ=='
            })
        swapped_urlopen = lambda x: self.Response(x, expected_query_url)
        swapped_request = lambda *args: args
        swap_urlopen_context = self.swap(python_utils, 'url_open',
                                         swapped_urlopen)
        swap_request_context = self.swap(python_utils, 'url_request',
                                         swapped_request)
        swap_api = self.swap(feconf, 'MAILGUN_API_KEY', 'key')
        swap_domain = self.swap(feconf, 'MAILGUN_DOMAIN_NAME', 'domain')
        with swap_urlopen_context, swap_request_context, swap_api, swap_domain:
            resp = mailgun_email_services.send_email_to_recipients(
                '*****@*****.**', ['*****@*****.**'],
                'Hola 😂 - invitation to collaborate', 'plaintext_body 😂',
                'Hi abc,<br> 😂')
            self.assertTrue(resp)

        # Test sending email with single bcc and single recipient email.
        expected_query_url = ('https://api.mailgun.net/v3/domain/messages', {
            'from': ['*****@*****.**'],
            'h:Reply-To': ['abc'],
            'text': ['plaintext_body 😂'],
            'bcc': ['*****@*****.**'],
            'recipient_variables': [{
                '*****@*****.**': {
                    'first': 'Bob',
                    'id': 1
                }
            }],
            'to': ['*****@*****.**'],
            'html': ['Hi abc,<br> 😂'],
            'subject': ['Hola 😂 - invitation to collaborate']
        }, {
            'Authorization': 'Basic YXBpOmtleQ=='
        })
        swapped_urlopen = lambda x: self.Response(x, expected_query_url)
        swap_urlopen_context = self.swap(python_utils, 'url_open',
                                         swapped_urlopen)
        swap_request_context = self.swap(python_utils, 'url_request',
                                         swapped_request)
        swap_api = self.swap(feconf, 'MAILGUN_API_KEY', 'key')
        swap_domain = self.swap(feconf, 'MAILGUN_DOMAIN_NAME', 'domain')
        with swap_urlopen_context, swap_request_context, swap_api, swap_domain:
            resp = mailgun_email_services.send_email_to_recipients(
                '*****@*****.**', ['*****@*****.**'],
                'Hola 😂 - invitation to collaborate',
                'plaintext_body 😂',
                'Hi abc,<br> 😂',
                bcc=['*****@*****.**'],
                reply_to='abc',
                recipient_variables={'*****@*****.**': {
                    'first': 'Bob',
                    'id': 1
                }})
            self.assertTrue(resp)

        # Test sending email with single bcc, and multiple recipient emails
        # differentiated by recipient_variables ids.
        expected_query_url = ('https://api.mailgun.net/v3/domain/messages', {
            'from': ['*****@*****.**'],
            'h:Reply-To': ['abc'],
            'text': ['plaintext_body 😂'],
            'bcc': ['[\'[email protected]\', \'[email protected]\']'],
            'recipient_variables': [{
                '*****@*****.**': {
                    'id': 1,
                    'first': 'Bob'
                }
            }],
            'to': ['*****@*****.**'],
            'html': ['Hi abc,<br> 😂'],
            'subject': ['Hola 😂 - invitation to collaborate']
        }, {
            'Authorization': 'Basic YXBpOmtleQ=='
        })
        swapped_urlopen = lambda x: self.Response(x, expected_query_url)
        swap_urlopen_context = self.swap(python_utils, 'url_open',
                                         swapped_urlopen)
        swap_request_context = self.swap(python_utils, 'url_request',
                                         swapped_request)
        swap_api = self.swap(feconf, 'MAILGUN_API_KEY', 'key')
        swap_domain = self.swap(feconf, 'MAILGUN_DOMAIN_NAME', 'domain')
        with swap_urlopen_context, swap_request_context, swap_api, swap_domain:
            resp = mailgun_email_services.send_email_to_recipients(
                '*****@*****.**', ['*****@*****.**'],
                'Hola 😂 - invitation to collaborate',
                'plaintext_body 😂',
                'Hi abc,<br> 😂',
                bcc=['*****@*****.**', '*****@*****.**'],
                reply_to='abc',
                recipient_variables=({
                    '*****@*****.**': {
                        'first': 'Bob',
                        'id': 1
                    }
                }))
            self.assertTrue(resp)
Ejemplo n.º 8
0
    def test_send_email_to_mailgun(self) -> None:
        """Test for sending HTTP POST request."""
        # Test sending email without bcc, reply_to or recipient_variables.
        expected_query_url: MailgunQueryType = (
            'https://api.mailgun.net/v3/domain/messages', b'from=a%40a.com&'
            b'subject=Hola+%F0%9F%98%82+-+invitation+to+collaborate&'
            b'text=plaintext_body+%F0%9F%98%82&'
            b'html=Hi+abc%2C%3Cbr%3E+%F0%9F%98%82&'
            b'to=b%40b.com&'
            b'recipient_variables=%7B%7D', {
                'Authorization': 'Basic YXBpOmtleQ=='
            })
        swapped_urlopen = lambda x: self.Response(x, expected_query_url)
        swapped_request = lambda *args: args
        swap_urlopen_context = self.swap(python_utils, 'url_open',
                                         swapped_urlopen)
        swap_request_context = self.swap(python_utils, 'url_request',
                                         swapped_request)
        swap_api = self.swap(feconf, 'MAILGUN_API_KEY', 'key')
        swap_domain = self.swap(feconf, 'MAILGUN_DOMAIN_NAME', 'domain')
        with swap_urlopen_context, swap_request_context, swap_api, swap_domain:
            resp = mailgun_email_services.send_email_to_recipients(
                '*****@*****.**', ['*****@*****.**'],
                'Hola 😂 - invitation to collaborate', 'plaintext_body 😂',
                'Hi abc,<br> 😂')
            self.assertTrue(resp)

        # Test sending email with single bcc and single recipient email.
        expected_query_url = (
            'https://api.mailgun.net/v3/domain/messages', b'from=a%40a.com&'
            b'subject=Hola+%F0%9F%98%82+-+invitation+to+collaborate&'
            b'text=plaintext_body+%F0%9F%98%82&'
            b'html=Hi+abc%2C%3Cbr%3E+%F0%9F%98%82&'
            b'to=b%40b.com&'
            b'bcc=c%40c.com&'
            b'h%3AReply-To=abc&'
            b'recipient_variables=%7B%27b%40b.com'
            b'%27%3A+%7B%27first%27%3A+%27Bob%27%2C+%27id%27%3A+1%7D%7D', {
                'Authorization': 'Basic YXBpOmtleQ=='
            })
        swapped_urlopen = lambda x: self.Response(x, expected_query_url)
        swap_urlopen_context = self.swap(python_utils, 'url_open',
                                         swapped_urlopen)
        swap_request_context = self.swap(python_utils, 'url_request',
                                         swapped_request)
        swap_api = self.swap(feconf, 'MAILGUN_API_KEY', 'key')
        swap_domain = self.swap(feconf, 'MAILGUN_DOMAIN_NAME', 'domain')
        with swap_urlopen_context, swap_request_context, swap_api, swap_domain:
            resp = mailgun_email_services.send_email_to_recipients(
                '*****@*****.**', ['*****@*****.**'],
                'Hola 😂 - invitation to collaborate',
                'plaintext_body 😂',
                'Hi abc,<br> 😂',
                bcc=['*****@*****.**'],
                reply_to='abc',
                recipient_variables={'*****@*****.**': {
                    'first': 'Bob',
                    'id': 1
                }})
            self.assertTrue(resp)

        # Test sending email with single bcc, and multiple recipient emails
        # differentiated by recipient_variables ids.
        expected_query_url = (
            'https://api.mailgun.net/v3/domain/messages', b'from=a%40a.com&'
            b'subject=Hola+%F0%9F%98%82+-+invitation+to+collaborate&'
            b'text=plaintext_body+%F0%9F%98%82&'
            b'html=Hi+abc%2C%3Cbr%3E+%F0%9F%98%82&'
            b'to=b%40b.com&'
            b'bcc=%5B%27c%40c.com%27%2C+%27d%40d.com%27%5D&'
            b'h%3AReply-To=abc&'
            b'recipient_variables=%7B%27b%40b.com'
            b'%27%3A+%7B%27first%27%3A+%27Bob%27%2C+%27id%27%3A+1%7D%7D', {
                'Authorization': 'Basic YXBpOmtleQ=='
            })
        swapped_urlopen = lambda x: self.Response(x, expected_query_url)
        swap_urlopen_context = self.swap(python_utils, 'url_open',
                                         swapped_urlopen)
        swap_request_context = self.swap(python_utils, 'url_request',
                                         swapped_request)
        swap_api = self.swap(feconf, 'MAILGUN_API_KEY', 'key')
        swap_domain = self.swap(feconf, 'MAILGUN_DOMAIN_NAME', 'domain')
        with swap_urlopen_context, swap_request_context, swap_api, swap_domain:
            resp = mailgun_email_services.send_email_to_recipients(
                '*****@*****.**', ['*****@*****.**'],
                'Hola 😂 - invitation to collaborate',
                'plaintext_body 😂',
                'Hi abc,<br> 😂',
                bcc=['*****@*****.**', '*****@*****.**'],
                reply_to='abc',
                recipient_variables=({
                    '*****@*****.**': {
                        'first': 'Bob',
                        'id': 1
                    }
                }))
            self.assertTrue(resp)