forked from luci/luci-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifications_test.py
executable file
·122 lines (100 loc) · 3.59 KB
/
notifications_test.py
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
121
122
#!/usr/bin/env python
# Copyright 2015 The LUCI Authors. All rights reserved.
# Use of this source code is governed under the Apache License, Version 2.0
# that can be found in the LICENSE file.
import test_env
test_env.setup_test_env()
import datetime
import mock
from components.config import validation
from components import auth
from components import gitiles
from components import template
from google.appengine.ext import ndb
from test_support import test_case
import notifications
import storage
class NotificationsTestCase(test_case.TestCase):
def test_notify_gitiles_rejection(self):
ctx = validation.Context()
ctx.error('err')
ctx.warning('warn')
base = gitiles.Location.parse('https://example.com/x/+/infra/config')
new_rev = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
new_loc = base._replace(treeish=new_rev)
old_rev = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
old_loc = base._replace(treeish=old_rev)
self.mock(notifications, '_send', mock.Mock())
john = gitiles.Contribution(
'John', '[email protected]', datetime.datetime(2015, 1, 1))
commit = gitiles.Commit(
sha=new_rev,
tree='badcoffee',
parents=[],
author=john,
committer=john,
message='New config',
tree_diff=None)
self.mock(gitiles, 'get_log_async', mock.Mock(return_value=ndb.Future()))
gitiles.get_log_async.return_value.set_result(
gitiles.Log(commits=[commit], next_cursor=None))
self.mock(template, 'render', mock.Mock())
self.mock(auth, 'list_group', mock.Mock())
auth.list_group.return_value = [
auth.Identity('user', '[email protected]'),
auth.Identity('service', 'foo'),
]
# Notify.
notifications.notify_gitiles_rejection('projects/x', new_loc, ctx.result())
self.assertTrue(notifications._send.called)
email = notifications._send.call_args[0][0]
self.assertEqual(
email.sender,
'sample-app.appspot.com <[email protected]>')
self.assertEqual(email.subject, 'Config revision aaaaaaa is rejected')
self.assertEqual(email.to, ['John <[email protected]>'])
self.assertEqual(email.cc, {'[email protected]'})
template.render.assert_called_with(
'templates/validation_notification.html',
{
'author': 'John',
'messages': [
{'severity': 'ERROR', 'text': 'err'},
{'severity': 'WARNING', 'text': 'warn'}
],
'rev_link': new_loc,
'rev_hash': 'aaaaaaa',
'rev_repo': 'x',
'cur_rev_hash': None,
'cur_rev_link': None,
})
# Do not send second time.
notifications._send.reset_mock()
notifications.notify_gitiles_rejection('projects/x', new_loc, ctx.result())
self.assertFalse(notifications._send.called)
# Now with config set.
ndb.Key(notifications.Notification, str(new_loc)).delete()
storage.ConfigSet(
id='projects/x',
latest_revision=old_rev,
latest_revision_url=str(old_loc),
location=str(base)
).put()
template.render.reset_mock()
notifications.notify_gitiles_rejection('projects/x', new_loc, ctx.result())
template.render.assert_called_with(
'templates/validation_notification.html',
{
'author': 'John',
'messages': [
{'severity': 'ERROR', 'text': 'err'},
{'severity': 'WARNING', 'text': 'warn'}
],
'rev_link': new_loc,
'rev_hash': 'aaaaaaa',
'rev_repo': 'x',
'cur_rev_hash': 'bbbbbbb',
'cur_rev_link': old_loc,
})
if __name__ == '__main__':
test_env.main()