4
0

integration_server_test.go 3.4 KB

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