annotate MoinMoin/events/_tests/test_events.py @ 6042:ca4a1bfdf2db

events tests: use PageEditor instances, like in the production code
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 01 Mar 2014 19:07:03 +0100
parents e5a9570d3001
children
rev   line source
2311
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
2 """
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
3 MoinMoin - tests for MoinMoin.events module
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
4
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
5 @copyright: 2007 by Karol Nowak <grywacz@gmail.com>
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
6 @license: GNU GPL, see COPYING for details.
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
7 """
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
8
2327
e6ebe6853e51 Unit test; check if page_change_message() raises an exception.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2318
diff changeset
9 import py
e6ebe6853e51 Unit test; check if page_change_message() raises an exception.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2318
diff changeset
10
2311
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
11 import MoinMoin.events as events
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2314
diff changeset
12 import MoinMoin.events.notification as notification
2328
495b09143c10 Add a new unit test for _filter_subscriber_list()
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2327
diff changeset
13 import MoinMoin.events.jabbernotify as jabbernotify
6042
ca4a1bfdf2db events tests: use PageEditor instances, like in the production code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3873
diff changeset
14 from MoinMoin.PageEditor import PageEditor
2328
495b09143c10 Add a new unit test for _filter_subscriber_list()
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2327
diff changeset
15 from MoinMoin.user import User
2311
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
16
2314
38b6ba04671b Fix issues pointed out by xorAxAx.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2311
diff changeset
17 def test_get_handlers(request):
2311
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
18 """Test if there are any event handlers. There should be some internal ones"""
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2314
diff changeset
19
2314
38b6ba04671b Fix issues pointed out by xorAxAx.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2311
diff changeset
20 assert events.get_handlers(request.cfg)
2311
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
21
2314
38b6ba04671b Fix issues pointed out by xorAxAx.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2311
diff changeset
22 def test_send_event(request):
2311
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
23 """Test if event handlers are called and if proper messages are returned"""
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2328
diff changeset
24
2835
9c4c20145a1f test_PageEditor, test_events: skip broken test
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 2484
diff changeset
25 py.test.skip('broken test - global state is changed somehow that test_GetVal looks broken')
2311
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
26 return_string = u"test_send_event"
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2328
diff changeset
27
2311
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
28 def event_handler(event):
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2314
diff changeset
29 return notification.Failure("Just a test")
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2328
diff changeset
30
2311
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
31 request.cfg.event_handlers = [event_handler]
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
32 event = events.Event(request)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
33
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2314
diff changeset
34 print "A proper event handler must be called and an 1-element list of results returned"
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2314
diff changeset
35 results = events.send_event(event)
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2314
diff changeset
36 assert issubclass(results[0].__class__, events.EventResult)
2311
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
37
2314
38b6ba04671b Fix issues pointed out by xorAxAx.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2311
diff changeset
38 def test_subscribable_events(request):
2311
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
39 """Test if there are any subscribable events. There should be some."""
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2328
diff changeset
40
2311
1833361481ff Some unit test for the MoinMoin.events module.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
41 print "There should be at least a few subscribable events!"
2314
38b6ba04671b Fix issues pointed out by xorAxAx.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2311
diff changeset
42 assert events.get_subscribable_events()
2327
e6ebe6853e51 Unit test; check if page_change_message() raises an exception.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2318
diff changeset
43
e6ebe6853e51 Unit test; check if page_change_message() raises an exception.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2318
diff changeset
44 def test_page_change_message(request):
6042
ca4a1bfdf2db events tests: use PageEditor instances, like in the production code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3873
diff changeset
45 page = PageEditor(request, "FrontPage")
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
46
2327
e6ebe6853e51 Unit test; check if page_change_message() raises an exception.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2318
diff changeset
47 print "Provided with a dumb change type argument, this should raise an exception!"
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
48 py.test.raises(notification.UnknownChangeType, notification.page_change_message,
2327
e6ebe6853e51 Unit test; check if page_change_message() raises an exception.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2318
diff changeset
49 "StupidType", request, page, "en", revisions=page.getRevList())
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
50
2947
a7ed0a410ced Add a test for handle_user_created
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2835
diff changeset
51 def test_user_created_event(request):
3652
0b04ad4db631 event tests: skip wrong test
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2947
diff changeset
52 py.test.skip("Test is wrong, because it assumes send_notification will be called - but if there is no superuser subscribed to UserCreatedEvent, then no notification needs to be sent.")
2947
a7ed0a410ced Add a test for handle_user_created
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2835
diff changeset
53 class server_dummy:
a7ed0a410ced Add a test for handle_user_created
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2835
diff changeset
54 def __init__(self):
a7ed0a410ced Add a test for handle_user_created
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2835
diff changeset
55 self.sent = False
a7ed0a410ced Add a test for handle_user_created
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2835
diff changeset
56
a7ed0a410ced Add a test for handle_user_created
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2835
diff changeset
57 def send_notification(self, *args):
a7ed0a410ced Add a test for handle_user_created
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2835
diff changeset
58 self.sent = True
a7ed0a410ced Add a test for handle_user_created
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2835
diff changeset
59
a7ed0a410ced Add a test for handle_user_created
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2835
diff changeset
60 event = events.UserCreatedEvent(request, User(request))
a7ed0a410ced Add a test for handle_user_created
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2835
diff changeset
61 request.cfg.notification_server = server_dummy()
3873
e5a9570d3001 secrets configuration refactored - see the snippet for an example usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3652
diff changeset
62 request.cfg.secrets = "thisisnotsecret"
2947
a7ed0a410ced Add a test for handle_user_created
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2835
diff changeset
63
a7ed0a410ced Add a test for handle_user_created
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2835
diff changeset
64 jabbernotify.handle_user_created(event)
a7ed0a410ced Add a test for handle_user_created
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2835
diff changeset
65 assert request.cfg.notification_server.sent is True
a7ed0a410ced Add a test for handle_user_created
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2835
diff changeset
66
2328
495b09143c10 Add a new unit test for _filter_subscriber_list()
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2327
diff changeset
67 def test_filter_subscriber_list(request):
495b09143c10 Add a new unit test for _filter_subscriber_list()
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2327
diff changeset
68 user = User(request)
495b09143c10 Add a new unit test for _filter_subscriber_list()
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2327
diff changeset
69 event = events.Event(request)
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2328
diff changeset
70
2426
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2360
diff changeset
71 print "User is subscribed to this event on jabber."
2328
495b09143c10 Add a new unit test for _filter_subscriber_list()
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2327
diff changeset
72 print "This means, that he should stay on the list."
2360
6a24e3a77aea Fix test_events so that it works with updated code.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2337
diff changeset
73 user.jid = "user@example.com"
2426
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2360
diff changeset
74 user.jabber_subscribed_events = [events.Event.__name__]
2328
495b09143c10 Add a new unit test for _filter_subscriber_list()
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2327
diff changeset
75 subscribers = {"en": [user]}
2360
6a24e3a77aea Fix test_events so that it works with updated code.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2337
diff changeset
76 notification.filter_subscriber_list(event, subscribers, True)
2328
495b09143c10 Add a new unit test for _filter_subscriber_list()
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2327
diff changeset
77 assert subscribers["en"]
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2328
diff changeset
78
2426
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2360
diff changeset
79 print "User is not subscribed to this event on jabber."
2328
495b09143c10 Add a new unit test for _filter_subscriber_list()
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2327
diff changeset
80 print "The list should be empty."
2360
6a24e3a77aea Fix test_events so that it works with updated code.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2337
diff changeset
81 user.jid = "user@example.com"
2426
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2360
diff changeset
82 user.jabber_subscribed_events = []
2328
495b09143c10 Add a new unit test for _filter_subscriber_list()
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2327
diff changeset
83 subscribers = {"en": [user]}
2360
6a24e3a77aea Fix test_events so that it works with updated code.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2337
diff changeset
84 notification.filter_subscriber_list(event, subscribers, True)
2328
495b09143c10 Add a new unit test for _filter_subscriber_list()
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2327
diff changeset
85 assert not subscribers["en"]
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
86
2426
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2360
diff changeset
87 print "User is subscribed to this event on email."
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2360
diff changeset
88 print "This means, that he should stay on the list."
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2360
diff changeset
89 user.email = "user@example.com"
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2360
diff changeset
90 user.email_subscribed_events = [events.Event.__name__]
2328
495b09143c10 Add a new unit test for _filter_subscriber_list()
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2327
diff changeset
91 subscribers = {"en": [user]}
2426
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2360
diff changeset
92 notification.filter_subscriber_list(event, subscribers, False)
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2360
diff changeset
93 assert subscribers["en"]
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2328
diff changeset
94
2426
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2360
diff changeset
95 print "User is not subscribed to this event on email."
2328
495b09143c10 Add a new unit test for _filter_subscriber_list()
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2327
diff changeset
96 print "The list should be empty."
2426
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2360
diff changeset
97 user.email = "user@example.com"
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2360
diff changeset
98 user.email_subscribed_events = []
2328
495b09143c10 Add a new unit test for _filter_subscriber_list()
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2327
diff changeset
99 subscribers = {"en": [user]}
2426
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2360
diff changeset
100 notification.filter_subscriber_list(event, subscribers, False)
2328
495b09143c10 Add a new unit test for _filter_subscriber_list()
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2327
diff changeset
101 assert not subscribers["en"]
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2328
diff changeset
102
2314
38b6ba04671b Fix issues pointed out by xorAxAx.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2311
diff changeset
103 coverage_modules = ["MoinMoin.events"]