From e385bf34d5ac7568fcb381e549f58e82964a7eef Mon Sep 17 00:00:00 2001 From: Luc Charland Date: Mon, 1 Jun 2015 10:52:32 -0400 Subject: [PATCH] added simple event testing json->dav --- Tests/Integration/carddav.py | 94 ++++++++++++++++++++++--------- Tests/Integration/test-carddav.py | 64 ++++++++++++++++++++- 2 files changed, 131 insertions(+), 27 deletions(-) diff --git a/Tests/Integration/carddav.py b/Tests/Integration/carddav.py index 62eb8e9bb..68a859723 100644 --- a/Tests/Integration/carddav.py +++ b/Tests/Integration/carddav.py @@ -39,29 +39,14 @@ class Carddav: authCookie = sogoLogin.getAuthCookie(hostname, port, self.login, self.passw) self.cookie = authCookie self.cards = None + self.calendars = None + self.fields = None + self.events = None #- If this is not set, we CAN'T save preferences self.preferences = None - def load_cards(self): - if not self.cards: - url = "/SOGo/%s/Contacts/personal/view" % (self.login) - get = HTTPPreferencesGET(url) - get.cookie = self.cookie - self.client.execute(get) - if DEBUG: print "(url):", url - if DEBUG: print "(status):", get.response["status"] - if DEBUG: print "(body):", get.response['body'] - content = simplejson.loads(get.response['body']) - self.cards = content['cards'] - return self.cards - - def get_cards(self, pattern): - self.load_cards() - return [a for a in self.cards if pattern in a.values()] - - def get_card(self, idstr): - url = "/SOGo/%s/Contacts/personal/%s/view" % (self.login, idstr) + def _get(self, url): get = HTTPPreferencesGET(url) get.cookie = self.cookie self.client.execute(get) @@ -71,17 +56,74 @@ class Carddav: content = simplejson.loads(get.response['body']) return content - def save_card(self, card): - url = "/SOGo/%s/Contacts/personal/%s/saveAsContact" % (self.login, card['id']) + def _post(self, url, data, errormsg="failure POST"): if DEBUG: print "URL:", url - - post = HTTPPreferencesPOST(url, simplejson.dumps(card)) + post = HTTPPreferencesPOST(url, simplejson.dumps(data)) post.content_type = "application/json" post.cookie = self.cookie self.client.execute(post) - # Raise an exception if the pref wasn't properly set if post.response["status"] != 200: - raise Exception ("failure setting prefs, (code = %d)" \ - % post.response["status"]) + raise Exception ("%s, (code = %d)" % (errormsg, post.response["status"])) + + def load_cards(self): + if not self.cards: + url = "/SOGo/so/%s/Contacts/personal/view" % (self.login) + content = self._get(url) + self.cards = content['cards'] + return self.cards + + def get_cards(self, pattern): + self.load_cards() + return [a for a in self.cards if pattern in a.values()] + + def get_card(self, idstr): + url = "/SOGo/so/%s/Contacts/personal/%s/view" % (self.login, idstr) + content = self._get(url) + return content + + def save_card(self, card): + url = "/SOGo/so/%s/Contacts/personal/%s/saveAsContact" % (self.login, card['id']) + self._post(url, card, "failure saving card") + + def load_calendars(self): + if not self.calendars: + url = "/SOGo/so/%s/Calendar/calendarslist" % (self.login) + content = self._get(url) + self.calendars = content['calendars'] + return self.calendars + + def get_calendars(self, pattern): + self.load_calendars() + return [a for a in self.calendars if pattern in a.values()] + + def get_calendar(self, idstr): + self.load_calendars() + callist = [a for a in self.calendars if a['id'] == idstr] + if len(callist): + return callist[0] + return None + + def save_calendar(self, calendar): + url = "/SOGo/so/%s/Contacts/personal/%s/saveAsContact" % (self.login, calendar['id']) + self._post(url, card, "failure saving calendar") + + def load_events(self): + if not self.events: + url = "/SOGo/so/%s/Calendar/eventslist" % (self.login) + content = self._get(url) + tmp_events = content['events'] + self.fields = content['fields'] + self.events = [dict(zip(self.fields, event)) for event in tmp_events] + return self.events + + def newguid(self, folderpath): + url = "/SOGo/so/%s/%s/newguid" % (self.login, folderpath) + content = self._get(url) + return content['id'] + + def save_event(self, event, folder, gid): + #url = "/SOGo/so/%s/%s/%s/save" % (self.login, event['c_folder'], event['c_name']) + url = "/SOGo/so/%s/%s/%s/saveAsAppointment" % (self.login, folder, gid) + self._post(url, event, "failure saving event") diff --git a/Tests/Integration/test-carddav.py b/Tests/Integration/test-carddav.py index d7a771daf..5748730ed 100755 --- a/Tests/Integration/test-carddav.py +++ b/Tests/Integration/test-carddav.py @@ -7,9 +7,71 @@ import carddav import sogotests import unittest import webdavlib +import time -class JsonDavTests(unittest.TestCase): +class JsonDavEventTests(unittest.TestCase): + def setUp(self): + self._connect_as_user() + + def _connect_as_user(self, newuser=username, newpassword=password): + self.dv = carddav.Carddav(newuser, newpassword) + + def _create_new_event(self, path): + gid = self.dv.newguid(path) + event = {'startDate': "2015-12-25", + 'startTime': "10:00", + 'endDate': "2015-12-25", + 'endTime': "23:00", + 'isTransparent': 0, + 'sendAppointmentNotifications': 0, + 'summary': "Big party", + 'alarm': {'action': 'display', + 'quantity': 10, + 'unit': "MINUTES", + 'reference': "BEFORE", + 'relation': "START", + 'email': "sogo1@example.com"}, + 'organizer': {'name': u"Balthazar C\xe9sar", + 'email': "sogo2@example.com"}, + 'c_name': gid, + 'c_folder': path + } + return (event, path, gid) + + def _get_dav_data(self, filename, user=username, passwd=password): + w = webdavlib.WebDAVClient(hostname, port, user, passwd) + query = webdavlib.HTTPGET("http://localhost/SOGo/dav/%s/Calendar/personal/%s" % (username, filename)) + w.execute(query) + self.assertEquals(query.response['status'], 200) + return query.response['body'].split("\r\n") + + def _get_dav_field(self, davdata, fieldname): + try: + data = [a.split(':')[1] for a in davdata if fieldname in a][0] + except IndexError: + data = '' + return data + + def test_create_new_event(self): + path = 'Calendar/personal' + (event, folder, gid) = self._create_new_event(path) + #print "Saving Event to:", folder, gid + self.dv.save_event(event, folder, gid) + #- Get the event back with JSON + self._connect_as_user() + self.dv.load_events() + elist = [e for e in self.dv.events if e['c_name'] == gid] + #- MUST have this event -- only once + self.assertEquals(len(elist), 1) + strdate = "%d-%.02d-%.02d" % time.gmtime(elist[0]['c_startdate'])[0:3] + self.assertEquals(strdate, event['startDate']) + #- Get the event back with DAV + dav = self._get_dav_data(gid, username, password) + self.assertEquals(self._get_dav_field(dav, 'SUMMARY:'), event['summary']) + + +class JsonDavPhoneTests(unittest.TestCase): def setUp(self): self._connect_as_user()