mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-17 22:14:26 +01:00
GreenField: Get Stores
This commit is contained in:
16
BTCPayServer.Client/BTCPayServerClient.Stores.cs
Normal file
16
BTCPayServer.Client/BTCPayServerClient.Stores.cs
Normal file
@@ -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<IEnumerable<StoreData>> GetStores(CancellationToken token = default)
|
||||
{
|
||||
var response = await _httpClient.SendAsync(CreateHttpRequest("api/v1/api-keys/stores"), token);
|
||||
return await HandleResponse<IEnumerable<StoreData>>(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
BTCPayServer.Client/Models/StoreData.cs
Normal file
14
BTCPayServer.Client/Models/StoreData.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace BTCPayServer.Client.Models
|
||||
{
|
||||
public class StoreData
|
||||
{
|
||||
/// <summary>
|
||||
/// the id of the store
|
||||
/// </summary>
|
||||
public string Id { get; set; }
|
||||
/// <summary>
|
||||
/// the name of the store
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ using Newtonsoft.Json;
|
||||
using OpenQA.Selenium;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
using StoreData = BTCPayServer.Data.StoreData;
|
||||
|
||||
namespace BTCPayServer.Tests
|
||||
{
|
||||
|
||||
@@ -157,6 +157,24 @@ namespace BTCPayServer.Tests
|
||||
}
|
||||
}
|
||||
|
||||
[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<Task> act)
|
||||
{
|
||||
var ex = await Assert.ThrowsAsync<HttpRequestException>(act);
|
||||
|
||||
35
BTCPayServer/Controllers/RestApi/StoresController.cs
Normal file
35
BTCPayServer/Controllers/RestApi/StoresController.cs
Normal file
@@ -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<IEnumerable<Client.Models.StoreData>> 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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user