added simple event testing json->dav

pull/91/head
Luc Charland 2015-06-01 10:52:32 -04:00 committed by Francis Lachapelle
parent 4f75499439
commit e385bf34d5
2 changed files with 131 additions and 27 deletions

View File

@ -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")

View File

@ -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()