170 lines
7.4 KiB
Go
170 lines
7.4 KiB
Go
package domain
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestParseGitHubRepositoryURLAcceptsOnlyCanonicalGitHubAddresses(t *testing.T) {
|
|
for _, test := range []struct {
|
|
raw, url, key string
|
|
}{
|
|
{"https://github.com/Example-Org/Payments.Ledger", "https://github.com/Example-Org/Payments.Ledger", "example-org/payments.ledger"},
|
|
{"https://github.com/example/repo/", "https://github.com/example/repo", "example/repo"},
|
|
{"https://github.com/example/repo.git", "https://github.com/example/repo", "example/repo"},
|
|
} {
|
|
repository, err := ParseGitHubRepositoryURL(test.raw)
|
|
if err != nil || repository.URL() != test.url || repository.Key() != test.key {
|
|
t.Errorf("ParseGitHubRepositoryURL(%q) = %q %q, %v", test.raw, repository.URL(), repository.Key(), err)
|
|
}
|
|
}
|
|
for _, raw := range []string{
|
|
"http://github.com/example/repo",
|
|
"https://gitlab.com/example/repo",
|
|
"https://github.com.evil.example/example/repo",
|
|
"https://user:secret@github.com/example/repo",
|
|
"https://github.com:443/example/repo",
|
|
"https://github.com/example",
|
|
"https://github.com/example/repo/tree/main",
|
|
"https://github.com/example/repo?tab=readme",
|
|
"https://github.com/example/repo#readme",
|
|
"https://github.com/-example/repo",
|
|
"https://github.com/exa--mple/repo",
|
|
"https://github.com/example/..",
|
|
"https://github.com/example/re po",
|
|
"https://github.com/../../evil",
|
|
"https://api.github.com/repos/example/repo",
|
|
"https://github.com/" + strings.Repeat("a", 40) + "/repo",
|
|
"https://GITHUB.com/example/repo",
|
|
} {
|
|
if _, err := ParseGitHubRepositoryURL(raw); !errors.Is(err, ErrInvalidGitHubRepository) {
|
|
t.Errorf("ParseGitHubRepositoryURL(%q) accepted an address outside the allowed form", raw)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGitHubRepositoryRedactsItselfInLogs(t *testing.T) {
|
|
repository, err := ParseGitHubRepositoryURL("https://github.com/private-owner/repo")
|
|
if err != nil {
|
|
t.Fatalf("ParseGitHubRepositoryURL() error = %v", err)
|
|
}
|
|
if text := fmt.Sprintf("%v %+v %#v %s", repository, repository, repository, repository); strings.Contains(text, "private-owner") {
|
|
t.Fatalf("formatting leaks the owner: %s", text)
|
|
}
|
|
}
|
|
|
|
func projectInput(title, description string, stack []string, repository string) ProfileClaimInput[ProfileProject] {
|
|
return ProfileClaimInput[ProfileProject]{Value: NewProfileProject(title, description, stack, repository), Status: ProfileClaimConfirmed}
|
|
}
|
|
|
|
func TestProfileProjectsAreValidatedNormalizedAndOptionalInARequest(t *testing.T) {
|
|
at := time.Date(2026, 10, 11, 9, 0, 0, 0, time.UTC)
|
|
projects := []ProfileClaimInput[ProfileProject]{
|
|
projectInput("Ledger", "Idempotent\r\ncallbacks", []string{"Go", "PostgreSQL"}, "https://github.com/example/ledger.git"),
|
|
projectInput("Notes", "", nil, ""),
|
|
}
|
|
content, err := ResolveCandidateProfileContent(CandidateProfileContent{}, CandidateProfileInput{Projects: &projects}, at)
|
|
if err != nil {
|
|
t.Fatalf("ResolveCandidateProfileContent() error = %v", err)
|
|
}
|
|
stored := content.Projects()
|
|
if len(stored) != 2 || stored[0].Value().RepositoryURL != "https://github.com/example/ledger" ||
|
|
stored[0].Value().Description != "Idempotent\ncallbacks" || strings.Join(stored[0].Value().StackTags(), ",") != "Go,PostgreSQL" {
|
|
t.Fatalf("projects = %+v, want the normalized values", stored)
|
|
}
|
|
if repositories := content.ProjectRepositories(); len(repositories) != 1 || repositories[0].Key() != "example/ledger" {
|
|
t.Fatalf("ProjectRepositories() = %d, want the linked repository", len(repositories))
|
|
}
|
|
|
|
// A request without the section keeps the stored projects.
|
|
kept, err := ResolveCandidateProfileContent(content, CandidateProfileInput{}, at)
|
|
if err != nil || len(kept.Projects()) != 2 || !kept.Equal(content) {
|
|
t.Fatalf("a request without projects changed them: %v", err)
|
|
}
|
|
// An empty list removes them.
|
|
empty := []ProfileClaimInput[ProfileProject]{}
|
|
cleared, err := ResolveCandidateProfileContent(content, CandidateProfileInput{Projects: &empty}, at)
|
|
if err != nil || len(cleared.Projects()) != 0 {
|
|
t.Fatalf("an empty list kept the projects: %v", err)
|
|
}
|
|
|
|
// The stored rows restore to the same content.
|
|
rows, err := content.StoredClaims()
|
|
if err != nil {
|
|
t.Fatalf("StoredClaims() error = %v", err)
|
|
}
|
|
restored, err := RestoreCandidateProfileContent(rows)
|
|
if err != nil || !restored.Equal(content) {
|
|
t.Fatalf("RestoreCandidateProfileContent() = %v, want the same projects", err)
|
|
}
|
|
|
|
for _, test := range []struct {
|
|
name string
|
|
projects []ProfileClaimInput[ProfileProject]
|
|
pointer string
|
|
}{
|
|
{"empty title", []ProfileClaimInput[ProfileProject]{projectInput("", "", nil, "")}, "/projects/0/title"},
|
|
{"long title", []ProfileClaimInput[ProfileProject]{projectInput(strings.Repeat("a", 101), "", nil, "")}, "/projects/0/title"},
|
|
{"another host", []ProfileClaimInput[ProfileProject]{projectInput("A", "", nil, "https://gitlab.com/a/b")}, "/projects/0/repository_url"},
|
|
{"repeated tag", []ProfileClaimInput[ProfileProject]{projectInput("A", "", []string{"Go", "go"}, "")}, "/projects/0/stack/1"},
|
|
{"repeated repository", []ProfileClaimInput[ProfileProject]{
|
|
projectInput("A", "", nil, "https://github.com/a/b"), projectInput("B", "", nil, "https://github.com/A/B/"),
|
|
}, "/projects/1/repository_url"},
|
|
{"too many", func() []ProfileClaimInput[ProfileProject] {
|
|
many := make([]ProfileClaimInput[ProfileProject], 0, maxProfileProjects+1)
|
|
for index := range maxProfileProjects + 1 {
|
|
many = append(many, projectInput(fmt.Sprintf("P%d", index), "", nil, ""))
|
|
}
|
|
return many
|
|
}(), "/projects"},
|
|
} {
|
|
_, err := ResolveCandidateProfileContent(CandidateProfileContent{}, CandidateProfileInput{Projects: &test.projects}, at)
|
|
var field *CandidateProfileFieldError
|
|
if !errors.As(err, &field) || field.Pointer != test.pointer {
|
|
t.Errorf("%s: error = %v, want pointer %s", test.name, err, test.pointer)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPublishedProjectsReachTheCardWithoutTheRepository(t *testing.T) {
|
|
at := time.Date(2026, 10, 11, 9, 0, 0, 0, time.UTC)
|
|
projects := []ProfileClaimInput[ProfileProject]{
|
|
projectInput("Ledger", "Callbacks", []string{"Go"}, "https://github.com/private-owner/ledger"),
|
|
}
|
|
content, err := ResolveCandidateProfileContent(CandidateProfileContent{}, CandidateProfileInput{Projects: &projects}, at)
|
|
if err != nil {
|
|
t.Fatalf("ResolveCandidateProfileContent() error = %v", err)
|
|
}
|
|
profile := CandidateViewProfileFromContentWithRepositories(content, map[string]bool{"private-owner/ledger": true})
|
|
pseudonym := "cv1_" + strings.Repeat("A", 43)
|
|
for _, test := range []struct {
|
|
fields []CardField
|
|
want int
|
|
}{
|
|
{[]CardField{CardFieldStack}, 0},
|
|
{[]CardField{CardFieldStack, CardFieldProjects}, 1},
|
|
} {
|
|
fields, err := NewCardFieldSet(test.fields)
|
|
if err != nil {
|
|
t.Fatalf("NewCardFieldSet() error = %v", err)
|
|
}
|
|
view, err := NewAnonymousCandidateView(CandidateViewInput{Pseudonym: pseudonym, PublishedFields: fields, Profile: profile, AsOf: at})
|
|
if err != nil {
|
|
t.Fatalf("NewAnonymousCandidateView() error = %v", err)
|
|
}
|
|
card := view.DTO()
|
|
if len(card.Projects) != test.want {
|
|
t.Fatalf("fields %v: projects = %d, want %d", test.fields, len(card.Projects), test.want)
|
|
}
|
|
if test.want == 1 && (!card.Projects[0].RepositoryVerified || card.Projects[0].Title != "Ledger") {
|
|
t.Fatalf("card project = %+v, want the verified title", card.Projects[0])
|
|
}
|
|
if text := fmt.Sprintf("%+v", card.Projects); strings.Contains(text, "private-owner") || strings.Contains(text, "github") {
|
|
t.Fatalf("card projects carry the repository: %s", text)
|
|
}
|
|
}
|
|
}
|