auth_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package ui // import "miniflux.app/v2/internal/ui"
  4. import (
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. "miniflux.app/v2/internal/config"
  9. )
  10. func configureTestOptions(t *testing.T, env map[string]string) {
  11. t.Helper()
  12. for key, value := range env {
  13. t.Setenv(key, value)
  14. }
  15. parsedOptions, err := config.NewConfigParser().ParseEnvironmentVariables()
  16. if err != nil {
  17. t.Fatalf(`Unable to configure test options: %v`, err)
  18. }
  19. previousOptions := config.Opts
  20. config.Opts = parsedOptions
  21. t.Cleanup(func() {
  22. config.Opts = previousOptions
  23. })
  24. }
  25. func TestSetPreferencesCookie(t *testing.T) {
  26. scenarios := []struct {
  27. name string
  28. env map[string]string
  29. wantPath string
  30. wantSecure bool
  31. }{
  32. {"defaults", map[string]string{}, "/", false},
  33. {"base path over HTTPS", map[string]string{"BASE_URL": "https://example.org/reader", "HTTPS": "1"}, "/reader", true},
  34. }
  35. for _, scenario := range scenarios {
  36. t.Run(scenario.name, func(t *testing.T) {
  37. configureTestOptions(t, scenario.env)
  38. w := httptest.NewRecorder()
  39. setPreferencesCookie(w, "de_DE", "dark_serif")
  40. cookies := w.Result().Cookies()
  41. if len(cookies) != 1 {
  42. t.Fatalf(`Expected one cookie, got %d`, len(cookies))
  43. }
  44. cookie := cookies[0]
  45. if cookie.Name != preferencesCookieName || cookie.Value != "de_DE.dark_serif" {
  46. t.Errorf(`Unexpected cookie: %s=%s`, cookie.Name, cookie.Value)
  47. }
  48. if cookie.Path != scenario.wantPath {
  49. t.Errorf(`Unexpected path, got %q, want %q`, cookie.Path, scenario.wantPath)
  50. }
  51. if cookie.Secure != scenario.wantSecure {
  52. t.Errorf(`Unexpected Secure attribute, got %v, want %v`, cookie.Secure, scenario.wantSecure)
  53. }
  54. if !cookie.HttpOnly || cookie.SameSite != http.SameSiteLaxMode {
  55. t.Errorf(`Expected an HttpOnly cookie with SameSite=Lax`)
  56. }
  57. })
  58. }
  59. }
  60. func TestPreferencesFromCookie(t *testing.T) {
  61. scenarios := []struct {
  62. name string
  63. value string
  64. wantLanguage string
  65. wantTheme string
  66. wantOK bool
  67. }{
  68. {"valid", "de_DE.dark_serif", "de_DE", "dark_serif", true},
  69. {"unknown language", "xx_XX.dark_serif", "", "", false},
  70. {"unknown theme", "de_DE.neon", "", "", false},
  71. {"missing separator", "de_DE", "", "", false},
  72. {"empty", "", "", "", false},
  73. {"no cookie", "", "", "", false},
  74. }
  75. for _, scenario := range scenarios {
  76. t.Run(scenario.name, func(t *testing.T) {
  77. r := httptest.NewRequest(http.MethodGet, "/", nil)
  78. if scenario.name != "no cookie" {
  79. r.AddCookie(&http.Cookie{Name: preferencesCookieName, Value: scenario.value})
  80. }
  81. language, theme, ok := preferencesFromCookie(r)
  82. if language != scenario.wantLanguage || theme != scenario.wantTheme || ok != scenario.wantOK {
  83. t.Errorf(`Got (%q, %q, %v), want (%q, %q, %v)`, language, theme, ok, scenario.wantLanguage, scenario.wantTheme, scenario.wantOK)
  84. }
  85. })
  86. }
  87. }