summaryrefslogtreecommitdiffstats
path: root/comm/calendar/test/unit/test_itip_message_sender.js
blob: 77a110a875f4507cdf8532722a21d4b996d2c4a0 (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
/* 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");
const { CalAttendee } = ChromeUtils.import("resource:///modules/CalAttendee.jsm");
var { CalEvent } = ChromeUtils.import("resource:///modules/CalEvent.jsm");
var { CalItipMessageSender } = ChromeUtils.import("resource:///modules/CalItipMessageSender.jsm");
var { MailServices } = ChromeUtils.import("resource:///modules/MailServices.jsm");

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

const identityEmail = "user@example.com";
const eventOrganizerEmail = "eventorganizer@example.com";

/**
 * Creates a calendar event mimicking an event to which we have received an
 * invitation.
 *
 * @param {string} organizerEmail - The email address of the event organizer.
 * @param {string} attendeeEmail - The email address of an attendee who has
 *   accepted the invitation.
 * @returns {calIItemBase} - The new calendar event.
 */
function createIncomingEvent(organizerEmail, attendeeEmail) {
  const organizerId = cal.email.prependMailTo(organizerEmail);
  const attendeeId = cal.email.prependMailTo(attendeeEmail);

  const icalString = 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=${organizerEmail}:${organizerId}
      ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;
        RSVP=TRUE;CN=other@example.com;:mailto:other@example.com
      ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;
        RSVP=TRUE;CN=${attendeeEmail};:${attendeeId}
      X-MOZ-RECEIVED-SEQUENCE:0
      X-MOZ-RECEIVED-DTSTAMP:20210501T000000Z
      X-MOZ-GENERATION:0
      END:VEVENT
    `;

  return new CalEvent(icalString);
}

let calendar;

/**
 * Ensure the calendar manager is available, initialize the calendar and
 * identity we use for testing.
 */
add_setup(async function () {
  do_get_profile();

  await new Promise(resolve => do_load_calmgr(resolve));
  calendar = CalendarTestUtils.createCalendar("Test", "memory");

  const identity = MailServices.accounts.createIdentity();
  identity.email = identityEmail;

  const account = MailServices.accounts.createAccount();
  account.incomingServer = MailServices.accounts.createIncomingServer(
    `${account.key}user`,
    "localhost",
    "none"
  );
  account.addIdentity(identity);

  registerCleanupFunction(() => {
    MailServices.accounts.removeIncomingServer(account.incomingServer, false);
    MailServices.accounts.removeAccount(account);
  });

  calendar.setProperty("imip.identity.key", identity.key);
  calendar.setProperty("organizerId", cal.email.prependMailTo(identityEmail));
});

add_task(async function testAddAttendeesToOwnEvent() {
  const icalString = 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
      X-MOZ-SEND-INVITATIONS:TRUE
      END:VEVENT
    `;

  const item = new CalEvent(icalString);
  const savedItem = await calendar.addItem(item);

  // Modify the event to include an attendee not in the original, as well as the
  // organizer. As of the writing of this test, this is the expected behavior
  // for adding an attendee to an event which previously had none.
  const newAttendeeEmail = "foo@example.com";
  const newAttendee = new CalAttendee();
  newAttendee.id = newAttendeeEmail;

  const organizer = new CalAttendee();
  organizer.isOrganizer = true;
  organizer.id = identityEmail;

  const organizerAsAttendee = new CalAttendee();
  organizerAsAttendee.id = identityEmail;

  const targetItem = savedItem.clone();
  targetItem.addAttendee(newAttendee);
  targetItem.addAttendee(organizer);
  targetItem.addAttendee(organizerAsAttendee);
  const modifiedItem = await calendar.modifyItem(targetItem, savedItem);

  // Test that a sender with an original item and for which the current user is
  // both an attendee and the organizer will generate a REQUEST, but not send a
  // message to the organizer.
  const sender = new CalItipMessageSender(savedItem, null);

  const result = sender.buildOutgoingMessages(Ci.calIOperationListener.MODIFY, modifiedItem);
  Assert.equal(result, 1, "return value should indicate there are pending messages");
  Assert.equal(sender.pendingMessageCount, 1, "there should be one pending message");

  const [msg] = sender.pendingMessages;
  Assert.equal(msg.method, "REQUEST", "message method should be 'REQUEST'");
  Assert.equal(msg.recipients.length, 1, "message should have one recipient");

  const [recipient] = msg.recipients;
  Assert.equal(
    recipient.id,
    cal.email.prependMailTo(newAttendeeEmail),
    "recipient should be the non-organizer attendee"
  );

  await calendar.deleteItem(modifiedItem);

  // Now also cancel the event. No mail should be sent to self.
  const targetItem2 = modifiedItem.clone();

  targetItem2.setProperty("STATUS", "CANCELLED");
  targetItem2.setProperty("SEQUENCE", "2");
  const modifiedItem2 = await calendar.addItem(targetItem2);
  const sender2 = new CalItipMessageSender(modifiedItem2, null);

  const result2 = sender2.buildOutgoingMessages(Ci.calIOperationListener.MODIFY, modifiedItem2);
  Assert.equal(result2, 1, "return value should indicate there are pending messages");
  Assert.equal(sender2.pendingMessageCount, 1, "there should be one pending message");

  const [msg2] = sender2.pendingMessages;
  Assert.equal(msg2.method, "CANCEL", "deletion message method should be 'CANCEL'");
  Assert.equal(msg2.recipients.length, 1, "deletion message should have one recipient");

  const [recipient2] = msg2.recipients;
  Assert.equal(
    recipient2.id,
    cal.email.prependMailTo(newAttendeeEmail),
    "for deletion message, recipient should be the non-organizer attendee"
  );
});

add_task(async function testAddAdditionalAttendee() {
  const icalString = 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=${identityEmail}:${cal.email.prependMailTo(identityEmail)}
      ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;
        RSVP=TRUE;CN=other@example.com;:mailto:other@example.com
      ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;
        RSVP=TRUE;CN=${identityEmail};:${cal.email.prependMailTo(identityEmail)}
      X-MOZ-SEND-INVITATIONS:TRUE
      END:VEVENT
    `;

  const item = new CalEvent(icalString);
  const savedItem = await calendar.addItem(item);

  // Modify the event to include an attendee not in the original.
  const newAttendeeEmail = "bar@example.com";
  const newAttendee = new CalAttendee();
  newAttendee.id = newAttendeeEmail;

  const organizer = new CalAttendee();
  organizer.isOrganizer = true;
  organizer.id = identityEmail;

  const organizerAsAttendee = new CalAttendee();
  organizerAsAttendee.id = identityEmail;

  const targetItem = savedItem.clone();
  targetItem.addAttendee(newAttendee);
  const modifiedItem = await calendar.modifyItem(targetItem, savedItem);

  // Test that adding an attendee won't cause messages to be sent to the
  // existing attendees.
  const sender = new CalItipMessageSender(savedItem, null);

  const result = sender.buildOutgoingMessages(Ci.calIOperationListener.MODIFY, modifiedItem);
  Assert.equal(result, 1, "return value should indicate there are pending messages");
  Assert.equal(sender.pendingMessageCount, 1, "there should be one pending message");

  const [msg] = sender.pendingMessages;
  Assert.equal(msg.method, "REQUEST", "message method should be 'REQUEST'");
  Assert.equal(msg.recipients.length, 1, "message should have one recipient");

  const [recipient] = msg.recipients;
  Assert.equal(
    recipient.id,
    cal.email.prependMailTo(newAttendeeEmail),
    "recipient should be the new attendee"
  );

  await calendar.deleteItem(modifiedItem);
});

add_task(async function testInvitationReceived() {
  const item = createIncomingEvent(eventOrganizerEmail, identityEmail);
  const savedItem = await calendar.addItem(item);

  const attendeeId = cal.email.prependMailTo(identityEmail);

  // Test that a sender with no original item and for which the current user is
  // an attendee but not the organizer (representing a new incoming invitation)
  // generates a single pending REPLY message on ADD.
  const currentUserAsAttendee = savedItem.getAttendeeById(attendeeId);
  const sender = new CalItipMessageSender(null, currentUserAsAttendee);

  const result = sender.buildOutgoingMessages(Ci.calIOperationListener.ADD, savedItem);
  Assert.equal(result, 1, "return value should indicate there are pending messages");
  Assert.equal(sender.pendingMessageCount, 1, "there should be one pending message");

  const [msg] = sender.pendingMessages;
  Assert.equal(msg.method, "REPLY", "message method should be 'REPLY'");
  Assert.equal(msg.recipients.length, 1, "message should have one recipient");

  const [recipient] = msg.recipients;
  Assert.equal(
    recipient.id,
    cal.email.prependMailTo(eventOrganizerEmail),
    "recipient should be the event organizer"
  );

  const attendeeList = msg.item.getAttendees();
  Assert.equal(attendeeList.length, 1, "there should be one attendee listed in the message");

  const [attendee] = attendeeList;
  Assert.equal(attendee.id, attendeeId, "listed attendee should be the current user");
  Assert.equal(
    attendee.participationStatus,
    "ACCEPTED",
    "current user's participation status should be 'ACCEPTED'"
  );

  await calendar.deleteItem(savedItem);
});

add_task(async function testParticipationStatusUpdated() {
  const item = createIncomingEvent(eventOrganizerEmail, identityEmail);
  const savedItem = await calendar.addItem(item);

  const attendeeId = cal.email.prependMailTo(identityEmail);

  // Modify the event to update the user's participation status.
  const targetItem = savedItem.clone();
  const currentUserAsAttendee = targetItem.getAttendeeById(attendeeId);
  currentUserAsAttendee.participationStatus = "TENTATIVE";
  const modifiedItem = await calendar.modifyItem(targetItem, savedItem);

  // Test that a sender for which the current user is an attendee but not the
  // organizer will generate a pending REPLY message on MODIFY.
  const sender = new CalItipMessageSender(savedItem, currentUserAsAttendee);
  const result = sender.buildOutgoingMessages(Ci.calIOperationListener.MODIFY, modifiedItem);

  Assert.equal(result, 1, "return value should indicate there are pending messages");
  Assert.equal(sender.pendingMessageCount, 1, "there should be one pending message");

  const [msg] = sender.pendingMessages;
  Assert.equal(msg.method, "REPLY", "message method should be 'REPLY'");
  Assert.equal(msg.recipients.length, 1, "message should have one recipient");

  const [recipient] = msg.recipients;
  Assert.equal(
    recipient.id,
    cal.email.prependMailTo(eventOrganizerEmail),
    "recipient should be the event organizer"
  );

  const attendeeList = msg.item.getAttendees();
  Assert.equal(attendeeList.length, 1, "there should be one attendee listed in the message");

  const [attendee] = attendeeList;
  Assert.equal(attendee.id, attendeeId, "listed attendee should be the current user");
  Assert.equal(
    attendee.participationStatus,
    "TENTATIVE",
    "current user's participation status should be 'TENTATIVE'"
  );

  await calendar.deleteItem(modifiedItem);
});

add_task(async function testEventDeleted() {
  const item = createIncomingEvent(eventOrganizerEmail, identityEmail);
  const savedItem = await calendar.addItem(item);

  const attendeeId = cal.email.prependMailTo(identityEmail);

  await calendar.deleteItem(savedItem);
  const currentUserAsAttendee = savedItem.getAttendeeById(attendeeId);

  // Test that a sender with no original item and for which the current user is
  // an attendee but not the organizer (representing the user deleting an event
  // from their calendar) generates a single REPLY message to the organizer on
  // DELETE.
  const sender = new CalItipMessageSender(null, currentUserAsAttendee);
  const result = sender.buildOutgoingMessages(Ci.calIOperationListener.DELETE, savedItem);

  Assert.equal(result, 1, "return value should indicate there are pending messages");
  Assert.equal(sender.pendingMessageCount, 1, "there should be one pending message");

  const [msg] = sender.pendingMessages;
  Assert.equal(msg.method, "REPLY", "message method should be 'REPLY'");
  Assert.equal(msg.recipients.length, 1, "message should have one recipient");

  const [recipient] = msg.recipients;
  Assert.equal(
    recipient.id,
    cal.email.prependMailTo(eventOrganizerEmail),
    "recipient should be the event organizer"
  );

  const attendeeList = msg.item.getAttendees();
  Assert.equal(attendeeList.length, 1, "there should be one attendee listed in the message");

  const [attendee] = attendeeList;
  Assert.equal(attendee.id, attendeeId, "listed attendee should be the current user");
  Assert.equal(
    attendee.participationStatus,
    "DECLINED",
    "current user's participation status should be 'DECLINED'"
  );
});