eintopf.info@v0.13.16/service/revent/notify_expired_test.go (about) 1 // Copyright (C) 2024 The Eintopf authors 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <https://www.gnu.org/licenses/>. 15 16 package revent_test 17 18 import ( 19 "context" 20 "fmt" 21 "testing" 22 "time" 23 24 "eintopf.info/service/event" 25 "eintopf.info/service/notification" 26 "eintopf.info/service/revent" 27 "eintopf.info/test" 28 "github.com/google/go-cmp/cmp" 29 ) 30 31 func TestNotifyAction(t *testing.T) { 32 now := time.Now() 33 tests := []struct { 34 name string 35 events []event.NewEvent 36 revents []revent.NewRepeatingEvent 37 notifications []test.Notification 38 }{ 39 { 40 name: "no notification", 41 revents: []revent.NewRepeatingEvent{ 42 { 43 Name: "MyRepeatingEvent", 44 Intervals: []revent.Interval{ 45 { 46 Type: revent.IntervalDay, 47 Interval: 1, 48 }, 49 }, 50 }, 51 }, 52 events: []event.NewEvent{ 53 { 54 Name: "MyRepeatingEvent", 55 Parent: "revent:0", 56 Start: time.Now().AddDate(0, 0, 20), 57 }, 58 }, 59 notifications: []test.Notification{}, 60 }, 61 { 62 name: "notification", 63 revents: []revent.NewRepeatingEvent{ 64 { 65 Name: "MyRepeatingEvent", 66 Intervals: []revent.Interval{ 67 { 68 Type: revent.IntervalDay, 69 Interval: 1, 70 }, 71 }, 72 OwnedBy: []string{"007"}, 73 }, 74 }, 75 events: []event.NewEvent{ 76 { 77 Published: true, 78 Name: "MyRepeatingEvent", 79 Parent: "revent:0", 80 Start: now.AddDate(0, 0, -14), 81 }, 82 }, 83 notifications: []test.Notification{ 84 { 85 UserID: "007", 86 Subject: "Regelmäßige Veranstaltung abgelaufen", 87 Message: fmt.Sprintf("Für die regelmäßige Veranstaltung 'MyRepeatingEvent' wurden keine neuen Veranstaltungen mehr erstellt. Die letzte Veranstaltung war am %s", now.AddDate(0, 0, -14).Format("02.01.2006")), 88 Link: notification.Link{Title: "Zur Regelmäßigen Veranstaltung", URL: "/backstage/repeatingevents/0#generate"}, 89 }, 90 }, 91 }, 92 } 93 94 for _, tc := range tests { 95 t.Run(tc.name, func(t *testing.T) { 96 expiredStore := revent.NewExpiredMemoryStore() 97 reventStore := revent.NewMemoryStore() 98 for _, e := range tc.revents { 99 if _, err := reventStore.Create(context.Background(), &e); err != nil { 100 t.Fatalf("create event: %s", err) 101 } 102 } 103 eventStore := event.NewMemoryStore() 104 for _, e := range tc.events { 105 if _, err := eventStore.Create(context.Background(), &e); err != nil { 106 t.Fatalf("create event: %s", err) 107 } 108 } 109 notifier := test.NewNotifier() 110 expiredNotifier := revent.NewExpiredNotifier(expiredStore, reventStore, eventStore, notifier) 111 112 err := expiredNotifier.NotifyAction(context.Background()) 113 if err != nil { 114 t.Error(err) 115 } 116 // Run NotifyAction twice to check that it is idempotent 117 err = expiredNotifier.NotifyAction(context.Background()) 118 if err != nil { 119 t.Error(err) 120 } 121 122 if diff := cmp.Diff(tc.notifications, notifier.Notifications); diff != "" { 123 t.Errorf("notifications mismatch (-want +got):\n%s", diff) 124 } 125 }) 126 } 127 }