integration_server_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package googlereader_test
  4. import (
  5. "net/url"
  6. "testing"
  7. miniflux "miniflux.app/v2/client"
  8. "miniflux.app/v2/internal/crypto"
  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. // testGoogleReaderPassword is the Google Reader password of the test users, used by ClientLogin.
  15. testGoogleReaderPassword = "googlereader_test_password"
  16. )
  17. func TestMain(m *testing.M) {
  18. servertest.Main(m, nil)
  19. }
  20. // testUser is a Miniflux user with the Google Reader API enabled.
  21. type testUser struct {
  22. id int64
  23. username string
  24. // reader calls the Google Reader API with the token returned by ClientLogin.
  25. reader *googleReaderClient
  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 Google Reader 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. setGoogleReaderIntegration(t, user.ID, true, testGoogleReaderPassword)
  35. return &testUser{
  36. id: user.ID,
  37. username: user.Username,
  38. reader: &googleReaderClient{baseURL: server.URL, token: clientLogin(t, user.Username, testGoogleReaderPassword)},
  39. rest: miniflux.NewClient(server.URL, user.Username, testUserPassword),
  40. }
  41. }
  42. // setGoogleReaderIntegration changes the Google Reader settings of the user, using the Miniflux username.
  43. func setGoogleReaderIntegration(t *testing.T, userID int64, enabled bool, password string) {
  44. t.Helper()
  45. server := servertest.Require(t)
  46. integration, err := server.Store.Integration(userID)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. user, err := server.Store.UserByID(userID)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. passwordHash, err := crypto.HashPassword(password)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. integration.GoogleReaderEnabled = enabled
  59. integration.GoogleReaderUsername = user.Username
  60. integration.GoogleReaderPassword = passwordHash
  61. if err := server.Store.UpdateIntegration(integration); err != nil {
  62. t.Fatal(err)
  63. }
  64. }
  65. // clientLogin authenticates with ClientLogin and returns the auth token.
  66. func clientLogin(t *testing.T, username, password string) string {
  67. t.Helper()
  68. var result loginResult
  69. anonymous := &googleReaderClient{baseURL: servertest.Require(t).URL}
  70. anonymous.postJSON(t, "/accounts/ClientLogin", url.Values{
  71. "Email": {username},
  72. "Passwd": {password},
  73. }, &result)
  74. if result.Auth == "" {
  75. t.Fatalf(`ClientLogin returned an empty token: %+v`, result)
  76. }
  77. return result.Auth
  78. }
  79. // subscribeToFixture subscribes the user to a new fixture feed and returns the feed ID with its entries.
  80. func subscribeToFixture(t *testing.T, user *testUser) (int64, miniflux.Entries) {
  81. t.Helper()
  82. feedID := servertest.NewFeedFixture(t).Subscribe(t, user.rest)
  83. return feedID, servertest.FeedEntries(t, user.rest, feedID)
  84. }