File: netrc_test.go

package info (click to toggle)
git-lfs 3.3.0-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,676 kB
  • sloc: sh: 19,133; makefile: 487; ruby: 228
file content (82 lines) | stat: -rw-r--r-- 1,751 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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, loginName string) *netrc.Machine {
	if strings.Contains(host, "netrc") {
		return &netrc.Machine{Login: "abc", Password: "def"}
	}
	return nil
}