From 05ad7a8d6d07a2353802284a7c9a96a00ca640db Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Fri, 20 Aug 2021 17:36:05 -0400 Subject: [PATCH] test: migration from Python to JavaScript --- Tests/lib/WebDAV.js | 35 ++++++++++- Tests/spec/DAVCalendarClassificationSpec.js | 2 +- Tests/spec/DAVContactsCategoriesSpec.js | 68 +++++++++++++++++++++ 3 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 Tests/spec/DAVContactsCategoriesSpec.js diff --git a/Tests/lib/WebDAV.js b/Tests/lib/WebDAV.js index ce34beef7..82c6d0dde 100644 --- a/Tests/lib/WebDAV.js +++ b/Tests/lib/WebDAV.js @@ -83,6 +83,29 @@ class WebDAV { }) } + propfindWebdav(resource, properties, depth = 0) { + const formattedProperties = properties.map(p => { + return { [`i:${p}`]: '' } + }) + return davRequest({ + url: this.serverUrl + resource, + init: { + method: 'PROPFIND', + headers: { ...this.headers, depth: new String(depth) }, + namespace: DAVNamespaceShorthandMap[DAVNamespace.DAV], + body: { + propfind: { + _attributes: { + ...getDAVAttribute([DAVNamespace.DAV]), + 'xmlns:i': 'urn:inverse:params:xml:ns:inverse-dav' + }, + prop: formattedProperties + } + } + } + }) + } + propfindEvent(resource) { return propfind({ url: this.serverUrl + resource, @@ -264,15 +287,21 @@ class WebDAV { }) } - proppatchWebdav(resource, properties) { + proppatchWebdav(resource, properties, depth = 0) { const formattedProperties = Object.keys(properties).map(p => { - return { [`i:${p}`]: properties[p] } + if (typeof properties[p] == 'object') { + return { [`i:${p}`]: properties[p].map(pp => { + const [ key ] = Object.keys(pp) + return { [`i:${key}`]: pp[key] || '' } + })} + } + return { [`i:${p}`]: properties[p] || '' } }) return davRequest({ url: this.serverUrl + resource, init: { method: 'PROPPATCH', - headers: this.headers, + headers: { ...this.headers, depth: new String(depth) }, namespace: DAVNamespaceShorthandMap[DAVNamespace.DAV], body: { propertyupdate: { diff --git a/Tests/spec/DAVCalendarClassificationSpec.js b/Tests/spec/DAVCalendarClassificationSpec.js index c14dde0d3..8c81f58a4 100644 --- a/Tests/spec/DAVCalendarClassificationSpec.js +++ b/Tests/spec/DAVCalendarClassificationSpec.js @@ -1,7 +1,7 @@ import config from '../lib/config' import WebDAV from '../lib/WebDAV' -describe('default classification', function() { +describe('calendar classification', function() { const webdav = new WebDAV(config.username, config.password) const _setClassification = async function(component, classification = '') { diff --git a/Tests/spec/DAVContactsCategoriesSpec.js b/Tests/spec/DAVContactsCategoriesSpec.js new file mode 100644 index 000000000..e420e3eab --- /dev/null +++ b/Tests/spec/DAVContactsCategoriesSpec.js @@ -0,0 +1,68 @@ +import config from '../lib/config' +import WebDAV from '../lib/WebDAV' + +describe('contacts categories', function() { + const webdav = new WebDAV(config.username, config.password) + + const _setCategories = async function(categories = []) { + const resource = `/SOGo/dav/${config.username}/Contacts/` + const elements = categories.map(c => { + return { 'category': c } + }) + const properties = { 'contacts-categories': elements.length ? elements : '' } + + const results = await webdav.proppatchWebdav(resource, properties) + expect(results.length) + .withContext(`Set contacts categories to ${categories.join(', ')}`) + .toBe(1) + + return results[0].status + } + + const _getCategories = async function() { + const resource = `/SOGo/dav/${config.username}/Contacts/` + const properties = ['contacts-categories'] + + const results = await webdav.propfindWebdav(resource, properties) + expect(results.length) + .toBe(1) + const { props: { contactsCategories: { category } = {} } = {} } = results[0] + + return category + } + + // HTTPContactCategoriesTest + + it('setting contacts categories', async function() { + let status, results + + status = await _setCategories() + expect(status) + .withContext('Removing contacts categories') + .toBe(207) + results = await _getCategories() + expect(results) + .toBeUndefined() + + status = await _setCategories(['Coucou']) + expect(status) + .withContext('Setting one contacts category') + .toBe(207) + results = await _getCategories() + expect(results) + .toBe('Coucou') + + status = await _setCategories(['Toto', 'Cuicui']) + expect(status) + .withContext('Setting two contacts category') + .toBe(207) + + results = await _getCategories() + expect(results.length) + .toBe(2) + expect(results) + .toContain('Toto') + expect(results) + .toContain('Cuicui') + }) +}) \ No newline at end of file