|
| 1 | +from unittest.mock import Mock, patch |
| 2 | + |
| 3 | +from nylas.resources.transactional_send import TransactionalSend |
| 4 | + |
| 5 | + |
| 6 | +class TestTransactionalSend: |
| 7 | + def test_send_transactional_message(self, http_client_response): |
| 8 | + transactional_send = TransactionalSend(http_client_response) |
| 9 | + request_body = { |
| 10 | + "subject": "Welcome", |
| 11 | + "to": [{"name": "Jane Doe", "email": "jane.doe@example.com"}], |
| 12 | + "from_": {"name": "ACME Support", "email": "support@acme.com"}, |
| 13 | + "body": "Welcome to ACME.", |
| 14 | + } |
| 15 | + |
| 16 | + transactional_send.send(domain_name="mail.acme.com", request_body=request_body) |
| 17 | + |
| 18 | + http_client_response._execute.assert_called_once_with( |
| 19 | + method="POST", |
| 20 | + path="/v3/domains/mail.acme.com/messages/send", |
| 21 | + request_body={ |
| 22 | + "subject": "Welcome", |
| 23 | + "to": [{"name": "Jane Doe", "email": "jane.doe@example.com"}], |
| 24 | + "from": {"name": "ACME Support", "email": "support@acme.com"}, |
| 25 | + "body": "Welcome to ACME.", |
| 26 | + }, |
| 27 | + data=None, |
| 28 | + overrides=None, |
| 29 | + ) |
| 30 | + |
| 31 | + def test_send_domain_name_url_encoded(self, http_client_response): |
| 32 | + transactional_send = TransactionalSend(http_client_response) |
| 33 | + request_body = { |
| 34 | + "to": [{"email": "a@b.com"}], |
| 35 | + "from_": {"email": "support@acme.com"}, |
| 36 | + } |
| 37 | + |
| 38 | + transactional_send.send( |
| 39 | + domain_name="weird/slash.com", |
| 40 | + request_body=request_body, |
| 41 | + ) |
| 42 | + |
| 43 | + http_client_response._execute.assert_called_once_with( |
| 44 | + method="POST", |
| 45 | + path="/v3/domains/weird%2Fslash.com/messages/send", |
| 46 | + request_body={ |
| 47 | + "to": [{"email": "a@b.com"}], |
| 48 | + "from": {"email": "support@acme.com"}, |
| 49 | + }, |
| 50 | + data=None, |
| 51 | + overrides=None, |
| 52 | + ) |
| 53 | + |
| 54 | + def test_send_small_attachment(self, http_client_response): |
| 55 | + transactional_send = TransactionalSend(http_client_response) |
| 56 | + request_body = { |
| 57 | + "to": [{"email": "j@example.com"}], |
| 58 | + "from_": {"email": "support@acme.com"}, |
| 59 | + "attachments": [ |
| 60 | + { |
| 61 | + "filename": "file1.txt", |
| 62 | + "content_type": "text/plain", |
| 63 | + "content": "this is a file", |
| 64 | + "size": 3, |
| 65 | + }, |
| 66 | + ], |
| 67 | + } |
| 68 | + |
| 69 | + transactional_send.send(domain_name="acme.com", request_body=request_body) |
| 70 | + |
| 71 | + http_client_response._execute.assert_called_once_with( |
| 72 | + method="POST", |
| 73 | + path="/v3/domains/acme.com/messages/send", |
| 74 | + request_body=request_body, |
| 75 | + data=None, |
| 76 | + overrides=None, |
| 77 | + ) |
| 78 | + |
| 79 | + def test_send_large_attachment(self, http_client_response): |
| 80 | + transactional_send = TransactionalSend(http_client_response) |
| 81 | + mock_encoder = Mock() |
| 82 | + request_body = { |
| 83 | + "to": [{"email": "j@example.com"}], |
| 84 | + "from_": {"email": "support@acme.com"}, |
| 85 | + "attachments": [ |
| 86 | + { |
| 87 | + "filename": "file1.txt", |
| 88 | + "content_type": "text/plain", |
| 89 | + "content": "this is a file", |
| 90 | + "size": 3 * 1024 * 1024, |
| 91 | + }, |
| 92 | + ], |
| 93 | + } |
| 94 | + |
| 95 | + with patch( |
| 96 | + "nylas.resources.transactional_send._build_form_request", |
| 97 | + return_value=mock_encoder, |
| 98 | + ): |
| 99 | + transactional_send.send(domain_name="acme.com", request_body=request_body) |
| 100 | + |
| 101 | + http_client_response._execute.assert_called_once_with( |
| 102 | + method="POST", |
| 103 | + path="/v3/domains/acme.com/messages/send", |
| 104 | + request_body=None, |
| 105 | + data=mock_encoder, |
| 106 | + overrides=None, |
| 107 | + ) |
| 108 | + |
| 109 | + def test_send_with_existing_from_field_unchanged(self, http_client_response): |
| 110 | + transactional_send = TransactionalSend(http_client_response) |
| 111 | + request_body = { |
| 112 | + "to": [{"email": "j@example.com"}], |
| 113 | + "from": {"email": "direct@acme.com"}, |
| 114 | + "from_": {"email": "ignored@acme.com"}, |
| 115 | + } |
| 116 | + |
| 117 | + transactional_send.send(domain_name="acme.com", request_body=request_body) |
| 118 | + |
| 119 | + http_client_response._execute.assert_called_once_with( |
| 120 | + method="POST", |
| 121 | + path="/v3/domains/acme.com/messages/send", |
| 122 | + request_body=request_body, |
| 123 | + data=None, |
| 124 | + overrides=None, |
| 125 | + ) |
0 commit comments