auth_integration_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package fever_test
  4. import (
  5. "encoding/json"
  6. "net/url"
  7. "strconv"
  8. "strings"
  9. "testing"
  10. "time"
  11. miniflux "miniflux.app/v2/client"
  12. )
  13. func TestAuthentication(t *testing.T) {
  14. t.Parallel()
  15. user := newTestUser(t)
  16. t.Run("API key in the query string", func(t *testing.T) {
  17. before := time.Now().Unix()
  18. var result baseResult
  19. user.fever.getJSON(t, nil, &result)
  20. after := time.Now().Unix()
  21. // Miniflux reports the time of the response, not the time of the last feed refresh.
  22. if result.LastRefreshedOnTime < before || result.LastRefreshedOnTime > after {
  23. t.Errorf(`Expected last_refreshed_on_time between %d and %d, got %d`, before, after, result.LastRefreshedOnTime)
  24. }
  25. })
  26. t.Run("API key in the form body", func(t *testing.T) {
  27. var result baseResult
  28. user.fever.postJSON(t, nil, &result)
  29. })
  30. t.Run("API key in uppercase", func(t *testing.T) {
  31. client := &feverClient{baseURL: requireTestServer(t).URL, apiKey: strings.ToUpper(user.fever.apiKey)}
  32. var result baseResult
  33. client.getJSON(t, nil, &result)
  34. })
  35. t.Run("selector without a value", func(t *testing.T) {
  36. status, body := user.fever.getRawQuery(t, "groups&api_key="+user.fever.apiKey)
  37. var result map[string]json.RawMessage
  38. decodeAuthenticatedJSON(t, status, body, &result)
  39. if _, found := result["groups"]; !found {
  40. t.Errorf(`Expected the groups member, got %s`, body)
  41. }
  42. })
  43. }
  44. func TestAuthenticationRejectsInvalidAPIKeys(t *testing.T) {
  45. t.Parallel()
  46. user := newTestUser(t)
  47. disabledUser := newTestUser(t)
  48. disabledAPIKey := setFeverIntegration(t, disabledUser.id, false, testFeverPassword)
  49. // The anonymous client sends the API key of each scenario as is.
  50. anonymous := &feverClient{baseURL: requireTestServer(t).URL}
  51. scenarios := []struct {
  52. name string
  53. params url.Values
  54. }{
  55. {"no API key", nil},
  56. {"an empty API key", url.Values{"api_key": {""}}},
  57. {"the key of a wrong password", url.Values{"api_key": {feverAPIKey(user.username, "wrong password")}}},
  58. {"the key of the Miniflux password", url.Values{"api_key": {feverAPIKey(user.username, testUserPassword)}}},
  59. {"the key of an unknown username", url.Values{"api_key": {feverAPIKey("unknown_fever_user", testFeverPassword)}}},
  60. {"the key of a user with Fever disabled", url.Values{"api_key": {disabledAPIKey}}},
  61. }
  62. for _, scenario := range scenarios {
  63. t.Run("GET with "+scenario.name, func(t *testing.T) {
  64. status, body := anonymous.get(t, scenario.params)
  65. requireAuthFailure(t, status, body)
  66. })
  67. t.Run("POST with "+scenario.name, func(t *testing.T) {
  68. status, body := anonymous.post(t, scenario.params)
  69. requireAuthFailure(t, status, body)
  70. })
  71. }
  72. }
  73. func TestRejectedRequestsDoNotChangeEntries(t *testing.T) {
  74. t.Parallel()
  75. user := newTestUser(t)
  76. _, entries := subscribeToFixture(t, user)
  77. entryID := entries[0].ID
  78. markAsRead := url.Values{"mark": {"item"}, "as": {"read"}, "id": {strconv.FormatInt(entryID, 10)}}
  79. intruder := &feverClient{baseURL: requireTestServer(t).URL, apiKey: feverAPIKey(user.username, "wrong password")}
  80. status, body := intruder.post(t, markAsRead)
  81. requireAuthFailure(t, status, body)
  82. requireEntryStatus(t, user, entryID, miniflux.EntryStatusUnread)
  83. // The same request with the right API key changes the entry, so the rejection above is what kept it unread.
  84. var result baseResult
  85. user.fever.postJSON(t, markAsRead, &result)
  86. requireEntryStatus(t, user, entryID, miniflux.EntryStatusRead)
  87. }
  88. // requireEntryStatus fails the test unless the REST API reports the expected status for the entry.
  89. func requireEntryStatus(t *testing.T, user *testUser, entryID int64, expected string) {
  90. t.Helper()
  91. entry, err := user.rest.Entry(entryID)
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. if entry.Status != expected {
  96. t.Fatalf(`Expected entry %d to be %q, got %q`, entryID, expected, entry.Status)
  97. }
  98. }