<?xml version="1.0" standalone="no"?>

<!DOCTYPE caldavtest SYSTEM "caldavtest.dtd">

<!--
 Copyright (c) 2006-2015 Apple Inc. All rights reserved.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 -->

<caldavtest>
	<description>Test calendar sharing calendars</description>

	<require-feature>
		<feature>caldav</feature>
		<feature>shared-calendars</feature>
	</require-feature>

	<start>
		<!--
		<request user="$userid1:" pswd="$pswd1:">
			<method>DELETEALL</method>
			<ruri>$notificationpath1:/</ruri>
		</request>
		<request user="$userid2:" pswd="$pswd2:">
			<method>DELETEALL</method>
			<ruri>$notificationpath2:/</ruri>
		</request>
		<request end-delete="yes">
			<method>MKCALENDAR</method>
			<ruri>$calendarhome1:/shared/</ruri>
			<verify>
				<callback>statusCode</callback>
			</verify>
		</request>
		<request>
			<method>PROPPATCH</method>
			<ruri>$calendarhome1:/shared/</ruri>
			<data>
				<content-type>text/xml; charset=utf-8</content-type>
				<filepath>Resource/Common/PROPPATCH/calendar-transp-opaque.xml</filepath>
			</data>
		</request>
		-->
	</start>
	
	<test-suite name='Read-write calendar'>
		<test name='1'>
			<description>POST invitation</description>
			<request>
				<method>POST</method>
				<ruri>$calendarhome1:/shared/</ruri>
				<data>
					<content-type>text/xml; charset=utf-8</content-type>
					<filepath>Resource/CalDAV/sharing/calendars/read-write/1.xml</filepath>
				</data>
				<verify>
					<callback>statusCode</callback>
				</verify>
			</request>
		</test>
		<test name='4'>
			<description>Shared calendar exists</description>
			<request user="$userid2:" pswd="$pswd2:">
				<method>PROPFIND</method>
				<ruri>$calendarhome1:/shared/</ruri>
				<header>
					<name>Depth</name>
					<value>0</value>
				</header>
				<data>
					<content-type>text/xml; charset=utf-8</content-type>
					<filepath>Resource/CalDAV/sharing/calendars/read-write/4.xml</filepath>
				</data>
				<verify>
					<callback>xmlElementMatch</callback>
					<arg>
						<name>exists</name>
						<value>$verify-property-prefix:/{DAV:}owner/{DAV:}href[=$principaluri1:]</value>
						<value>$verify-property-prefix:/{DAV:}resourcetype/{DAV:}collection</value>
						<value>$verify-property-prefix:/{DAV:}resourcetype/{urn:ietf:params:xml:ns:caldav}calendar</value>
						<!-- value>$verify-property-prefix:/{DAV:}resourcetype/{http://calendarserver.org/ns/}shared</value -->
						<value>$verify-property-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}read</value>
						<value>$verify-property-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}write</value>
						<value>$verify-property-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}bind</value>
						<value>$verify-property-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}unbind</value>
						<!-- value>$verify-property-prefix:/{urn:ietf:params:xml:ns:caldav}schedule-calendar-transp/{urn:ietf:params:xml:ns:caldav}transparent</value -->
					</arg>
					<arg>
						<name>notexists</name>
						<value>$verify-property-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}admin</value>
						<value>$verify-property-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}all</value>
					</arg>
				</verify>
			</request>
		</test>
		<test name='4a'>
			<description>Shared calendar exists Depth:1</description>
			<request user="$userid2:" pswd="$pswd2:">
				<method>PROPFIND</method>
				<ruri>$calendarhome2:</ruri>
				<header>
					<name>Depth</name>
					<value>1</value>
				</header>
				<data>
					<content-type>text/xml; charset=utf-8</content-type>
					<filepath>Resource/CalDAV/sharing/calendars/read-write/4.xml</filepath>
				</data>
				<verify>
					<callback>xmlElementMatch</callback>
					<arg>
						<name>parent</name>
						<value>$multistatus-response-prefix:[^{DAV:}href=$calendarhome2:/shared_shared_by_user01/]</value>
					</arg>
					<arg>
						<name>exists</name>
						<value>$verify-response-prefix:/{DAV:}owner/{DAV:}href[=$principaluri1:]</value>
						<value>$verify-response-prefix:/{DAV:}resourcetype/{DAV:}collection</value>
						<value>$verify-response-prefix:/{DAV:}resourcetype/{urn:ietf:params:xml:ns:caldav}calendar</value>
						<!-- value>$verify-response-prefix:/{DAV:}resourcetype/{http://calendarserver.org/ns/}shared</value -->
						<value>$verify-response-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}read</value>
						<value>$verify-response-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}write</value>
						<value>$verify-response-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}bind</value>
						<value>$verify-response-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}unbind</value>
						<!-- value>$verify-response-prefix:/{urn:ietf:params:xml:ns:caldav}schedule-calendar-transp/{urn:ietf:params:xml:ns:caldav}transparent</value -->
					</arg>
					<arg>
						<name>notexists</name>
						<value>$verify-response-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}admin</value>
						<value>$verify-response-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}all</value>
					</arg>
				</verify>
			</request>
		</test>
		<test name='5'>
			<description>Original calendar unchanged</description>
			<request>
				<method>PROPFIND</method>
				<ruri>$calendarhome1:/shared/</ruri>
				<header>
					<name>Depth</name>
					<value>0</value>
				</header>
				<data>
					<content-type>text/xml; charset=utf-8</content-type>
					<filepath>Resource/CalDAV/sharing/calendars/read-write/4.xml</filepath>
				</data>
				<verify>
					<callback>xmlElementMatch</callback>
					<arg>
						<name>exists</name>
						<value>$verify-property-prefix:/{DAV:}owner/{DAV:}href[=$principaluri1:]</value>
						<!--<value>$verify-property-prefix:/{urn:ietf:params:xml:ns:caldav}schedule-calendar-transp/{urn:ietf:params:xml:ns:caldav}opaque</value>-->
					</arg>
				</verify>
			</request>
		</test>
		<test name='5a'>
			<description>Invite propfind returns sharees</description>
			<request>
				<method>PROPFIND</method>
				<ruri>$calendarhome1:/shared/</ruri>
				<header>
					<name>Depth</name>
					<value>0</value>
				</header>
				<data>
					<content-type>text/xml; charset=utf-8</content-type>
					<filepath>Resource/CalDAV/sharing/calendars/read-write/5.xml</filepath>
				</data>
				<verify>
					<callback>xmlElementMatch</callback>
					<arg>
						<name>exists</name>
						<value>$verify-property-prefix:/{http://owncloud.org/ns}invite/{http://owncloud.org/ns}user/{DAV:}href</value>
						<value>$verify-property-prefix:/{http://owncloud.org/ns}invite/{http://owncloud.org/ns}user/{http://owncloud.org/ns}invite-accepted</value>
					</arg>
				</verify>
			</request>
		</test>
		<test name='6'>
			<description>Sharee creates event</description>
			<request user="$userid2:" pswd="$pswd2:">
				<method>PUT</method>
				<ruri>$calendarhome1:/shared/1.ics</ruri>
				<data>
					<content-type>text/calendar; charset=utf-8</content-type>
					<filepath>Resource/CalDAV/sharing/calendars/read-write/5.ics</filepath>
				</data>
				<verify>
					<callback>statusCode</callback>
				</verify>
			</request>
		</test>
		<test name='7'>
			<description>Sharer sees event</description>
			<request>
				<method>GET</method>
				<ruri>$calendarhome1:/shared/1.ics</ruri>
				<verify>
					<callback>calendarDataMatch</callback>
					<arg>
						<name>filepath</name>
						<value>Resource/CalDAV/sharing/calendars/read-write/5.ics</value>
					</arg>
				</verify>
			</request>
		</test>
		<test name='8'>
			<description>Sharer changes event</description>
			<request>
				<method>PUT</method>
				<ruri>$calendarhome1:/shared/1.ics</ruri>
				<data>
					<content-type>text/calendar; charset=utf-8</content-type>
					<filepath>Resource/CalDAV/sharing/calendars/read-write/6.ics</filepath>
				</data>
				<verify>
					<callback>statusCode</callback>
				</verify>
			</request>
		</test>
		<test name='9'>
			<description>Sharee sees changed event</description>
			<request user="$userid2:" pswd="$pswd2:">
				<method>GET</method>
				<ruri>$calendarhome1:/shared/1.ics</ruri>
				<verify>
					<callback>calendarDataMatch</callback>
					<arg>
						<name>filepath</name>
						<value>Resource/CalDAV/sharing/calendars/read-write/6.ics</value>
					</arg>
				</verify>
			</request>
		</test>
		<test name='10'>
			<description>Sharer creates event</description>
			<request>
				<method>PUT</method>
				<ruri>$calendarhome1:/shared/2.ics</ruri>
				<data>
					<content-type>text/calendar; charset=utf-8</content-type>
					<filepath>Resource/CalDAV/sharing/calendars/read-write/7.ics</filepath>
				</data>
				<verify>
					<callback>statusCode</callback>
				</verify>
			</request>
		</test>
		<test name='11'>
			<description>Sharee sees new event</description>
			<request user="$userid2:" pswd="$pswd2:">
				<method>GET</method>
				<ruri>$calendarhome1:/shared/2.ics</ruri>
				<verify>
					<callback>calendarDataMatch</callback>
					<arg>
						<name>filepath</name>
						<value>Resource/CalDAV/sharing/calendars/read-write/7.ics</value>
					</arg>
				</verify>
			</request>
		</test>
		<test name='12'>
			<description>Sharee changes event</description>
			<request user="$userid2:" pswd="$pswd2:">
				<method>PUT</method>
				<ruri>$calendarhome1:/shared/2.ics</ruri>
				<data>
					<content-type>text/calendar; charset=utf-8</content-type>
					<filepath>Resource/CalDAV/sharing/calendars/read-write/8.ics</filepath>
				</data>
				<verify>
					<callback>statusCode</callback>
				</verify>
			</request>
		</test>
		<test name='13'>
			<description>Sharer sees changed event</description>
			<request>
				<method>GET</method>
				<ruri>$calendarhome1:/shared/2.ics</ruri>
				<verify>
					<callback>calendarDataMatch</callback>
					<arg>
						<name>filepath</name>
						<value>Resource/CalDAV/sharing/calendars/read-write/8.ics</value>
					</arg>
				</verify>
			</request>
		</test>
		<test name='14'>
			<description>Un-share by delete</description>
			<request user="$userid2:" pswd="$pswd2:">
				<method>DELETE</method>
				<ruri>$calendarhome2:/shared_shared_by_user01/</ruri>
				<verify>
					<callback>statusCode</callback>
				</verify>
			</request>
		</test>
		<test name='15'>
			<description>Original calendar still exists</description>
			<request>
				<method>PROPFIND</method>
				<ruri>$calendarhome1:/shared/</ruri>
				<header>
					<name>Depth</name>
					<value>0</value>
				</header>
				<data>
					<content-type>text/xml; charset=utf-8</content-type>
					<filepath>Resource/CalDAV/sharing/calendars/read-write/4.xml</filepath>
				</data>
				<verify>
					<callback>xmlElementMatch</callback>
					<arg>
						<name>exists</name>
						<value>$verify-property-prefix:/{DAV:}owner/{DAV:}href[=$principaluri1:]</value>
						<value>$verify-property-prefix:/{DAV:}resourcetype/{DAV:}collection</value>
						<value>$verify-property-prefix:/{DAV:}resourcetype/{urn:ietf:params:xml:ns:caldav}calendar</value>
						<!-- value>$verify-property-prefix:/{DAV:}resourcetype/{http://calendarserver.org/ns/}shared</value -->
						<value>$verify-property-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}read</value>
						<value>$verify-property-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}write</value>
						<value>$verify-property-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}bind</value>
						<value>$verify-property-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}unbind</value>
						<!-- value>$verify-property-prefix:/{urn:ietf:params:xml:ns:caldav}schedule-calendar-transp/{urn:ietf:params:xml:ns:caldav}transparent</value -->
					</arg>
					<arg>
						<name>notexists</name>
						<value>$verify-property-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}admin</value>
						<value>$verify-property-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}all</value>
					</arg>
				</verify>
			</request>
		</test>
		<test name='16'>
			<description>Shared calendar no longer exists Depth:1</description>
			<request user="$userid2:" pswd="$pswd2:">
				<method>PROPFIND</method>
				<ruri>$calendarhome2:</ruri>
				<header>
					<name>Depth</name>
					<value>1</value>
				</header>
				<data>
					<content-type>text/xml; charset=utf-8</content-type>
					<filepath>Resource/CalDAV/sharing/calendars/read-write/4.xml</filepath>
				</data>
				<verify>
					<callback>xmlElementMatch</callback>
					<arg>
						<name>notexists</name>
						<value>$multistatus-response-prefix:[^{DAV:}href=$calendarhome2:/shared_shared_by_user01/]</value>
					</arg>
				</verify>
			</request>
		</test>
	</test-suite>

	<!--
	<test-suite name='Default calendar cannot be shared calendar'>
		<test name='1'>
			<description>Set property on Inbox</description>
			<request user="$userid2:" pswd="$pswd2:">
				<method>PROPPATCH</method>
				<ruri>$inboxpath2:/</ruri>
				<data>
					<content-type>text/xml; charset=utf-8</content-type>
					<filepath>Resource/CalDAV/sharing/calendars/defaultcalendar/1.xml</filepath>
				</data>
				<verify>
					<callback>propfindItems</callback>
					<arg>
						<name>badprops</name>
						<value>{urn:ietf:params:xml:ns:caldav}schedule-default-calendar-URL</value>
					</arg>
				</verify>
			</request>
		</test>
		<test name='2'>
			<description>Verify property on inbox</description>
			<request user="$userid2:" pswd="$pswd2:">
				<method>PROPFIND</method>
				<ruri>$inboxpath2:/</ruri>
				<header>
					<name>Depth</name>
					<value>0</value>
				</header>
				<data>
					<content-type>text/xml; charset=utf-8</content-type>
					<filepath>Resource/CalDAV/sharing/calendars/defaultcalendar/2.xml</filepath>
				</data>
				<verify>
					<callback>propfindItems</callback>
					<arg>
						<name>okprops</name>
						<value><![CDATA[{urn:ietf:params:xml:ns:caldav}schedule-default-calendar-URL$<href xmlns="DAV:">$calendarpath2:</href>]]></value>
					</arg>
				</verify>
			</request>
		</test>
	</test-suite>

	<test-suite name='Change to read-only calendar'>
		<test name='1'>
			<description>POST invitation</description>
			<request>
				<method>POST</method>
				<ruri>$calendarhome1:/shared/</ruri>
				<data>
					<content-type>text/xml; charset=utf-8</content-type>
					<filepath>Resource/CalDAV/sharing/calendars/read-only/1.xml</filepath>
				</data>
				<verify>
					<callback>statusCode</callback>
				</verify>
			</request>
		</test>
		<test name='2'>
			<description>Check Sharee notification collection</description>
			<request user="$userid2:" pswd="$pswd2:">
				<method>WAITCOUNT 1</method>
				<ruri>$notificationpath2:/</ruri>
			</request>
			<request user="$userid2:" pswd="$pswd2:">
				<method>GETNEW</method>
				<ruri>$notificationpath2:/</ruri>
				<verify>
					<callback>xmlDataMatch</callback>
					<arg>
						<name>filepath</name>
						<value>Resource/CalDAV/sharing/calendars/read-only/2.xml</value>
					</arg>
					<arg>
						<name>filter</name>
						<value>{http://calendarserver.org/ns/}dtstamp</value>
						<value>{http://calendarserver.org/ns/}uid</value>
					</arg>
				</verify>
				<grabelement>
					<name>{http://calendarserver.org/ns/}invite-notification/{http://calendarserver.org/ns/}uid</name>
					<variable>$inviteuid:</variable>
				</grabelement>
			</request>
		</test>
		<test name='3'>
			<description>Sharee replies ACCEPTED</description>
			<request user="$userid2:" pswd="$pswd2:">
				<method>POST</method>
				<ruri>$calendarhome2:/</ruri>
				<data>
					<content-type>application/xml; charset=utf-8</content-type>
					<filepath>Resource/CalDAV/sharing/calendars/read-only/3.xml</filepath>
				</data>
				<verify>
					<callback>statusCode</callback>
				</verify>
				<grabelement>
					<name>{DAV:}href</name>
					<variable>$sharedcalendar:</variable>
				</grabelement>
			</request>
		</test>
		<test name='4'>
			<description>Shared calendar exists</description>
			<request user="$userid2:" pswd="$pswd2:">
				<method>PROPFIND</method>
				<ruri>$sharedcalendar:/</ruri>
				<header>
					<name>Depth</name>
					<value>0</value>
				</header>
				<data>
					<content-type>text/xml; charset=utf-8</content-type>
					<filepath>Resource/CalDAV/sharing/calendars/read-only/4.xml</filepath>
				</data>
				<verify>
					<callback>xmlElementMatch</callback>
					<arg>
						<name>exists</name>
						<value>$verify-property-prefix:/{DAV:}owner/{DAV:}href[=$principaluri1:]</value>
						<value>$verify-property-prefix:/{DAV:}resourcetype/{http://calendarserver.org/ns/}shared</value>
						<value>$verify-property-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}read</value>
					</arg>
					<arg>
						<name>notexists</name>
						<value>$verify-property-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}write</value>
						<value>$verify-property-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}bind</value>
						<value>$verify-property-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}unbind</value>
						<value>$verify-property-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}admin</value>
						<value>$verify-property-prefix:/{DAV:}current-user-privilege-set/{DAV:}privilege/{DAV:}all</value>
					</arg>
				</verify>
			</request>
		</test>
		<test name='5'>
			<description>Create event</description>
			<request user="$userid2:" pswd="$pswd2:">
				<method>PUT</method>
				<ruri>$sharedcalendar:/3.ics</ruri>
				<data>
					<content-type>text/calendar; charset=utf-8</content-type>
					<filepath>Resource/CalDAV/sharing/calendars/read-only/5.ics</filepath>
				</data>
				<verify>
					<callback>statusCode</callback>
					<arg>
						<name>status</name>
						<value>403</value>
					</arg>
				</verify>
			</request>
		</test>
		<test name='6'>
			<description>Sharer creates event</description>
			<request>
				<method>PUT</method>
				<ruri>$calendarhome1:/shared/4.ics</ruri>
				<data>
					<content-type>text/calendar; charset=utf-8</content-type>
					<filepath>Resource/CalDAV/sharing/calendars/read-only/6.ics</filepath>
				</data>
				<verify>
					<callback>statusCode</callback>
				</verify>
			</request>
		</test>
		<test name='7'>
			<description>Sharee sees new event</description>
			<request user="$userid2:" pswd="$pswd2:">
				<method>GET</method>
				<ruri>$sharedcalendar:/4.ics</ruri>
				<verify>
					<callback>calendarDataMatch</callback>
					<arg>
						<name>filepath</name>
						<value>Resource/CalDAV/sharing/calendars/read-only/6.ics</value>
					</arg>
				</verify>
			</request>
		</test>
		<test name='8'>
			<description>Sharee cannot change event</description>
			<request user="$userid2:" pswd="$pswd2:">
				<method>PUT</method>
				<ruri>$sharedcalendar:/4.ics</ruri>
				<data>
					<content-type>text/calendar; charset=utf-8</content-type>
					<filepath>Resource/CalDAV/sharing/calendars/read-only/7.ics</filepath>
				</data>
				<verify>
					<callback>statusCode</callback>
					<arg>
						<name>status</name>
						<value>403</value>
					</arg>
				</verify>
			</request>
		</test>
	</test-suite>

-->

	<end>
		<!--
		<request user="$useradmin:" pswd="$pswdadmin:">
			<method>DELETEALL</method>
			<ruri>$notificationpath1:/</ruri>
			<ruri>$notificationpath2:/</ruri>
			<ruri>$notificationpath3:/</ruri>
			<ruri>$notificationpath4:/</ruri>
		</request>
		-->
	</end>
	
</caldavtest>
