index_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package web
  2. import (
  3. "context"
  4. "net/http/httptest"
  5. "strings"
  6. "testing"
  7. "github.com/TecharoHQ/anubis"
  8. "github.com/TecharoHQ/anubis/lib/config"
  9. "github.com/TecharoHQ/anubis/lib/localization"
  10. "github.com/a-h/templ"
  11. )
  12. func TestBasePrefixInLinks(t *testing.T) {
  13. tests := []struct {
  14. name string
  15. basePrefix string
  16. wantInLink string
  17. }{
  18. {
  19. name: "no prefix",
  20. basePrefix: "",
  21. wantInLink: "/.within.website/x/cmd/anubis/api/",
  22. },
  23. {
  24. name: "with rififi prefix",
  25. basePrefix: "/rififi",
  26. wantInLink: "/rififi/.within.website/x/cmd/anubis/api/",
  27. },
  28. {
  29. name: "with myapp prefix",
  30. basePrefix: "/myapp",
  31. wantInLink: "/myapp/.within.website/x/cmd/anubis/api/",
  32. },
  33. }
  34. for _, tt := range tests {
  35. t.Run(tt.name, func(t *testing.T) {
  36. // Save original BasePrefix and restore after test
  37. origPrefix := anubis.BasePrefix
  38. defer func() { anubis.BasePrefix = origPrefix }()
  39. anubis.BasePrefix = tt.basePrefix
  40. // Create test impressum
  41. impressum := &config.Impressum{
  42. Footer: "<p>Test footer</p>",
  43. Page: config.ImpressumPage{
  44. Title: "Test Imprint",
  45. Body: "<p>Test imprint body</p>",
  46. },
  47. }
  48. // Create localizer using a dummy request
  49. req := httptest.NewRequest("GET", "/", nil)
  50. localizer := &localization.SimpleLocalizer{}
  51. localizer.Localizer = localization.NewLocalizationService().GetLocalizerFromRequest(req)
  52. // Render the base template to a buffer
  53. var buf strings.Builder
  54. component := base(tt.name, templ.NopComponent, impressum, nil, nil, localizer)
  55. err := component.Render(context.Background(), &buf)
  56. if err != nil {
  57. t.Fatalf("failed to render template: %v", err)
  58. }
  59. output := buf.String()
  60. // Check that honeypot link includes the base prefix
  61. if !strings.Contains(output, `href="`+tt.wantInLink+`honeypot/`) {
  62. t.Errorf("honeypot link does not contain base prefix %q\noutput: %s", tt.wantInLink, output)
  63. }
  64. // Check that imprint link includes the base prefix
  65. if !strings.Contains(output, `href="`+tt.wantInLink+`imprint`) {
  66. t.Errorf("imprint link does not contain base prefix %q\noutput: %s", tt.wantInLink, output)
  67. }
  68. })
  69. }
  70. }