mirror of
https://github.com/inverse-inc/sogo.git
synced 2026-02-17 07:33:57 +00:00
test adding single and multiple phones with JSON and getting it with DAV
This commit is contained in:
committed by
Francis Lachapelle
parent
cdd52a0b37
commit
9a3f4080c0
87
Tests/Integration/carddav.py
Normal file
87
Tests/Integration/carddav.py
Normal file
@@ -0,0 +1,87 @@
|
||||
from config import hostname, port, username, password
|
||||
import webdavlib
|
||||
import simplejson
|
||||
import sogoLogin
|
||||
|
||||
|
||||
DEBUG=True
|
||||
DEBUG=False
|
||||
|
||||
|
||||
class HTTPPreferencesPOST (webdavlib.HTTPPOST):
|
||||
cookie = None
|
||||
|
||||
def prepare_headers (self):
|
||||
headers = webdavlib.HTTPPOST.prepare_headers(self)
|
||||
if self.cookie:
|
||||
headers["Cookie"] = self.cookie
|
||||
return headers
|
||||
|
||||
class HTTPPreferencesGET (webdavlib.HTTPGET):
|
||||
cookie = None
|
||||
|
||||
def prepare_headers (self):
|
||||
headers = webdavlib.HTTPGET.prepare_headers(self)
|
||||
if self.cookie:
|
||||
headers["Cookie"] = self.cookie
|
||||
return headers
|
||||
|
||||
class Carddav:
|
||||
login = username
|
||||
passw = password
|
||||
|
||||
def __init__(self, otherLogin = None, otherPassword = None):
|
||||
if otherLogin and otherPassword:
|
||||
self.login = otherLogin
|
||||
self.passw = otherPassword
|
||||
|
||||
self.client = webdavlib.WebDAVClient(hostname, port)
|
||||
authCookie = sogoLogin.getAuthCookie(hostname, port, self.login, self.passw)
|
||||
self.cookie = authCookie
|
||||
self.cards = 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)
|
||||
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'])
|
||||
return content
|
||||
|
||||
def save_card(self, card):
|
||||
url = "/SOGo/%s/Contacts/personal/%s/saveAsContact" % (self.login, card['id'])
|
||||
if DEBUG: print "URL:", url
|
||||
|
||||
post = HTTPPreferencesPOST(url, simplejson.dumps(card))
|
||||
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"])
|
||||
|
||||
94
Tests/Integration/test-carddav.py
Executable file
94
Tests/Integration/test-carddav.py
Executable file
@@ -0,0 +1,94 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
|
||||
from config import hostname, port, username, password
|
||||
|
||||
import carddav
|
||||
import sogotests
|
||||
import unittest
|
||||
import webdavlib
|
||||
|
||||
|
||||
class JsonDavTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self._connect_as_user()
|
||||
self.newphone = [{'type': 'home', 'value': '123.456.7890'}]
|
||||
self.newphones_difftype = [{'type': 'home', 'value': '123.456.7890'},
|
||||
{'type': 'work', 'value': '987.654.3210'},
|
||||
{'type': 'fax', 'value': '555.666.7777'}]
|
||||
self.newphones_sametype = [{'type': 'work', 'value': '123.456.7890'},
|
||||
{'type': 'work', 'value': '987.654.3210'}]
|
||||
# Easier to erase them all in tearDown
|
||||
self.allphones = list(self.newphone)
|
||||
self.allphones.extend(self.newphones_difftype)
|
||||
self.allphones.extend(self.newphones_sametype)
|
||||
|
||||
def tearDown(self):
|
||||
self._connect_as_user()
|
||||
self._get_card()
|
||||
#- Remove the phones we just added
|
||||
for phone in self.allphones:
|
||||
try:
|
||||
self.card['phones'].pop(self.card['phones'].index(phone))
|
||||
except ValueError:
|
||||
#print "Can't find", phone
|
||||
pass
|
||||
self._save_card()
|
||||
|
||||
|
||||
def _connect_as_user(self, newuser=username, newpassword=password):
|
||||
self.dv = carddav.Carddav(newuser, newpassword)
|
||||
|
||||
def _get_card(self, name="John Doe"):
|
||||
tmp_card = self.dv.get_cards(name)[0]
|
||||
self.card = self.dv.get_card(tmp_card['c_name'])
|
||||
|
||||
def _save_card(self, card=None):
|
||||
if card:
|
||||
self.dv.save_card(card)
|
||||
else:
|
||||
self.dv.save_card(self.card)
|
||||
|
||||
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/Contacts/personal/%s" % (username, filename))
|
||||
w.execute(query)
|
||||
self.assertEquals(query.response['status'], 200)
|
||||
return query.response['body'].split("\r\n")
|
||||
|
||||
def _phone_to_dav_str(self, phonedict):
|
||||
return "TEL;TYPE=%s:%s" % (phonedict['type'], phonedict['value'])
|
||||
|
||||
def _testMultiplePhones(self, phones):
|
||||
""" Add Multiple Phones to Contact JSON and verify with DAV """
|
||||
#- Use JSON to get CARD and add a phone and save it back
|
||||
self._get_card()
|
||||
oldphones = self.card['phones']
|
||||
oldphones.extend(phones)
|
||||
self._save_card()
|
||||
#- Make sure that the phone is there when using JSON
|
||||
self._connect_as_user()
|
||||
self._get_card()
|
||||
#print "C:::", self.card
|
||||
testphones = self.card['phones']
|
||||
#print "P1:", oldphones
|
||||
#print "P2:", testphones
|
||||
self.assertEquals(sorted(oldphones), sorted(testphones))
|
||||
#- Verify that DAV has the same values
|
||||
dav = self._get_dav_data(self.card['id'], username, password)
|
||||
for phone in phones:
|
||||
found = dav.index(self._phone_to_dav_str(phone))
|
||||
self.assertTrue(found > 0)
|
||||
|
||||
def testSinglePhone(self):
|
||||
self._testMultiplePhones(self.newphone)
|
||||
|
||||
def testMultipleDifferentPhones(self):
|
||||
self._testMultiplePhones(self.newphones_difftype)
|
||||
|
||||
def testMultipleSameTypePhones(self):
|
||||
self._testMultiplePhones(self.newphones_sametype)
|
||||
|
||||
if __name__ == "__main__":
|
||||
sogotests.runTests()
|
||||
Reference in New Issue
Block a user