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 Nov 5, 2021
1 parent 262400a commit d43b28c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
38 changes: 31 additions & 7 deletions Tests/lib/WebDAV.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}

Expand Down
4 changes: 4 additions & 0 deletions Tests/spec/WebDAVSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
Expand Down
29 changes: 17 additions & 12 deletions Tests/spec/WebDavSyncSpec.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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:
Expand All @@ -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)
})
})

0 comments on commit d43b28c

Please sign in to comment.