auth_settings_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package form // import "miniflux.app/v2/internal/ui/form"
  4. import (
  5. "net/http"
  6. "net/http/httptest"
  7. "net/url"
  8. "strings"
  9. "testing"
  10. "miniflux.app/v2/internal/model"
  11. )
  12. func TestAuthSettingsValid(t *testing.T) {
  13. form := &AuthSettingsForm{
  14. Username: "user",
  15. Password: "hunter2",
  16. Confirmation: "hunter2",
  17. }
  18. if err := form.Validate(); err != nil {
  19. t.Error(err)
  20. }
  21. }
  22. func TestAuthSettingsUsernameEmpty(t *testing.T) {
  23. form := &AuthSettingsForm{
  24. Username: "",
  25. }
  26. if err := form.Validate(); err == nil {
  27. t.Error("Validate should return an error")
  28. }
  29. }
  30. func TestAuthSettingsConfirmationEmpty(t *testing.T) {
  31. form := &AuthSettingsForm{
  32. Username: "user",
  33. Password: "hunter2",
  34. Confirmation: "",
  35. }
  36. if err := form.Validate(); err != nil {
  37. t.Error(err)
  38. }
  39. if form.Password != "" {
  40. t.Error("Password should have been cleared")
  41. }
  42. }
  43. func TestAuthSettingsConfirmationIncorrect(t *testing.T) {
  44. form := &AuthSettingsForm{
  45. Username: "user",
  46. Password: "hunter2",
  47. Confirmation: "unter2",
  48. }
  49. if err := form.Validate(); err == nil {
  50. t.Error("Validate should return an error")
  51. }
  52. }
  53. func TestAuthSettingsMergeKeepsPasswordWhenEmpty(t *testing.T) {
  54. user := &model.User{Username: "old", Password: "existing"}
  55. form := &AuthSettingsForm{Username: "new"}
  56. form.Merge(user)
  57. if user.Username != "new" {
  58. t.Errorf("Expected username to be updated, got %q", user.Username)
  59. }
  60. if user.Password != "existing" {
  61. t.Errorf("Expected password to be unchanged, got %q", user.Password)
  62. }
  63. }
  64. func TestNewAuthSettingsFormReadsCurrentPassword(t *testing.T) {
  65. values := url.Values{
  66. "username": {"user"},
  67. "current_password": {"old-secret"},
  68. "password": {"new-secret"},
  69. "confirmation": {"new-secret"},
  70. }
  71. request := httptest.NewRequest(http.MethodPost, "/authentication", strings.NewReader(values.Encode()))
  72. request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  73. form := NewAuthSettingsForm(request)
  74. if form.CurrentPassword != "old-secret" {
  75. t.Errorf("Expected current password to be read from the request, got %q", form.CurrentPassword)
  76. }
  77. }