aboutsummaryrefslogtreecommitdiff
path: root/cros_utils/email_sender_unittest.py
blob: ae41f14329c4adf889c07a3eb4496f0777523cf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Copyright 2020 The Chromium OS Authors. All rights reserved.
# 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
import json
import unittest
import unittest.mock as mock

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()