functions_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package template // import "miniflux.app/template"
  5. import (
  6. "net/http"
  7. "os"
  8. "testing"
  9. "time"
  10. "miniflux.app/config"
  11. "miniflux.app/locale"
  12. "github.com/gorilla/mux"
  13. )
  14. func TestDict(t *testing.T) {
  15. d, err := dict("k1", "v1", "k2", "v2")
  16. if err != nil {
  17. t.Fatalf(`The dict should be valid: %v`, err)
  18. }
  19. if value, found := d["k1"]; found {
  20. if value != "v1" {
  21. t.Fatalf(`Unexpected value for k1: got %q`, value)
  22. }
  23. }
  24. if value, found := d["k2"]; found {
  25. if value != "v2" {
  26. t.Fatalf(`Unexpected value for k2: got %q`, value)
  27. }
  28. }
  29. }
  30. func TestDictWithInvalidNumberOfArguments(t *testing.T) {
  31. _, err := dict("k1")
  32. if err == nil {
  33. t.Fatal(`An error should be returned if the number of arguments are not even`)
  34. }
  35. }
  36. func TestDictWithInvalidMap(t *testing.T) {
  37. _, err := dict(1, 2)
  38. if err == nil {
  39. t.Fatal(`An error should be returned if the dict keys are not string`)
  40. }
  41. }
  42. func TestHasKey(t *testing.T) {
  43. input := map[string]string{"k": "v"}
  44. if !hasKey(input, "k") {
  45. t.Fatal(`This key exists in the map and should returns true`)
  46. }
  47. if hasKey(input, "missing") {
  48. t.Fatal(`This key doesn't exists in the given map and should returns false`)
  49. }
  50. }
  51. func TestTruncateWithShortTexts(t *testing.T) {
  52. scenarios := []string{"Short text", "Короткий текст"}
  53. for _, input := range scenarios {
  54. result := truncate(input, 25)
  55. if result != input {
  56. t.Fatalf(`Unexpected output, got %q instead of %q`, result, input)
  57. }
  58. result = truncate(input, len(input))
  59. if result != input {
  60. t.Fatalf(`Unexpected output, got %q instead of %q`, result, input)
  61. }
  62. }
  63. }
  64. func TestTruncateWithLongTexts(t *testing.T) {
  65. scenarios := map[string]string{
  66. "This is a really pretty long English text": "This is a really pretty l…",
  67. "Это реально очень длинный русский текст": "Это реально очень длинный…",
  68. }
  69. for input, expected := range scenarios {
  70. result := truncate(input, 25)
  71. if result != expected {
  72. t.Fatalf(`Unexpected output, got %q instead of %q`, result, expected)
  73. }
  74. }
  75. }
  76. func TestIsEmail(t *testing.T) {
  77. if !isEmail("user@domain.tld") {
  78. t.Fatal(`This email is valid and should returns true`)
  79. }
  80. if isEmail("invalid") {
  81. t.Fatal(`This email is not valid and should returns false`)
  82. }
  83. }
  84. func TestElapsedTime(t *testing.T) {
  85. printer := locale.NewPrinter("en_US")
  86. var dt = []struct {
  87. in time.Time
  88. out string
  89. }{
  90. {time.Time{}, printer.Printf("time_elapsed.not_yet")},
  91. {time.Now().Add(time.Hour), printer.Printf("time_elapsed.not_yet")},
  92. {time.Now(), printer.Printf("time_elapsed.now")},
  93. {time.Now().Add(-time.Minute), printer.Plural("time_elapsed.minutes", 1, 1)},
  94. {time.Now().Add(-time.Minute * 40), printer.Plural("time_elapsed.minutes", 40, 40)},
  95. {time.Now().Add(-time.Hour), printer.Plural("time_elapsed.hours", 1, 1)},
  96. {time.Now().Add(-time.Hour * 3), printer.Plural("time_elapsed.hours", 3, 3)},
  97. {time.Now().Add(-time.Hour * 32), printer.Printf("time_elapsed.yesterday")},
  98. {time.Now().Add(-time.Hour * 24 * 3), printer.Plural("time_elapsed.days", 3, 3)},
  99. {time.Now().Add(-time.Hour * 24 * 14), printer.Plural("time_elapsed.days", 14, 14)},
  100. {time.Now().Add(-time.Hour * 24 * 15), printer.Plural("time_elapsed.days", 15, 15)},
  101. {time.Now().Add(-time.Hour * 24 * 21), printer.Plural("time_elapsed.weeks", 3, 3)},
  102. {time.Now().Add(-time.Hour * 24 * 32), printer.Plural("time_elapsed.months", 1, 1)},
  103. {time.Now().Add(-time.Hour * 24 * 60), printer.Plural("time_elapsed.months", 2, 2)},
  104. {time.Now().Add(-time.Hour * 24 * 366), printer.Plural("time_elapsed.years", 1, 1)},
  105. {time.Now().Add(-time.Hour * 24 * 365 * 3), printer.Plural("time_elapsed.years", 3, 3)},
  106. }
  107. for i, tt := range dt {
  108. if out := elapsedTime(printer, "Local", tt.in); out != tt.out {
  109. t.Errorf(`%d. content mismatch for "%v": expected=%q got=%q`, i, tt.in, tt.out, out)
  110. }
  111. }
  112. }
  113. func TestProxyFilterWithHttpDefault(t *testing.T) {
  114. os.Clearenv()
  115. os.Setenv("PROXY_IMAGES", "http-only")
  116. c := config.NewConfig()
  117. r := mux.NewRouter()
  118. r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
  119. input := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`
  120. output := imageProxyFilter(r, c, input)
  121. expected := `<p><img src="/proxy/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" alt="Test"/></p>`
  122. if expected != output {
  123. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  124. }
  125. }
  126. func TestProxyFilterWithHttpsDefault(t *testing.T) {
  127. os.Clearenv()
  128. os.Setenv("PROXY_IMAGES", "http-only")
  129. c := config.NewConfig()
  130. r := mux.NewRouter()
  131. r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
  132. input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
  133. output := imageProxyFilter(r, c, input)
  134. expected := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
  135. if expected != output {
  136. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  137. }
  138. }
  139. func TestProxyFilterWithHttpNever(t *testing.T) {
  140. os.Clearenv()
  141. os.Setenv("PROXY_IMAGES", "none")
  142. c := config.NewConfig()
  143. r := mux.NewRouter()
  144. r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
  145. input := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`
  146. output := imageProxyFilter(r, c, input)
  147. expected := input
  148. if expected != output {
  149. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  150. }
  151. }
  152. func TestProxyFilterWithHttpsNever(t *testing.T) {
  153. os.Clearenv()
  154. os.Setenv("PROXY_IMAGES", "none")
  155. c := config.NewConfig()
  156. r := mux.NewRouter()
  157. r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
  158. input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
  159. output := imageProxyFilter(r, c, input)
  160. expected := input
  161. if expected != output {
  162. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  163. }
  164. }
  165. func TestProxyFilterWithHttpAlways(t *testing.T) {
  166. os.Clearenv()
  167. os.Setenv("PROXY_IMAGES", "all")
  168. c := config.NewConfig()
  169. r := mux.NewRouter()
  170. r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
  171. input := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`
  172. output := imageProxyFilter(r, c, input)
  173. expected := `<p><img src="/proxy/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" alt="Test"/></p>`
  174. if expected != output {
  175. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  176. }
  177. }
  178. func TestProxyFilterWithHttpsAlways(t *testing.T) {
  179. os.Clearenv()
  180. os.Setenv("PROXY_IMAGES", "all")
  181. c := config.NewConfig()
  182. r := mux.NewRouter()
  183. r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
  184. input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
  185. output := imageProxyFilter(r, c, input)
  186. expected := `<p><img src="/proxy/aHR0cHM6Ly93ZWJzaXRlL2ZvbGRlci9pbWFnZS5wbmc=" alt="Test"/></p>`
  187. if expected != output {
  188. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  189. }
  190. }
  191. func TestProxyFilterWithHttpInvalid(t *testing.T) {
  192. os.Clearenv()
  193. os.Setenv("PROXY_IMAGES", "invalid")
  194. c := config.NewConfig()
  195. r := mux.NewRouter()
  196. r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
  197. input := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`
  198. output := imageProxyFilter(r, c, input)
  199. expected := `<p><img src="/proxy/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" alt="Test"/></p>`
  200. if expected != output {
  201. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  202. }
  203. }
  204. func TestProxyFilterWithHttpsInvalid(t *testing.T) {
  205. os.Clearenv()
  206. os.Setenv("PROXY_IMAGES", "invalid")
  207. c := config.NewConfig()
  208. r := mux.NewRouter()
  209. r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
  210. input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
  211. output := imageProxyFilter(r, c, input)
  212. expected := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
  213. if expected != output {
  214. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  215. }
  216. }