github.com/greenpau/go-authcrunch@v1.1.4/pkg/messaging/email_test.go (about) 1 // Copyright 2022 Paul Greenberg greenpau@outlook.com 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package messaging 16 17 import ( 18 "github.com/google/go-cmp/cmp" 19 "github.com/greenpau/go-authcrunch/pkg/errors" 20 "testing" 21 ) 22 23 func TestValidateEmailProvider(t *testing.T) { 24 testcases := []struct { 25 name string 26 entry *EmailProvider 27 want string 28 shouldErr bool 29 err error 30 }{ 31 { 32 name: "test valid email provider config", 33 entry: &EmailProvider{ 34 Name: "default", 35 Address: "localhost", 36 Protocol: "smtp", 37 Credentials: "default_email_creds", 38 SenderEmail: "root@localhost", 39 }, 40 }, 41 { 42 name: "test email provider config with credentials and passwordless", 43 entry: &EmailProvider{ 44 Name: "default", 45 Address: "localhost", 46 Protocol: "smtp", 47 Credentials: "default_email_creds", 48 Passwordless: true, 49 SenderEmail: "root@localhost", 50 }, 51 shouldErr: true, 52 err: errors.ErrMessagingProviderCredentialsWithPasswordless, 53 }, 54 { 55 name: "test email provider config without credentials and passwordless", 56 entry: &EmailProvider{ 57 Name: "default", 58 Address: "localhost", 59 Protocol: "smtp", 60 SenderEmail: "root@localhost", 61 }, 62 shouldErr: true, 63 err: errors.ErrMessagingProviderKeyValueEmpty.WithArgs("credentials"), 64 }, 65 { 66 name: "test email provider config without address", 67 entry: &EmailProvider{ 68 Name: "default", 69 // Address: "localhost", 70 Protocol: "smtp", 71 Credentials: "default_email_creds", 72 SenderEmail: "root@localhost", 73 }, 74 shouldErr: true, 75 err: errors.ErrMessagingProviderKeyValueEmpty.WithArgs("address"), 76 }, 77 { 78 name: "test email provider config without protocol", 79 entry: &EmailProvider{ 80 Name: "default", 81 Address: "localhost", 82 // Protocol: "smtp", 83 Credentials: "default_email_creds", 84 SenderEmail: "root@localhost", 85 }, 86 shouldErr: true, 87 err: errors.ErrMessagingProviderKeyValueEmpty.WithArgs("protocol"), 88 }, 89 { 90 name: "test email provider config with unsupported protocol", 91 entry: &EmailProvider{ 92 Name: "default", 93 Address: "localhost", 94 Protocol: "foobar", 95 Credentials: "default_email_creds", 96 SenderEmail: "root@localhost", 97 }, 98 shouldErr: true, 99 err: errors.ErrMessagingProviderProtocolUnsupported.WithArgs("foobar"), 100 }, 101 { 102 name: "test email provider config without sender email", 103 entry: &EmailProvider{ 104 Name: "default", 105 Address: "localhost", 106 Protocol: "smtp", 107 Credentials: "default_email_creds", 108 // SenderEmail: "root@localhost", 109 }, 110 shouldErr: true, 111 err: errors.ErrMessagingProviderKeyValueEmpty.WithArgs("sender_email"), 112 }, 113 { 114 name: "test email provider config without name", 115 entry: &EmailProvider{}, 116 shouldErr: true, 117 err: errors.ErrMessagingProviderKeyValueEmpty.WithArgs("name"), 118 }, 119 { 120 name: "test email provider config with invalid template", 121 entry: &EmailProvider{ 122 Name: "default", 123 Address: "localhost", 124 Protocol: "smtp", 125 Credentials: "default_email_creds", 126 SenderEmail: "root@localhost", 127 Templates: map[string]string{ 128 "foo": "bar", 129 }, 130 }, 131 shouldErr: true, 132 err: errors.ErrMessagingProviderInvalidTemplate.WithArgs("foo"), 133 }, 134 } 135 for _, tc := range testcases { 136 t.Run(tc.name, func(t *testing.T) { 137 err := tc.entry.Validate() 138 if err != nil { 139 if !tc.shouldErr { 140 t.Fatalf("expected success, got: %v", err) 141 } 142 if diff := cmp.Diff(err.Error(), tc.err.Error()); diff != "" { 143 t.Fatalf("unexpected error: %v, want: %v", err, tc.err) 144 } 145 return 146 } 147 if tc.shouldErr { 148 t.Fatalf("unexpected success, want: %v", tc.err) 149 } 150 }) 151 } 152 }