From d43b28ca7306f6b5a7a6c5e387644e9cf4543b1b Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Fri, 5 Nov 2021 16:43:43 -0400 Subject: [PATCH] test: migration from Python to JavaScript --- Tests/lib/WebDAV.js | 38 +++++++++++++++++++++++++++++------- Tests/spec/WebDAVSpec.js | 4 ++++ Tests/spec/WebDavSyncSpec.js | 29 +++++++++++++++------------ 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/Tests/lib/WebDAV.js b/Tests/lib/WebDAV.js index e8ed12abd5..e608313460 100644 --- a/Tests/lib/WebDAV.js +++ b/Tests/lib/WebDAV.js @@ -296,13 +296,37 @@ class WebDAV { } syncQuery(resource, token = '', properties) { - const formattedProperties = properties.map(p => { return { name: p, namespace: DAVNamespace.DAV } }) - return syncCollection({ - url: this.serverUrl + resource, - props: formattedProperties, - syncLevel: 1, - syncToken: token, - headers: this.headers + const formattedProperties = properties.map((p) => { + return { [`${DAVNamespaceShorthandMap[DAVNamespace.DAV]}:${p}`]: '' } + }); + let xmlBody = convert.js2xml( + { + 'sync-collection': { + _attributes: getDAVAttribute([DAVNamespace.DAV]), + 'sync-level': 1, + 'sync-token': token, + prop: formattedProperties + } + }, + { + compact: true, + spaces: 2, + elementNameFn: (name) => { + // add namespace to all keys without namespace + if (!/^.+:.+/.test(name)) { + return `${DAVNamespaceShorthandMap[DAVNamespace.DAV]}:${name}` + } + return name + } + } + ) + return fetch(this.serverUrl + resource, { + headers: { + 'Content-Type': 'application/xml; charset="utf-8"', + ...this.headers + }, + method: 'REPORT', + body: xmlBody }) } diff --git a/Tests/spec/WebDAVSpec.js b/Tests/spec/WebDAVSpec.js index 64cb9638c5..44516ffc52 100644 --- a/Tests/spec/WebDAVSpec.js +++ b/Tests/spec/WebDAVSpec.js @@ -63,6 +63,10 @@ describe('WebDAV', function() { const results = await webdav.principalPropertySearch(resource) expect(results.length).toBe(1) results.forEach(o => { + expect(o.status) + .withContext(`HTTP status code when a performing a property search`) + .toBe(207) + expect(o.href).toBe(`/SOGo/dav/${config.username}/`) expect(o.props.displayname).toBe(user.displayname) }) }) diff --git a/Tests/spec/WebDavSyncSpec.js b/Tests/spec/WebDavSyncSpec.js index da83cb6aeb..ea3b28aa1e 100644 --- a/Tests/spec/WebDavSyncSpec.js +++ b/Tests/spec/WebDavSyncSpec.js @@ -1,5 +1,7 @@ import config from '../lib/config' import WebDAV from '../lib/WebDAV' +import { DAVNamespace, DAVNamespaceShorthandMap } from 'tsdav' +import convert from 'xml-js' describe('webdav sync', function() { const webdav = new WebDAV(config.username, config.password) @@ -14,16 +16,17 @@ describe('webdav sync', function() { }) it("webdav sync", async function() { - let results + const nsShort = DAVNamespaceShorthandMap[DAVNamespace.DAV].toUpperCase() + let response, xml, token // missing tests: // invalid tokens: negative, non-numeric, > current timestamp // non-empty collections: token validity, status codes for added, // modified and removed elements - results = await webdav.makeCalendar(resource) - expect(results.length).toBe(1) - expect(results[0].status).toBe(201) + response = await webdav.makeCalendar(resource) + expect(response.length).toBe(1) + expect(response[0].status).toBe(201) // test queries: // empty collection: @@ -33,16 +36,18 @@ describe('webdav sync', function() { // without a token (query3) // with a token (query4)) - results = await webdav.syncQuery(resource, null, [ 'getetag' ]) - expect(results.length).toBe(1) - expect(results[0].status).toBe(207) - // TODO: sync-token is not returned by the tsdav library -- grep raw + response = await webdav.syncQuery(resource, null, [ 'getetag' ]) + xml = await response.text(); + ({ [`${nsShort}:multistatus`]: { [`${nsShort}:sync-token`]: { _text: token } } } = convert.xml2js(xml, {compact: true, nativeType: true})) + expect(response.status).toBe(207) + expect(token).toBeGreaterThanOrEqual(0) // we make sure that any token is accepted when the collection is // empty, but that the returned token differs - results = await webdav.syncQuery(resource, '1234', [ 'getetag' ]) - expect(results.length).toBe(1) - expect(results[0].status).toBe(207) - // TODO: sync-token is not returned by the tsdav library -- grep raw? + response = await webdav.syncQuery(resource, '1234', [ 'getetag' ]) + xml = await response.text(); + ({ [`${nsShort}:multistatus`]: { [`${nsShort}:sync-token`]: { _text: token } } } = convert.xml2js(xml, {compact: true, nativeType: true})) + expect(response.status).toBe(207) + expect(token).toBeGreaterThanOrEqual(0) }) }) \ No newline at end of file