| 1 | package creds |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/git-lfs/go-netrc/netrc" |
| 8 | ) |
| 9 | |
| 10 | func TestNetrcWithHostAndPort(t *testing.T) { |
| 11 | var netrcHelper netrcCredentialHelper |
| 12 | netrcHelper.netrcFinder = &fakeNetrc{} |
| 13 | |
| 14 | what := make(Creds) |
| 15 | what["protocol"] = []string{"http"} |
| 16 | what["host"] = []string{"netrc-host:123"} |
| 17 | what["path"] = []string{"/foo/bar"} |
| 18 | |
| 19 | creds, err := netrcHelper.Fill(what) |
| 20 | if err != nil { |
| 21 | t.Fatalf("error retrieving netrc credentials: %s", err) |
| 22 | } |
| 23 | |
| 24 | username := creds["username"][0] |
| 25 | if username != "abc" { |
| 26 | t.Fatalf("bad username: %s", username) |
| 27 | } |
| 28 | |
| 29 | password := creds["password"][0] |
| 30 | if password != "def" { |
| 31 | t.Fatalf("bad password: %s", password) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestNetrcWithHost(t *testing.T) { |
| 36 | var netrcHelper netrcCredentialHelper |
| 37 | netrcHelper.netrcFinder = &fakeNetrc{} |
| 38 | |
| 39 | what := make(Creds) |
| 40 | what["protocol"] = []string{"http"} |
| 41 | what["host"] = []string{"netrc-host"} |
| 42 | what["path"] = []string{"/foo/bar"} |
| 43 | |
| 44 | creds, err := netrcHelper.Fill(what) |
| 45 | if err != nil { |
| 46 | t.Fatalf("error retrieving netrc credentials: %s", err) |
| 47 | } |
| 48 | |
| 49 | username := creds["username"][0] |
| 50 | if username != "abc" { |
| 51 | t.Fatalf("bad username: %s", username) |
| 52 | } |
| 53 | |
| 54 | password := creds["password"][0] |
| 55 | if password != "def" { |
| 56 | t.Fatalf("bad password: %s", password) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestNetrcWithBadHost(t *testing.T) { |
| 61 | var netrcHelper netrcCredentialHelper |
| 62 | netrcHelper.netrcFinder = &fakeNetrc{} |
| 63 | |
| 64 | what := make(Creds) |
| 65 | what["protocol"] = []string{"http"} |
| 66 | what["host"] = []string{"other-host"} |
| 67 | what["path"] = []string{"/foo/bar"} |
| 68 | |
| 69 | _, err := netrcHelper.Fill(what) |
| 70 | if err != credHelperNoOp { |
| 71 | t.Fatalf("expected no-op for unknown host other-host") |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | type fakeNetrc struct{} |
| 76 | |
| 77 | func (n *fakeNetrc) FindMachine(host string, loginName string) *netrc.Machine { |
| 78 | if strings.Contains(host, "netrc") { |
| 79 | return &netrc.Machine{Login: "abc", Password: "def"} |
| 80 | } |
| 81 | return nil |
| 82 | } |