imip-agent

tests/test_handle.py

789:c90e7281106d
2015-09-29 Paul Boddie Added missing escaping for text within elements.
     1 #!/usr/bin/env python     2      3 """     4 A handler to help with testing.     5      6 Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk>     7      8 This program is free software; you can redistribute it and/or modify it under     9 the terms of the GNU General Public License as published by the Free Software    10 Foundation; either version 3 of the License, or (at your option) any later    11 version.    12     13 This program is distributed in the hope that it will be useful, but WITHOUT    14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    15 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more    16 details.    17     18 You should have received a copy of the GNU General Public License along with    19 this program.  If not, see <http://www.gnu.org/licenses/>.    20 """    21     22 from imiptools.client import ClientForObject    23 from imiptools.data import Object, get_address, parse_object    24 from imiptools.mail import Messenger    25 import imip_store    26 import sys    27     28 class TestClient(ClientForObject):    29     30     """    31     A content handler for use in testing, as opposed to operating within the    32     mail processing pipeline.    33     """    34     35     # Action methods.    36     37     def handle_request(self, accept):    38     39         """    40         Process the current request for the current user. Return whether any    41         action was taken.    42         """    43     44         # Reply only on behalf of this user.    45     46         attendee_attr = self.update_participation(self.obj, accept and "ACCEPTED" or "DECLINED")    47     48         if not attendee_attr:    49             return None    50     51         # NOTE: This is a simpler form of the code in imipweb.client.    52     53         organiser = get_address(self.obj.get_value("ORGANIZER"))    54     55         self.obj["ATTENDEE"] = [(self.user, attendee_attr)]    56         self.update_dtstamp()    57         self.set_sequence(False)    58     59         message = self.messenger.make_outgoing_message(    60             [self.obj.to_part("REPLY")],    61             [organiser],    62             outgoing_bcc=get_address(self.user)    63             )    64     65         return message.as_string()    66     67 # A simple main program that attempts to handle a stored request, writing the    68 # response message to standard output.    69     70 if __name__ == "__main__":    71     try:    72         minargs = 3; maxargs = 5    73         accept, store_dir, user, uid, recurrenceid = (sys.argv[1:maxargs+1] + ([None] * (maxargs - minargs)))[:maxargs]    74     except ValueError:    75         print >>sys.stderr, "Need 'accept' or 'decline', a store directory, user URI, event UID and optional RECURRENCE-ID."    76         print >>sys.stderr, "The RECURRENCE-ID must be in the form employed by the store."    77         print >>sys.stderr    78         print >>sys.stderr, "Alternatively, omit the UID and RECURRENCE-ID and provide event-only details on standard input"    79         print >>sys.stderr, "to force the script to handle an event not already present in the store."    80         sys.exit(1)    81     82     store = imip_store.FileStore(store_dir)    83     84     if uid is not None:    85         fragment = store.get_event(user, uid, recurrenceid)    86     87         if not fragment:    88             print >>sys.stderr, "No such event:", uid, recurrenceid    89             sys.exit(1)    90     else:    91         fragment = parse_object(sys.stdin, "utf-8")    92     93     obj = Object(fragment)    94     handler = TestClient(obj, user, Messenger(), store)    95     response = handler.handle_request(accept == "accept")    96     97     if response:    98         if uid is not None:    99             store.dequeue_request(user, uid, recurrenceid)   100         print response   101     else:   102         sys.exit(1)   103    104 # vim: tabstop=4 expandtab shiftwidth=4