2015-10-03 | Paul Boddie | raw files shortlog changelog graph | Added a test of counter-proposals with the outgoing handler. Added counter-proposal support to the test handler/client script. | |
tests/test_handle.py (file) tests/test_person_invitation_counter.sh (file) |
1.1 --- a/tests/test_handle.py Sat Oct 03 01:04:41 2015 +0200 1.2 +++ b/tests/test_handle.py Sat Oct 03 01:05:28 2015 +0200 1.3 @@ -21,7 +21,9 @@ 1.4 1.5 from imiptools.client import ClientForObject 1.6 from imiptools.data import Object, get_address, parse_object 1.7 +from imiptools.dates import get_datetime, to_timezone 1.8 from imiptools.mail import Messenger 1.9 +from imiptools.period import RecurringPeriod 1.10 import imip_store 1.11 import sys 1.12 1.13 @@ -34,16 +36,38 @@ 1.14 1.15 # Action methods. 1.16 1.17 - def handle_request(self, accept): 1.18 + def handle_request(self, action, start=None, end=None): 1.19 1.20 """ 1.21 - Process the current request for the current user. Return whether any 1.22 - action was taken. 1.23 + Process the current request for the current user. Return whether the 1.24 + given 'action' was taken. 1.25 + 1.26 + If 'start' and 'end' are specified, they will be used in any 1.27 + counter-proposal. 1.28 """ 1.29 1.30 # Reply only on behalf of this user. 1.31 1.32 - attendee_attr = self.update_participation(self.obj, accept and "ACCEPTED" or "DECLINED") 1.33 + if action in ("accept", "decline"): 1.34 + attendee_attr = self.update_participation(self.obj, action == "accept" and "ACCEPTED" or "DECLINED") 1.35 + method = "REPLY" 1.36 + 1.37 + # For counter-proposals, set a new main period for the event. 1.38 + 1.39 + elif action == "counter": 1.40 + attendee_attr = self.obj.get_value_map("ATTENDEE").get(self.user) 1.41 + period = self.obj.get_main_period(self.get_tzid()) 1.42 + 1.43 + # Use the existing or configured time zone for the specified 1.44 + # datetimes. 1.45 + 1.46 + start = to_timezone(get_datetime(start), period.tzid) 1.47 + end = to_timezone(get_datetime(end), period.tzid) 1.48 + period = RecurringPeriod(start, end, period.tzid, period.origin, period.get_start_attr(), period.get_end_attr()) 1.49 + self.obj.set_period(period) 1.50 + method = "COUNTER" 1.51 + else: 1.52 + return None 1.53 1.54 if not attendee_attr: 1.55 return None 1.56 @@ -57,7 +81,7 @@ 1.57 self.set_sequence(False) 1.58 1.59 message = self.messenger.make_outgoing_message( 1.60 - [self.obj.to_part("REPLY")], 1.61 + [self.obj.to_part(method)], 1.62 [organiser], 1.63 outgoing_bcc=get_address(self.user) 1.64 ) 1.65 @@ -69,14 +93,30 @@ 1.66 1.67 if __name__ == "__main__": 1.68 try: 1.69 - minargs = 3; maxargs = 5 1.70 - accept, store_dir, user, uid, recurrenceid = (sys.argv[1:maxargs+1] + ([None] * (maxargs - minargs)))[:maxargs] 1.71 + action, store_dir, user = sys.argv[1:4] 1.72 + if action == "counter": 1.73 + start, end = sys.argv[4:6] 1.74 + i = 6 1.75 + else: 1.76 + start, end = None, None 1.77 + i = 4 1.78 + uid, recurrenceid = (sys.argv[i:i+2] + [None] * 2)[:2] 1.79 except ValueError: 1.80 - print >>sys.stderr, "Need 'accept' or 'decline', a store directory, user URI, event UID and optional RECURRENCE-ID." 1.81 - print >>sys.stderr, "The RECURRENCE-ID must be in the form employed by the store." 1.82 - print >>sys.stderr 1.83 - print >>sys.stderr, "Alternatively, omit the UID and RECURRENCE-ID and provide event-only details on standard input" 1.84 - print >>sys.stderr, "to force the script to handle an event not already present in the store." 1.85 + print >>sys.stderr, """\ 1.86 +Need 'accept', 'counter' or 'decline', a store directory, user URI, any 1.87 +counter-proposal datetimes (see below), plus the appropriate event UID and 1.88 +RECURRENCE-ID (if a recurrence is involved). 1.89 + 1.90 +The RECURRENCE-ID must be in exactly the form employed by the store, not a 1.91 +different but equivalent representation. 1.92 + 1.93 +Alternatively, omit the UID and RECURRENCE-ID and provide event-only details on 1.94 +standard input to force the script to handle an event not already present in the 1.95 +store. 1.96 + 1.97 +If 'counter' has been indicated, alternative start and end datetimes are also 1.98 +required. 1.99 +""" 1.100 sys.exit(1) 1.101 1.102 store = imip_store.FileStore(store_dir) 1.103 @@ -92,7 +132,7 @@ 1.104 1.105 obj = Object(fragment) 1.106 handler = TestClient(obj, user, Messenger(), store) 1.107 - response = handler.handle_request(accept == "accept") 1.108 + response = handler.handle_request(action, start, end) 1.109 1.110 if response: 1.111 if uid is not None:
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/tests/test_person_invitation_counter.sh Sat Oct 03 01:05:28 2015 +0200 2.3 @@ -0,0 +1,122 @@ 2.4 +#!/bin/sh 2.5 + 2.6 +THIS_DIR=`dirname $0` 2.7 + 2.8 +TEMPLATES="$THIS_DIR/templates" 2.9 +PERSON_SCRIPT="$THIS_DIR/../imip_person.py" 2.10 +SHOWMAIL="$THIS_DIR/../tools/showmail.py" 2.11 +STORE=/tmp/store 2.12 +STATIC=/tmp/static 2.13 +PREFS=/tmp/prefs 2.14 +ARGS="-S $STORE -P $STATIC -p $PREFS -d" 2.15 +USER="mailto:vincent.vole@example.com" 2.16 +SENDER="mailto:paul.boddie@example.com" 2.17 +FBFILE="$STORE/$USER/freebusy" 2.18 +FBOFFERFILE="$STORE/$USER/freebusy-offers" 2.19 +FBSENDERFILE="$STORE/$SENDER/freebusy" 2.20 +FBSENDEROTHERFILE="$STORE/$SENDER/freebusy-other/$USER" 2.21 +FBSENDERREQUESTS="$STORE/$SENDER/requests" 2.22 +TAB=`printf '\t'` 2.23 + 2.24 +OUTGOING_SCRIPT="$THIS_DIR/../imip_person_outgoing.py" 2.25 + 2.26 +PYTHONPATH="$THIS_DIR/.." 2.27 +export PYTHONPATH 2.28 + 2.29 +ACCEPT_SCRIPT="$THIS_DIR/test_handle.py" 2.30 +ACCEPT_ARGS="accept $STORE" 2.31 + 2.32 +COUNTER_SCRIPT="$THIS_DIR/test_handle.py" 2.33 +COUNTER_ARGS="counter $STORE" 2.34 + 2.35 +DECLINE_SCRIPT="$THIS_DIR/test_handle.py" 2.36 +DECLINE_ARGS="decline $STORE" 2.37 + 2.38 +ERROR=err.tmp 2.39 + 2.40 +rm -r $STORE 2.41 +rm -r $STATIC 2.42 +rm -r $PREFS 2.43 +rm $ERROR 2.44 +rm out*.tmp 2.45 + 2.46 +mkdir -p "$PREFS/$USER" 2.47 +echo 'Europe/Oslo' > "$PREFS/$USER/TZID" 2.48 +echo 'share' > "$PREFS/$USER/freebusy_sharing" 2.49 +echo 'PT60S' > "$PREFS/$USER/freebusy_offers" 2.50 + 2.51 + "$PERSON_SCRIPT" $ARGS < "$TEMPLATES/fb-request-person-all.txt" 2>> $ERROR \ 2.52 +| "$SHOWMAIL" \ 2.53 +> out0.tmp 2.54 + 2.55 + grep -q 'METHOD:REPLY' out0.tmp \ 2.56 +&& ! grep -q '^FREEBUSY' out0.tmp \ 2.57 +&& echo "Success" \ 2.58 +|| echo "Failed" 2.59 + 2.60 +# Attempt to schedule an event. 2.61 + 2.62 +"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-request-person.txt" 2>> $ERROR 2.63 + 2.64 + grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBSENDERFILE" \ 2.65 +&& echo "Success" \ 2.66 +|| echo "Failed" 2.67 + 2.68 +# Present the request to the recipient. 2.69 + 2.70 + "$PERSON_SCRIPT" $ARGS < "$TEMPLATES/event-request-person.txt" 2>> $ERROR \ 2.71 +| "$SHOWMAIL" \ 2.72 +> out1.tmp 2.73 + 2.74 + ! grep -q 'METHOD:REPLY' out1.tmp \ 2.75 +&& echo "Success" \ 2.76 +|| echo "Failed" 2.77 + 2.78 + ! [ -e "$FBFILE" ] \ 2.79 +|| ! grep -q "event6@example.com" "$FBFILE" \ 2.80 +&& echo "Success" \ 2.81 +|| echo "Failed" 2.82 + 2.83 + ! [ -e "$FBOTHERFILE" ] \ 2.84 +|| ! grep -q "event6@example.com" "$FBOTHERFILE" \ 2.85 +&& echo "Success" \ 2.86 +|| echo "Failed" 2.87 + 2.88 +# Counter the invitation. 2.89 + 2.90 + "$COUNTER_SCRIPT" $COUNTER_ARGS "$USER" 20141126T170000 20141126T180000 "event6@example.com" 2>> $ERROR \ 2.91 +| tee out2r.tmp \ 2.92 +| "$SHOWMAIL" \ 2.93 +> out2.tmp 2.94 + 2.95 +# Note that the invitation has only been prepared, not processed. 2.96 + 2.97 + ! [ -e "$FBFILE" ] \ 2.98 +|| ( ! grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBFILE" \ 2.99 + && ! grep -q "^20141126T160000Z${TAB}20141126T170000Z" "$FBFILE" ) \ 2.100 +&& echo "Success" \ 2.101 +|| echo "Failed" 2.102 + 2.103 + grep -q 'METHOD:COUNTER' out2.tmp \ 2.104 +&& grep -q 'DTSTART;TZID=Europe/Oslo.*:20141126T170000' out2.tmp \ 2.105 +&& echo "Success" \ 2.106 +|| echo "Failed" 2.107 + 2.108 + ! [ -e "$FBOFFERFILE" ] \ 2.109 +|| ! grep -q "^20141126T160000Z${TAB}20141126T170000Z" "$FBOFFERFILE" \ 2.110 +&& echo "Success" \ 2.111 +|| echo "Failed" 2.112 + 2.113 +# Send the counter-proposal. 2.114 + 2.115 +"$OUTGOING_SCRIPT" $ARGS < out2r.tmp 2>> $ERROR 2.116 + 2.117 + ! [ -e "$FBFILE" ] \ 2.118 +|| ( ! grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBFILE" \ 2.119 + && ! grep -q "^20141126T160000Z${TAB}20141126T170000Z" "$FBFILE" ) \ 2.120 +&& echo "Success" \ 2.121 +|| echo "Failed" 2.122 + 2.123 + grep -q "^20141126T160000Z${TAB}20141126T170000Z" "$FBOFFERFILE" \ 2.124 +&& echo "Success" \ 2.125 +|| echo "Failed"