1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tests/test_handle.py Thu May 21 16:32:20 2015 +0200
1.3 @@ -0,0 +1,102 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +A handler to help with testing.
1.8 +
1.9 +Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +from imiptools.client import Client
1.26 +from imiptools.data import Object, get_address
1.27 +from imiptools.handlers import Handler
1.28 +from imiptools.mail import Messenger
1.29 +import imip_store
1.30 +import sys
1.31 +
1.32 +class TestHandler(Handler):
1.33 +
1.34 + """
1.35 + A content handler for use in testing, as opposed to operating within the
1.36 + mail processing pipeline.
1.37 + """
1.38 +
1.39 + def __init__(self, obj, user, messenger):
1.40 + Handler.__init__(self, messenger=messenger)
1.41 + Client.__init__(self, user) # this redefines the Handler initialisation
1.42 +
1.43 + self.set_object(obj)
1.44 +
1.45 + # Action methods.
1.46 +
1.47 + def handle_request(self, accept):
1.48 +
1.49 + """
1.50 + Process the current request for the current user. Return whether any
1.51 + action was taken.
1.52 + """
1.53 +
1.54 + # Reply only on behalf of this user.
1.55 +
1.56 + attendee_attr = self.update_participation(self.obj, accept and "ACCEPTED" or "DECLINED")
1.57 +
1.58 + if not attendee_attr:
1.59 + return None
1.60 +
1.61 + # NOTE: This is a simpler form of the code in imipweb.handler.
1.62 +
1.63 + organiser = get_address(self.obj.get_value("ORGANIZER"))
1.64 +
1.65 + self.obj["ATTENDEE"] = [(self.user, attendee_attr)]
1.66 + self.update_dtstamp()
1.67 + self.set_sequence(False)
1.68 +
1.69 + message = self.messenger.make_outgoing_message(
1.70 + [self.obj.to_part("REPLY")],
1.71 + [organiser],
1.72 + outgoing_bcc=get_address(self.user)
1.73 + )
1.74 +
1.75 + return message.as_string()
1.76 +
1.77 +# A simple main program that attempts to handle a stored request, writing the
1.78 +# response message to standard output.
1.79 +
1.80 +if __name__ == "__main__":
1.81 + try:
1.82 + nargs = 5
1.83 + accept, store_dir, user, uid, recurrenceid = (sys.argv[1:nargs+1] + [None])[:nargs]
1.84 + except ValueError:
1.85 + print >>sys.stderr, "Need 'accept' or 'decline', a store directory, user URI, event UID and optional RECURRENCE-ID."
1.86 + sys.exit(1)
1.87 +
1.88 + store = imip_store.FileStore(store_dir)
1.89 + fragment = store.get_event(user, uid, recurrenceid)
1.90 +
1.91 + if not fragment:
1.92 + print >>sys.stderr, "No such event:", uid, recurrenceid
1.93 + sys.exit(1)
1.94 +
1.95 + obj = Object(fragment)
1.96 + handler = TestHandler(obj, user, Messenger())
1.97 + response = handler.handle_request(accept == "accept")
1.98 +
1.99 + if response:
1.100 + store.dequeue_request(user, uid, recurrenceid)
1.101 + print response
1.102 + else:
1.103 + sys.exit(1)
1.104 +
1.105 +# vim: tabstop=4 expandtab shiftwidth=4