summaryrefslogtreecommitdiff
path: root/gnus-icalendar-request-tests.el
blob: b9a295403692fd36c2e139452db9b9a1bc98712f (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
;;; gnus-icalendar-request-tests.el --- tests                -*- lexical-binding: t; -*-

;; Copyright (C) 2024  Ferdinand Pieper

;; Author: Ferdinand Pieper <mail@pie.tf>
;; Keywords:

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;

;;; Code:

(require 'ert)
(require 'gnus-icalendar-request)

(ert-deftest gnus-icalendar--build-vcalendar-from-vevent ()
  ""
  (let ((tz (getenv "TZ"))
        (event "\
BEGIN:VEVENT
DTSTAMP:20240915T120000Z
DTSTART:20240917T080000Z
DTEND:20240917T100000Z
SUMMARY:Party
DESCRIPTION:Lots of reasons to celebrate!
ATTENDEE;PARTSTAT=NEEDS-ACTION;ROLE=REQ-PARTICIPANT;RSVP=TRUE:mailto:required@company.invalid
ATTENDEE;PARTSTAT=NEEDS-ACTION;ROLE=OPT-PARTICIPANT;RSVP=TRUE:mailto:optional@company.invalid
LOCATION:Party room
ORGANIZER:mailto:organizer@company.invalid
UID:ac44f43e-f5cd-4b0a-878e-add01aeb12dd
SEQUENCE:0
END:VEVENT"))
    (setenv "TZ" "CET-1CEST,M3.5.0/2,M10.5.0/3")
    (let ((vcalendar (gnus-icalendar--build-vcalendar-from-vevent event)))
      ;; Ensure there is exactly one of VERSION and PRODID
      (should (string-match "^VERSION:2.0\\(\n\\|.\\)*END:VCALENDAR" vcalendar))
      (should-not (string-match "^VERSION:" (match-string 1 vcalendar)))
      (should (string-match "^PRODID:\\(\n\\|.\\)*END:VCALENDAR" vcalendar))
      (should-not (string-match "^PRODID:" (match-string 1 vcalendar)))
      ;; METHOD should be REQUEST
      (should (string-match "^METHOD:REQUEST$" vcalendar))
      ;; Ensure the vevent remains intact
      (should (string-match "^\\(BEGIN:VEVENT\\(\n\\|.\\)*\nEND:VEVENT\\)" vcalendar))
      (should (string-match (match-string 1 vcalendar) event)))
    (setenv "TZ" tz)))

(ert-deftest gnus-icalendar--ical-from-event ()
  ""
  (let* ((event-string "\
BEGIN:VEVENT
DTSTAMP:20240915T120000Z
DTSTART:20240917T080000Z
DTEND:20240917T100000Z
SUMMARY:Party
DESCRIPTION:Lots of reasons to celebrate!
ATTENDEE;PARTSTAT=NEEDS-ACTION;ROLE=REQ-PARTICIPANT;RSVP=TRUE;CN=Required CN:mailto:required@company.invalid
ATTENDEE;PARTSTAT=NEEDS-ACTION;ROLE=REQ-PARTICIPANT;RSVP=TRUE:mailto:required2@company.invalid
ATTENDEE;PARTSTAT=NEEDS-ACTION;ROLE=OPT-PARTICIPANT;RSVP=TRUE:mailto:optional@company.invalid
LOCATION:Party room
ORGANIZER:mailto:organizer@company.invalid
UID:ac44f43e-f5cd-4b0a-878e-add01aeb12dd
SEQUENCE:0
END:VEVENT")
         (vcalendar-string (format "\
BEGIN:VCALENDAR
PRODID:-//Google Inc//Google Calendar 70.9054//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:REQUEST
%s
END:VCALENDAR"
                                   event-string))
         (event (gnus-icalendar-tests--get-ical-event vcalendar-string))
         (ical (gnus-icalendar--ical-from-event event)))
    (should (string-match "^BEGIN:VEVENT$" ical))
    (should (string-match "^END:VEVENT$" ical))
    (should (string-match "^DTSTAMP:" ical))
    (should (string-match "^DTSTART:20240917T080000Z$" ical))
    (should (string-match "^DTEND:20240917T100000Z$" ical))
    (should (string-match "^SUMMARY:Party$" ical))
    (should (string-match "^DESCRIPTION:Lots of reasons to celebrate!$" ical))
    (should (string-match "^ATTENDEE;PARTSTAT=NEEDS-ACTION;ROLE=REQ-PARTICIPANT;RSVP=TRUE:mailto:required@company.invalid$" ical))
    (should (string-match "^ATTENDEE;PARTSTAT=NEEDS-ACTION;ROLE=OPT-PARTICIPANT;RSVP=TRUE:mailto:optional@company.invalid$" ical))
    (should (string-match "^LOCATION:Party room$" ical))
    (should (string-match "^ORGANIZER:mailto:organizer@company.invalid$" ical))
    (should (string-match "^UID:ac44f43e-f5cd-4b0a-878e-add01aeb12dd$" ical))
    (should (string-match "^SEQUENCE:0$" ical))))

(ert-deftest gnus-icalendar--create-attendee-list ()
  ""
  (let* ((req-part '("Colleague1 <required@company.invalid>"
                     "Cool Colleague <required2@company.invalid>"))
         (opt-part '("My boss <required@company.invalid>"))
         (attendees (gnus-icalendar--create-attendee-list req-part opt-part))
         (expected-attendee-entry "^ATTENDEE.*;ROLE=%s-PARTICIPANT.*;RSVP=TRUE:mailto:%s.*$"))
    (should (string-match (format expected-attendee-entry
                                  "REQ" (nth 0 req-part))
                          attendees))
    (should (string-match (format expected-attendee-entry
                                  "REQ" (nth 1 req-part))
                          attendees))
    (should (string-match (format expected-attendee-entry
                                  "OPT" (nth 0 opt-part))
                          attendees))))
(ert-deftest gnus-icalendar--create-empty-required-attendee-list ()
  ""
  (let* ((req-part nil)
         (opt-part '("My boss <required@company.invalid>"))
         (attendees (gnus-icalendar--create-attendee-list req-part opt-part))
         (expected-attendee-entry "^ATTENDEE.*;ROLE=%s-PARTICIPANT.*;RSVP=TRUE:mailto:%s.*$"))
    (should (string-match (format expected-attendee-entry
                                  "OPT" (nth 0 opt-part))
                          attendees))))