Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

2.3 KB
Raw Blame
package gitwork

import (
	"context"
	"strings"
	"testing"
)

func TestHTTPSCredentialMatchesOnlyItsHostAndProtocol(t *testing.T) {
	ctx := WithHTTPRemote(WithHTTPCredentials(WithToken(context.Background(), "legacy-must-not-answer"), map[string]HTTPCredential{
		"cnb.cool":             {Username: "cnb", Token: "fake-cnb-token"},
		"git.example.org:8443": {Username: "oauth2", Token: "fake-other-token"},
	}), "https://cnb.cool/owner/repository")
	for _, tc := range []struct{ protocol, host, want string }{
		{"https", "cnb.cool", "fake-cnb-token"},
		{"https", "CNB.cool:443", "fake-cnb-token"},
		{"https", "unconfigured.example.org", ""},
		{"https", "cnb.cool:444", ""},
		{"http", "cnb.cool", ""},
	} {
		cmd := command(ctx, t.TempDir(), nil, []string{"credential", "fill"})
		cmd.Stdin = strings.NewReader("protocol=" + tc.protocol + "\nhost=" + tc.host + "\n\n")
		out, err := cmd.CombinedOutput()
		if tc.want == "" {
			if err == nil || strings.Contains(string(out), "password=") {
				t.Fatalf("unexpected credentials for %s %s", tc.protocol, tc.host)
			}
		} else if err != nil || !strings.Contains(string(out), "username=cnb\npassword="+tc.want) {
			t.Fatalf("matching host: %v %s", err, out)
		}
		if strings.Contains(strings.Join(cmd.Args, " "), "fake-cnb-token") {
			t.Fatal("token was put in argv")
		}
	}
	credential, host, scoped := httpCredentialForCommand(ctx, []string{"ls-remote", "https://git.example.org:8443/owner/other"})
	if !scoped || host != "git.example.org:8443" || credential.Token != "fake-other-token" {
		t.Fatal("explicit URL borrowed the bound repository credential")
	}
}

func TestHTTPSRepositoryURLDoesNotNeedAPlatform(t *testing.T) {
	remote, name, err := HTTPSRepositoryURL("https://Unfamiliar.example.org:443/team/code.git")
	if err != nil || remote != "https://unfamiliar.example.org/team/code.git" || name != "code" {
		t.Fatalf("%q %q %v", remote, name, err)
	}
	for _, raw := range []string{"http://unfamiliar.example.org/team/code", "https://user:[email protected]/team/code", "https://unfamiliar.example.org/", "https://unfamiliar.example.org/team/../code", "https://unfamiliar.example.org/team/code?token=x", "https://127.0.0.1/team/code"} {
		if _, _, err := HTTPSRepositoryURL(raw); err == nil {
			t.Fatalf("unsafe URL accepted: %s", raw)
		}
	}
}