integration_server_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "testing"
  8. miniflux "miniflux.app/v2/client"
  9. "miniflux.app/v2/internal/crypto"
  10. "miniflux.app/v2/internal/http/server/servertest"
  11. )
  12. const (
  13. // testUserPassword is the Miniflux password of the test users, used by the REST API.
  14. testUserPassword = "miniflux_test_password"
  15. // testFeverPassword is the Fever password of the test users, hashed into their API key.
  16. testFeverPassword = "fever_test_password"
  17. )
  18. func TestMain(m *testing.M) {
  19. servertest.Main(m, nil)
  20. }
  21. // testUser is a Miniflux user with the Fever API enabled.
  22. type testUser struct {
  23. id int64
  24. username string
  25. // fever calls the Fever API with the API key of the user.
  26. fever *feverClient
  27. // rest calls the REST API, to set up data and to check changes independently.
  28. rest *miniflux.Client
  29. }
  30. // newTestUser creates a user with the Fever API enabled that is removed when the test ends.
  31. func newTestUser(t *testing.T) *testUser {
  32. t.Helper()
  33. server := servertest.Require(t)
  34. user := server.NewUser(t, testUserPassword)
  35. apiKey := setFeverIntegration(t, user.ID, true, testFeverPassword)
  36. return &testUser{
  37. id: user.ID,
  38. username: user.Username,
  39. fever: &feverClient{baseURL: server.URL, apiKey: apiKey},
  40. rest: miniflux.NewClient(server.URL, user.Username, testUserPassword),
  41. }
  42. }
  43. // setFeverIntegration changes the Fever settings of the user, using the Miniflux username,
  44. // and returns the API key. No API can change these settings, so they are written to the store.
  45. func setFeverIntegration(t *testing.T, userID int64, enabled bool, password string) string {
  46. t.Helper()
  47. server := servertest.Require(t)
  48. integration, err := server.Store.Integration(userID)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. user, err := server.Store.UserByID(userID)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. apiKey := feverAPIKey(user.Username, password)
  57. integration.FeverEnabled = enabled
  58. integration.FeverUsername = user.Username
  59. integration.FeverTokenHash = crypto.HashToken(apiKey)
  60. if err := server.Store.UpdateIntegration(integration); err != nil {
  61. t.Fatal(err)
  62. }
  63. return apiKey
  64. }
  65. // feverAPIKey returns the API key that Fever clients send: the MD5 hash of "username:password",
  66. // whose SHA-256 digest the integrations page stores.
  67. func feverAPIKey(username, password string) string {
  68. sum := md5.Sum([]byte(username + ":" + password))
  69. return hex.EncodeToString(sum[:])
  70. }
  71. // subscribeToFixture subscribes the user to a new fixture feed and returns the feed ID with its entries.
  72. func subscribeToFixture(t *testing.T, user *testUser) (int64, miniflux.Entries) {
  73. t.Helper()
  74. feedID := servertest.NewFeedFixture(t).Subscribe(t, user.rest)
  75. return feedID, servertest.FeedEntries(t, user.rest, feedID)
  76. }
  77. // createCategory creates a category, which the Fever API calls a group.
  78. func createCategory(t *testing.T, user *testUser, title string) *miniflux.Category {
  79. t.Helper()
  80. category, err := user.rest.CreateCategory(title)
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. return category
  85. }
  86. // subscribeInCategory subscribes the user to a new fixture feed in the category and returns the feed ID.
  87. func subscribeInCategory(t *testing.T, user *testUser, categoryID int64) int64 {
  88. t.Helper()
  89. feedID, err := user.rest.CreateFeed(&miniflux.FeedCreationRequest{
  90. FeedURL: servertest.NewFeedFixture(t).FeedURL,
  91. CategoryID: categoryID,
  92. })
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. return feedID
  97. }