eintopf.info@v0.13.16/service/notification/notification_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 notification_test 17 18 import ( 19 "context" 20 "testing" 21 22 "eintopf.info/service/notification" 23 "eintopf.info/service/user" 24 "eintopf.info/test" 25 "github.com/google/go-cmp/cmp" 26 ) 27 28 func TestNotify(t *testing.T) { 29 mailer := test.NewMailer() 30 notificationStore := notification.NewMemoryStore() 31 settingsStore := notification.NewSettingsMemoryStore() 32 userStore := user.NewMemoryStore() 33 service := notification.NewService(mailer, notificationStore, settingsStore, userStore) 34 35 user, err := userStore.Create(context.Background(), &user.NewUser{ 36 Email: "foo@example.com", 37 }) 38 if err != nil { 39 t.Fatal(err) 40 } 41 42 // Set email to false 43 _, err = settingsStore.Create(context.Background(), ¬ification.NewSettings{ 44 UserID: user.ID, 45 Email: false, 46 }) 47 if err != nil { 48 t.Fatal(err) 49 } 50 51 err = service.Notify(context.Background(), user.ID, "hello", "a notification", notification.Link{Title: "foo", URL: "/foo"}) 52 if err != nil { 53 t.Error(err) 54 } 55 if diff := cmp.Diff([]test.Mail{}, mailer.Mails); diff != "" { 56 t.Errorf("mails mismatch (-want +got):\n%s", diff) 57 } 58 59 // TODO: enable when email setup is enabled 60 // // Set email to true 61 // _, err = settingsStore.Update(context.Background(), ¬ification.Settings{ 62 // UserID: user.ID, 63 // Email: true, 64 // }) 65 // if err != nil { 66 // t.Fatal(err) 67 // } 68 // 69 // err = service.Notify(context.Background(), user.ID, "hello", "a notification", notification.Link{Title: "foo", URL: "/foo"}) 70 // if err != nil { 71 // t.Error(err) 72 // } 73 // if diff := cmp.Diff([]test.Mail{ 74 // {Email: "foo@example.com", Subject: "hello", Body: "a notification"}, 75 // }, mailer.Mails); diff != "" { 76 // t.Errorf("mails mismatch (-want +got):\n%s", diff) 77 // } 78 }