github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/edgegrid/config_test.go (about) 1 package edgegrid 2 3 import ( 4 "errors" 5 "fmt" 6 "os" 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 "github.com/tj/assert" 11 ) 12 13 func TestConfig_FromFile(t *testing.T) { 14 tests := map[string]struct { 15 fileName string 16 section string 17 expected Config 18 withError error 19 }{ 20 "valid file and section": { 21 fileName: "edgerc", 22 section: "test", 23 expected: Config{ 24 Host: "xxxx-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx.luna.akamaiapis.net", 25 ClientToken: "xxxx-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx", 26 ClientSecret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=", 27 AccessToken: "xxxx-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx", 28 MaxBody: 131072, 29 }, 30 }, 31 "file does not exist": { 32 fileName: "test", 33 section: "test", 34 withError: ErrLoadingFile, 35 }, 36 "section does not exist": { 37 fileName: "edgerc", 38 section: "abc", 39 withError: ErrSectionDoesNotExist, 40 }, 41 "missing host": { 42 fileName: "edgerc", 43 section: "missing-host", 44 withError: ErrRequiredOptionEdgerc, 45 }, 46 "missing client secret": { 47 fileName: "edgerc", 48 section: "missing-client-secret", 49 withError: ErrRequiredOptionEdgerc, 50 }, 51 "missing client token": { 52 fileName: "edgerc", 53 section: "missing-client-token", 54 withError: ErrRequiredOptionEdgerc, 55 }, 56 "missing access token": { 57 fileName: "edgerc", 58 section: "missing-access-token", 59 withError: ErrRequiredOptionEdgerc, 60 }, 61 } 62 for name, test := range tests { 63 t.Run(name, func(t *testing.T) { 64 cfg := Config{} 65 err := cfg.FromFile(fmt.Sprintf("test/%s", test.fileName), test.section) 66 if test.withError != nil { 67 assert.True(t, errors.Is(err, test.withError), "want: %v; got: %v", test.withError, err) 68 return 69 } 70 require.NoError(t, err) 71 assert.Equal(t, test.expected, cfg) 72 }) 73 } 74 } 75 76 func TestConfig_FromEnv(t *testing.T) { 77 tests := map[string]struct { 78 section string 79 envs map[string]string 80 expected Config 81 withError error 82 }{ 83 "default section, valid envs, default max body": { 84 section: "default", 85 envs: map[string]string{ 86 "AKAMAI_HOST": "test-host", 87 "AKAMAI_CLIENT_TOKEN": "test-client-token", 88 "AKAMAI_CLIENT_SECRET": "test-client-secret", 89 "AKAMAI_ACCESS_TOKEN": "test-access-token", 90 }, 91 expected: Config{ 92 Host: "test-host", 93 ClientToken: "test-client-token", 94 ClientSecret: "test-client-secret", 95 AccessToken: "test-access-token", 96 MaxBody: 131072, 97 }, 98 }, 99 "default section, valid envs": { 100 section: "default", 101 envs: map[string]string{ 102 "AKAMAI_HOST": "test-host", 103 "AKAMAI_CLIENT_TOKEN": "test-client-token", 104 "AKAMAI_CLIENT_SECRET": "test-client-secret", 105 "AKAMAI_ACCESS_TOKEN": "test-access-token", 106 "AKAMAI_MAX_BODY": "123", 107 }, 108 expected: Config{ 109 Host: "test-host", 110 ClientToken: "test-client-token", 111 ClientSecret: "test-client-secret", 112 AccessToken: "test-access-token", 113 MaxBody: 123, 114 }, 115 }, 116 "default section, valid envs, account key": { 117 section: "default", 118 envs: map[string]string{ 119 "AKAMAI_HOST": "test-host", 120 "AKAMAI_CLIENT_TOKEN": "test-client-token", 121 "AKAMAI_CLIENT_SECRET": "test-client-secret", 122 "AKAMAI_ACCESS_TOKEN": "test-access-token", 123 "AKAMAI_MAX_BODY": "123", 124 "AKAMAI_ACCOUNT_KEY": "account-key-123", 125 }, 126 expected: Config{ 127 Host: "test-host", 128 ClientToken: "test-client-token", 129 ClientSecret: "test-client-secret", 130 AccessToken: "test-access-token", 131 MaxBody: 123, 132 AccountKey: "account-key-123", 133 }, 134 }, 135 "custom section, valid envs": { 136 section: "test", 137 envs: map[string]string{ 138 "AKAMAI_TEST_HOST": "test-host", 139 "AKAMAI_TEST_CLIENT_TOKEN": "test-client-token", 140 "AKAMAI_TEST_CLIENT_SECRET": "test-client-secret", 141 "AKAMAI_TEST_ACCESS_TOKEN": "test-access-token", 142 }, 143 expected: Config{ 144 Host: "test-host", 145 ClientToken: "test-client-token", 146 ClientSecret: "test-client-secret", 147 AccessToken: "test-access-token", 148 MaxBody: 131072, 149 }, 150 }, 151 "custom section, missing host": { 152 section: "test", 153 envs: map[string]string{ 154 "AKAMAI_TEST_CLIENT_TOKEN": "test-client-token", 155 "AKAMAI_TEST_CLIENT_SECRET": "test-client-secret", 156 "AKAMAI_TEST_ACCESS_TOKEN": "test-access-token", 157 }, 158 withError: ErrRequiredOptionEnv, 159 }, 160 "custom section, missing client secret": { 161 section: "test", 162 envs: map[string]string{ 163 "AKAMAI_TEST_HOST": "test-host", 164 "AKAMAI_TEST_CLIENT_TOKEN": "test-client-token", 165 "AKAMAI_TEST_ACCESS_TOKEN": "test-access-token", 166 }, 167 withError: ErrRequiredOptionEnv, 168 }, 169 "custom section, missing client token": { 170 section: "test", 171 envs: map[string]string{ 172 "AKAMAI_TEST_HOST": "test-host", 173 "AKAMAI_TEST_CLIENT_SECRET": "test-client-secret", 174 "AKAMAI_TEST_ACCESS_TOKEN": "test-access-token", 175 }, 176 withError: ErrRequiredOptionEnv, 177 }, 178 "custom section, missing access token": { 179 section: "test", 180 envs: map[string]string{ 181 "AKAMAI_TEST_HOST": "test-host", 182 "AKAMAI_TEST_CLIENT_TOKEN": "test-client-token", 183 "AKAMAI_TEST_CLIENT_SECRET": "test-client-secret", 184 }, 185 withError: ErrRequiredOptionEnv, 186 }, 187 } 188 for name, test := range tests { 189 t.Run(name, func(t *testing.T) { 190 for k, v := range test.envs { 191 require.NoError(t, os.Setenv(k, v)) 192 } 193 defer func() { 194 for k := range test.envs { 195 require.NoError(t, os.Unsetenv(k)) 196 } 197 }() 198 cfg := Config{} 199 err := cfg.FromEnv(test.section) 200 if test.withError != nil { 201 assert.True(t, errors.Is(err, test.withError), "want: %v; got: %v", test.withError, err) 202 return 203 } 204 require.NoError(t, err) 205 assert.Equal(t, test.expected, cfg) 206 }) 207 } 208 } 209 210 func TestConfig_Validate(t *testing.T) { 211 tests := map[string]struct { 212 fileName string 213 section string 214 expected Config 215 errorIsExpected bool 216 }{ 217 "invalid host from file with slash at the end": { 218 fileName: "edgerc", 219 section: "slash-at-the-end-of-host-value", 220 expected: Config{ 221 Host: "xxxx-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx.luna.akamaiapis.net/", 222 ClientToken: "xxxx-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx", 223 ClientSecret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=", 224 AccessToken: "xxxx-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx", 225 MaxBody: 131072, 226 }, 227 errorIsExpected: true, 228 }, 229 "valid host from file": { 230 fileName: "edgerc", 231 section: "test", 232 expected: Config{ 233 Host: "xxxx-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx.luna.akamaiapis.net", 234 ClientToken: "xxxx-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx", 235 ClientSecret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=", 236 AccessToken: "xxxx-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx", 237 MaxBody: 131072, 238 }, 239 errorIsExpected: false, 240 }, 241 } 242 243 for name, test := range tests { 244 t.Run(name, func(t *testing.T) { 245 cfg := Config{} 246 _ = cfg.FromFile(fmt.Sprintf("test/%s", test.fileName), test.section) 247 err := cfg.Validate() 248 if err != nil && test.errorIsExpected == true { 249 assert.Equal(t, test.expected, cfg) 250 assert.True(t, errors.Is(err, ErrHostContainsSlashAtTheEnd)) 251 } else { 252 assert.True(t, errors.Is(err, nil)) 253 } 254 }) 255 } 256 }