aboutsummaryrefslogtreecommitdiff
path: root/cros_utils/email_sender_unittest.py
diff options
context:
space:
mode:
Diffstat (limited to 'cros_utils/email_sender_unittest.py')
-rwxr-xr-xcros_utils/email_sender_unittest.py204
1 files changed, 103 insertions, 101 deletions
diff --git a/cros_utils/email_sender_unittest.py b/cros_utils/email_sender_unittest.py
index ae41f143..66ec6a2d 100755
--- a/cros_utils/email_sender_unittest.py
+++ b/cros_utils/email_sender_unittest.py
@@ -1,13 +1,12 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
-# Copyright 2020 The Chromium OS Authors. All rights reserved.
+# Copyright 2020 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Tests for email_sender."""
-from __future__ import print_function
import contextlib
import io
@@ -19,102 +18,105 @@ import cros_utils.email_sender as email_sender
class Test(unittest.TestCase):
- """Tests for email_sender."""
-
- @mock.patch('cros_utils.email_sender.AtomicallyWriteFile')
- def test_x20_email_sending_rejects_invalid_inputs(self, write_file):
- test_cases = [
- {
- # no subject
- 'subject': '',
- 'identifier': 'foo',
- 'direct_recipients': ['gbiv@google.com'],
- 'text_body': 'hi',
- },
- {
- 'subject': 'foo',
- # no identifier
- 'identifier': '',
- 'direct_recipients': ['gbiv@google.com'],
- 'text_body': 'hi',
- },
- {
- 'subject': 'foo',
- 'identifier': 'foo',
- # no recipients
- 'direct_recipients': [],
- 'text_body': 'hi',
- },
- {
- 'subject': 'foo',
- 'identifier': 'foo',
- 'direct_recipients': ['gbiv@google.com'],
- # no body
- },
- {
- 'subject': 'foo',
- 'identifier': 'foo',
- # direct recipients lack @google.
- 'direct_recipients': ['gbiv'],
- 'text_body': 'hi',
- },
- {
- 'subject': 'foo',
- 'identifier': 'foo',
- # non-list recipients
- 'direct_recipients': 'gbiv@google.com',
- 'text_body': 'hi',
- },
- {
- 'subject': 'foo',
- 'identifier': 'foo',
- # non-list recipients
- 'well_known_recipients': 'detective',
- 'text_body': 'hi',
- },
- ]
-
- sender = email_sender.EmailSender()
- for case in test_cases:
- with self.assertRaises(ValueError):
- sender.SendX20Email(**case)
-
- write_file.assert_not_called()
-
- @mock.patch('cros_utils.email_sender.AtomicallyWriteFile')
- def test_x20_email_sending_translates_to_reasonable_json(self, write_file):
- written_obj = None
-
- @contextlib.contextmanager
- def actual_write_file(file_path):
- nonlocal written_obj
-
- self.assertTrue(file_path.startswith(email_sender.X20_PATH + '/'),
- file_path)
- f = io.StringIO()
- yield f
- written_obj = json.loads(f.getvalue())
-
- write_file.side_effect = actual_write_file
- email_sender.EmailSender().SendX20Email(
- subject='hello',
- identifier='world',
- well_known_recipients=['detective'],
- direct_recipients=['gbiv@google.com'],
- text_body='text',
- html_body='html',
- )
-
- self.assertEqual(
- written_obj, {
- 'subject': 'hello',
- 'email_identifier': 'world',
- 'well_known_recipients': ['detective'],
- 'direct_recipients': ['gbiv@google.com'],
- 'body': 'text',
- 'html_body': 'html',
- })
-
-
-if __name__ == '__main__':
- unittest.main()
+ """Tests for email_sender."""
+
+ @mock.patch("cros_utils.email_sender.AtomicallyWriteFile")
+ def test_x20_email_sending_rejects_invalid_inputs(self, write_file):
+ test_cases = [
+ {
+ # no subject
+ "subject": "",
+ "identifier": "foo",
+ "direct_recipients": ["gbiv@google.com"],
+ "text_body": "hi",
+ },
+ {
+ "subject": "foo",
+ # no identifier
+ "identifier": "",
+ "direct_recipients": ["gbiv@google.com"],
+ "text_body": "hi",
+ },
+ {
+ "subject": "foo",
+ "identifier": "foo",
+ # no recipients
+ "direct_recipients": [],
+ "text_body": "hi",
+ },
+ {
+ "subject": "foo",
+ "identifier": "foo",
+ "direct_recipients": ["gbiv@google.com"],
+ # no body
+ },
+ {
+ "subject": "foo",
+ "identifier": "foo",
+ # direct recipients lack @google.
+ "direct_recipients": ["gbiv"],
+ "text_body": "hi",
+ },
+ {
+ "subject": "foo",
+ "identifier": "foo",
+ # non-list recipients
+ "direct_recipients": "gbiv@google.com",
+ "text_body": "hi",
+ },
+ {
+ "subject": "foo",
+ "identifier": "foo",
+ # non-list recipients
+ "well_known_recipients": "detective",
+ "text_body": "hi",
+ },
+ ]
+
+ sender = email_sender.EmailSender()
+ for case in test_cases:
+ with self.assertRaises(ValueError):
+ sender.SendX20Email(**case)
+
+ write_file.assert_not_called()
+
+ @mock.patch("cros_utils.email_sender.AtomicallyWriteFile")
+ def test_x20_email_sending_translates_to_reasonable_json(self, write_file):
+ written_obj = None
+
+ @contextlib.contextmanager
+ def actual_write_file(file_path):
+ nonlocal written_obj
+
+ self.assertTrue(
+ file_path.startswith(email_sender.X20_PATH + "/"), file_path
+ )
+ f = io.StringIO()
+ yield f
+ written_obj = json.loads(f.getvalue())
+
+ write_file.side_effect = actual_write_file
+ email_sender.EmailSender().SendX20Email(
+ subject="hello",
+ identifier="world",
+ well_known_recipients=["detective"],
+ direct_recipients=["gbiv@google.com"],
+ text_body="text",
+ html_body="html",
+ )
+
+ self.assertEqual(
+ written_obj,
+ {
+ "subject": "hello",
+ "email_identifier": "world",
+ "well_known_recipients": ["detective"],
+ "direct_recipients": ["gbiv@google.com"],
+ "body": "text",
+ "html_body": "html",
+ },
+ )
+
+
+if __name__ == "__main__":
+ unittest.main()