api_key_integration_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package api_test
  4. import (
  5. "errors"
  6. "testing"
  7. miniflux "miniflux.app/v2/client"
  8. "miniflux.app/v2/internal/http/server/servertest"
  9. )
  10. func TestAPIKeysEndpoint(t *testing.T) {
  11. t.Parallel()
  12. regularUserClient, regularTestUser := newRegularUser(t)
  13. apiKeys, err := regularUserClient.APIKeys()
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. if len(apiKeys) != 0 {
  18. t.Errorf(`Expected no API keys, got %d`, len(apiKeys))
  19. }
  20. // Create an API key for the user.
  21. apiKey, err := regularUserClient.CreateAPIKey("Test API Key")
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. if apiKey.ID == 0 {
  26. t.Errorf(`Unexpected API key ID: got %d, want nonzero`, apiKey.ID)
  27. }
  28. if apiKey.UserID != regularTestUser.ID {
  29. t.Errorf(`Unexpected API key user ID: got %d, want %d`, apiKey.UserID, regularTestUser.ID)
  30. }
  31. if apiKey.Token == "" {
  32. t.Fatal(`Expected an API key token, got none`)
  33. }
  34. if apiKey.Description != "Test API Key" {
  35. t.Errorf(`Unexpected API key description: got %q, want %q`, apiKey.Description, "Test API Key")
  36. }
  37. // Create a duplicate API key with the same description.
  38. if _, err := regularUserClient.CreateAPIKey("Test API Key"); !errors.Is(err, miniflux.ErrBadRequest) {
  39. t.Errorf(`Creating a duplicate API key: got error %v, want %v`, err, miniflux.ErrBadRequest)
  40. }
  41. // Fetch the API keys again.
  42. apiKeys, err = regularUserClient.APIKeys()
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. if len(apiKeys) != 1 {
  47. t.Fatalf(`Unexpected number of API keys: got %d, want 1`, len(apiKeys))
  48. }
  49. if apiKeys[0].ID != apiKey.ID {
  50. t.Errorf(`Unexpected API key ID: got %d, want %d`, apiKeys[0].ID, apiKey.ID)
  51. }
  52. if apiKeys[0].UserID != regularTestUser.ID {
  53. t.Errorf(`Unexpected API key user ID: got %d, want %d`, apiKeys[0].UserID, regularTestUser.ID)
  54. }
  55. if apiKeys[0].Token != "" {
  56. t.Errorf(`Unexpected API key token in the list: got %q, want ""`, apiKeys[0].Token)
  57. }
  58. if apiKeys[0].Description != "Test API Key" {
  59. t.Errorf(`Unexpected API key description: got %q, want %q`, apiKeys[0].Description, "Test API Key")
  60. }
  61. // Create a new client using the API key.
  62. apiKeyClient := miniflux.NewClient(servertest.Require(t).URL, apiKey.Token)
  63. // Fetch the user using the API key client.
  64. user, err := apiKeyClient.Me()
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. // Verify the user matches the regular test user.
  69. if user.ID != regularTestUser.ID {
  70. t.Errorf(`Unexpected user ID: got %d, want %d`, user.ID, regularTestUser.ID)
  71. }
  72. // Delete the API key.
  73. if err := regularUserClient.DeleteAPIKey(apiKey.ID); err != nil {
  74. t.Fatal(err)
  75. }
  76. // Verify the API key is deleted.
  77. apiKeys, err = regularUserClient.APIKeys()
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. if len(apiKeys) != 0 {
  82. t.Errorf(`Expected no API keys after deletion, got %d`, len(apiKeys))
  83. }
  84. // Try to delete the API key again, it should return an error.
  85. err = regularUserClient.DeleteAPIKey(apiKey.ID)
  86. if !errors.Is(err, miniflux.ErrNotFound) {
  87. t.Errorf(`Deleting a deleted API key: got error %v, want %v`, err, miniflux.ErrNotFound)
  88. }
  89. // Try to create an API key with an empty description.
  90. if _, err := regularUserClient.CreateAPIKey(""); !errors.Is(err, miniflux.ErrBadRequest) {
  91. t.Errorf(`Creating an API key with an empty description: got error %v, want %v`, err, miniflux.ErrBadRequest)
  92. }
  93. }