integration_server_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package fever_test
  4. import (
  5. "crypto/md5"
  6. "encoding/hex"
  7. "fmt"
  8. "math/rand/v2"
  9. "os"
  10. "testing"
  11. miniflux "miniflux.app/v2/client"
  12. "miniflux.app/v2/internal/http/server/servertest"
  13. "miniflux.app/v2/internal/model"
  14. )
  15. const (
  16. skipIntegrationTestsMessage = `Set TEST_DATABASE_URL to a PostgreSQL database to run the Fever API integration tests`
  17. // testUserPassword is the Miniflux password of the test users, used by the REST API.
  18. testUserPassword = "miniflux_test_password"
  19. // testFeverPassword is the Fever password of the test users, hashed into their API key.
  20. testFeverPassword = "fever_test_password"
  21. )
  22. // testServer is the Miniflux server started by TestMain, or nil when TEST_DATABASE_URL is not set.
  23. var testServer *servertest.Server
  24. func TestMain(m *testing.M) {
  25. os.Exit(runTests(m))
  26. }
  27. func runTests(m *testing.M) int {
  28. databaseURL := os.Getenv("TEST_DATABASE_URL")
  29. if databaseURL == "" {
  30. return m.Run()
  31. }
  32. server, err := servertest.NewServer(databaseURL)
  33. if err != nil {
  34. fmt.Fprintf(os.Stderr, "Unable to start the integration test server: %v\n", err)
  35. return 1
  36. }
  37. defer server.Close()
  38. testServer = server
  39. return m.Run()
  40. }
  41. // requireTestServer returns the integration test server, or skips the test when TEST_DATABASE_URL is not set.
  42. func requireTestServer(t *testing.T) *servertest.Server {
  43. t.Helper()
  44. if testServer == nil {
  45. t.Skip(skipIntegrationTestsMessage)
  46. }
  47. return testServer
  48. }
  49. // testUser is a Miniflux user with the Fever API enabled.
  50. type testUser struct {
  51. id int64
  52. username string
  53. // fever calls the Fever API with the API key of the user.
  54. fever *feverClient
  55. // rest calls the REST API, to set up data and to check changes independently.
  56. rest *miniflux.Client
  57. }
  58. // newTestUser creates a user with the Fever API enabled that is removed when the test ends.
  59. func newTestUser(t *testing.T) *testUser {
  60. t.Helper()
  61. server := requireTestServer(t)
  62. user, err := server.Store.CreateUser(&model.UserCreationRequest{
  63. Username: fmt.Sprintf("fever_test_user_%d", rand.Int()),
  64. Password: testUserPassword,
  65. })
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. t.Cleanup(func() {
  70. if err := server.Store.RemoveUser(user.ID); err != nil {
  71. t.Errorf(`Unable to remove user %d: %v`, user.ID, err)
  72. }
  73. })
  74. apiKey := setFeverIntegration(t, user.ID, true, testFeverPassword)
  75. return &testUser{
  76. id: user.ID,
  77. username: user.Username,
  78. fever: &feverClient{baseURL: server.URL, apiKey: apiKey},
  79. rest: miniflux.NewClient(server.URL, user.Username, testUserPassword),
  80. }
  81. }
  82. // setFeverIntegration changes the Fever settings of the user, using the Miniflux username,
  83. // and returns the API key. No API can change these settings, so they are written to the store.
  84. func setFeverIntegration(t *testing.T, userID int64, enabled bool, password string) string {
  85. t.Helper()
  86. server := requireTestServer(t)
  87. integration, err := server.Store.Integration(userID)
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. user, err := server.Store.UserByID(userID)
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. apiKey := feverAPIKey(user.Username, password)
  96. integration.FeverEnabled = enabled
  97. integration.FeverUsername = user.Username
  98. integration.FeverToken = apiKey
  99. if err := server.Store.UpdateIntegration(integration); err != nil {
  100. t.Fatal(err)
  101. }
  102. return apiKey
  103. }
  104. // feverAPIKey returns the API key that Fever clients send: the MD5 hash of "username:password",
  105. // which is also the token that the integrations page stores.
  106. func feverAPIKey(username, password string) string {
  107. sum := md5.Sum([]byte(username + ":" + password))
  108. return hex.EncodeToString(sum[:])
  109. }
  110. // subscribeToFixture subscribes the user to a new fixture feed and returns the feed ID with its entries.
  111. func subscribeToFixture(t *testing.T, user *testUser) (int64, miniflux.Entries) {
  112. t.Helper()
  113. feedID := servertest.NewFeedFixture(t).Subscribe(t, user.rest)
  114. return feedID, feedEntries(t, user, feedID)
  115. }
  116. // feedEntries returns the entries of the feed oldest first: entry 1 is published on 2025-01-01, entry 3 on 2025-01-03.
  117. func feedEntries(t *testing.T, user *testUser, feedID int64) miniflux.Entries {
  118. t.Helper()
  119. result, err := user.rest.FeedEntries(feedID, &miniflux.Filter{Order: "published_at", Direction: "asc"})
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. if len(result.Entries) != servertest.EntryCount {
  124. t.Fatalf(`Expected %d entries, got %d`, servertest.EntryCount, len(result.Entries))
  125. }
  126. return result.Entries
  127. }