This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / appview / models / repo_test.go
3.9 kB 139 lines
1package models 2 3import ( 4 "strings" 5 "testing" 6) 7 8func TestValidateRepoName_ValidRkeys(t *testing.T) { 9 valid := []string{ 10 "myrepo", 11 "MyRepo", 12 "my-repo", 13 "my_repo", 14 "my.repo", 15 "a", 16 "repo123", 17 strings.Repeat("a", 100), 18 } 19 for _, name := range valid { 20 if err := ValidateRepoName(name); err != nil { 21 t.Errorf("ValidateRepoName(%q) = %v, want nil", name, err) 22 } 23 } 24} 25 26func TestValidateRepoName_InvalidRkeys(t *testing.T) { 27 cases := []struct { 28 input string 29 substr string 30 }{ 31 {"", "empty"}, 32 {strings.Repeat("a", 101), "100 characters"}, 33 {"has space", "alphanumeric"}, 34 {"has/slash", "invalid path"}, 35 {"has\\backslash", "invalid path"}, 36 {".dotprefix", "invalid path"}, 37 {"dotsuffix.", "invalid path"}, 38 {"two..dots", "sequential dots"}, 39 {"../traversal", "invalid path"}, 40 {"self", "reserved"}, 41 {"SELF", "reserved"}, 42 } 43 for _, tc := range cases { 44 err := ValidateRepoName(tc.input) 45 if err == nil { 46 t.Errorf("ValidateRepoName(%q) = nil, want error containing %q", tc.input, tc.substr) 47 continue 48 } 49 if !strings.Contains(strings.ToLower(err.Error()), strings.ToLower(tc.substr)) { 50 t.Errorf("ValidateRepoName(%q) = %q, want substring %q", tc.input, err.Error(), tc.substr) 51 } 52 } 53} 54 55func TestStripGitExt(t *testing.T) { 56 cases := []struct{ in, want string }{ 57 {"repo.git", "repo"}, 58 {"repo", "repo"}, 59 {"repo.git.git", "repo.git"}, 60 {".git", ""}, 61 } 62 for _, tc := range cases { 63 if got := StripGitExt(tc.in); got != tc.want { 64 t.Errorf("StripGitExt(%q) = %q, want %q", tc.in, got, tc.want) 65 } 66 } 67} 68 69func TestCosmeticName_NilWhenMatchesRkey(t *testing.T) { 70 r := Repo{Name: "myrepo", Rkey: "myrepo"} 71 rec := r.AsRecord() 72 if rec.Name != nil { 73 t.Errorf("cosmeticName should be nil when Name == Rkey, got %q", *rec.Name) 74 } 75} 76 77func TestCosmeticName_PresentWhenDiffers(t *testing.T) { 78 r := Repo{Name: "MyRepo", Rkey: "myrepo", Knot: "k"} 79 rec := r.AsRecord() 80 if rec.Name == nil { 81 t.Fatal("cosmeticName should be non-nil when Name != Rkey") 82 } 83 if *rec.Name != "MyRepo" { 84 t.Errorf("cosmeticName = %q, want %q", *rec.Name, "MyRepo") 85 } 86} 87 88func TestRepoSlug(t *testing.T) { 89 cases := []struct { 90 name string 91 repo Repo 92 want string 93 }{ 94 {"name set distinct from rkey", Repo{Name: "anemone", Rkey: "3kabc"}, "anemone"}, 95 {"name equals rkey", Repo{Name: "scallop", Rkey: "scallop"}, "scallop"}, 96 {"name empty falls to rkey", Repo{Name: "", Rkey: "whelk"}, "whelk"}, 97 {"both empty", Repo{}, ""}, 98 } 99 for _, c := range cases { 100 t.Run(c.name, func(t *testing.T) { 101 if got := c.repo.Slug(); got != c.want { 102 t.Errorf("Slug() = %q, want %q", got, c.want) 103 } 104 }) 105 } 106} 107 108func TestValidateSpindle(t *testing.T) { 109 cases := []struct { 110 name string 111 raw string 112 dev bool 113 want string 114 wantErr bool 115 }{ 116 {"empty means no spindle", "", false, "", false}, 117 {"whitespace only", " ", false, "", false}, 118 {"bare hostname", "spindle.example.com", false, "spindle.example.com", false}, 119 {"scheme and trailing slash stripped", " https://Spindle.Example.com/ ", false, "spindle.example.com", false}, 120 {"localhost in dev", "localhost:6555", true, "localhost:6555", false}, 121 {"localhost in prod", "localhost:6555", false, "", true}, 122 {"plain http in prod", "http://spindle.example.com", false, "", true}, 123 {"link-local ip", "169.254.169.254", false, "", true}, 124 {"port on public host", "spindle.example.com:8443", false, "", true}, 125 {"single word", "spindle", false, "", true}, 126 {"not a host", "not a host", false, "", true}, 127 } 128 for _, c := range cases { 129 t.Run(c.name, func(t *testing.T) { 130 got, err := ValidateSpindle(c.raw, c.dev) 131 if (err != nil) != c.wantErr { 132 t.Fatalf("ValidateSpindle(%q, %v) error = %v, wantErr %v", c.raw, c.dev, err, c.wantErr) 133 } 134 if got != c.want { 135 t.Errorf("ValidateSpindle(%q, %v) = %q, want %q", c.raw, c.dev, got, c.want) 136 } 137 }) 138 } 139}