git-lfs/lfs/credentials_test.go

263 lines
7.2 KiB
Go
Raw Normal View History

2015-03-19 19:30:55 +00:00
package lfs
2015-02-17 19:16:59 +00:00
import (
"encoding/base64"
2015-08-28 22:27:40 +00:00
"fmt"
2015-02-17 19:16:59 +00:00
"net/http"
"testing"
)
2015-08-28 22:27:40 +00:00
func TestGetCredentialsForApi(t *testing.T) {
checkGetCredentials(t, getCredsForAPI, []*getCredentialCheck{
{
Desc: "simple",
Config: map[string]string{"lfs.url": "https://git-server.com"},
Method: "GET",
Href: "https://git-server.com/foo",
Protocol: "https",
Host: "git-server.com",
Username: "git-server.com",
Password: "monkey",
},
{
Desc: "username in url",
Config: map[string]string{"lfs.url": "https://user@git-server.com"},
Method: "GET",
Href: "https://git-server.com/foo",
Protocol: "https",
Host: "git-server.com",
Username: "user",
Password: "monkey",
},
{
Desc: "auth header",
2015-08-28 22:27:40 +00:00
Config: map[string]string{"lfs.url": "https://git-server.com"},
Header: map[string]string{"Authorization": "Test monkey"},
Method: "GET",
Href: "https://git-server.com/foo",
Authorization: "Test monkey",
},
{
Desc: "scheme mismatch",
Config: map[string]string{"lfs.url": "https://git-server.com"},
Method: "GET",
Href: "http://git-server.com/foo",
Protocol: "http",
Host: "git-server.com",
Path: "foo",
Username: "git-server.com",
Password: "monkey",
},
{
Desc: "host mismatch",
Config: map[string]string{"lfs.url": "https://git-server.com"},
Method: "GET",
Href: "https://git-server2.com/foo",
Protocol: "https",
Host: "git-server2.com",
Path: "foo",
Username: "git-server2.com",
Password: "monkey",
},
{
Desc: "port mismatch",
Config: map[string]string{"lfs.url": "https://git-server.com"},
Method: "GET",
Href: "https://git-server.com:8080/foo",
Protocol: "https",
Host: "git-server.com:8080",
Path: "foo",
Username: "git-server.com:8080",
Password: "monkey",
},
{
Desc: "api url auth",
Config: map[string]string{"lfs.url": "https://testuser:testpass@git-server.com"},
Method: "GET",
Href: "https://git-server.com/foo",
Authorization: "Basic " + base64.URLEncoding.EncodeToString([]byte("testuser:testpass")),
},
{
Desc: "git url auth",
CurrentRemote: "origin",
Config: map[string]string{
"lfs.url": "https://git-server.com",
"remote.origin.url": "https://gituser:gitpass@git-server.com",
},
Method: "GET",
Href: "https://git-server.com/foo",
Authorization: "Basic " + base64.URLEncoding.EncodeToString([]byte("gituser:gitpass")),
},
{
Desc: "username in url",
Config: map[string]string{"lfs.url": "https://user@git-server.com"},
Method: "GET",
Href: "https://git-server.com/foo",
Protocol: "https",
Host: "git-server.com",
Username: "user",
Password: "monkey",
},
{
Desc: "?token query",
Config: map[string]string{"lfs.url": "https://git-server.com"},
Method: "GET",
Href: "https://git-server.com/foo?token=abc",
SkipAuth: true,
},
2015-08-28 22:27:40 +00:00
})
2015-02-17 19:16:59 +00:00
}
2015-08-28 22:27:40 +00:00
func TestGetCredentials(t *testing.T) {
checks := []*getCredentialCheck{
{
Desc: "git server",
Method: "GET",
Href: "https://git-server.com/foo",
Protocol: "https",
Host: "git-server.com",
Username: "git-server.com",
Password: "monkey",
},
{
Desc: "separate lfs server",
Method: "GET",
Href: "https://lfs-server.com/foo",
Protocol: "https",
Host: "lfs-server.com",
Username: "lfs-server.com",
Password: "monkey",
},
{
Desc: "?token query",
Config: map[string]string{"lfs.url": "https://git-server.com"},
Method: "GET",
Href: "https://git-server.com/foo?token=abc",
SkipAuth: true,
},
2015-08-28 22:27:40 +00:00
}
// these properties should not change the outcome
for _, check := range checks {
check.CurrentRemote = "origin"
check.Config = map[string]string{
"lfs.url": "https://testuser:testuser@git-server.com",
"remote.origin.url": "https://gituser:gitpass@git-server.com",
}
2015-02-17 19:16:59 +00:00
}
2015-08-28 22:27:40 +00:00
checkGetCredentials(t, getCreds, checks)
2015-03-19 21:16:52 +00:00
}
2015-02-17 19:16:59 +00:00
2015-08-28 22:27:40 +00:00
func checkGetCredentials(t *testing.T, getCredsFunc func(*http.Request) (Creds, error), checks []*getCredentialCheck) {
existingRemote := Config.CurrentRemote
for _, check := range checks {
t.Logf("Checking %q", check.Desc)
Config.CurrentRemote = check.CurrentRemote
2015-08-28 22:27:40 +00:00
for key, value := range check.Config {
Config.SetConfig(key, value)
}
2015-03-19 21:16:52 +00:00
2015-08-28 22:27:40 +00:00
req, err := http.NewRequest(check.Method, check.Href, nil)
if err != nil {
t.Errorf("[%s] %s", check.Desc, err)
continue
}
2015-03-19 21:16:52 +00:00
2015-08-28 22:27:40 +00:00
for key, value := range check.Header {
req.Header.Set(key, value)
}
2015-03-19 21:16:52 +00:00
2015-08-28 22:27:40 +00:00
creds, err := getCredsFunc(req)
if err != nil {
t.Errorf("[%s] %s", check.Desc, err)
continue
}
2015-03-19 21:16:52 +00:00
2015-08-28 22:27:40 +00:00
if check.ExpectCreds() {
if creds == nil {
t.Errorf("[%s], no credentials returned", check.Desc)
continue
}
if value := creds["protocol"]; len(check.Protocol) > 0 && value != check.Protocol {
t.Errorf("[%s] bad protocol: %q, expected: %q", check.Desc, value, check.Protocol)
}
if value := creds["host"]; len(check.Host) > 0 && value != check.Host {
t.Errorf("[%s] bad host: %q, expected: %q", check.Desc, value, check.Host)
}
if value := creds["username"]; len(check.Username) > 0 && value != check.Username {
t.Errorf("[%s] bad username: %q, expected: %q", check.Desc, value, check.Username)
}
if value := creds["password"]; len(check.Password) > 0 && value != check.Password {
t.Errorf("[%s] bad password: %q, expected: %q", check.Desc, value, check.Password)
}
if value := creds["path"]; len(check.Path) > 0 && value != check.Path {
t.Errorf("[%s] bad path: %q, expected: %q", check.Desc, value, check.Path)
}
} else {
if creds != nil {
t.Errorf("[%s], unexpected credentials: %v // %v", check.Desc, creds, check)
continue
}
}
reqAuth := req.Header.Get("Authorization")
if check.SkipAuth {
} else if len(check.Authorization) > 0 {
if reqAuth != check.Authorization {
t.Errorf("[%s] Unexpected Authorization header: %s", check.Desc, reqAuth)
2015-08-28 22:27:40 +00:00
}
} else {
rawtoken := fmt.Sprintf("%s:%s", check.Username, check.Password)
expected := "Basic " + base64.URLEncoding.EncodeToString([]byte(rawtoken))
if reqAuth != expected {
t.Errorf("[%s] Bad Authorization. Expected '%s', got '%s'", check.Desc, expected, reqAuth)
2015-08-28 22:27:40 +00:00
}
}
2015-08-28 22:27:40 +00:00
Config.ResetConfig()
Config.CurrentRemote = existingRemote
2015-02-17 19:16:59 +00:00
}
}
2015-08-28 22:27:40 +00:00
type getCredentialCheck struct {
Desc string
Config map[string]string
Header map[string]string
Method string
Href string
Protocol string
Host string
Username string
Password string
Path string
Authorization string
CurrentRemote string
SkipAuth bool
2015-02-17 19:16:59 +00:00
}
2015-08-28 22:27:40 +00:00
func (c *getCredentialCheck) ExpectCreds() bool {
return len(c.Protocol) > 0 || len(c.Host) > 0 || len(c.Username) > 0 ||
len(c.Password) > 0 || len(c.Path) > 0
}
2015-08-28 22:27:40 +00:00
2015-02-17 19:16:59 +00:00
func init() {
2015-08-28 19:38:56 +00:00
execCreds = func(input Creds, subCommand string) (Creds, error) {
output := make(Creds)
for key, value := range input {
output[key] = value
}
if _, ok := output["username"]; !ok {
output["username"] = input["host"]
}
2015-08-28 19:38:56 +00:00
output["password"] = "monkey"
return output, nil
2015-02-17 19:16:59 +00:00
}
}