annotate bitten/tests/notify.py @ 895:7d93d6358fe0

Use our own HTTPBasicAuthHandler under Python 2.6 to avoid issue http://bugs.python.org/issue8797. Fixes #658.
author hodgestar
date Wed, 09 Mar 2011 14:48:35 +0000
parents a4676056c8d3
children
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
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
15 from bitten.notify import BittenNotify, BuildNotifyEmail
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.*'])
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
21 repos = Mock(get_changeset=lambda rev: Mock(author='author', rev=rev))
625
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
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
65 class BuildNotifyEmailTest(BittenNotifyBaseTest):
539
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
66 """unit tests for BittenNotifyEmail class"""
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
67 def setUp(self):
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
68 BittenNotifyBaseTest.setUp(self)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
69 self.env.config.set('notification','smtp_enabled','true')
539
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
70 self.notifications_sent_to = []
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
71 def send(to, cc, hdrs={}):
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
72 self.notifications_sent_to = to
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
73 def noop(*args, **kw):
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
74 pass
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
75 self.email = Mock(BuildNotifyEmail, self.env,
539
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
76 begin_send=noop,
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
77 finish_send=noop,
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
78 send=send)
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
79 self.build = 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
80
624
cc2383034358 Remove user->email lookup from since this is already handled in the parent class
mgood
parents: 541
diff changeset
81 def test_notification_is_sent_to_author(self):
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
82 self.email.notify(self.build)
539
01f1ee3c8a4f Similar mocking clean-up for `BittenNotifyEmail` tests
mgood
parents: 538
diff changeset
83 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
84 'Recipient list should contain the author')
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
85
824
a4676056c8d3 Changed notification to always use Genshi `NewTextTemplate` which lets us keep just one template for both Trac 0.11 and 0.12+.
osimons
parents: 626
diff changeset
86 def test_notification_body_render(self):
a4676056c8d3 Changed notification to always use Genshi `NewTextTemplate` which lets us keep just one template for both Trac 0.11 and 0.12+.
osimons
parents: 626
diff changeset
87 self.email.notify(self.build)
a4676056c8d3 Changed notification to always use Genshi `NewTextTemplate` which lets us keep just one template for both Trac 0.11 and 0.12+.
osimons
parents: 626
diff changeset
88 output = self.email.template.generate(**self.email.data).render('text')
a4676056c8d3 Changed notification to always use Genshi `NewTextTemplate` which lets us keep just one template for both Trac 0.11 and 0.12+.
osimons
parents: 626
diff changeset
89 self.assertTrue('Successful build of My Project [123]' in output)
a4676056c8d3 Changed notification to always use Genshi `NewTextTemplate` which lets us keep just one template for both Trac 0.11 and 0.12+.
osimons
parents: 626
diff changeset
90 self.assertTrue('<http://example.org/trac.cgi/changeset/123>' in output)
a4676056c8d3 Changed notification to always use Genshi `NewTextTemplate` which lets us keep just one template for both Trac 0.11 and 0.12+.
osimons
parents: 626
diff changeset
91
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
92 # TODO functional tests of generated mails
540
2cc5457e6610 Find a user's email by only fetching their session instead of all known users
mgood
parents: 539
diff changeset
93
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
94
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
95 def suite():
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
96 suite = unittest.TestSuite()
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
97 suite.addTest(unittest.makeSuite(BittenNotifyTest, 'test'))
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
98 suite.addTest(unittest.makeSuite(BuildNotifyEmailTest, 'test'))
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
99 return suite
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
100
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
101 if __name__ == '__main__':
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
102 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software