def testDefaultClient(self): """When no client is given to a DAVObject, but the parent is given, parent.client will be used""" client = DAVClient(url="http://*****:*****@calendar.example:80/") calhome = CalendarSet(client, "http://*****:*****@calendar.example:80/me/") calendar = Calendar(parent=calhome) assert_equal(calendar.client, calhome.client)
def setup(self): logging.debug("############## test setup") if self.server_params.get('unique_calendar_ids', False): self.testcal_id = 'testcalendar-' + str(uuid.uuid4()) self.testcal_id2 = 'testcalendar-' + str(uuid.uuid4()) else: self.testcal_id = "pythoncaldav-test" self.testcal_id2 = "pythoncaldav-test2" self.conn_params = self.server_params.copy() for x in list(self.conn_params.keys()): if x not in ('url', 'proxy', 'username', 'password', 'ssl_verify_cert'): self.conn_params.pop(x) self.caldav = DAVClient(**self.conn_params) self.principal = self.caldav.principal() logging.debug("## going to tear down old test calendars, " "in case teardown wasn't properly executed " "last time tests were run") self._teardown() logging.debug("##############################") logging.debug("############## test setup done") logging.debug("##############################")
def testAbsoluteURL(self): """Version 0.7.0 does not handle responses with absolute URLs very well, ref https://github.com/python-caldav/caldav/pull/103""" ## none of this should initiate any communication client = DAVClient(url='http://cal.example.com/') principal = Principal(client=client, url='http://cal.example.com/home/bernard/') ## now, ask for the calendar_home_set, but first we need to mock up client.propfind mocked_response = mock.MagicMock() mocked_response.status_code = 207 mocked_response.reason = 'multistatus' mocked_response.headers = {} mocked_response.content = """ <xml> <d:multistatus xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav"> <d:response> <d:href>http://cal.example.com/home/bernard/</d:href> <d:propstat> <d:prop> <c:calendar-home-set> <d:href>http://cal.example.com/home/bernard/calendars/</d:href> </c:calendar-home-set> </d:prop> <d:status>HTTP/1.1 200 OK</d:status> </d:propstat> </d:response> </d:multistatus> </xml>""" mocked_davresponse = DAVResponse(mocked_response) client.propfind = mock.MagicMock(return_value=mocked_davresponse) bernards_calendars = principal.calendar_home_set assert_equal(bernards_calendars.url, URL('http://cal.example.com/home/bernard/calendars/'))
def testProxy(self): if self.caldav.url.scheme == 'https': logging.info( "Skipping %s.testProxy as the TinyHTTPProxy implementation doesn't support https" ) return server_address = ('127.0.0.1', 8080) proxy_httpd = NonThreadingHTTPServer( server_address, ProxyHandler, logging.getLogger("TinyHTTPProxy")) threadobj = threading.Thread(target=proxy_httpd.serve_forever) try: threadobj.start() assert (threadobj.is_alive()) conn_params = self.conn_params.copy() conn_params['proxy'] = proxy c = DAVClient(**conn_params) p = c.principal() assert_not_equal(len(p.calendars()), 0) finally: proxy_httpd.shutdown() ## this should not be necessary, but I've observed some failures if threadobj.is_alive(): time.sleep(0.05) assert (not threadobj.is_alive()) threadobj = threading.Thread(target=proxy_httpd.serve_forever) try: threadobj.start() assert (threadobj.is_alive()) conn_params = self.conn_params.copy() conn_params['proxy'] = proxy_noport c = DAVClient(**conn_params) p = c.principal() assert_not_equal(len(p.calendars()), 0) assert (threadobj.is_alive()) finally: proxy_httpd.shutdown() ## this should not be necessary if threadobj.is_alive(): time.sleep(0.05) assert (not threadobj.is_alive())
def MockedDAVClient(xml_returned): """ For unit testing - a mocked DAVClient returning some specific content every time a request is performed """ client = DAVClient( url='https://somwhere.in.the.universe.example/some/caldav/root') client.request = mock.MagicMock( return_value=MockedDAVResponse(xml_returned)) return client
def setup(self): logging.debug("############## test setup") self.conn_params = self.server_params.copy() for x in self.conn_params.keys(): if not x in ('url', 'proxy', 'username', 'password'): self.conn_params.pop(x) self.caldav = DAVClient(**self.conn_params) self.principal = self.caldav.principal() ## tear down old test calendars, in case teardown wasn't properly ## executed last time tests were run self._teardown() logging.debug("############## test setup done")
def push_caldavs(icals): '''Regist ical formatted events to CalDAV server''' client = DAVClient(url=CALDAV_URL, username=CALDAV_USER, password=CALDAV_PASS) principal = client.principal() # get target calendar and remove all events calendar = [c for c in principal.calendars() if c.name == CAL_NAME][0] for event in calendar.events(): event.delete() # put events if icals: for ical in icals: try: calendar.add_event(ical) except BaseException: print(ical)
def testInstance(self): cal_url = "http://*****:*****@calendar.example:80/" client = DAVClient(url=cal_url) my_event = Event(client, data=ev1) my_event.vobject_instance.vevent.summary.value = 'new summary' assert ('new summary' in my_event.data) icalobj = my_event.icalendar_instance icalobj.subcomponents[0]['SUMMARY'] = 'yet another summary' assert_equal(my_event.vobject_instance.vevent.summary.value, 'yet another summary') ## Now the data has been converted from string to vobject to string to icalendar to string to vobject and ... will the string still match the original? lines_now = my_event.data.split('\r\n') lines_orig = ev1.replace('Bastille Day Party', 'yet another summary').split('\n') lines_now.sort() lines_orig.sort() assert_equal(lines_now, lines_orig)
def testRequestNonAscii(self, mocked): """ ref https://github.com/python-caldav/caldav/issues/83 """ mocked().status_code = 200 cal_url = "http://*****:*****@calendar.møøh.example:80/" client = DAVClient(url=cal_url) response = client.put('/foo/møøh/bar', 'bringebærsyltetøy 北京 пиво', {}) assert_equal(response.status, 200) assert (response.tree is None) if PY3: response = client.put('/foo/møøh/bar'.encode('utf-8'), 'bringebærsyltetøy 北京 пиво'.encode('utf-8'), {}) else: response = client.put(u'/foo/møøh/bar', u'bringebærsyltetøy 北京 пиво', {}) assert_equal(response.status, 200) assert (response.tree is None)
def testCalendar(self): """ Principal.calendar() and CalendarSet.calendar() should create Calendar objects without initiating any communication with the server. Calendar.event() should create Event object without initiating any communication with the server. DAVClient.__init__ also doesn't do any communication Principal.__init__ as well, if the principal_url is given Principal.calendar_home_set needs to be set or the server will be queried """ cal_url = "http://*****:*****@calendar.example:80/" client = DAVClient(url=cal_url) principal = Principal(client, cal_url + "me/") principal.calendar_home_set = cal_url + "me/calendars/" # calendar_home_set is actually a CalendarSet object assert (isinstance(principal.calendar_home_set, CalendarSet)) calendar1 = principal.calendar(name="foo", cal_id="bar") calendar2 = principal.calendar_home_set.calendar(name="foo", cal_id="bar") calendar3 = principal.calendar(cal_id="bar") assert_equal(calendar1.url, calendar2.url) assert_equal(calendar1.url, calendar3.url) assert_equal(calendar1.url, "http://calendar.example:80/me/calendars/bar/") # principal.calendar_home_set can also be set to an object # This should be noop principal.calendar_home_set = principal.calendar_home_set calendar1 = principal.calendar(name="foo", cal_id="bar") assert_equal(calendar1.url, calendar2.url) # When building a calendar from a relative URL and a client, # the relative URL should be appended to the base URL in the client calendar1 = Calendar(client, 'someoneelse/calendars/main_calendar') calendar2 = Calendar( client, 'http://*****:*****@calendar.example:80/someoneelse/calendars/main_calendar' ) assert_equal(calendar1.url, calendar2.url)
def testFailedQuery(self): """ ref https://github.com/python-caldav/caldav/issues/54 """ cal_url = "http://*****:*****@calendar.example:80/" client = DAVClient(url=cal_url) calhome = CalendarSet(client, cal_url + "me/") ## syntesize a failed response class FailedResp: pass failedresp = FailedResp() failedresp.status = 400 failedresp.reason = "you are wrong" failedresp.raw = "your request does not adhere to standards" ## synthesize a new http method calhome.client.unknown_method = lambda url, body, depth: failedresp ## call it. assert_raises(error.DAVError, calhome._query, query_method='unknown_method')
def testBackwardCompatibility(self): """ Tobias Brox has done some API changes - but this thing should still be backward compatible. """ if not 'backwards_compatibility_url' in self.server_params: return caldav = DAVClient(self.server_params['backwards_compatibility_url']) principal = Principal( caldav, self.server_params['backwards_compatibility_url']) c = Calendar(caldav, name="Yep", parent=principal, id=testcal_id).save() assert_not_equal(c.url, None) c.set_properties([ dav.DisplayName("hooray"), ]) props = c.get_properties([ dav.DisplayName(), ]) assert_equal(props[dav.DisplayName.tag], "hooray") cc = Calendar(caldav, name="Yep", parent=principal).save() assert_not_equal(cc.url, None) cc.delete() e = Event(caldav, data=ev1, parent=c).save() assert_not_equal(e.url, None) ee = Event(caldav, url=url.make(e.url), parent=c) ee.load() assert_equal(e.instance.vevent.uid, ee.instance.vevent.uid) r = c.date_search(datetime(2006, 7, 13, 17, 00, 00), datetime(2006, 7, 15, 17, 00, 00)) assert_equal(e.instance.vevent.uid, r[0].instance.vevent.uid) assert_equal(len(r), 1) all = c.events() assert_equal(len(all), 1) e2 = Event(caldav, data=ev2, parent=c).save() assert_not_equal(e.url, None) tmp = c.event("*****@*****.**") assert_equal(e2.instance.vevent.uid, tmp.instance.vevent.uid) r = c.date_search(datetime(2007, 7, 13, 17, 00, 00), datetime(2007, 7, 15, 17, 00, 00)) assert_equal(len(r), 1) e.data = ev2 e.save() r = c.date_search(datetime(2007, 7, 13, 17, 00, 00), datetime(2007, 7, 15, 17, 00, 00)) for e in r: print(e.data) assert_equal(len(r), 1) e.instance = e2.instance e.save() r = c.date_search(datetime(2007, 7, 13, 17, 00, 00), datetime(2007, 7, 15, 17, 00, 00)) for e in r: print(e.data) assert_equal(len(r), 1)
def testProxy(self): c = DAVClient(principal_url, proxy) p = Principal(c, principal_url) assert_not_equal(len(p.calendars()), 0)
def setup(self): self.caldav = DAVClient(principal_url) self.principal = Principal(self.caldav, principal_url)