Skip to content

Commit

Permalink
test: migration from Python to JavaScript
Browse files Browse the repository at this point in the history
  • Loading branch information
cgx committed Aug 26, 2021
1 parent b81c4ea commit 5622eda
Show file tree
Hide file tree
Showing 5 changed files with 327 additions and 18 deletions.
77 changes: 64 additions & 13 deletions Tests/lib/WebDAV.js
Expand Up @@ -16,9 +16,15 @@ import {
} from 'tsdav'
import { formatProps, getDAVAttribute } from 'tsdav/dist/util/requestHelpers';
import { makeCollection } from 'tsdav/dist/collection';
import convert from 'xml-js'
import { fetch } from 'cross-fetch'
import config from './config'

const DAVInverse = 'urn:inverse:params:xml:ns:inverse-dav'
const DAVInverseShort = 'i'

export { DAVInverse, DAVInverseShort }

class WebDAV {
constructor(un, pw) {
this.serverUrl = `http://${config.hostname}:${config.port}`
Expand Down Expand Up @@ -83,21 +89,25 @@ class WebDAV {
})
}

propfindWebdav(resource, properties, depth = 0) {
propfindWebdav(resource, properties, namespace = DAVNamespace.DAV, headers = {}) {
const nsShort = DAVNamespaceShorthandMap[namespace] || DAVInverseShort
const formattedProperties = properties.map(p => {
return { [`i:${p}`]: '' }
return { [`${nsShort}:${p}`]: '' }
})
if (typeof headers.depth == 'undefined') {
headers.depth = new String(0)
}
return davRequest({
url: this.serverUrl + resource,
init: {
method: 'PROPFIND',
headers: { ...this.headers, depth: new String(depth) },
headers: { ...this.headers, ...headers },
namespace: DAVNamespaceShorthandMap[DAVNamespace.DAV],
body: {
propfind: {
_attributes: {
...getDAVAttribute([DAVNamespace.DAV]),
'xmlns:i': 'urn:inverse:params:xml:ns:inverse-dav'
[`xmlns:${nsShort}`]: namespace
},
prop: formattedProperties
}
Expand All @@ -106,6 +116,43 @@ class WebDAV {
})
}

propfindWebdavRaw(resource, properties, headers = {}) {
const namespace = DAVNamespaceShorthandMap[DAVNamespace.DAV]
const formattedProperties = properties.map(prop => {
return { [`${namespace}:${prop}`]: '' }
})

let xmlBody = convert.js2xml(
{
propfind: {
_attributes: getDAVAttribute([DAVNamespace.DAV]),
prop: formattedProperties
}
},
{
compact: true,
spaces: 2,
elementNameFn: (name) => {
// add namespace to all keys without namespace
if (!/^.+:.+/.test(name)) {
return `${namespace}:${name}`;
}
return name;
},
}
)

return fetch(this.serverUrl + resource, {
headers: {
'Content-Type': 'application/xml; charset="utf-8"',
...headers,
...this.headers
},
method: 'PROPFIND',
body: xmlBody
})
}

propfindEvent(resource) {
return propfind({
url: this.serverUrl + resource,
Expand Down Expand Up @@ -258,15 +305,15 @@ class WebDAV {
})
}

proppatchCaldav(resource, properties) {
proppatchCaldav(resource, properties, headers = {}) {
const formattedProperties = Object.keys(properties).map(p => {
return { name: p, namespace: DAVNamespace.CALDAV, value: properties[p] }
})
return davRequest({
url: this.serverUrl + resource,
init: {
method: 'PROPPATCH',
headers: this.headers,
headers: { ...this.headers, ...headers },
namespace: DAVNamespaceShorthandMap[DAVNamespace.DAV],
body: {
propertyupdate: {
Expand All @@ -287,27 +334,31 @@ class WebDAV {
})
}

proppatchWebdav(resource, properties, depth = 0) {
proppatchWebdav(resource, properties, namespace = DAVNamespace.DAV, headers = {}) {
const nsShort = DAVNamespaceShorthandMap[namespace] || DAVInverseShort
const formattedProperties = Object.keys(properties).map(p => {
if (typeof properties[p] == 'object') {
return { [`i:${p}`]: properties[p].map(pp => {
if (Array.isArray(properties[p])) {
return { [`${nsShort}:${p}`]: properties[p].map(pp => {
const [ key ] = Object.keys(pp)
return { [`i:${key}`]: pp[key] || '' }
return { [`${nsShort}:${key}`]: pp[key] || '' }
})}
}
return { [`i:${p}`]: properties[p] || '' }
return { [`${nsShort}:${p}`]: properties[p] || '' }
})
if (typeof headers.depth == 'undefined') {
headers.depth = new String(0)
}
return davRequest({
url: this.serverUrl + resource,
init: {
method: 'PROPPATCH',
headers: { ...this.headers, depth: new String(depth) },
headers: { ...this.headers, ...headers },
namespace: DAVNamespaceShorthandMap[DAVNamespace.DAV],
body: {
propertyupdate: {
_attributes: {
...getDAVAttribute([DAVNamespace.DAV]),
'xmlns:i': 'urn:inverse:params:xml:ns:inverse-dav'
[`xmlns:${nsShort}`]: namespace
},
set: {
prop: formattedProperties
Expand Down
29 changes: 29 additions & 0 deletions Tests/lib/utilities.js
Expand Up @@ -114,6 +114,35 @@ class TestUtility {
return this.setupRights(resource, username, sogoRights)
}

_subscriptionOperation(resource, subscribers, operation) {
return davRequest({
url: `${this.webdav.serverUrl}${resource}`,
init: {
method: 'POST',
headers: {
'Content-Type': 'application/xml; charset="utf-8"',
...this.webdav.headers
},
body: {
[operation]: {
_attributes: {
xmlns: 'urn:inverse:params:xml:ns:inverse-dav',
users: subscribers.join(',')
}
}
}
}
})
}

subscribe(resource, subscribers) {
return this._subscriptionOperation(resource, subscribers, 'subscribe')
}

unsubscribe(resource, subscribers) {
return this._subscriptionOperation(resource, subscribers, 'unsubscribe')
}

versitDict(cal) {
const comp = ICAL.Component.fromString(cal)
let props = {}
Expand Down

0 comments on commit 5622eda

Please sign in to comment.