|
5 | 5 | "text/template" |
6 | 6 |
|
7 | 7 | "github.com/stretchr/testify/assert" |
| 8 | + "gomodules.xyz/notify" |
| 9 | + "k8s.io/utils/strings/slices" |
8 | 10 | ) |
9 | 11 |
|
10 | 12 | func TestGetTemplater_Email(t *testing.T) { |
@@ -33,3 +35,74 @@ func TestGetTemplater_Email(t *testing.T) { |
33 | 35 | assert.Equal(t, "hello", notification.Email.Subject) |
34 | 36 | assert.Equal(t, "world", notification.Email.Body) |
35 | 37 | } |
| 38 | + |
| 39 | +type mockClient struct { |
| 40 | + notify.ByEmail |
| 41 | +} |
| 42 | + |
| 43 | +func (c *mockClient) Send() error { |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +func (c *mockClient) SendHtml() error { |
| 48 | + return nil |
| 49 | +} |
| 50 | + |
| 51 | +func (c *mockClient) WithSubject(string) notify.ByEmail { |
| 52 | + return c |
| 53 | +} |
| 54 | + |
| 55 | +func (c *mockClient) WithBody(string) notify.ByEmail { |
| 56 | + return c |
| 57 | +} |
| 58 | + |
| 59 | +func (c *mockClient) To(string, ...string) notify.ByEmail { |
| 60 | + return c |
| 61 | +} |
| 62 | + |
| 63 | +func TestSend_SingleRecepient(t *testing.T) { |
| 64 | + es := emailService{&mockClient{}, false} |
| 65 | + err := es. Send( Notification{}, Destination{ Recipient: "[email protected]"}) |
| 66 | + if err != nil { |
| 67 | + t.Error("Error while sending email") |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestSend_MultipleRecepient(t *testing.T) { |
| 72 | + es := emailService{&mockClient{}, true} |
| 73 | + // two email addresses |
| 74 | + err := es. Send( Notification{}, Destination{ Recipient: "[email protected],[email protected]"}) |
| 75 | + if err != nil { |
| 76 | + t.Error("Error while sending email") |
| 77 | + } |
| 78 | + // three email addresses |
| 79 | + err = es. Send( Notification{}, Destination{ Recipient: "[email protected],[email protected],[email protected]"}) |
| 80 | + if err != nil { |
| 81 | + t.Error("Error while sending email") |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestNewEmailService(t *testing.T) { |
| 86 | + es := NewEmailService(EmailOptions{Html: true}) |
| 87 | + if es.html != true { |
| 88 | + t.Error("Html set incorrectly") |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestParseTo(t *testing.T) { |
| 93 | + es := emailService{} |
| 94 | + testCases := []struct { |
| 95 | + recepient string |
| 96 | + want []string |
| 97 | + }{ |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + } |
| 102 | + for _, testCase := range testCases { |
| 103 | + got := es.parseTo(testCase.recepient) |
| 104 | + if !slices.Equal(testCase.want, got) { |
| 105 | + t.Errorf("Failed to parse \"%v\", got: %v", testCase.recepient, got) |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments