github.com/secman-team/gh-api@v1.8.2/core/config/from_env_test.go (about) 1 package config 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/MakeNowJust/heredoc" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestInheritEnv(t *testing.T) { 12 orig_GITHUB_TOKEN := os.Getenv("GITHUB_TOKEN") 13 orig_GITHUB_ENTERPRISE_TOKEN := os.Getenv("GITHUB_ENTERPRISE_TOKEN") 14 orig_GH_TOKEN := os.Getenv("GH_TOKEN") 15 orig_GH_ENTERPRISE_TOKEN := os.Getenv("GH_ENTERPRISE_TOKEN") 16 t.Cleanup(func() { 17 os.Setenv("GITHUB_TOKEN", orig_GITHUB_TOKEN) 18 os.Setenv("GITHUB_ENTERPRISE_TOKEN", orig_GITHUB_ENTERPRISE_TOKEN) 19 os.Setenv("GH_TOKEN", orig_GH_TOKEN) 20 os.Setenv("GH_ENTERPRISE_TOKEN", orig_GH_ENTERPRISE_TOKEN) 21 }) 22 23 type wants struct { 24 hosts []string 25 token string 26 source string 27 writeable bool 28 } 29 30 tests := []struct { 31 name string 32 baseConfig string 33 GITHUB_TOKEN string 34 GITHUB_ENTERPRISE_TOKEN string 35 GH_TOKEN string 36 GH_ENTERPRISE_TOKEN string 37 hostname string 38 wants wants 39 }{ 40 { 41 name: "blank", 42 baseConfig: ``, 43 hostname: "github.com", 44 wants: wants{ 45 hosts: []string(nil), 46 token: "", 47 source: ".config.gh.config.yml", 48 writeable: true, 49 }, 50 }, 51 { 52 name: "GITHUB_TOKEN over blank config", 53 baseConfig: ``, 54 GITHUB_TOKEN: "OTOKEN", 55 hostname: "github.com", 56 wants: wants{ 57 hosts: []string{"github.com"}, 58 token: "OTOKEN", 59 source: "GITHUB_TOKEN", 60 writeable: false, 61 }, 62 }, 63 { 64 name: "GH_TOKEN over blank config", 65 baseConfig: ``, 66 GH_TOKEN: "OTOKEN", 67 hostname: "github.com", 68 wants: wants{ 69 hosts: []string{"github.com"}, 70 token: "OTOKEN", 71 source: "GH_TOKEN", 72 writeable: false, 73 }, 74 }, 75 { 76 name: "GITHUB_TOKEN not applicable to GHE", 77 baseConfig: ``, 78 GITHUB_TOKEN: "OTOKEN", 79 hostname: "example.org", 80 wants: wants{ 81 hosts: []string{"github.com"}, 82 token: "", 83 source: ".config.gh.config.yml", 84 writeable: true, 85 }, 86 }, 87 { 88 name: "GH_TOKEN not applicable to GHE", 89 baseConfig: ``, 90 GH_TOKEN: "OTOKEN", 91 hostname: "example.org", 92 wants: wants{ 93 hosts: []string{"github.com"}, 94 token: "", 95 source: ".config.gh.config.yml", 96 writeable: true, 97 }, 98 }, 99 { 100 name: "GITHUB_ENTERPRISE_TOKEN over blank config", 101 baseConfig: ``, 102 GITHUB_ENTERPRISE_TOKEN: "ENTOKEN", 103 hostname: "example.org", 104 wants: wants{ 105 hosts: []string(nil), 106 token: "ENTOKEN", 107 source: "GITHUB_ENTERPRISE_TOKEN", 108 writeable: false, 109 }, 110 }, 111 { 112 name: "GH_ENTERPRISE_TOKEN over blank config", 113 baseConfig: ``, 114 GH_ENTERPRISE_TOKEN: "ENTOKEN", 115 hostname: "example.org", 116 wants: wants{ 117 hosts: []string(nil), 118 token: "ENTOKEN", 119 source: "GH_ENTERPRISE_TOKEN", 120 writeable: false, 121 }, 122 }, 123 { 124 name: "token from file", 125 baseConfig: heredoc.Doc(` 126 hosts: 127 github.com: 128 oauth_token: OTOKEN 129 `), 130 hostname: "github.com", 131 wants: wants{ 132 hosts: []string{"github.com"}, 133 token: "OTOKEN", 134 source: ".config.gh.hosts.yml", 135 writeable: true, 136 }, 137 }, 138 { 139 name: "GITHUB_TOKEN shadows token from file", 140 baseConfig: heredoc.Doc(` 141 hosts: 142 github.com: 143 oauth_token: OTOKEN 144 `), 145 GITHUB_TOKEN: "ENVTOKEN", 146 hostname: "github.com", 147 wants: wants{ 148 hosts: []string{"github.com"}, 149 token: "ENVTOKEN", 150 source: "GITHUB_TOKEN", 151 writeable: false, 152 }, 153 }, 154 { 155 name: "GH_TOKEN shadows token from file", 156 baseConfig: heredoc.Doc(` 157 hosts: 158 github.com: 159 oauth_token: OTOKEN 160 `), 161 GH_TOKEN: "ENVTOKEN", 162 hostname: "github.com", 163 wants: wants{ 164 hosts: []string{"github.com"}, 165 token: "ENVTOKEN", 166 source: "GH_TOKEN", 167 writeable: false, 168 }, 169 }, 170 { 171 name: "GITHUB_ENTERPRISE_TOKEN shadows token from file", 172 baseConfig: heredoc.Doc(` 173 hosts: 174 example.org: 175 oauth_token: OTOKEN 176 `), 177 GITHUB_ENTERPRISE_TOKEN: "ENVTOKEN", 178 hostname: "example.org", 179 wants: wants{ 180 hosts: []string{"example.org"}, 181 token: "ENVTOKEN", 182 source: "GITHUB_ENTERPRISE_TOKEN", 183 writeable: false, 184 }, 185 }, 186 { 187 name: "GH_ENTERPRISE_TOKEN shadows token from file", 188 baseConfig: heredoc.Doc(` 189 hosts: 190 example.org: 191 oauth_token: OTOKEN 192 `), 193 GH_ENTERPRISE_TOKEN: "ENVTOKEN", 194 hostname: "example.org", 195 wants: wants{ 196 hosts: []string{"example.org"}, 197 token: "ENVTOKEN", 198 source: "GH_ENTERPRISE_TOKEN", 199 writeable: false, 200 }, 201 }, 202 { 203 name: "GH_TOKEN shadows token from GITHUB_TOKEN", 204 baseConfig: ``, 205 GH_TOKEN: "GHTOKEN", 206 GITHUB_TOKEN: "GITHUBTOKEN", 207 hostname: "github.com", 208 wants: wants{ 209 hosts: []string{"github.com"}, 210 token: "GHTOKEN", 211 source: "GH_TOKEN", 212 writeable: false, 213 }, 214 }, 215 { 216 name: "GH_ENTERPRISE_TOKEN shadows token from GITHUB_ENTERPRISE_TOKEN", 217 baseConfig: ``, 218 GH_ENTERPRISE_TOKEN: "GHTOKEN", 219 GITHUB_ENTERPRISE_TOKEN: "GITHUBTOKEN", 220 hostname: "example.org", 221 wants: wants{ 222 hosts: []string(nil), 223 token: "GHTOKEN", 224 source: "GH_ENTERPRISE_TOKEN", 225 writeable: false, 226 }, 227 }, 228 { 229 name: "GITHUB_TOKEN adds host entry", 230 baseConfig: heredoc.Doc(` 231 hosts: 232 example.org: 233 oauth_token: OTOKEN 234 `), 235 GITHUB_TOKEN: "ENVTOKEN", 236 hostname: "github.com", 237 wants: wants{ 238 hosts: []string{"github.com", "example.org"}, 239 token: "ENVTOKEN", 240 source: "GITHUB_TOKEN", 241 writeable: false, 242 }, 243 }, 244 { 245 name: "GH_TOKEN adds host entry", 246 baseConfig: heredoc.Doc(` 247 hosts: 248 example.org: 249 oauth_token: OTOKEN 250 `), 251 GH_TOKEN: "ENVTOKEN", 252 hostname: "github.com", 253 wants: wants{ 254 hosts: []string{"github.com", "example.org"}, 255 token: "ENVTOKEN", 256 source: "GH_TOKEN", 257 writeable: false, 258 }, 259 }, 260 } 261 for _, tt := range tests { 262 t.Run(tt.name, func(t *testing.T) { 263 os.Setenv("GITHUB_TOKEN", tt.GITHUB_TOKEN) 264 os.Setenv("GITHUB_ENTERPRISE_TOKEN", tt.GITHUB_ENTERPRISE_TOKEN) 265 os.Setenv("GH_TOKEN", tt.GH_TOKEN) 266 os.Setenv("GH_ENTERPRISE_TOKEN", tt.GH_ENTERPRISE_TOKEN) 267 268 baseCfg := NewFromString(tt.baseConfig) 269 cfg := InheritEnv(baseCfg) 270 271 hosts, _ := cfg.Hosts() 272 assert.Equal(t, tt.wants.hosts, hosts) 273 274 val, source, _ := cfg.GetWithSource(tt.hostname, "oauth_token") 275 assert.Equal(t, tt.wants.token, val) 276 assert.Regexp(t, tt.wants.source, source) 277 278 val, _ = cfg.Get(tt.hostname, "oauth_token") 279 assert.Equal(t, tt.wants.token, val) 280 281 err := cfg.CheckWriteable(tt.hostname, "oauth_token") 282 if tt.wants.writeable != (err == nil) { 283 t.Errorf("CheckWriteable() = %v, wants %v", err, tt.wants.writeable) 284 } 285 }) 286 } 287 } 288 289 func TestAuthTokenProvidedFromEnv(t *testing.T) { 290 orig_GITHUB_TOKEN := os.Getenv("GITHUB_TOKEN") 291 orig_GITHUB_ENTERPRISE_TOKEN := os.Getenv("GITHUB_ENTERPRISE_TOKEN") 292 orig_GH_TOKEN := os.Getenv("GH_TOKEN") 293 orig_GH_ENTERPRISE_TOKEN := os.Getenv("GH_ENTERPRISE_TOKEN") 294 t.Cleanup(func() { 295 os.Setenv("GITHUB_TOKEN", orig_GITHUB_TOKEN) 296 os.Setenv("GITHUB_ENTERPRISE_TOKEN", orig_GITHUB_ENTERPRISE_TOKEN) 297 os.Setenv("GH_TOKEN", orig_GH_TOKEN) 298 os.Setenv("GH_ENTERPRISE_TOKEN", orig_GH_ENTERPRISE_TOKEN) 299 }) 300 301 tests := []struct { 302 name string 303 GITHUB_TOKEN string 304 GITHUB_ENTERPRISE_TOKEN string 305 GH_TOKEN string 306 GH_ENTERPRISE_TOKEN string 307 provided bool 308 }{ 309 { 310 name: "no env tokens", 311 provided: false, 312 }, 313 { 314 name: "GH_TOKEN", 315 GH_TOKEN: "TOKEN", 316 provided: true, 317 }, 318 { 319 name: "GITHUB_TOKEN", 320 GITHUB_TOKEN: "TOKEN", 321 provided: true, 322 }, 323 { 324 name: "GH_ENTERPRISE_TOKEN", 325 GH_ENTERPRISE_TOKEN: "TOKEN", 326 provided: true, 327 }, 328 { 329 name: "GITHUB_ENTERPRISE_TOKEN", 330 GITHUB_ENTERPRISE_TOKEN: "TOKEN", 331 provided: true, 332 }, 333 } 334 335 for _, tt := range tests { 336 t.Run(tt.name, func(t *testing.T) { 337 os.Setenv("GITHUB_TOKEN", tt.GITHUB_TOKEN) 338 os.Setenv("GITHUB_ENTERPRISE_TOKEN", tt.GITHUB_ENTERPRISE_TOKEN) 339 os.Setenv("GH_TOKEN", tt.GH_TOKEN) 340 os.Setenv("GH_ENTERPRISE_TOKEN", tt.GH_ENTERPRISE_TOKEN) 341 assert.Equal(t, tt.provided, AuthTokenProvidedFromEnv()) 342 }) 343 } 344 }