GreenField: Get Stores

This commit is contained in:
Kukks
2020-03-24 16:18:43 +01:00
parent 2710130667
commit deb197cfa5
5 changed files with 85 additions and 1 deletions

View 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);
}
}
}

View 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; }
}
}

View File

@@ -14,6 +14,7 @@ using Newtonsoft.Json;
using OpenQA.Selenium;
using Xunit;
using Xunit.Abstractions;
using StoreData = BTCPayServer.Data.StoreData;
namespace BTCPayServer.Tests
{

View File

@@ -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<Task> act)
{
var ex = await Assert.ThrowsAsync<HttpRequestException>(act);

View 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
};
}
}
}