github.com/greenpau/go-authcrunch@v1.1.4/pkg/messaging/config_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/internal/tests" 20 "github.com/greenpau/go-authcrunch/pkg/errors" 21 "testing" 22 ) 23 24 type dummyProvider struct { 25 } 26 27 func (p *dummyProvider) Validate() error { 28 return nil 29 } 30 31 func TestAddProviders(t *testing.T) { 32 tmpDir, err := tests.TempDir("TestAddMessagingProviders") 33 if err != nil { 34 t.Fatal(err) 35 } 36 37 testcases := []struct { 38 name string 39 providerName string 40 entry Provider 41 want string 42 shouldErr bool 43 err error 44 }{ 45 { 46 name: "test valid email provider config", 47 providerName: "default", 48 entry: &EmailProvider{ 49 Name: "default", 50 Address: "localhost", 51 Protocol: "smtp", 52 Credentials: "default_email_creds", 53 SenderEmail: "root@localhost", 54 }, 55 want: `{ 56 "email_providers": [ 57 { 58 "address": "localhost", 59 "credentials": "default_email_creds", 60 "name": "default", 61 "protocol": "smtp", 62 "sender_email": "root@localhost" 63 } 64 ] 65 }`, 66 }, 67 { 68 name: "test valid email provider passwordless config", 69 providerName: "default", 70 entry: &EmailProvider{ 71 Name: "default", 72 Address: "localhost", 73 Protocol: "smtp", 74 Passwordless: true, 75 SenderEmail: "root@localhost", 76 }, 77 want: `{ 78 "email_providers": [ 79 { 80 "address": "localhost", 81 "name": "default", 82 "protocol": "smtp", 83 "passwordless": true, 84 "sender_email": "root@localhost" 85 } 86 ] 87 }`, 88 }, 89 { 90 name: "test valid file provider config", 91 providerName: "default", 92 entry: &FileProvider{ 93 Name: "default", 94 RootDir: tmpDir, 95 }, 96 want: `{ 97 "file_providers": [ 98 { 99 "name": "default", 100 "root_dir": "` + tmpDir + `" 101 } 102 ] 103 }`, 104 }, 105 { 106 name: "test invalid messaging provider config", 107 entry: &dummyProvider{}, 108 shouldErr: true, 109 err: errors.ErrMessagingAddProviderConfigType.WithArgs(&dummyProvider{}), 110 }, 111 { 112 name: "test file provider config without root directory", 113 entry: &FileProvider{ 114 Name: "default", 115 }, 116 shouldErr: true, 117 err: errors.ErrMessagingProviderKeyValueEmpty.WithArgs("root_dir"), 118 }, 119 } 120 for _, tc := range testcases { 121 t.Run(tc.name, func(t *testing.T) { 122 cfg := &Config{} 123 err := cfg.Add(tc.entry) 124 if err != nil { 125 if !tc.shouldErr { 126 t.Fatalf("expected success, got: %v", err) 127 } 128 if diff := cmp.Diff(err.Error(), tc.err.Error()); diff != "" { 129 t.Fatalf("unexpected error: %v, want: %v", err, tc.err) 130 } 131 return 132 } 133 if tc.shouldErr { 134 t.Fatalf("unexpected success, want: %v", tc.err) 135 } 136 got := tests.Unpack(t, cfg) 137 want := tests.Unpack(t, tc.want) 138 139 if !cfg.FindProvider(tc.providerName) { 140 t.Fatalf("failed FindProvider with %q", tc.providerName) 141 } 142 143 switch tc.entry.(type) { 144 case *EmailProvider: 145 p := cfg.ExtractEmailProvider(tc.providerName) 146 if p == nil { 147 t.Fatalf("failed to extract %q file provider", tc.providerName) 148 } 149 providerCreds := cfg.FindProviderCredentials(tc.providerName) 150 switch providerCreds { 151 case "passwordless": 152 if !p.Passwordless { 153 t.Fatalf("provider credentials mismatch: %v, %v", providerCreds, p.Credentials) 154 } 155 case p.Credentials: 156 default: 157 t.Fatalf("provider credentials mismatch: %v, %v", providerCreds, p.Credentials) 158 } 159 case *FileProvider: 160 p := cfg.ExtractFileProvider(tc.providerName) 161 if p == nil { 162 t.Fatalf("failed to extract %q file provider", tc.providerName) 163 } 164 } 165 166 if tc.name == "test valid email provider config" { 167 if cfg.FindProvider("foobar") { 168 t.Fatal("unexpected success with FindProvider") 169 } 170 171 if cfg.ExtractEmailProvider("foo") != nil { 172 t.Fatal("unexpected success with ExtractEmailProvider") 173 } 174 175 if cfg.ExtractFileProvider("foo") != nil { 176 t.Fatal("unexpected success with ExtractEmailProvider") 177 } 178 if cfg.FindProviderCredentials("foo") != "" { 179 t.Fatal("unexpected success with FindProviderCredentials") 180 } 181 } 182 183 if diff := cmp.Diff(want, got); diff != "" { 184 t.Logf("JSON: %s", tests.UnpackJSON(t, got)) 185 t.Errorf("Add() mismatch (-want +got):\n%s", diff) 186 } 187 }) 188 } 189 }