Skip to content

Commit

Permalink
Add some quick tests of basic auth in client.validateUrl()
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Ihle <[email protected]>
  • Loading branch information
blurpy committed Jan 17, 2023
1 parent 960b2e0 commit 0c792a6
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions git/gogit/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,3 +653,66 @@ func TestHead(t *testing.T) {
g.Expect(err).ToNot(HaveOccurred())
g.Expect(hash.String()).To(Equal(cc))
}

func TestValidateUrl(t *testing.T) {
tests := []struct {
name string
transport git.TransportType
username string
password string
url string
credentialsOverHttp bool
expectedError string
}{
{
name: "blocked: basic auth over http",
transport: git.HTTP,
username: "user",
password: "pass",
url: "http://url",
expectedError: "basic auth cannot be sent over HTTP",
},
{
name: "allowed: basic auth over http with insecure enabled",
transport: git.HTTP,
username: "user",
password: "pass",
url: "http://url",
credentialsOverHttp: true,
},
{
name: "allowed: basic auth over https",
transport: git.HTTPS,
username: "user",
password: "pass",
url: "https://url",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

opts := []ClientOption{WithDiskStorage()}
if tt.credentialsOverHttp {
opts = append(opts, WithInsecureCredentialsOverHTTP())
}

ggc, err := NewClient(t.TempDir(), &git.AuthOptions{
Transport: tt.transport,
Username: tt.username,
Password: tt.password,
}, opts...)
g.Expect(err).ToNot(HaveOccurred())

err = ggc.validateUrl(tt.url)

if tt.expectedError == "" {
g.Expect(err).To(BeNil())
} else {
g.Expect(err).ToNot(BeNil())
g.Expect(err.Error()).To(ContainSubstring(tt.expectedError))
}
})
}
}

0 comments on commit 0c792a6

Please sign in to comment.