summaryrefslogtreecommitdiffstats
path: root/js/dav/test/unit/request/calendar_query_test.js
blob: a97e4d0be952a4222539a38a62e6ba2f3f1e28fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { assert } from 'chai';
import co from 'co';

import * as ns from '../../../lib/namespace';
import { Request, calendarQuery } from '../../../lib/request';
import * as transport from '../../../lib/transport';
import data from '../data';
import { nockWrapper } from '../nock_wrapper';

suite('request.calendarQuery', function() {
  let xhr;

  setup(function() {
    xhr = new transport.Basic({ username: 'admin', password: 'admin' });
  });

  teardown(() => nockWrapper.cleanAll());

  test('should set depth header', co.wrap(function *() {
    let mock = nockWrapper('http://127.0.0.1:1337')
      .matchHeader('Depth', 1)
      .intercept('/principals/admin/', 'REPORT')
      .reply(200);

    let req = calendarQuery({
      props: [ { name: 'calendar-data', namespace: ns.CALDAV } ],
      depth: 1
    });

    let send = xhr.send(req, 'http://127.0.0.1:1337/principals/admin/');
    yield mock.verify(send);
  }));

  test('should add specified props to report body', co.wrap(function *() {
    let mock = nockWrapper('http://127.0.0.1:1337')
      .matchRequestBody('/principals/admin/', 'REPORT', body => {
        return body.indexOf('<d:catdog />') !== -1;
      });

    let req = calendarQuery({
      props: [ { name: 'catdog', namespace: ns.DAV } ]
    });

    let send = xhr.send(req, 'http://127.0.0.1:1337/principals/admin/');
    yield mock.verify(send);
  }));

  test('should add specified filters to report body', co.wrap(function *() {
    let mock = nockWrapper('http://127.0.0.1:1337')
      .matchRequestBody('/principals/admin/', 'REPORT', body => {
        return body.indexOf('<c:comp-filter name="VCALENDAR"/>') !== -1;
      });

    let req = calendarQuery({
      filters: [{
        type: 'comp-filter',
        attrs: { name: 'VCALENDAR' },
      }]
    });

    let send = xhr.send(req, 'http://127.0.0.1:1337/principals/admin/');
    yield mock.verify(send);
  }));

  test('should add timezone to report body', co.wrap(function *() {
    let mock = nockWrapper('http://127.0.0.1:1337')
      .matchRequestBody('/principals/admin/', 'REPORT', body => {
        let data = '<c:timezone>BEGIN:VTIMEZONE\nEND:VTIMEZONE</c:timezone>';
        return body.indexOf(data) !== -1;
      });

    let req = calendarQuery({
      url: 'http://127.0.0.1:1337/principals/admin/',
      timezone: 'BEGIN:VTIMEZONE\nEND:VTIMEZONE'
    });

    let send = xhr.send(req, 'http://127.0.0.1:1337/principals/admin/');
    yield mock.verify(send);
  }));

  test('should resolve with appropriate data structure', co.wrap(function *() {
    nockWrapper('http://127.0.0.1:1337')
      .intercept('/', 'REPORT')
      .reply(200, data.calendarQuery);

    let req = calendarQuery({
      props: [
        { name: 'getetag', namespace: ns.DAV },
        { name: 'calendar-data', namespace: ns.CALDAV }
      ],
      filters: [ { type: 'comp', attrs: { name: 'VCALENDAR' } } ]
    });

    let calendars = yield xhr.send(req, 'http://127.0.0.1:1337/');
    assert.lengthOf(calendars, 2);
    calendars.forEach(calendar => {
      assert.typeOf(calendar.href, 'string');
      assert.operator(calendar.href.length, '>', 0);
      assert.include(calendar.href, '.ics');
      assert.typeOf(calendar.props, 'object');
      assert.typeOf(calendar.props.getetag, 'string');
      assert.operator(calendar.props.getetag.length, '>', 0);
      assert.typeOf(calendar.props.calendarData, 'string');
      assert.operator(calendar.props.calendarData.length, '>', 0);
    });
  }));
});