@@ -2,10 +2,13 @@ package toml
2
2
3
3
import (
4
4
"fmt"
5
+ "net/url"
6
+ "strings"
5
7
"testing"
6
8
7
9
"github.com/stretchr/testify/assert"
8
10
11
+ "github.com/smartcontractkit/chainlink/v2/core/build"
9
12
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
10
13
"github.com/smartcontractkit/chainlink/v2/core/store/models"
11
14
"github.com/smartcontractkit/chainlink/v2/core/utils"
@@ -97,3 +100,77 @@ func Test_validateDBURL(t *testing.T) {
97
100
})
98
101
}
99
102
}
103
+
104
+ func TestValidateConfig (t * testing.T ) {
105
+ validUrl := models .URL (url.URL {Scheme : "https" , Host : "localhost" })
106
+ validSecretURL := * models .NewSecretURL (& validUrl )
107
+
108
+ invalidEmptyUrl := models .URL (url.URL {})
109
+ invalidEmptySecretURL := * models .NewSecretURL (& invalidEmptyUrl )
110
+
111
+ invalidBackupURL := models .URL (url.URL {Scheme : "http" , Host : "localhost" })
112
+ invalidBackupSecretURL := * models .NewSecretURL (& invalidBackupURL )
113
+
114
+ tests := []struct {
115
+ name string
116
+ input * DatabaseSecrets
117
+ skip bool
118
+ expectedErrContains []string
119
+ }{
120
+ {
121
+ name : "Nil URL" ,
122
+ input : & DatabaseSecrets {
123
+ URL : nil ,
124
+ },
125
+ expectedErrContains : []string {"URL: empty: must be provided and non-empty" },
126
+ },
127
+ {
128
+ name : "Empty URL" ,
129
+ input : & DatabaseSecrets {
130
+ URL : & invalidEmptySecretURL ,
131
+ },
132
+ expectedErrContains : []string {"URL: empty: must be provided and non-empty" },
133
+ },
134
+ {
135
+ name : "Insecure Password in Production" ,
136
+ input : & DatabaseSecrets {
137
+ URL : & validSecretURL ,
138
+ AllowSimplePasswords : & []bool {true }[0 ],
139
+ },
140
+ skip : ! build .IsProd (),
141
+ expectedErrContains : []string {"insecure configs are not allowed on secure builds" },
142
+ },
143
+ {
144
+ name : "Invalid Backup URL with Simple Passwords Not Allowed" ,
145
+ input : & DatabaseSecrets {
146
+ URL : & validSecretURL ,
147
+ BackupURL : & invalidBackupSecretURL ,
148
+ AllowSimplePasswords : & []bool {false }[0 ],
149
+ },
150
+ expectedErrContains : []string {"missing or insufficiently complex password" },
151
+ },
152
+ }
153
+
154
+ for _ , tt := range tests {
155
+ t .Run (tt .name , func (t * testing.T ) {
156
+ // needed while -tags test is supported
157
+ if tt .skip {
158
+ t .SkipNow ()
159
+ }
160
+ err := tt .input .ValidateConfig ()
161
+ if err == nil && len (tt .expectedErrContains ) > 0 {
162
+ t .Errorf ("expected errors but got none" )
163
+ return
164
+ }
165
+
166
+ if err != nil {
167
+ errStr := err .Error ()
168
+ for _ , expectedErrSubStr := range tt .expectedErrContains {
169
+ if ! strings .Contains (errStr , expectedErrSubStr ) {
170
+ t .Errorf ("expected error to contain substring %q but got %v" , expectedErrSubStr , errStr )
171
+ }
172
+ }
173
+ }
174
+ })
175
+ }
176
+ }
0 commit comments