From de0e66a14419b608f6d81ebd12598fceb07a91ea Mon Sep 17 00:00:00 2001 From: Max Resnick Date: Sat, 20 Sep 2025 23:59:46 -0700 Subject: fix: some things --- internal/modules/handler_test.go | 316 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 315 insertions(+), 1 deletion(-) (limited to 'internal/modules/handler_test.go') diff --git a/internal/modules/handler_test.go b/internal/modules/handler_test.go index b173f98..943c74c 100644 --- a/internal/modules/handler_test.go +++ b/internal/modules/handler_test.go @@ -65,7 +65,7 @@ func TestHandleGoImport_ConfiguredModule(t *testing.T) { } // Check for correct module path and VCS info - expectedContent := `content="mymodule git https://git.example.com/mymodule"` + expectedContent := `content="git.example.com/mymodule git https://git.example.com/mymodule"` if !strings.Contains(body, expectedContent) { t.Errorf("response should contain %s", expectedContent) } @@ -548,3 +548,317 @@ func TestModuleMiddleware(t *testing.T) { } }) } + +// Tests for new helper functions +func TestNormalizeImportPath(t *testing.T) { + handler := &ModuleHandler{ + reposDir: "/tmp/repos", + serverHost: "git.example.com", + } + + tests := []struct { + name string + serverHost string + modulePath string + expected string + shouldErr bool + }{ + { + name: "valid_simple_path", + serverHost: "git.example.com", + modulePath: "mymodule", + expected: "git.example.com/mymodule", + shouldErr: false, + }, + { + name: "trailing_slash_on_host", + serverHost: "git.example.com/", + modulePath: "mymodule", + expected: "git.example.com/mymodule", + shouldErr: false, + }, + { + name: "leading_slash_on_module", + serverHost: "git.example.com", + modulePath: "/mymodule", + expected: "git.example.com/mymodule", + shouldErr: false, + }, + { + name: "nested_module_path", + serverHost: "git.example.com", + modulePath: "company/mymodule", + expected: "git.example.com/company/mymodule", + shouldErr: false, + }, + { + name: "empty_server_host", + serverHost: "", + modulePath: "mymodule", + expected: "", + shouldErr: true, + }, + { + name: "empty_module_path", + serverHost: "git.example.com", + modulePath: "", + expected: "", + shouldErr: true, + }, + { + name: "path_traversal_attempt", + serverHost: "git.example.com", + modulePath: "../../../etc/passwd", + expected: "", + shouldErr: true, + }, + { + name: "whitespace_in_module_path", + serverHost: "git.example.com", + modulePath: "my module", + expected: "", + shouldErr: true, + }, + { + name: "tab_in_module_path", + serverHost: "git.example.com", + modulePath: "my\tmodule", + expected: "", + shouldErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Update handler for this test + handler.serverHost = tt.serverHost + + result, err := handler.normalizeImportPath(tt.modulePath) + + if tt.shouldErr { + if err == nil { + t.Errorf("expected error but got none") + } + } else { + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if result != tt.expected { + t.Errorf("expected %q, got %q", tt.expected, result) + } + } + }) + } +} + +func TestBuildRepoURL(t *testing.T) { + handler := &ModuleHandler{ + reposDir: "/tmp/repos", + serverHost: "git.example.com", + } + + tests := []struct { + name string + modulePath string + expected string + shouldErr bool + }{ + { + name: "simple_module", + modulePath: "mymodule", + expected: "https://git.example.com/mymodule", + shouldErr: false, + }, + { + name: "nested_module", + modulePath: "company/mymodule", + expected: "https://git.example.com/company/mymodule", + shouldErr: false, + }, + { + name: "invalid_module_path", + modulePath: "", + expected: "", + shouldErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result, err := handler.buildRepoURL(tt.modulePath) + + if tt.shouldErr { + if err == nil { + t.Errorf("expected error but got none") + } + } else { + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if result != tt.expected { + t.Errorf("expected %q, got %q", tt.expected, result) + } + } + }) + } +} + +func TestGenerateGoImportHTML(t *testing.T) { + handler := &ModuleHandler{ + reposDir: "/tmp/repos", + serverHost: "git.example.com", + } + + tests := []struct { + name string + modulePath string + shouldErr bool + expectedHTML []string // Strings that should be present in the HTML + }{ + { + name: "valid_module", + modulePath: "mymodule", + shouldErr: false, + expectedHTML: []string{ + ``, + `