github.com/argoproj/argo-events@v1.9.1/sensors/triggers/email/email_test.go (about) 1 /* 2 Copyright 2020 BlackRock, Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 package email 17 18 import ( 19 "context" 20 "errors" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 corev1 "k8s.io/api/core/v1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 27 "github.com/argoproj/argo-events/common/logging" 28 "github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1" 29 "github.com/argoproj/notifications-engine/pkg/services" 30 ) 31 32 var sensorObj = &v1alpha1.Sensor{ 33 ObjectMeta: metav1.ObjectMeta{ 34 Name: "fake-sensor", 35 Namespace: "fake", 36 }, 37 Spec: v1alpha1.SensorSpec{ 38 Triggers: []v1alpha1.Trigger{ 39 { 40 Template: &v1alpha1.TriggerTemplate{ 41 Name: "fake-trigger", 42 Email: &v1alpha1.EmailTrigger{ 43 SMTPPassword: &corev1.SecretKeySelector{ 44 LocalObjectReference: corev1.LocalObjectReference{ 45 Name: "secret", 46 }, 47 Key: "password", 48 }, 49 Host: "fake-host", 50 Port: 468, 51 Username: "fake-username", 52 To: []string{"fake1@email.com", "fake2@email.com"}, 53 From: "fake-email", 54 Subject: "fake-subject", 55 Body: "fake-body", 56 }, 57 }, 58 }, 59 }, 60 }, 61 } 62 63 func getEmailTrigger(n services.NotificationService) *EmailTrigger { 64 return &EmailTrigger{ 65 Sensor: sensorObj.DeepCopy(), 66 Trigger: sensorObj.Spec.Triggers[0].DeepCopy(), 67 Logger: logging.NewArgoEventsLogger(), 68 emailSvc: n, 69 } 70 } 71 72 func TestEmailTrigger_FetchResource(t *testing.T) { 73 trigger := getEmailTrigger(nil) 74 resource, err := trigger.FetchResource(context.TODO()) 75 assert.Nil(t, err) 76 assert.NotNil(t, resource) 77 78 ot, ok := resource.(*v1alpha1.EmailTrigger) 79 assert.Equal(t, true, ok) 80 assert.Equal(t, "fake-host", ot.Host) 81 assert.Equal(t, int32(468), ot.Port) 82 assert.Equal(t, "fake-username", ot.Username) 83 assert.Equal(t, []string{"fake1@email.com", "fake2@email.com"}, ot.To) 84 assert.Equal(t, "fake-email", ot.From) 85 assert.Equal(t, "fake-subject", ot.Subject) 86 assert.Equal(t, "fake-body", ot.Body) 87 } 88 89 func TestEmailTrigger_ApplyResourceParameters(t *testing.T) { 90 trigger := getEmailTrigger(nil) 91 92 testEvents := map[string]*v1alpha1.Event{ 93 "fake-dependency": { 94 Context: &v1alpha1.EventContext{ 95 ID: "1", 96 Type: "webhook", 97 Source: "webhook-gateway", 98 DataContentType: "application/json", 99 SpecVersion: "1.0", 100 Subject: "example-1", 101 }, 102 Data: []byte(`{"to": "real@email.com", "name": "Luke"}`), 103 }, 104 } 105 106 trigger.Trigger.Template.Email.Parameters = []v1alpha1.TriggerParameter{ 107 { 108 Src: &v1alpha1.TriggerParameterSource{ 109 DependencyName: "fake-dependency", 110 DataKey: "to", 111 }, 112 Dest: "to.0", 113 }, 114 { 115 Src: &v1alpha1.TriggerParameterSource{ 116 DependencyName: "fake-dependency", 117 DataKey: "body", 118 DataTemplate: "Hi {{.Input.name}},\n\tHello There.\nThanks,\nObi", 119 }, 120 Dest: "body", 121 }, 122 } 123 124 resource, err := trigger.ApplyResourceParameters(testEvents, trigger.Trigger.Template.Email) 125 assert.Nil(t, err) 126 assert.NotNil(t, resource) 127 128 ot, ok := resource.(*v1alpha1.EmailTrigger) 129 assert.Equal(t, true, ok) 130 assert.Equal(t, "fake-host", ot.Host) 131 assert.Equal(t, int32(468), ot.Port) 132 assert.Equal(t, "fake-username", ot.Username) 133 assert.Equal(t, []string{"real@email.com", "fake2@email.com"}, ot.To) 134 assert.Equal(t, "fake-email", ot.From) 135 assert.Equal(t, "fake-subject", ot.Subject) 136 assert.Equal(t, "Hi Luke,\n\tHello There.\nThanks,\nObi", ot.Body) 137 } 138 139 // Mock Notification Service that returns an error on Send 140 type MockNotificationServiceError struct{} 141 142 // Mocks a send error 143 func (m *MockNotificationServiceError) Send(n services.Notification, d services.Destination) error { 144 return errors.New("") 145 } 146 147 // Mock Notification Service that returns nil on Send 148 type MockNotificationService struct{} 149 150 // Mocks a successful send 151 func (m *MockNotificationService) Send(n services.Notification, d services.Destination) error { 152 return nil 153 } 154 155 func TestEmailTrigger_Execute(t *testing.T) { 156 t.Run("Unmarshallable resource", func(t *testing.T) { 157 trigger := getEmailTrigger(&MockNotificationService{}) 158 _, err := trigger.Execute(context.TODO(), map[string]*v1alpha1.Event{}, nil) 159 assert.NotNil(t, err) 160 }) 161 162 t.Run("Empty to scenario", func(t *testing.T) { 163 trigger := getEmailTrigger(&MockNotificationService{}) 164 trigger.Trigger.Template.Email.To = make([]string, 0) 165 _, err := trigger.Execute(context.TODO(), map[string]*v1alpha1.Event{}, trigger.Trigger.Template.Email) 166 assert.NotNil(t, err) 167 }) 168 169 t.Run("Invalid to scenario", func(t *testing.T) { 170 trigger := getEmailTrigger(&MockNotificationService{}) 171 trigger.Trigger.Template.Email.To = []string{"not@a@valid.email"} 172 _, err := trigger.Execute(context.TODO(), map[string]*v1alpha1.Event{}, trigger.Trigger.Template.Email) 173 assert.NotNil(t, err) 174 }) 175 176 t.Run("Empty subject scenario", func(t *testing.T) { 177 trigger := getEmailTrigger(&MockNotificationService{}) 178 trigger.Trigger.Template.Email.Subject = "" 179 _, err := trigger.Execute(context.TODO(), map[string]*v1alpha1.Event{}, trigger.Trigger.Template.Email) 180 assert.NotNil(t, err) 181 }) 182 183 t.Run("Empty body scenario", func(t *testing.T) { 184 trigger := getEmailTrigger(&MockNotificationService{}) 185 trigger.Trigger.Template.Email.Body = "" 186 _, err := trigger.Execute(context.TODO(), map[string]*v1alpha1.Event{}, trigger.Trigger.Template.Email) 187 assert.NotNil(t, err) 188 }) 189 190 t.Run("Error when sending email", func(t *testing.T) { 191 trigger := getEmailTrigger(&MockNotificationServiceError{}) 192 _, err := trigger.Execute(context.TODO(), map[string]*v1alpha1.Event{}, trigger.Trigger.Template.Email) 193 assert.NotNil(t, err) 194 }) 195 196 t.Run("Email send successfully", func(t *testing.T) { 197 trigger := getEmailTrigger(&MockNotificationService{}) 198 _, err := trigger.Execute(context.TODO(), map[string]*v1alpha1.Event{}, trigger.Trigger.Template.Email) 199 assert.Nil(t, err) 200 }) 201 }