github.com/secman-team/gh-api@v1.8.2/pkg/cmd/factory/remote_resolver_test.go (about) 1 package factory 2 3 import ( 4 "net/url" 5 "os" 6 "testing" 7 8 "github.com/MakeNowJust/heredoc" 9 "github.com/secman-team/gh-api/git" 10 "github.com/secman-team/gh-api/core/config" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func Test_remoteResolver(t *testing.T) { 15 orig_GH_HOST := os.Getenv("GH_HOST") 16 t.Cleanup(func() { 17 os.Setenv("GH_HOST", orig_GH_HOST) 18 }) 19 20 tests := []struct { 21 name string 22 remotes func() (git.RemoteSet, error) 23 config func() (config.Config, error) 24 override string 25 output []string 26 wantsErr bool 27 }{ 28 { 29 name: "no authenticated hosts", 30 remotes: func() (git.RemoteSet, error) { 31 return git.RemoteSet{ 32 git.NewRemote("origin", "https://github.com/owner/repo.git"), 33 }, nil 34 }, 35 config: func() (config.Config, error) { 36 return config.NewFromString(heredoc.Doc(`hosts:`)), nil 37 }, 38 wantsErr: true, 39 }, 40 { 41 name: "no git remotes", 42 remotes: func() (git.RemoteSet, error) { 43 return git.RemoteSet{}, nil 44 }, 45 config: func() (config.Config, error) { 46 return config.NewFromString(heredoc.Doc(` 47 hosts: 48 example.com: 49 oauth_token: GHETOKEN 50 `)), nil 51 }, 52 wantsErr: true, 53 }, 54 { 55 name: "one authenticated host with no matching git remote and no fallback remotes", 56 remotes: func() (git.RemoteSet, error) { 57 return git.RemoteSet{ 58 git.NewRemote("origin", "https://test.com/owner/repo.git"), 59 }, nil 60 }, 61 config: func() (config.Config, error) { 62 return config.NewFromString(heredoc.Doc(` 63 hosts: 64 example.com: 65 oauth_token: GHETOKEN 66 `)), nil 67 }, 68 wantsErr: true, 69 }, 70 { 71 name: "one authenticated host with no matching git remote and fallback remotes", 72 remotes: func() (git.RemoteSet, error) { 73 return git.RemoteSet{ 74 git.NewRemote("origin", "https://github.com/owner/repo.git"), 75 }, nil 76 }, 77 config: func() (config.Config, error) { 78 return config.NewFromString(heredoc.Doc(` 79 hosts: 80 example.com: 81 oauth_token: GHETOKEN 82 `)), nil 83 }, 84 output: []string{"origin"}, 85 }, 86 { 87 name: "one authenticated host with matching git remote", 88 remotes: func() (git.RemoteSet, error) { 89 return git.RemoteSet{ 90 git.NewRemote("upstream", "https://github.com/owner/repo.git"), 91 git.NewRemote("origin", "https://example.com/owner/repo.git"), 92 }, nil 93 }, 94 config: func() (config.Config, error) { 95 return config.NewFromString(heredoc.Doc(` 96 hosts: 97 example.com: 98 oauth_token: GHETOKEN 99 `)), nil 100 }, 101 output: []string{"origin"}, 102 }, 103 { 104 name: "one authenticated host with multiple matching git remotes", 105 remotes: func() (git.RemoteSet, error) { 106 return git.RemoteSet{ 107 git.NewRemote("upstream", "https://example.com/owner/repo.git"), 108 git.NewRemote("github", "https://example.com/owner/repo.git"), 109 git.NewRemote("origin", "https://example.com/owner/repo.git"), 110 git.NewRemote("fork", "https://example.com/owner/repo.git"), 111 }, nil 112 }, 113 config: func() (config.Config, error) { 114 return config.NewFromString(heredoc.Doc(` 115 hosts: 116 example.com: 117 oauth_token: GHETOKEN 118 `)), nil 119 }, 120 output: []string{"upstream", "github", "origin", "fork"}, 121 }, 122 { 123 name: "multiple authenticated hosts with no matching git remote", 124 remotes: func() (git.RemoteSet, error) { 125 return git.RemoteSet{ 126 git.NewRemote("origin", "https://test.com/owner/repo.git"), 127 }, nil 128 }, 129 config: func() (config.Config, error) { 130 return config.NewFromString(heredoc.Doc(` 131 hosts: 132 example.com: 133 oauth_token: GHETOKEN 134 github.com: 135 oauth_token: GHTOKEN 136 `)), nil 137 }, 138 wantsErr: true, 139 }, 140 { 141 name: "multiple authenticated hosts with one matching git remote", 142 remotes: func() (git.RemoteSet, error) { 143 return git.RemoteSet{ 144 git.NewRemote("upstream", "https://test.com/owner/repo.git"), 145 git.NewRemote("origin", "https://example.com/owner/repo.git"), 146 }, nil 147 }, 148 config: func() (config.Config, error) { 149 return config.NewFromString(heredoc.Doc(` 150 hosts: 151 example.com: 152 oauth_token: GHETOKEN 153 github.com: 154 oauth_token: GHTOKEN 155 `)), nil 156 }, 157 output: []string{"origin"}, 158 }, 159 { 160 name: "multiple authenticated hosts with multiple matching git remotes", 161 remotes: func() (git.RemoteSet, error) { 162 return git.RemoteSet{ 163 git.NewRemote("upstream", "https://example.com/owner/repo.git"), 164 git.NewRemote("github", "https://github.com/owner/repo.git"), 165 git.NewRemote("origin", "https://example.com/owner/repo.git"), 166 git.NewRemote("fork", "https://github.com/owner/repo.git"), 167 git.NewRemote("test", "https://test.com/owner/repo.git"), 168 }, nil 169 }, 170 config: func() (config.Config, error) { 171 return config.NewFromString(heredoc.Doc(` 172 hosts: 173 example.com: 174 oauth_token: GHETOKEN 175 github.com: 176 oauth_token: GHTOKEN 177 `)), nil 178 }, 179 output: []string{"upstream", "github", "origin", "fork"}, 180 }, 181 { 182 name: "override host with no matching git remotes", 183 remotes: func() (git.RemoteSet, error) { 184 return git.RemoteSet{ 185 git.NewRemote("origin", "https://example.com/owner/repo.git"), 186 }, nil 187 }, 188 config: func() (config.Config, error) { 189 return config.InheritEnv(config.NewFromString(heredoc.Doc(` 190 hosts: 191 example.com: 192 oauth_token: GHETOKEN 193 `))), nil 194 }, 195 override: "test.com", 196 wantsErr: true, 197 }, 198 { 199 name: "override host with one matching git remote", 200 remotes: func() (git.RemoteSet, error) { 201 return git.RemoteSet{ 202 git.NewRemote("upstream", "https://example.com/owner/repo.git"), 203 git.NewRemote("origin", "https://test.com/owner/repo.git"), 204 }, nil 205 }, 206 config: func() (config.Config, error) { 207 return config.InheritEnv(config.NewFromString(heredoc.Doc(` 208 hosts: 209 example.com: 210 oauth_token: GHETOKEN 211 `))), nil 212 }, 213 override: "test.com", 214 output: []string{"origin"}, 215 }, 216 { 217 name: "override host with multiple matching git remotes", 218 remotes: func() (git.RemoteSet, error) { 219 return git.RemoteSet{ 220 git.NewRemote("upstream", "https://test.com/owner/repo.git"), 221 git.NewRemote("github", "https://example.com/owner/repo.git"), 222 git.NewRemote("origin", "https://test.com/owner/repo.git"), 223 }, nil 224 }, 225 config: func() (config.Config, error) { 226 return config.InheritEnv(config.NewFromString(heredoc.Doc(` 227 hosts: 228 example.com: 229 oauth_token: GHETOKEN 230 `))), nil 231 }, 232 override: "test.com", 233 output: []string{"upstream", "origin"}, 234 }, 235 } 236 237 for _, tt := range tests { 238 t.Run(tt.name, func(t *testing.T) { 239 if tt.override != "" { 240 os.Setenv("GH_HOST", tt.override) 241 } 242 rr := &remoteResolver{ 243 readRemotes: tt.remotes, 244 getConfig: tt.config, 245 urlTranslator: func(u *url.URL) *url.URL { 246 return u 247 }, 248 } 249 resolver := rr.Resolver() 250 remotes, err := resolver() 251 if tt.wantsErr { 252 assert.Error(t, err) 253 return 254 } 255 assert.NoError(t, err) 256 names := []string{} 257 for _, r := range remotes { 258 names = append(names, r.Name) 259 } 260 assert.Equal(t, tt.output, names) 261 }) 262 } 263 }