1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5# This is based on code from:
6# https://chromium.googlesource.com/chromium/tools/build/+/master/scripts/tools/blink_roller/auto_roll_test.py
7# Ideally we should share code between these.
8
9
10from webkitpy.common.system.outputcapture import OutputCaptureTestCaseBase
11import sheriff_calendar as calendar
12
13
14class SheriffCalendarTest(OutputCaptureTestCaseBase):
15    def test_complete_email(self):
16        expected_emails = ['foo@chromium.org', 'bar@google.com', 'baz@chromium.org']
17        names = ['foo', 'bar@google.com', 'baz']
18        self.assertEqual(map(calendar._complete_email, names), expected_emails)
19
20    def test_emails(self):
21        expected_emails = ['foo@bar.com', 'baz@baz.com']
22        calendar._emails_from_url = lambda urls: expected_emails
23        self.assertEqual(calendar.current_gardener_emails(), expected_emails)
24
25    def _assert_parse(self, js_string, expected_emails):
26        self.assertEqual(calendar._names_from_sheriff_js(js_string), expected_emails)
27
28    def test_names_from_sheriff_js(self):
29        self._assert_parse('document.write(\'none (channel is sheriff)\')', [])
30        self._assert_parse('document.write(\'foo, bar\')', ['foo', 'bar'])
31
32    def test_email_regexp(self):
33        self.assertTrue(calendar._email_is_valid('somebody@example.com'))
34        self.assertTrue(calendar._email_is_valid('somebody@example.domain.com'))
35        self.assertTrue(calendar._email_is_valid('somebody@example-domain.com'))
36        self.assertTrue(calendar._email_is_valid('some.body@example.com'))
37        self.assertTrue(calendar._email_is_valid('some_body@example.com'))
38        self.assertTrue(calendar._email_is_valid('some+body@example.com'))
39        self.assertTrue(calendar._email_is_valid('some+body@com'))
40        self.assertTrue(calendar._email_is_valid('some/body@example.com'))
41        # These are valid according to the standard, but not supported here.
42        self.assertFalse(calendar._email_is_valid('some~body@example.com'))
43        self.assertFalse(calendar._email_is_valid('some!body@example.com'))
44        self.assertFalse(calendar._email_is_valid('some?body@example.com'))
45        self.assertFalse(calendar._email_is_valid('some" "body@example.com'))
46        self.assertFalse(calendar._email_is_valid('"{somebody}"@example.com'))
47        # Bogus.
48        self.assertFalse(calendar._email_is_valid('rm -rf /#@example.com'))
49        self.assertFalse(calendar._email_is_valid('some body@example.com'))
50        self.assertFalse(calendar._email_is_valid('[some body]@example.com'))
51
52    def test_filter_emails(self):
53        input_emails = ['foo@bar.com', 'baz@baz.com', 'bogus email @ !!!']
54        expected_emails = ['foo@bar.com', 'baz@baz.com']
55        self.assertEquals(calendar._filter_emails(input_emails), expected_emails)
56        self.assertStdout('WARNING: Not including bogus email @ !!! (invalid email address)\n')
57