summaryrefslogtreecommitdiffstats
path: root/comm/calendar/test/unit/test_itip_utils.js
blob: 5c4678ab1d4dbfa68fb32a98271b2af3be8dbe9d (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");
var { MailServices } = ChromeUtils.import("resource:///modules/MailServices.jsm");
var { XPCOMUtils } = ChromeUtils.importESModule("resource://gre/modules/XPCOMUtils.sys.mjs");

var { CalendarTestUtils } = ChromeUtils.import(
  "resource://testing-common/calendar/CalendarTestUtils.jsm"
);

XPCOMUtils.defineLazyModuleGetters(this, {
  CalAttendee: "resource:///modules/CalAttendee.jsm",
  CalEvent: "resource:///modules/CalEvent.jsm",
  CalItipEmailTransport: "resource:///modules/CalItipEmailTransport.jsm",
});

// tests for calItipUtils.jsm

do_get_profile();

/*
 * Helper function to get an ics for testing sequence and stamp comparison
 *
 * @param {String} aAttendee - A serialized ATTENDEE property
 * @param {String} aSequence - A serialized SEQUENCE property
 * @param {String} aDtStamp - A serialized DTSTAMP property
 * @param {String} aXMozReceivedSequence - A serialized X-MOZ-RECEIVED-SEQUENCE property
 * @param {String} aXMozReceivedDtStamp - A serialized X-MOZ-RECEIVED-STAMP property
 */
function getSeqStampTestIcs(aProperties) {
  // we make sure to have a dtstamp property to get a valid ics
  let dtStamp = "20150909T181048Z";
  let additionalProperties = "";
  aProperties.forEach(aProp => {
    if (aProp.startsWith("DTSTAMP:")) {
      dtStamp = aProp;
    } else {
      additionalProperties += "\r\n" + aProp;
    }
  });

  return [
    "BEGIN:VCALENDAR",
    "PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN",
    "VERSION:2.0",
    "METHOD:REQUEST",
    "BEGIN:VTIMEZONE",
    "TZID:Europe/Berlin",
    "BEGIN:DAYLIGHT",
    "TZOFFSETFROM:+0100",
    "TZOFFSETTO:+0200",
    "TZNAME:CEST",
    "DTSTART:19700329T020000",
    "RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU",
    "END:DAYLIGHT",
    "BEGIN:STANDARD",
    "TZOFFSETFROM:+0200",
    "TZOFFSETTO:+0100",
    "TZNAME:CET",
    "DTSTART:19701025T030000",
    "RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU",
    "END:STANDARD",
    "END:VTIMEZONE",
    "BEGIN:VEVENT",
    "CREATED:20150909T180909Z",
    "LAST-MODIFIED:20150909T181048Z",
    dtStamp,
    "UID:cb189fdc-ed47-4db6-a8d7-31a08802249d",
    "SUMMARY:Test Event",
    "ORGANIZER;RSVP=TRUE;CN=Organizer;PARTSTAT=ACCEPTED;ROLE=CHAIR:mailto:organizer@example.net",
    "ATTENDEE;RSVP=TRUE;CN=Attendee;PARTSTAT=NEEDS-ACTION;ROLE=REQ-PARTICIPANT:mailto:attende" +
      "e@example.net" +
      additionalProperties,
    "DTSTART;TZID=Europe/Berlin:20150909T210000",
    "DTEND;TZID=Europe/Berlin:20150909T220000",
    "TRANSP:OPAQUE",
    "LOCATION:Room 1",
    "DESCRIPTION:Let us get together",
    "URL:http://www.example.com",
    "ATTACH:http://www.example.com",
    "END:VEVENT",
    "END:VCALENDAR",
  ].join("\r\n");
}

function getSeqStampTestItems(aTest) {
  let items = [];
  for (let input of aTest.input) {
    if (input.item) {
      // in this case, we need to return an event
      let attendee = "";
      if ("attendee" in input.item && input.item.attendee != {}) {
        let att = new CalAttendee();
        att.id = input.item.attendee.id || "mailto:otherattendee@example.net";
        if ("receivedSeq" in input.item.attendee && input.item.attendee.receivedSeq.length) {
          att.setProperty("RECEIVED-SEQUENCE", input.item.attendee.receivedSeq);
        }
        if ("receivedStamp" in input.item.attendee && input.item.attendee.receivedStamp.length) {
          att.setProperty("RECEIVED-DTSTAMP", input.item.attendee.receivedStamp);
        }
      }
      let sequence = "";
      if ("sequence" in input.item && input.item.sequence.length) {
        sequence = "SEQUENCE:" + input.item.sequence;
      }
      let dtStamp = "DTSTAMP:20150909T181048Z";
      if ("dtStamp" in input.item && input.item.dtStamp) {
        dtStamp = "DTSTAMP:" + input.item.dtStamp;
      }
      let xMozReceivedSeq = "";
      if ("xMozReceivedSeq" in input.item && input.item.xMozReceivedSeq.length) {
        xMozReceivedSeq = "X-MOZ-RECEIVED-SEQUENCE:" + input.item.xMozReceivedSeq;
      }
      let xMozReceivedStamp = "";
      if ("xMozReceivedStamp" in input.item && input.item.xMozReceivedStamp.length) {
        xMozReceivedStamp = "X-MOZ-RECEIVED-DTSTAMP:" + input.item.xMozReceivedStamp;
      }
      let xMsAptSeq = "";
      if ("xMsAptSeq" in input.item && input.item.xMsAptSeq.length) {
        xMsAptSeq = "X-MICROSOFT-CDO-APPT-SEQUENCE:" + input.item.xMsAptSeq;
      }
      let testItem = new CalEvent();
      testItem.icalString = getSeqStampTestIcs([
        attendee,
        sequence,
        dtStamp,
        xMozReceivedSeq,
        xMozReceivedStamp,
        xMsAptSeq,
      ]);
      items.push(testItem);
    } else {
      // in this case, we need to return an attendee
      let att = new CalAttendee();
      att.id = input.attendee.id || "mailto:otherattendee@example.net";
      if (input.attendee.receivedSeq && input.attendee.receivedSeq.length) {
        att.setProperty("RECEIVED-SEQUENCE", input.attendee.receivedSeq);
      }
      if (input.attendee.receivedStamp && input.attendee.receivedStamp.length) {
        att.setProperty("RECEIVED-DTSTAMP", input.attendee.receivedStamp);
      }
      items.push(att);
    }
  }
  return items;
}

add_task(function test_getMessageSender() {
  let data = [
    {
      input: null,
      expected: null,
    },
    {
      input: {},
      expected: null,
    },
    {
      input: { author: "Sender 1 <sender1@example.net>" },
      expected: "sender1@example.net",
    },
  ];
  for (let i = 1; i <= data.length; i++) {
    let test = data[i - 1];
    equal(cal.itip.getMessageSender(test.input), test.expected, "(test #" + i + ")");
  }
});

add_task(function test_getSequence() {
  // assigning an empty string results in not having the property in the ics here
  let data = [
    {
      input: [{ item: { sequence: "", xMozReceivedSeq: "" } }],
      expected: 0,
    },
    {
      input: [{ item: { sequence: "0", xMozReceivedSeq: "" } }],
      expected: 0,
    },
    {
      input: [{ item: { sequence: "", xMozReceivedSeq: "0" } }],
      expected: 0,
    },
    {
      input: [{ item: { sequence: "1", xMozReceivedSeq: "" } }],
      expected: 1,
    },
    {
      input: [{ item: { sequence: "", xMozReceivedSeq: "1" } }],
      expected: 1,
    },
    {
      input: [{ attendee: { receivedSeq: "" } }],
      expected: 0,
    },
    {
      input: [{ attendee: { receivedSeq: "0" } }],
      expected: 0,
    },
    {
      input: [{ attendee: { receivedSeq: "1" } }],
      expected: 1,
    },
  ];
  for (let i = 1; i <= data.length; i++) {
    let test = data[i - 1];
    let testItems = getSeqStampTestItems(test);
    equal(cal.itip.getSequence(testItems[0], testItems[1]), test.expected, "(test #" + i + ")");
  }
});

add_task(function test_getStamp() {
  // assigning an empty string results in not having the property in the ics here. However, there
  // must be always an dtStamp for item - if it's missing it will be set by the test code to make
  // sure we get a valid ics
  let data = [
    {
      // !dtStamp && !xMozReceivedStamp => test default value
      input: [{ item: { dtStamp: "", xMozReceivedStamp: "" } }],
      expected: "20150909T181048Z",
    },
    {
      // dtStamp && !xMozReceivedStamp => dtStamp
      input: [{ item: { dtStamp: "20150910T181048Z", xMozReceivedStamp: "" } }],
      expected: "20150910T181048Z",
    },
    {
      // dtStamp && xMozReceivedStamp => xMozReceivedStamp
      input: [{ item: { dtStamp: "20150909T181048Z", xMozReceivedStamp: "20150910T181048Z" } }],
      expected: "20150910T181048Z",
    },
    {
      input: [{ attendee: { receivedStamp: "" } }],
      expected: null,
    },
    {
      input: [{ attendee: { receivedStamp: "20150910T181048Z" } }],
      expected: "20150910T181048Z",
    },
  ];
  for (let i = 1; i <= data.length; i++) {
    let test = data[i - 1];
    let result = cal.itip.getStamp(getSeqStampTestItems(test)[0]);
    if (result) {
      result = result.icalString;
    }
    equal(result, test.expected, "(test #" + i + ")");
  }
});

add_task(function test_compareSequence() {
  // it is sufficient to test here with sequence for items - full test coverage for
  // x-moz-received-sequence is already provided by test_compareSequence
  let data = [
    {
      // item1.seq == item2.seq
      input: [{ item: { sequence: "2" } }, { item: { sequence: "2" } }],
      expected: 0,
    },
    {
      // item1.seq > item2.seq
      input: [{ item: { sequence: "3" } }, { item: { sequence: "2" } }],
      expected: 1,
    },
    {
      // item1.seq < item2.seq
      input: [{ item: { sequence: "2" } }, { item: { sequence: "3" } }],
      expected: -1,
    },
    {
      // attendee1.seq == attendee2.seq
      input: [{ attendee: { receivedSeq: "2" } }, { attendee: { receivedSeq: "2" } }],
      expected: 0,
    },
    {
      // attendee1.seq > attendee2.seq
      input: [{ attendee: { receivedSeq: "3" } }, { attendee: { receivedSeq: "2" } }],
      expected: 1,
    },
    {
      // attendee1.seq < attendee2.seq
      input: [{ attendee: { receivedSeq: "2" } }, { attendee: { receivedSeq: "3" } }],
      expected: -1,
    },
    {
      // item.seq == attendee.seq
      input: [{ item: { sequence: "2" } }, { attendee: { receivedSeq: "2" } }],
      expected: 0,
    },
    {
      // item.seq > attendee.seq
      input: [{ item: { sequence: "3" } }, { attendee: { receivedSeq: "2" } }],
      expected: 1,
    },
    {
      // item.seq < attendee.seq
      input: [{ item: { sequence: "2" } }, { attendee: { receivedSeq: "3" } }],
      expected: -1,
    },
  ];
  for (let i = 1; i <= data.length; i++) {
    let test = data[i - 1];
    let testItems = getSeqStampTestItems(test);
    equal(cal.itip.compareSequence(testItems[0], testItems[1]), test.expected, "(test #" + i + ")");
  }
});

add_task(function test_compareStamp() {
  // it is sufficient to test here with dtstamp for items - full test coverage for
  // x-moz-received-stamp is already provided by test_compareStamp
  let data = [
    {
      // item1.stamp == item2.stamp
      input: [{ item: { dtStamp: "20150910T181048Z" } }, { item: { dtStamp: "20150910T181048Z" } }],
      expected: 0,
    },
    {
      // item1.stamp > item2.stamp
      input: [{ item: { dtStamp: "20150911T181048Z" } }, { item: { dtStamp: "20150910T181048Z" } }],
      expected: 1,
    },
    {
      // item1.stamp < item2.stamp
      input: [{ item: { dtStamp: "20150910T181048Z" } }, { item: { dtStamp: "20150911T181048Z" } }],
      expected: -1,
    },
    {
      // attendee1.stamp == attendee2.stamp
      input: [
        { attendee: { receivedStamp: "20150910T181048Z" } },
        { attendee: { receivedStamp: "20150910T181048Z" } },
      ],
      expected: 0,
    },
    {
      // attendee1.stamp > attendee2.stamp
      input: [
        { attendee: { receivedStamp: "20150911T181048Z" } },
        { attendee: { receivedStamp: "20150910T181048Z" } },
      ],
      expected: 1,
    },
    {
      // attendee1.stamp < attendee2.stamp
      input: [
        { attendee: { receivedStamp: "20150910T181048Z" } },
        { attendee: { receivedStamp: "20150911T181048Z" } },
      ],
      expected: -1,
    },
    {
      // item.stamp == attendee.stamp
      input: [
        { item: { dtStamp: "20150910T181048Z" } },
        { attendee: { receivedStamp: "20150910T181048Z" } },
      ],
      expected: 0,
    },
    {
      // item.stamp > attendee.stamp
      input: [
        { item: { dtStamp: "20150911T181048Z" } },
        { attendee: { receivedStamp: "20150910T181048Z" } },
      ],
      expected: 1,
    },
    {
      // item.stamp < attendee.stamp
      input: [
        { item: { dtStamp: "20150910T181048Z" } },
        { attendee: { receivedStamp: "20150911T181048Z" } },
      ],
      expected: -1,
    },
  ];
  for (let i = 1; i <= data.length; i++) {
    let test = data[i - 1];
    let testItems = getSeqStampTestItems(test);
    equal(cal.itip.compareStamp(testItems[0], testItems[1]), test.expected, "(test #" + i + ")");
  }
});

add_task(function test_compare() {
  // it is sufficient to test here with items only - full test coverage for attendees or
  // item/attendee is already provided by test_compareSequence and test_compareStamp
  let data = [
    {
      // item1.seq == item2.seq && item1.stamp == item2.stamp
      input: [
        { item: { sequence: "2", dtStamp: "20150910T181048Z" } },
        { item: { sequence: "2", dtStamp: "20150910T181048Z" } },
      ],
      expected: 0,
    },
    {
      // item1.seq == item2.seq && item1.stamp > item2.stamp
      input: [
        { item: { sequence: "2", dtStamp: "20150911T181048Z" } },
        { item: { sequence: "2", dtStamp: "20150910T181048Z" } },
      ],
      expected: 1,
    },
    {
      // item1.seq == item2.seq && item1.stamp < item2.stamp
      input: [
        { item: { sequence: "2", dtStamp: "20150910T181048Z" } },
        { item: { sequence: "2", dtStamp: "20150911T181048Z" } },
      ],
      expected: -1,
    },
    {
      // item1.seq > item2.seq && item1.stamp == item2.stamp
      input: [
        { item: { sequence: "3", dtStamp: "20150910T181048Z" } },
        { item: { sequence: "2", dtStamp: "20150910T181048Z" } },
      ],
      expected: 1,
    },
    {
      // item1.seq > item2.seq && item1.stamp > item2.stamp
      input: [
        { item: { sequence: "3", dtStamp: "20150911T181048Z" } },
        { item: { sequence: "2", dtStamp: "20150910T181048Z" } },
      ],
      expected: 1,
    },
    {
      // item1.seq > item2.seq && item1.stamp < item2.stamp
      input: [
        { item: { sequence: "3", dtStamp: "20150910T181048Z" } },
        { item: { sequence: "2", dtStamp: "20150911T181048Z" } },
      ],
      expected: 1,
    },
    {
      // item1.seq < item2.seq && item1.stamp == item2.stamp
      input: [
        { item: { sequence: "2", dtStamp: "20150910T181048Z" } },
        { item: { sequence: "3", dtStamp: "20150910T181048Z" } },
      ],
      expected: -1,
    },
    {
      // item1.seq < item2.seq && item1.stamp > item2.stamp
      input: [
        { item: { sequence: "2", dtStamp: "20150911T181048Z" } },
        { item: { sequence: "3", dtStamp: "20150910T181048Z" } },
      ],
      expected: -1,
    },
    {
      // item1.seq < item2.seq && item1.stamp < item2.stamp
      input: [
        { item: { sequence: "2", dtStamp: "20150910T181048Z" } },
        { item: { sequence: "3", dtStamp: "20150911T181048Z" } },
      ],
      expected: -1,
    },
  ];
  for (let i = 1; i <= data.length; i++) {
    let test = data[i - 1];
    let testItems = getSeqStampTestItems(test);
    equal(cal.itip.compare(testItems[0], testItems[1]), test.expected, "(test #" + i + ")");
  }
});

add_task(function test_getAttendeesBySender() {
  let data = [
    {
      input: {
        attendees: [
          { id: "mailto:user1@example.net", sentBy: null },
          { id: "mailto:user2@example.net", sentBy: null },
        ],
        sender: "user1@example.net",
      },
      expected: ["mailto:user1@example.net"],
    },
    {
      input: {
        attendees: [
          { id: "mailto:user1@example.net", sentBy: null },
          { id: "mailto:user2@example.net", sentBy: null },
        ],
        sender: "user3@example.net",
      },
      expected: [],
    },
    {
      input: {
        attendees: [
          { id: "mailto:user1@example.net", sentBy: "mailto:user3@example.net" },
          { id: "mailto:user2@example.net", sentBy: null },
        ],
        sender: "user3@example.net",
      },
      expected: ["mailto:user1@example.net"],
    },
    {
      input: {
        attendees: [
          { id: "mailto:user1@example.net", sentBy: null },
          { id: "mailto:user2@example.net", sentBy: "mailto:user1@example.net" },
        ],
        sender: "user1@example.net",
      },
      expected: ["mailto:user1@example.net", "mailto:user2@example.net"],
    },
    {
      input: { attendees: [], sender: "user1@example.net" },
      expected: [],
    },
    {
      input: {
        attendees: [
          { id: "mailto:user1@example.net", sentBy: null },
          { id: "mailto:user2@example.net", sentBy: null },
        ],
        sender: "",
      },
      expected: [],
    },
    {
      input: {
        attendees: [
          { id: "mailto:user1@example.net", sentBy: null },
          { id: "mailto:user2@example.net", sentBy: null },
        ],
        sender: null,
      },
      expected: [],
    },
  ];

  for (let i = 1; i <= data.length; i++) {
    let test = data[i - 1];
    let attendees = [];
    for (let att of test.input.attendees) {
      let attendee = new CalAttendee();
      attendee.id = att.id;
      if (att.sentBy) {
        attendee.setProperty("SENT-BY", att.sentBy);
      }
      attendees.push(attendee);
    }
    let detected = [];
    cal.itip.getAttendeesBySender(attendees, test.input.sender).forEach(att => {
      detected.push(att.id);
    });
    ok(
      detected.every(aId => test.expected.includes(aId)),
      "(test #" + i + " ok1)"
    );
    ok(
      test.expected.every(aId => detected.includes(aId)),
      "(test #" + i + " ok2)"
    );
  }
});

add_task(function test_resolveDelegation() {
  let data = [
    {
      input: {
        attendee:
          'ATTENDEE;DELEGATED-FROM="mailto:attendee2@example.net";CN="Attendee 1":mailto:at' +
          "tendee1@example.net",
        attendees: [
          'ATTENDEE;DELEGATED-FROM="mailto:attendee2@example.net";CN="Attendee 1":mailto:at' +
            "tendee1@example.net",
          'ATTENDEE;DELEGATED-TO="mailto:attendee1@example.net";CN="Attendee 2":mailto:atte' +
            "ndee2@example.net",
        ],
      },
      expected: {
        delegatees: "",
        delegators: "Attendee 2 <attendee2@example.net>",
      },
    },
    {
      input: {
        attendee:
          'ATTENDEE;DELEGATED-FROM="mailto:attendee2@example.net":mailto:attendee1@example.net',
        attendees: [
          'ATTENDEE;DELEGATED-FROM="mailto:attendee2@example.net":mailto:attendee1@example.net',
          'ATTENDEE;DELEGATED-TO="mailto:attendee1@example.net":mailto:attendee2@example.net',
        ],
      },
      expected: {
        delegatees: "",
        delegators: "attendee2@example.net",
      },
    },
    {
      input: {
        attendee:
          'ATTENDEE;DELEGATED-TO="mailto:attendee2@example.net";CN="Attendee 1":mailto:atte' +
          "ndee1@example.net",
        attendees: [
          'ATTENDEE;DELEGATED-TO="mailto:attendee2@example.net";CN="Attendee 1":mailto:atte' +
            "ndee1@example.net",
          'ATTENDEE;DELEGATED-FROM="mailto:attendee1@example.net";CN="Attendee 2":mailto:at' +
            "tendee2@example.net",
        ],
      },
      expected: {
        delegatees: "Attendee 2 <attendee2@example.net>",
        delegators: "",
      },
    },
    {
      input: {
        attendee:
          'ATTENDEE;DELEGATED-TO="mailto:attendee2@example.net":mailto:attendee1@example.net',
        attendees: [
          'ATTENDEE;DELEGATED-TO="mailto:attendee2@example.net":mailto:attendee1@example.net',
          'ATTENDEE;DELEGATED-FROM="mailto:attendee1@example.net":mailto:attendee2@example.net',
        ],
      },
      expected: {
        delegatees: "attendee2@example.net",
        delegators: "",
      },
    },
    {
      input: {
        attendee: "ATTENDEE:mailto:attendee1@example.net",
        attendees: [
          "ATTENDEE:mailto:attendee1@example.net",
          "ATTENDEE:mailto:attendee2@example.net",
        ],
      },
      expected: {
        delegatees: "",
        delegators: "",
      },
    },
    {
      input: {
        attendee:
          'ATTENDEE;DELEGATED-FROM="mailto:attendee2@example.net";DELEGATED-TO="mailto:atte' +
          'ndee3@example.net":mailto:attendee1@example.net',
        attendees: [
          'ATTENDEE;DELEGATED-FROM="mailto:attendee2@example.net";DELEGATED-TO="mailto:atte' +
            'ndee3@example.net":mailto:attendee1@example.net',
          'ATTENDEE;DELEGATED-TO="mailto:attendee1@example.net":mailto:attendee2@example.net',
          'ATTENDEE;DELEGATED-FROM="mailto:attendee1@example.net":mailto:attendee3@example.net',
        ],
      },
      expected: {
        delegatees: "attendee3@example.net",
        delegators: "attendee2@example.net",
      },
    },
  ];
  let i = 0;
  for (let test of data) {
    i++;
    let attendees = [];
    for (let att of test.input.attendees) {
      let attendee = new CalAttendee();
      attendee.icalString = att;
      attendees.push(attendee);
    }
    let attendee = new CalAttendee();
    attendee.icalString = test.input.attendee;
    let result = cal.itip.resolveDelegation(attendee, attendees);
    equal(result.delegatees, test.expected.delegatees, "(test #" + i + " - delegatees)");
    equal(result.delegators, test.expected.delegators, "(test #" + i + " - delegators)");
  }
});

/**
 * Tests the various ways to use the getInvitedAttendee function.
 */
add_task(async function test_getInvitedAttendee() {
  class MockCalendar {
    supportsScheduling = true;

    constructor(invitedAttendee) {
      this.invitedAttendee = invitedAttendee;
    }

    getSchedulingSupport() {
      return this;
    }

    getInvitedAttendee() {
      return this.invitedAttendee;
    }
  }

  let invitedAttendee = new CalAttendee();
  invitedAttendee.id = "mailto:invited@example.com";

  let calendar = new MockCalendar(invitedAttendee);
  let event = new CalEvent(CalendarTestUtils.dedent`
        BEGIN:VEVENT
        CREATED:20210105T000000Z
        DTSTAMP:20210501T000000Z
        UID:c1a6cfe7-7fbb-4bfb-a00d-861e07c649a5
        SUMMARY:Test Invitation
        DTSTART:20210105T000000Z
        DTEND:20210105T100000Z
        STATUS:CONFIRMED
        SUMMARY:Test Event
        ORGANIZER;CN=events@example.com:mailto:events@example.com
        ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;
          RSVP=TRUE;CN=invited@example.com;:mailto:invited@example.com
        END:VEVENT
      `);

  // No calendar configured or provided.
  Assert.ok(
    !cal.itip.getInvitedAttendee(event),
    "returns falsy when item has no calendar and none provided"
  );

  // No calendar configured but one provided.
  Assert.ok(
    cal.itip.getInvitedAttendee(event, calendar) == invitedAttendee,
    "returns the result from the provided calendar when item has none configured"
  );

  // Calendar configured, none provided.
  event.calendar = calendar;
  Assert.ok(
    cal.itip.getInvitedAttendee(event) == invitedAttendee,
    "returns the result of the item's calendar when calendar not provided"
  );

  // Calendar configured, one provided.
  Assert.ok(
    !cal.itip.getInvitedAttendee(event, new MockCalendar()),
    "returns the result of the provided calendar even if item's calendar is configured"
  );

  // Calendar does not implement nsISchedulingSupport.
  calendar.supportsScheduling = false;
  Assert.ok(
    !cal.itip.getInvitedAttendee(event),
    "returns falsy if the calendar does not indicate nsISchedulingSupport"
  );

  // X-MOZ-INVITED-ATTENDEE set on event.
  event.setProperty("X-MOZ-INVITED-ATTENDEE", "mailto:invited@example.com");

  let attendee = cal.itip.getInvitedAttendee(event);
  Assert.ok(
    attendee && attendee.id == "mailto:invited@example.com",
    "returns the attendee matching X-MOZ-INVITED-ATTENDEE if set"
  );

  // X-MOZ-INVITED-ATTENDEE set to non-existent attendee
  event.setProperty("X-MOZ-INVITED-ATTENDEE", "mailto:nobody@example.com");
  Assert.ok(
    !cal.itip.getInvitedAttendee(event),
    "returns falsy for non-existent X-MOZ-INVITED-ATTENDEE"
  );
});

/**
 * Tests the getImipTransport function returns the correct calIItipTransport.
 */
add_task(function test_getImipTransport() {
  let event = new CalEvent(CalendarTestUtils.dedent`
        BEGIN:VEVENT
        CREATED:20210105T000000Z
        DTSTAMP:20210501T000000Z
        UID:c1a6cfe7-7fbb-4bfb-a00d-861e07c649a5
        SUMMARY:Test Invitation
        DTSTART:20210105T000000Z
        DTEND:20210105T100000Z
        STATUS:CONFIRMED
        SUMMARY:Test Event
        END:VEVENT
      `);

  // Without X-MOZ-INVITED-ATTENDEE property.
  let account1 = MailServices.accounts.createAccount();
  let identity1 = MailServices.accounts.createIdentity();
  identity1.email = "id1@example.com";
  account1.addIdentity(identity1);

  let calendarTransport = new CalItipEmailTransport(account1, identity1);
  event.calendar = {
    getProperty(key) {
      switch (key) {
        case "itip.transport":
          return calendarTransport;
        case "imip.idenity":
          return identity1;
        default:
          return null;
      }
    },
  };

  Assert.ok(
    cal.itip.getImipTransport(event) == calendarTransport,
    "returns the calendar's transport when no X-MOZ-INVITED-ATTENDEE property"
  );

  // With X-MOZ-INVITED-ATTENDEE property.
  let account2 = MailServices.accounts.createAccount();
  let identity2 = MailServices.accounts.createIdentity();
  identity2.email = "id2@example.com";
  account2.addIdentity(identity2);
  account2.incomingServer = MailServices.accounts.createIncomingServer(
    "id2",
    "example.com",
    "imap"
  );

  event.setProperty("X-MOZ-INVITED-ATTENDEE", "mailto:id2@example.com");

  let customTransport = cal.itip.getImipTransport(event);
  Assert.ok(customTransport);

  Assert.ok(
    customTransport.mDefaultAccount == account2,
    "returns a transport using an account for the X-MOZ-INVITED-ATTENDEE identity when set"
  );

  Assert.ok(
    customTransport.mDefaultIdentity == identity2,
    "returns a transport using the identity of the X-MOZ-INVITED-ATTENDEE property when set"
  );
});