4
0

api_key_integration_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. )
  9. func TestAPIKeysEndpoint(t *testing.T) {
  10. t.Parallel()
  11. testConfig := requireIntegrationTestConfig(t)
  12. regularUserClient, regularTestUser := testConfig.newRegularUser(t)
  13. apiKeys, err := regularUserClient.APIKeys()
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. if len(apiKeys) != 0 {
  18. t.Fatalf(`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.Fatalf(`Invalid API key ID, got "%v"`, apiKey.ID)
  27. }
  28. if apiKey.UserID != regularTestUser.ID {
  29. t.Fatalf(`Invalid user ID for API key, got "%v" instead of "%v"`, apiKey.UserID, regularTestUser.ID)
  30. }
  31. if apiKey.Token == "" {
  32. t.Fatalf(`Invalid API key token, got "%v"`, apiKey.Token)
  33. }
  34. if apiKey.Description != "Test API Key" {
  35. t.Fatalf(`Invalid API key description, got "%v" instead of "Test API Key"`, apiKey.Description)
  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.Fatalf(`Creating a duplicate API key with the same description should raise an error, got %v`, err)
  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(`Expected 1 API key, got %d`, len(apiKeys))
  48. }
  49. if apiKeys[0].ID != apiKey.ID {
  50. t.Fatalf(`Invalid API key ID, got "%v" instead of "%v"`, apiKeys[0].ID, apiKey.ID)
  51. }
  52. if apiKeys[0].UserID != regularTestUser.ID {
  53. t.Fatalf(`Invalid user ID for API key, got "%v" instead of "%v"`, apiKeys[0].UserID, regularTestUser.ID)
  54. }
  55. if apiKeys[0].Token != apiKey.Token {
  56. t.Fatalf(`Invalid API key token, got "%v" instead of "%v"`, apiKeys[0].Token, apiKey.Token)
  57. }
  58. if apiKeys[0].Description != "Test API Key" {
  59. t.Fatalf(`Invalid API key description, got "%v" instead of "Test API Key"`, apiKeys[0].Description)
  60. }
  61. // Create a new client using the API key.
  62. apiKeyClient := miniflux.NewClient(testConfig.testBaseURL, 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.Fatalf(`Expected user ID %d, got %d`, regularTestUser.ID, user.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.Fatalf(`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.Fatalf(`Deleting a non-existent API key should raise a "not found" error, got %v`, err)
  88. }
  89. // Try to create an API key with an empty description.
  90. if _, err := regularUserClient.CreateAPIKey(""); !errors.Is(err, miniflux.ErrBadRequest) {
  91. t.Fatalf(`Creating an API key with an empty description should raise an error, got %v`, err)
  92. }
  93. }