diff --git a/BTCPayServer.Client/BTCPayServerClient.Stores.cs b/BTCPayServer.Client/BTCPayServerClient.Stores.cs new file mode 100644 index 000000000..e39e53c45 --- /dev/null +++ b/BTCPayServer.Client/BTCPayServerClient.Stores.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using BTCPayServer.Client.Models; + +namespace BTCPayServer.Client +{ + public partial class BTCPayServerClient + { + public virtual async Task> GetStores(CancellationToken token = default) + { + var response = await _httpClient.SendAsync(CreateHttpRequest("api/v1/api-keys/stores"), token); + return await HandleResponse>(response); + } + } +} diff --git a/BTCPayServer.Client/Models/StoreData.cs b/BTCPayServer.Client/Models/StoreData.cs new file mode 100644 index 000000000..886e4a38f --- /dev/null +++ b/BTCPayServer.Client/Models/StoreData.cs @@ -0,0 +1,14 @@ +namespace BTCPayServer.Client.Models +{ + public class StoreData + { + /// + /// the id of the store + /// + public string Id { get; set; } + /// + /// the name of the store + /// + public string Name { get; set; } + } +} diff --git a/BTCPayServer.Tests/ApiKeysTests.cs b/BTCPayServer.Tests/ApiKeysTests.cs index 342952332..9f35b8aff 100644 --- a/BTCPayServer.Tests/ApiKeysTests.cs +++ b/BTCPayServer.Tests/ApiKeysTests.cs @@ -14,6 +14,7 @@ using Newtonsoft.Json; using OpenQA.Selenium; using Xunit; using Xunit.Abstractions; +using StoreData = BTCPayServer.Data.StoreData; namespace BTCPayServer.Tests { diff --git a/BTCPayServer.Tests/GreenfieldAPITests.cs b/BTCPayServer.Tests/GreenfieldAPITests.cs index d0b9dbeb7..e27491696 100644 --- a/BTCPayServer.Tests/GreenfieldAPITests.cs +++ b/BTCPayServer.Tests/GreenfieldAPITests.cs @@ -156,7 +156,25 @@ namespace BTCPayServer.Tests await AssertHttpError(403, async () => await user1Client.CreateUser(new CreateApplicationUserRequest() { Email = "admin8@gmail.com", Password = "afewfoiewiou", IsAdministrator = true })); } } - + + [Fact(Timeout = TestTimeout)] + [Trait("Integration", "Integration")] + public async Task StoresControllerTests() + { + using (var tester = ServerTester.Create()) + { + await tester.StartAsync(); + var user = tester.NewAccount(); + user.GrantAccess(); + await user.MakeAdmin(); + var client = await user.CreateClient(Policies.Unrestricted); + var stores = await client.GetStores(); + Assert.NotNull(stores); + Assert.Single(stores); + Assert.Equal(user.StoreId,stores.First().Id); + } + } + private async Task AssertHttpError(int code, Func act) { var ex = await Assert.ThrowsAsync(act); diff --git a/BTCPayServer/Controllers/RestApi/StoresController.cs b/BTCPayServer/Controllers/RestApi/StoresController.cs new file mode 100644 index 000000000..df7ea1c4a --- /dev/null +++ b/BTCPayServer/Controllers/RestApi/StoresController.cs @@ -0,0 +1,35 @@ +using System.Collections.Generic; +using System.Linq; +using BTCPayServer.Security; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using BTCPayServer.Client; + +namespace BTCPayServer.Controllers.RestApi +{ + [ApiController] + [Authorize(AuthenticationSchemes = AuthenticationSchemes.Greenfield)] + public class StoresController : ControllerBase + { + public StoresController() + { + } + + [Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)] + [HttpGet("~/api/v1/stores")] + public ActionResult> GetStores() + { + var stores = this.HttpContext.GetStoresData(); + return Ok(stores.Select(FromModel)); + } + + private static Client.Models.StoreData FromModel(Data.StoreData data) + { + return new Client.Models.StoreData() + { + Id = data.Id, + Name = data.StoreName + }; + } + } +}