4
0

integration_server_test.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package api_test
  4. import (
  5. "fmt"
  6. "math"
  7. "math/rand/v2"
  8. "net/http"
  9. "testing"
  10. miniflux "miniflux.app/v2/client"
  11. "miniflux.app/v2/internal/http/server/servertest"
  12. "miniflux.app/v2/internal/model"
  13. )
  14. const (
  15. adminUsername = "admin"
  16. adminPassword = "test123"
  17. // regularUserPassword is the password of the users created by newRegularUser.
  18. regularUserPassword = "regular_test_user_password"
  19. // nonexistentID is an ID that no category, entry, enclosure, feed or icon has.
  20. // It fits the integer columns of PostgreSQL, so that lookups reach the "not found" path.
  21. nonexistentID int64 = math.MaxInt32
  22. )
  23. func TestMain(m *testing.M) {
  24. servertest.Main(m, createAdminUser)
  25. }
  26. // createAdminUser creates the admin user that the tests share, unless an earlier run left it in the database.
  27. func createAdminUser(server *servertest.Server) error {
  28. if server.Store.UserExists(adminUsername) {
  29. return nil
  30. }
  31. _, err := server.Store.CreateUser(&model.UserCreationRequest{
  32. Username: adminUsername,
  33. Password: adminPassword,
  34. IsAdmin: true,
  35. })
  36. return err
  37. }
  38. func randomUsername() string {
  39. return fmt.Sprintf("regular_test_user_%d", rand.Int())
  40. }
  41. func newAdminClient(t *testing.T) *miniflux.Client {
  42. t.Helper()
  43. return miniflux.NewClient(servertest.Require(t).URL, adminUsername, adminPassword)
  44. }
  45. // deleteUserOnCleanup removes the user when the test and its subtests finish,
  46. // and fails the test if the user cannot be removed.
  47. func deleteUserOnCleanup(t *testing.T, userID int64) {
  48. t.Helper()
  49. client := newAdminClient(t)
  50. t.Cleanup(func() {
  51. if err := client.DeleteUser(userID); err != nil {
  52. t.Errorf(`Unable to delete user %d: %v`, userID, err)
  53. }
  54. })
  55. }
  56. // newRegularUser creates a regular user that is removed when the test ends,
  57. // and returns a client authenticated as that user.
  58. func newRegularUser(t *testing.T) (*miniflux.Client, *miniflux.User) {
  59. t.Helper()
  60. user, err := newAdminClient(t).CreateUser(randomUsername(), regularUserPassword, false)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. deleteUserOnCleanup(t, user.ID)
  65. return miniflux.NewClient(servertest.Require(t).URL, user.Username, regularUserPassword), user
  66. }
  67. // requestStatus sends an API request authenticated as the regular user and returns the status code.
  68. // It covers routes that the client does not use, such as aliases and alternative methods.
  69. func requestStatus(t *testing.T, user *miniflux.User, method, path string) int {
  70. t.Helper()
  71. request, err := http.NewRequest(method, servertest.Require(t).URL+path, nil)
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. request.SetBasicAuth(user.Username, regularUserPassword)
  76. status, _ := servertest.Send(t, request)
  77. return status
  78. }