2022-04-22 20:33:52 +02:00
|
|
|
package notify
|
|
|
|
|
|
|
|
import (
|
2022-04-26 01:17:52 +02:00
|
|
|
"context"
|
2022-04-22 20:33:52 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2022-04-25 23:53:28 +02:00
|
|
|
|
|
|
|
"github.com/nikoksr/notify/service/mail"
|
2022-04-22 20:33:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNew(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
n1 := New()
|
2024-10-24 15:07:36 +02:00
|
|
|
|
2022-04-22 20:33:52 +02:00
|
|
|
if n1.Disabled {
|
|
|
|
t.Fatal("New() returned disabled Notifier")
|
|
|
|
}
|
|
|
|
|
|
|
|
n2 := NewWithOptions()
|
|
|
|
if n2 == nil {
|
|
|
|
t.Fatal("NewWithOptions() returned nil")
|
|
|
|
}
|
|
|
|
diff := cmp.Diff(n1, n2, cmp.AllowUnexported(Notify{}))
|
|
|
|
if diff != "" {
|
|
|
|
t.Errorf("New() and NewWithOptions() returned different Notifiers:\n%s", diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
n3 := NewWithOptions(Disable)
|
|
|
|
if !n3.Disabled {
|
|
|
|
t.Error("NewWithOptions(Disable) did not disable Notifier")
|
|
|
|
}
|
|
|
|
|
|
|
|
n3.WithOptions(Enable)
|
|
|
|
if n3.Disabled {
|
|
|
|
t.Error("WithOptions(Enable) did not enable Notifier")
|
|
|
|
}
|
|
|
|
|
|
|
|
n3Copy := *n3
|
|
|
|
n3.WithOptions()
|
|
|
|
diff = cmp.Diff(n3, &n3Copy, cmp.AllowUnexported(Notify{}))
|
|
|
|
if diff != "" {
|
|
|
|
t.Errorf("WithOptions() altered the Notifier:\n%s", diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
n3.WithOptions(nil)
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
t.Errorf("WithOptions(nil) panicked: %v", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefault(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
n := Default()
|
2024-10-24 15:07:36 +02:00
|
|
|
|
2022-04-22 20:33:52 +02:00
|
|
|
if n.Disabled {
|
|
|
|
t.Fatal("Default() returned disabled Notifier")
|
|
|
|
}
|
|
|
|
// Compare addresses on purpose.
|
|
|
|
if n != std {
|
|
|
|
t.Error("Default() did not return the default Notifier")
|
|
|
|
}
|
|
|
|
}
|
2022-04-25 23:53:28 +02:00
|
|
|
|
|
|
|
func TestNewWithServices(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
n2 := NewWithServices(nil)
|
2024-10-24 15:07:36 +02:00
|
|
|
|
2022-04-25 23:53:28 +02:00
|
|
|
if len(n2.notifiers) != 0 {
|
|
|
|
t.Error("NewWithServices(nil) did not return empty Notifier")
|
|
|
|
}
|
|
|
|
|
|
|
|
mailService := mail.New("", "")
|
|
|
|
n3 := NewWithServices(mailService)
|
|
|
|
if len(n3.notifiers) != 1 {
|
|
|
|
t.Errorf("NewWithServices(mail.New()) was expected to have 1 notifier but had %d", len(n3.notifiers))
|
|
|
|
} else {
|
|
|
|
diff := cmp.Diff(n3.notifiers[0], mailService, cmp.AllowUnexported(mail.Mail{}))
|
|
|
|
if diff != "" {
|
|
|
|
t.Errorf("NewWithServices(mail.New()) did not correctly use service:\n%s", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-26 01:17:52 +02:00
|
|
|
|
|
|
|
func TestGlobal(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
if err := Send(ctx, "subject", "message"); err != nil {
|
|
|
|
t.Errorf("Send() with no receivers returned error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
UseServices(mail.New("", ""), nil)
|
|
|
|
if len(std.notifiers) != 1 {
|
|
|
|
t.Errorf("UseServices(mail.New()) was expected to have 1 notifier but had %d", len(std.notifiers))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := Send(ctx, "subject", "message"); err == nil {
|
|
|
|
t.Error("Send() with invalid mail returned no error")
|
|
|
|
}
|
|
|
|
}
|