// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package api_test import ( "errors" "testing" miniflux "miniflux.app/v2/client" "miniflux.app/v2/internal/http/server/servertest" ) func TestAPIKeysEndpoint(t *testing.T) { t.Parallel() regularUserClient, regularTestUser := newRegularUser(t) apiKeys, err := regularUserClient.APIKeys() if err != nil { t.Fatal(err) } if len(apiKeys) != 0 { t.Errorf(`Expected no API keys, got %d`, len(apiKeys)) } // Create an API key for the user. apiKey, err := regularUserClient.CreateAPIKey("Test API Key") if err != nil { t.Fatal(err) } if apiKey.ID == 0 { t.Errorf(`Unexpected API key ID: got %d, want nonzero`, apiKey.ID) } if apiKey.UserID != regularTestUser.ID { t.Errorf(`Unexpected API key user ID: got %d, want %d`, apiKey.UserID, regularTestUser.ID) } if apiKey.Token == "" { t.Fatal(`Expected an API key token, got none`) } if apiKey.Description != "Test API Key" { t.Errorf(`Unexpected API key description: got %q, want %q`, apiKey.Description, "Test API Key") } // Create a duplicate API key with the same description. if _, err := regularUserClient.CreateAPIKey("Test API Key"); !errors.Is(err, miniflux.ErrBadRequest) { t.Errorf(`Creating a duplicate API key: got error %v, want %v`, err, miniflux.ErrBadRequest) } // Fetch the API keys again. apiKeys, err = regularUserClient.APIKeys() if err != nil { t.Fatal(err) } if len(apiKeys) != 1 { t.Fatalf(`Unexpected number of API keys: got %d, want 1`, len(apiKeys)) } if apiKeys[0].ID != apiKey.ID { t.Errorf(`Unexpected API key ID: got %d, want %d`, apiKeys[0].ID, apiKey.ID) } if apiKeys[0].UserID != regularTestUser.ID { t.Errorf(`Unexpected API key user ID: got %d, want %d`, apiKeys[0].UserID, regularTestUser.ID) } if apiKeys[0].Token != "" { t.Errorf(`Unexpected API key token in the list: got %q, want ""`, apiKeys[0].Token) } if apiKeys[0].Description != "Test API Key" { t.Errorf(`Unexpected API key description: got %q, want %q`, apiKeys[0].Description, "Test API Key") } // Create a new client using the API key. apiKeyClient := miniflux.NewClient(servertest.Require(t).URL, apiKey.Token) // Fetch the user using the API key client. user, err := apiKeyClient.Me() if err != nil { t.Fatal(err) } // Verify the user matches the regular test user. if user.ID != regularTestUser.ID { t.Errorf(`Unexpected user ID: got %d, want %d`, user.ID, regularTestUser.ID) } // Delete the API key. if err := regularUserClient.DeleteAPIKey(apiKey.ID); err != nil { t.Fatal(err) } // Verify the API key is deleted. apiKeys, err = regularUserClient.APIKeys() if err != nil { t.Fatal(err) } if len(apiKeys) != 0 { t.Errorf(`Expected no API keys after deletion, got %d`, len(apiKeys)) } // Try to delete the API key again, it should return an error. err = regularUserClient.DeleteAPIKey(apiKey.ID) if !errors.Is(err, miniflux.ErrNotFound) { t.Errorf(`Deleting a deleted API key: got error %v, want %v`, err, miniflux.ErrNotFound) } // Try to create an API key with an empty description. if _, err := regularUserClient.CreateAPIKey(""); !errors.Is(err, miniflux.ErrBadRequest) { t.Errorf(`Creating an API key with an empty description: got error %v, want %v`, err, miniflux.ErrBadRequest) } }