package web import ( "context" "net/http/httptest" "strings" "testing" "git.sad.ovh/sophie/nuke" "git.sad.ovh/sophie/nuke/lib/config" "git.sad.ovh/sophie/nuke/lib/localization" "github.com/a-h/templ" ) func TestBasePrefixInLinks(t *testing.T) { tests := []struct { name string basePrefix string wantInLink string }{ { name: "no prefix", basePrefix: "", wantInLink: "/.within.website/x/cmd/nuke/api/", }, { name: "with rififi prefix", basePrefix: "/rififi", wantInLink: "/rififi/.within.website/x/cmd/nuke/api/", }, { name: "with myapp prefix", basePrefix: "/myapp", wantInLink: "/myapp/.within.website/x/cmd/nuke/api/", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // Save original BasePrefix and restore after test origPrefix := nuke.BasePrefix defer func() { nuke.BasePrefix = origPrefix }() nuke.BasePrefix = tt.basePrefix // Create test impressum impressum := &config.Impressum{ Footer: "

Test footer

", Page: config.ImpressumPage{ Title: "Test Imprint", Body: "

Test imprint body

", }, } // Create localizer using a dummy request req := httptest.NewRequest("GET", "/", nil) localizer := &localization.SimpleLocalizer{} localizer.Localizer = localization.NewLocalizationService().GetLocalizerFromRequest(req) // Render the base template to a buffer var buf strings.Builder component := base(tt.name, templ.NopComponent, impressum, nil, nil, localizer) err := component.Render(context.Background(), &buf) if err != nil { t.Fatalf("failed to render template: %v", err) } output := buf.String() // Check that honeypot link includes the base prefix if !strings.Contains(output, `href="`+tt.wantInLink+`honeypot/`) { t.Errorf("honeypot link does not contain base prefix %q\noutput: %s", tt.wantInLink, output) } // Check that imprint link includes the base prefix if !strings.Contains(output, `href="`+tt.wantInLink+`imprint`) { t.Errorf("imprint link does not contain base prefix %q\noutput: %s", tt.wantInLink, output) } }) } }