annotate bitten/tests/notify.py @ 625:98675686ec4d

notify.py import cleanup and remove config name constants
author mgood
date Sun, 09 Aug 2009 08:47:10 +0000
parents cc2383034358
children 73ed8c171063
rev   line source
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
1 #-*- coding: utf-8 -*-
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
2 #
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
3 # Copyright (C) 2007 Ole Trenner, <ole@jayotee.de>
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
4 # All rights reserved.
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
5 #
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
6 # This software is licensed as described in the file COPYING, which
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
7 # you should have received as part of this distribution.
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
8
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
9 import unittest
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
10
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
11 from trac.db import DatabaseManager
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
12 from trac.test import EnvironmentStub, Mock
540
2cc5457e6610 Find a user's email by only fetching their session instead of all known users
mgood
parents: 539
diff changeset
13 from trac.web.session import DetachedSession
625
98675686ec4d notify.py import cleanup and remove config name constants
mgood
parents: 624
diff changeset
14 from bitten.model import schema, Build, BuildStep, BuildLog
98675686ec4d notify.py import cleanup and remove config name constants
mgood
parents: 624
diff changeset
15 from bitten.notify import BittenNotify, BittenNotifyEmail, BuildInfo
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
16
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
17
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
18 class BittenNotifyBaseTest(unittest.TestCase):
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
19 def setUp(self):
536
1f29360d8fd0 Make notification tests enable the components instead of monkey-patching `env.get_templates_dir`
mgood
parents: 534
diff changeset
20 self.env = EnvironmentStub(enable=['trac.*', 'bitten.notify.*'])
625
98675686ec4d notify.py import cleanup and remove config name constants
mgood
parents: 624
diff changeset
21 repos = Mock(get_changeset=lambda rev: Mock(author='author'))
98675686ec4d notify.py import cleanup and remove config name constants
mgood
parents: 624
diff changeset
22 self.env.get_repository = lambda authname=None: repos
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
23
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
24 db = self.env.get_db_cnx()
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
25 cursor = db.cursor()
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
26 connector, _ = DatabaseManager(self.env)._get_connector()
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
27 for table in schema:
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
28 for stmt in connector.to_sql(table):
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
29 cursor.execute(stmt)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
30 db.commit()
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
31
536
1f29360d8fd0 Make notification tests enable the components instead of monkey-patching `env.get_templates_dir`
mgood
parents: 534
diff changeset
32
534
79dd34e914a7 Merge `BittenNotify` and `BittenNotifyDispatcher` into one component
mgood
parents: 533
diff changeset
33 class BittenNotifyTest(BittenNotifyBaseTest):
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
34 """unit tests for BittenNotify dispatcher class"""
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
35 def setUp(self):
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
36 BittenNotifyBaseTest.setUp(self)
541
2479e2a75afa `BittenNotifyEmail` objects have state, so the same instance cannot be shared
mgood
parents: 540
diff changeset
37 self.dispatcher = BittenNotify(self.env)
538
23b7bdfb878d Make notifier mocking and called state testing clearer
mgood
parents: 536
diff changeset
38 self.failed_build = Build(self.env, status=Build.FAILURE)
23b7bdfb878d Make notifier mocking and called state testing clearer
mgood
parents: 536
diff changeset
39 self.successful_build = Build(self.env, status=Build.SUCCESS)
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
40
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
41 def test_do_notify_on_failed_build(self):
625
98675686ec4d notify.py import cleanup and remove config name constants
mgood
parents: 624
diff changeset
42 self.set_option(BittenNotify.notify_on_failure, 'true')
541
2479e2a75afa `BittenNotifyEmail` objects have state, so the same instance cannot be shared
mgood
parents: 540
diff changeset
43 self.assertTrue(self.dispatcher._should_notify(self.failed_build),
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
44 'notifier should be called for failed builds.')
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
45
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
46 def test_do_not_notify_on_failed_build(self):
625
98675686ec4d notify.py import cleanup and remove config name constants
mgood
parents: 624
diff changeset
47 self.set_option(BittenNotify.notify_on_failure, 'false')
541
2479e2a75afa `BittenNotifyEmail` objects have state, so the same instance cannot be shared
mgood
parents: 540
diff changeset
48 self.assertFalse(self.dispatcher._should_notify(self.failed_build),
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
49 'notifier should not be called for failed build.')
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
50
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
51 def test_do_notify_on_successful_build(self):
625
98675686ec4d notify.py import cleanup and remove config name constants
mgood
parents: 624
diff changeset
52 self.set_option(BittenNotify.notify_on_success, 'true')
541
2479e2a75afa `BittenNotifyEmail` objects have state, so the same instance cannot be shared
mgood
parents: 540
diff changeset
53 self.assertTrue(self.dispatcher._should_notify(self.successful_build),
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
54 'notifier should be called for successful builds when configured.')
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
55
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
56 def test_do_not_notify_on_successful_build(self):
625
98675686ec4d notify.py import cleanup and remove config name constants
mgood
parents: 624
diff changeset
57 self.set_option(BittenNotify.notify_on_success, 'false')
541
2479e2a75afa `BittenNotifyEmail` objects have state, so the same instance cannot be shared
mgood
parents: 540
diff changeset
58 self.assertFalse(self.dispatcher._should_notify(self.successful_build),
625
98675686ec4d notify.py import cleanup and remove config name constants
mgood
parents: 624
diff changeset
59 'notifier should not be called for successful build.')
98675686ec4d notify.py import cleanup and remove config name constants
mgood
parents: 624
diff changeset
60
98675686ec4d notify.py import cleanup and remove config name constants
mgood
parents: 624
diff changeset
61 def set_option(self, option, value):
98675686ec4d notify.py import cleanup and remove config name constants
mgood
parents: 624
diff changeset
62 self.env.config.set(option.section, option.name, value)
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
63
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
64
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
65 class BuildInfoTest(BittenNotifyBaseTest):
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
66 """unit tests for BuildInfo class"""
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
67
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
68 def setUp(self):
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
69 BittenNotifyBaseTest.setUp(self)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
70 #fixture
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
71 self.failed_build = Build(self.env,
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
72 config = 'config',
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
73 slave = 'slave',
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
74 rev = 10,
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
75 status = Build.FAILURE)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
76 self.failed_build.id = 1
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
77 self.successful_build = Build(self.env, status = Build.SUCCESS)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
78 self.successful_build.id = 2
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
79 step = BuildStep(self.env,
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
80 build = 1,
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
81 name = 'test',
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
82 status = BuildStep.FAILURE)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
83 step.errors = ['msg']
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
84 step.insert()
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
85 log = BuildLog(self.env, build = 1, step = 'test')
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
86 log.messages = [('info','msg')]
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
87 log.insert()
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
88
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
89 def test_exposed_properties(self):
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
90 build_info = BuildInfo(self.env, self.failed_build)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
91 self.assertEquals(self.failed_build.id, build_info.id)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
92 self.assertEquals('Failed', build_info.status)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
93 self.assertEquals('http://example.org/trac.cgi/build/config/1',
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
94 build_info.link)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
95 self.assertEquals('config', build_info.config)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
96 self.assertEquals('slave', build_info.slave)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
97 self.assertEquals('10', build_info.changeset)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
98 self.assertEquals('http://example.org/trac.cgi/changeset/10',
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
99 build_info.changesetlink)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
100 self.assertEquals('author', build_info.author)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
101 self.assertEquals('test: msg', build_info.errors)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
102 self.assertEquals(' info: msg', build_info.faillog)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
103
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
104 def test_exposed_properties_on_successful_build(self):
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
105 build_info = BuildInfo(self.env, self.successful_build)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
106 self.assertEquals(self.successful_build.id, build_info.id)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
107 self.assertEquals('Successful', build_info.status)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
108
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
109
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
110 class BittenNotifyEmailTest(BittenNotifyBaseTest):
539
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
111 """unit tests for BittenNotifyEmail class"""
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
112 def setUp(self):
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
113 BittenNotifyBaseTest.setUp(self)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
114 self.env.config.set('notification','smtp_enabled','true')
539
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
115 self.notifications_sent_to = []
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
116 def send(to, cc, hdrs={}):
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
117 self.notifications_sent_to = to
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
118 def noop(*args, **kw):
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
119 pass
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
120 self.email = Mock(BittenNotifyEmail, self.env,
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
121 begin_send=noop,
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
122 finish_send=noop,
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
123 send=send)
624
cc2383034358 Remove user->email lookup from since this is already handled in the parent class
mgood
parents: 541
diff changeset
124 self.build_info = BuildInfo(self.env,
cc2383034358 Remove user->email lookup from since this is already handled in the parent class
mgood
parents: 541
diff changeset
125 Build(self.env, status=Build.SUCCESS, rev=123))
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
126
624
cc2383034358 Remove user->email lookup from since this is already handled in the parent class
mgood
parents: 541
diff changeset
127 def test_notification_is_sent_to_author(self):
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
128 self.email.notify(self.build_info)
539
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
129 self.assertTrue('author' in self.notifications_sent_to,
624
cc2383034358 Remove user->email lookup from since this is already handled in the parent class
mgood
parents: 541
diff changeset
130 'Recipient list should contain the author')
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
131
540
2cc5457e6610 Find a user's email by only fetching their session instead of all known users
mgood
parents: 539
diff changeset
132 def add_known_user(self, username, name, email):
2cc5457e6610 Find a user's email by only fetching their session instead of all known users
mgood
parents: 539
diff changeset
133 session = DetachedSession(self.env, username)
2cc5457e6610 Find a user's email by only fetching their session instead of all known users
mgood
parents: 539
diff changeset
134 if name is not None:
2cc5457e6610 Find a user's email by only fetching their session instead of all known users
mgood
parents: 539
diff changeset
135 session['name'] = name
2cc5457e6610 Find a user's email by only fetching their session instead of all known users
mgood
parents: 539
diff changeset
136 if email is not None:
2cc5457e6610 Find a user's email by only fetching their session instead of all known users
mgood
parents: 539
diff changeset
137 session['email'] = email
2cc5457e6610 Find a user's email by only fetching their session instead of all known users
mgood
parents: 539
diff changeset
138 session.save()
2cc5457e6610 Find a user's email by only fetching their session instead of all known users
mgood
parents: 539
diff changeset
139
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
140
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
141 def suite():
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
142 suite = unittest.TestSuite()
534
79dd34e914a7 Merge `BittenNotify` and `BittenNotifyDispatcher` into one component
mgood
parents: 533
diff changeset
143 suite.addTest(unittest.makeSuite(BittenNotifyTest,'test'))
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
144 suite.addTest(unittest.makeSuite(BuildInfoTest,'test'))
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
145 suite.addTest(unittest.makeSuite(BittenNotifyEmailTest,'test'))
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
146 return suite
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
147
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
148 if __name__ == '__main__':
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
149 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software