git-lfs/creds/netrc_test.go
Preben Ingvaldsen 1026ff073c creds: Add new NetrcCredentialHelper
This commit adds a new credential helper, NetrcCredentialHelper,
to retrieve credentials from a .netrc file. This replaces the
old .netrc authorization behaviour, which was done directly from
the `doWithAuth()` code.

Additionally, this commit moves all of the `.netrc` functionality
out of `lfsapi` and into `creds`. This was done both because
`creds` is now the logical place for the `.netrc` functionality,
and to prevent an import cycle between `creds` and `lfsapi`.
2018-10-08 16:18:06 -07:00

83 lines
1.7 KiB
Go

package creds
import (
"strings"
"testing"
"github.com/git-lfs/go-netrc/netrc"
)
func TestNetrcWithHostAndPort(t *testing.T) {
var netrcHelper netrcCredentialHelper
netrcHelper.netrcFinder = &fakeNetrc{}
what := make(Creds)
what["protocol"] = "http"
what["host"] = "netrc-host:123"
what["path"] = "/foo/bar"
creds, err := netrcHelper.Fill(what)
if err != nil {
t.Fatalf("error retrieving netrc credentials: %s", err)
}
username := creds["username"]
if username != "abc" {
t.Fatalf("bad username: %s", username)
}
password := creds["password"]
if password != "def" {
t.Fatalf("bad password: %s", password)
}
}
func TestNetrcWithHost(t *testing.T) {
var netrcHelper netrcCredentialHelper
netrcHelper.netrcFinder = &fakeNetrc{}
what := make(Creds)
what["protocol"] = "http"
what["host"] = "netrc-host"
what["path"] = "/foo/bar"
creds, err := netrcHelper.Fill(what)
if err != nil {
t.Fatalf("error retrieving netrc credentials: %s", err)
}
username := creds["username"]
if username != "abc" {
t.Fatalf("bad username: %s", username)
}
password := creds["password"]
if password != "def" {
t.Fatalf("bad password: %s", password)
}
}
func TestNetrcWithBadHost(t *testing.T) {
var netrcHelper netrcCredentialHelper
netrcHelper.netrcFinder = &fakeNetrc{}
what := make(Creds)
what["protocol"] = "http"
what["host"] = "other-host"
what["path"] = "/foo/bar"
_, err := netrcHelper.Fill(what)
if err != credHelperNoOp {
t.Fatalf("expected no-op for unknown host other-host")
}
}
type fakeNetrc struct{}
func (n *fakeNetrc) FindMachine(host string) *netrc.Machine {
if strings.Contains(host, "netrc") {
return &netrc.Machine{Login: "abc", Password: "def"}
}
return nil
}