Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / shared / test / calendar / deserialize.spec.ts
blob6b44d4375ca2ea4aeb551be1dcc6a7d979e19a61
1 import { ICAL_ATTENDEE_STATUS } from '@proton/shared/lib/calendar/constants';
2 import { getSelfAddressData } from '@proton/shared/lib/calendar/deserialize';
3 import { buildVcalOrganizer } from '@proton/shared/lib/calendar/vcalConverter';
4 import { ADDRESS_RECEIVE, ADDRESS_SEND, ADDRESS_STATUS, ADDRESS_TYPE } from '@proton/shared/lib/constants';
5 import { buildMailTo } from '@proton/shared/lib/helpers/email';
6 import type { Address } from '@proton/shared/lib/interfaces';
8 const buildVcalAttendee = ({
9     email,
10     cn,
11     partstat,
12 }: {
13     email: string;
14     cn?: string;
15     partstat?: ICAL_ATTENDEE_STATUS;
16 }) => {
17     return {
18         value: buildMailTo(email),
19         parameters: {
20             cn: cn || email,
21             partstat: partstat || ICAL_ATTENDEE_STATUS.NEEDS_ACTION,
22         },
23     };
26 describe('getSelfAddressData()', () => {
27     const originalAddress = {
28         DisplayName: 'I',
29         Email: 'me@proton.me',
30         ID: '1',
31         Status: ADDRESS_STATUS.STATUS_ENABLED,
32         Receive: ADDRESS_RECEIVE.RECEIVE_YES,
33         Send: ADDRESS_SEND.SEND_YES,
34         Type: ADDRESS_TYPE.TYPE_ORIGINAL,
35     } as Address;
36     const freePmMeAddress = {
37         DisplayName: "It's me",
38         Email: 'me@pm.me',
39         ID: '2',
40         Status: ADDRESS_STATUS.STATUS_ENABLED,
41         Receive: ADDRESS_RECEIVE.RECEIVE_YES,
42         Send: ADDRESS_SEND.SEND_NO,
43         Type: ADDRESS_TYPE.TYPE_PREMIUM,
44     } as Address;
45     const protonMailAddress = {
46         DisplayName: "It's still me",
47         Email: 'me@protonmail.com',
48         ID: '3',
49         Status: ADDRESS_STATUS.STATUS_ENABLED,
50         Receive: ADDRESS_RECEIVE.RECEIVE_YES,
51         Send: ADDRESS_SEND.SEND_YES,
52         Type: ADDRESS_TYPE.TYPE_ALIAS,
53     } as Address;
54     const protonMailChAddress = {
55         DisplayName: "It's me in CH",
56         Email: 'me@protonmail.ch',
57         ID: '4',
58         Status: ADDRESS_STATUS.STATUS_ENABLED,
59         Receive: ADDRESS_RECEIVE.RECEIVE_YES,
60         Send: ADDRESS_SEND.SEND_YES,
61         Type: ADDRESS_TYPE.TYPE_ALIAS,
62     } as Address;
63     const aliasDisabledAddress = {
64         DisplayName: 'Disabled me',
65         Email: 'disabled_alias@protonmail.com',
66         ID: '5',
67         Status: ADDRESS_STATUS.STATUS_DISABLED,
68         Receive: ADDRESS_RECEIVE.RECEIVE_NO,
69         Send: ADDRESS_SEND.SEND_NO,
70         Type: ADDRESS_TYPE.TYPE_ALIAS,
71     } as Address;
72     const customDomainAddress = {
73         DisplayName: 'Never gonna give you up',
74         Email: 'rick@does.not.give.you.up',
75         ID: '6',
76         Status: ADDRESS_STATUS.STATUS_ENABLED,
77         Receive: ADDRESS_RECEIVE.RECEIVE_YES,
78         Send: ADDRESS_SEND.SEND_YES,
79         Type: ADDRESS_TYPE.TYPE_CUSTOM_DOMAIN,
80     } as Address;
81     const externalAddress = {
82         DisplayName: 'Much crypto',
83         Email: 'someone_else@else.888',
84         ID: '7',
85         Status: ADDRESS_STATUS.STATUS_ENABLED,
86         Receive: ADDRESS_RECEIVE.RECEIVE_NO,
87         Send: ADDRESS_SEND.SEND_NO,
88         Type: ADDRESS_TYPE.TYPE_EXTERNAL,
89     } as Address;
91     const addresses = [
92         originalAddress,
93         freePmMeAddress,
94         protonMailAddress,
95         protonMailChAddress,
96         aliasDisabledAddress,
97         customDomainAddress,
98         externalAddress,
99     ];
101     it('identifies me as an organizer when organizing an event', () => {
102         const organizer = buildVcalOrganizer('me@proton.me');
104         const protonAttendee = buildVcalAttendee({
105             email: 'someone@proton.me',
106             cn: 'proton',
107             partstat: ICAL_ATTENDEE_STATUS.DECLINED,
108         });
109         const externalAttendee = buildVcalAttendee({
110             email: 'someone_else@else.888',
111             cn: 'BTC',
112             partstat: ICAL_ATTENDEE_STATUS.ACCEPTED,
113         });
114         const attendees = [protonAttendee, externalAttendee];
116         expect(getSelfAddressData({ organizer, attendees, addresses })).toEqual({
117             isOrganizer: true,
118             isAttendee: false,
119             selfAddress: originalAddress,
120         });
121     });
123     it('does not identify me as an organizer when organizing an event with an external address', () => {
124         const organizer = buildVcalOrganizer(externalAddress.Email);
126         const protonAttendee = buildVcalAttendee({
127             email: 'someone@proton.me',
128             cn: 'proton',
129             partstat: ICAL_ATTENDEE_STATUS.DECLINED,
130         });
131         const otherAttendee = buildVcalAttendee({
132             email: 'other@888',
133             cn: 'The others',
134             partstat: ICAL_ATTENDEE_STATUS.ACCEPTED,
135         });
136         const attendees = [protonAttendee, otherAttendee];
138         expect(getSelfAddressData({ organizer, attendees, addresses })).toEqual({
139             isOrganizer: false,
140             isAttendee: false,
141             selfAddress: undefined,
142             selfAttendee: undefined,
143             selfAttendeeIndex: undefined,
144         });
145     });
147     it('does not identify me as an attendee when attending an event with an external address', () => {
148         const organizer = buildVcalOrganizer('someone@proton.me');
150         const externalAddressAttendee = buildVcalAttendee({
151             email: externalAddress.Email,
152             cn: 'proton',
153             partstat: ICAL_ATTENDEE_STATUS.ACCEPTED,
154         });
155         const otherAttendee = buildVcalAttendee({
156             email: 'other@888',
157             cn: 'The others',
158             partstat: ICAL_ATTENDEE_STATUS.TENTATIVE,
159         });
160         const attendees = [externalAddressAttendee, otherAttendee];
162         expect(getSelfAddressData({ organizer, attendees, addresses })).toEqual({
163             isOrganizer: false,
164             isAttendee: false,
165             selfAddress: undefined,
166             selfAttendee: undefined,
167             selfAttendeeIndex: undefined,
168         });
169     });
171     describe('identifies me as an attendee when attending an event', () => {
172         const organizer = buildVcalOrganizer('someone@proton.me');
174         it('picks first active answered attendee', () => {
175             const originalAttendee = buildVcalAttendee({
176                 email: originalAddress.Email,
177                 partstat: ICAL_ATTENDEE_STATUS.NEEDS_ACTION,
178             });
179             const freePmMeAttendee = buildVcalAttendee({
180                 email: freePmMeAddress.Email,
181                 partstat: ICAL_ATTENDEE_STATUS.ACCEPTED,
182             });
183             const protonMailAttendee = buildVcalAttendee({
184                 email: protonMailAddress.Email,
185                 partstat: ICAL_ATTENDEE_STATUS.NEEDS_ACTION,
186             });
187             const protonMailChAttendee = buildVcalAttendee({
188                 email: protonMailChAddress.Email,
189                 partstat: ICAL_ATTENDEE_STATUS.TENTATIVE,
190             });
191             const aliasDisabledAttendee = buildVcalAttendee({
192                 email: aliasDisabledAddress.Email,
193                 partstat: ICAL_ATTENDEE_STATUS.DECLINED,
194             });
195             const customDomainAttendee = buildVcalAttendee({
196                 email: customDomainAddress.Email,
197                 partstat: ICAL_ATTENDEE_STATUS.ACCEPTED,
198             });
199             const externalAttendee = buildVcalAttendee({
200                 email: externalAddress.Email,
201                 partstat: ICAL_ATTENDEE_STATUS.ACCEPTED,
202             });
204             expect(
205                 getSelfAddressData({
206                     organizer,
207                     attendees: [
208                         externalAttendee,
209                         customDomainAttendee,
210                         freePmMeAttendee,
211                         aliasDisabledAttendee,
212                         originalAttendee,
213                         protonMailAttendee,
214                         protonMailChAttendee,
215                     ],
216                     addresses,
217                 })
218             ).toEqual({
219                 isOrganizer: false,
220                 isAttendee: true,
221                 selfAddress: protonMailChAddress,
222                 selfAttendee: protonMailChAttendee,
223                 selfAttendeeIndex: 6,
224             });
225         });
227         it('picks first active unanswered attendee if only inactive answered', () => {
228             const originalAttendee = buildVcalAttendee({
229                 email: originalAddress.Email,
230                 partstat: ICAL_ATTENDEE_STATUS.NEEDS_ACTION,
231             });
232             const freePmMeAttendee = buildVcalAttendee({
233                 email: freePmMeAddress.Email,
234                 partstat: ICAL_ATTENDEE_STATUS.ACCEPTED,
235             });
236             const protonMailAttendee = buildVcalAttendee({
237                 email: protonMailAddress.Email,
238                 partstat: ICAL_ATTENDEE_STATUS.NEEDS_ACTION,
239             });
240             const protonMailChAttendee = buildVcalAttendee({
241                 email: protonMailChAddress.Email,
242                 partstat: ICAL_ATTENDEE_STATUS.NEEDS_ACTION,
243             });
244             const aliasDisabledAttendee = buildVcalAttendee({
245                 email: aliasDisabledAddress.Email,
246                 partstat: ICAL_ATTENDEE_STATUS.ACCEPTED,
247             });
248             const customDomainAttendee = buildVcalAttendee({
249                 email: customDomainAddress.Email,
250                 partstat: ICAL_ATTENDEE_STATUS.NEEDS_ACTION,
251             });
252             const externalAttendee = buildVcalAttendee({
253                 email: externalAddress.Email,
254                 partstat: ICAL_ATTENDEE_STATUS.ACCEPTED,
255             });
257             expect(
258                 getSelfAddressData({
259                     organizer,
260                     attendees: [
261                         externalAttendee,
262                         customDomainAttendee,
263                         freePmMeAttendee,
264                         aliasDisabledAttendee,
265                         originalAttendee,
266                         protonMailAttendee,
267                         protonMailChAttendee,
268                     ],
269                     addresses,
270                 })
271             ).toEqual({
272                 isOrganizer: false,
273                 isAttendee: true,
274                 selfAddress: originalAddress,
275                 selfAttendee: originalAttendee,
276                 selfAttendeeIndex: 4,
277             });
278         });
280         it('picks first inactive answered attendee if all inactive', () => {
281             const freePmMeAttendee = buildVcalAttendee({
282                 email: freePmMeAddress.Email,
283                 partstat: ICAL_ATTENDEE_STATUS.NEEDS_ACTION,
284             });
285             const aliasDisabledAttendee = buildVcalAttendee({
286                 email: aliasDisabledAddress.Email,
287                 partstat: ICAL_ATTENDEE_STATUS.DECLINED,
288             });
289             const externalAttendee = buildVcalAttendee({
290                 email: externalAddress.Email,
291                 partstat: ICAL_ATTENDEE_STATUS.ACCEPTED,
292             });
294             expect(
295                 getSelfAddressData({
296                     organizer,
297                     attendees: [externalAttendee, freePmMeAttendee, aliasDisabledAttendee],
298                     addresses,
299                 })
300             ).toEqual({
301                 isOrganizer: false,
302                 isAttendee: true,
303                 selfAddress: aliasDisabledAddress,
304                 selfAttendee: aliasDisabledAttendee,
305                 selfAttendeeIndex: 2,
306             });
307         });
308     });
310     it('identifies me as an attendee when using a pm.me address', () => {
311         const organizer = buildVcalOrganizer('other@proton.me');
313         const anotherAttendee = buildVcalAttendee({ email: 'another@not_proton.me', cn: 'external' });
314         const pmMeAttendee = buildVcalAttendee({
315             email: 'me@pm.me',
316             cn: 'MEEE',
317             partstat: ICAL_ATTENDEE_STATUS.ACCEPTED,
318         });
319         const attendees = [anotherAttendee, pmMeAttendee];
321         const originalAddress = {
322             DisplayName: 'I',
323             Email: 'me@proton.me',
324             ID: '1',
325             Receive: ADDRESS_RECEIVE.RECEIVE_YES,
326             Send: ADDRESS_SEND.SEND_YES,
327             Type: ADDRESS_TYPE.TYPE_ORIGINAL,
328         } as Address;
329         const pmMeAddress = {
330             DisplayName: "It's me",
331             Email: 'me@pm.me',
332             ID: '2',
333             Receive: ADDRESS_RECEIVE.RECEIVE_YES,
334             Send: ADDRESS_SEND.SEND_NO,
335             Type: ADDRESS_TYPE.TYPE_PREMIUM,
336         } as Address;
337         const addresses = [originalAddress, pmMeAddress];
339         expect(getSelfAddressData({ organizer, attendees, addresses })).toEqual({
340             isOrganizer: false,
341             isAttendee: true,
342             selfAttendee: pmMeAttendee,
343             selfAddress: pmMeAddress,
344             selfAttendeeIndex: 1,
345         });
346     });