git@cosmocr.at/gitweb.git @ main · summary refs log
main / internal / auth / token_test.go /
internal/auth/token_test.go · 1.4 KiB · raw
view: SourceMarkdown ☑ force

package auth

import ( "crypto/sha256" "encoding/hex" "testing" )

func hashHex(token string) string { h := sha256.Sum256([]byte(token)) return hex.EncodeToString(h[:]) }

func TestPATVerify(t *testing.T) { const user = "u@d.com" file := "# my tokens\n" + hashHex("goodtoken") + " laptop\n" + hashHex("second") + "\n" + "garbage line that is not hex\n" st := writeTree(t, map[string]string{ user + "/gitweb/tokens": file, }) v := NewPATVerifier(st)

cases := []struct {
	token string
	want  bool
}{
	{"goodtoken", true},
	{"second", true},
	{"wrong", false},
	{"", false},
}
for _, c := range cases {
	got, err := v.Verify(user, c.token)
	if err != nil {
		t.Fatalf("Verify(%q): %v", c.token, err)
	}
	if got != c.want {
		t.Errorf("Verify(%q) = %v, want %v", c.token, got, c.want)
	}
}

}

func TestPATVerifyNoFile(t *testing.T) { st := writeTree(t, map[string]string{ "u@d.com/other": "x", // no tokens file }) v := NewPATVerifier(st) got, err := v.Verify("u@d.com", "anything") if err != nil { t.Fatalf("Verify: %v", err) } if got { t.Error("Verify with no tokens file = true, want false") } }

func TestParseTokenFileSkipsMalformed(t *testing.T) { data := []byte("#c\n\n \nnothex\n" + hashHex("x") + " label\n" + "abcd\n") // too short got := parseTokenFile(data) if len(got) != 1 { t.Fatalf("parsed %d hashes, want 1", len(got)) } }