2020-03-13 11:47:22 +01:00
|
|
|
using System;
|
2020-05-19 19:59:23 +02:00
|
|
|
using System.IO;
|
2020-03-12 14:59:24 +01:00
|
|
|
using System.Linq;
|
2020-02-24 18:43:28 +01:00
|
|
|
using System.Net.Http;
|
|
|
|
using System.Threading.Tasks;
|
2020-03-02 16:50:28 +01:00
|
|
|
using BTCPayServer.Client;
|
2020-03-13 11:47:22 +01:00
|
|
|
using BTCPayServer.Client.Models;
|
2020-05-19 19:59:23 +02:00
|
|
|
using BTCPayServer.JsonConverters;
|
2020-03-18 15:10:15 +01:00
|
|
|
using BTCPayServer.Services;
|
2020-02-24 18:43:28 +01:00
|
|
|
using BTCPayServer.Tests.Logging;
|
2020-05-19 19:59:23 +02:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json.Linq;
|
2020-02-24 18:43:28 +01:00
|
|
|
using Xunit;
|
|
|
|
using Xunit.Abstractions;
|
2020-03-13 11:47:22 +01:00
|
|
|
using CreateApplicationUserRequest = BTCPayServer.Client.Models.CreateApplicationUserRequest;
|
2020-05-19 19:59:23 +02:00
|
|
|
using JsonReader = Newtonsoft.Json.JsonReader;
|
2020-02-24 18:43:28 +01:00
|
|
|
|
|
|
|
namespace BTCPayServer.Tests
|
|
|
|
{
|
|
|
|
public class GreenfieldAPITests
|
|
|
|
{
|
|
|
|
public const int TestTimeout = TestUtils.TestTimeout;
|
|
|
|
|
|
|
|
public const string TestApiPath = "api/test/apikey";
|
|
|
|
|
|
|
|
public GreenfieldAPITests(ITestOutputHelper helper)
|
|
|
|
{
|
2020-05-19 19:59:23 +02:00
|
|
|
Logs.Tester = new XUnitLog(helper) {Name = "Tests"};
|
2020-02-24 18:43:28 +01:00
|
|
|
Logs.LogProvider = new XUnitLogProvider(helper);
|
|
|
|
}
|
|
|
|
|
2020-03-12 14:59:24 +01:00
|
|
|
[Fact(Timeout = TestTimeout)]
|
2020-02-24 18:43:28 +01:00
|
|
|
[Trait("Integration", "Integration")]
|
|
|
|
public async Task ApiKeysControllerTests()
|
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
|
|
|
await tester.StartAsync();
|
|
|
|
var user = tester.NewAccount();
|
|
|
|
user.GrantAccess();
|
|
|
|
await user.MakeAdmin();
|
2020-03-27 06:46:51 +01:00
|
|
|
var client = await user.CreateClient(Policies.CanViewProfile);
|
2020-03-20 17:14:47 +01:00
|
|
|
var clientBasic = await user.CreateClient();
|
2020-02-24 18:43:28 +01:00
|
|
|
//Get current api key
|
2020-03-02 16:50:28 +01:00
|
|
|
var apiKeyData = await client.GetCurrentAPIKeyInfo();
|
2020-02-24 18:43:28 +01:00
|
|
|
Assert.NotNull(apiKeyData);
|
2020-03-16 08:36:55 +01:00
|
|
|
Assert.Equal(client.APIKey, apiKeyData.ApiKey);
|
2020-03-20 12:25:10 +01:00
|
|
|
Assert.Single(apiKeyData.Permissions);
|
2020-05-19 19:59:23 +02:00
|
|
|
|
2020-03-20 17:14:47 +01:00
|
|
|
//a client using Basic Auth has no business here
|
2020-03-27 05:13:40 +01:00
|
|
|
await AssertHttpError(401, async () => await clientBasic.GetCurrentAPIKeyInfo());
|
2020-05-19 19:59:23 +02:00
|
|
|
|
2020-03-02 16:50:28 +01:00
|
|
|
//revoke current api key
|
|
|
|
await client.RevokeCurrentAPIKeyInfo();
|
2020-03-20 12:25:10 +01:00
|
|
|
await AssertHttpError(401, async () => await client.GetCurrentAPIKeyInfo());
|
2020-03-20 17:14:47 +01:00
|
|
|
//a client using Basic Auth has no business here
|
2020-03-27 05:13:40 +01:00
|
|
|
await AssertHttpError(401, async () => await clientBasic.RevokeCurrentAPIKeyInfo());
|
2020-02-24 18:43:28 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 19:59:23 +02:00
|
|
|
|
2020-03-27 06:17:31 +01:00
|
|
|
[Fact(Timeout = TestTimeout)]
|
|
|
|
[Trait("Integration", "Integration")]
|
2020-03-27 06:46:51 +01:00
|
|
|
public async Task CanCreateAndDeleteAPIKeyViaAPI()
|
2020-03-27 06:17:31 +01:00
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
|
|
|
await tester.StartAsync();
|
|
|
|
var acc = tester.NewAccount();
|
|
|
|
await acc.GrantAccessAsync();
|
|
|
|
var unrestricted = await acc.CreateClient();
|
|
|
|
var apiKey = await unrestricted.CreateAPIKey(new CreateApiKeyRequest()
|
|
|
|
{
|
|
|
|
Label = "Hello world",
|
2020-05-19 19:59:23 +02:00
|
|
|
Permissions = new Permission[] {Permission.Create(Policies.CanViewProfile)}
|
2020-03-27 06:17:31 +01:00
|
|
|
});
|
|
|
|
Assert.Equal("Hello world", apiKey.Label);
|
|
|
|
var p = Assert.Single(apiKey.Permissions);
|
|
|
|
Assert.Equal(Policies.CanViewProfile, p.Policy);
|
|
|
|
|
|
|
|
var restricted = acc.CreateClientFromAPIKey(apiKey.ApiKey);
|
2020-05-19 19:59:23 +02:00
|
|
|
await AssertHttpError(403,
|
|
|
|
async () => await restricted.CreateAPIKey(new CreateApiKeyRequest()
|
|
|
|
{
|
|
|
|
Label = "Hello world2",
|
|
|
|
Permissions = new Permission[] {Permission.Create(Policies.CanViewProfile)}
|
|
|
|
}));
|
2020-03-27 06:46:51 +01:00
|
|
|
|
|
|
|
await unrestricted.RevokeAPIKey(apiKey.ApiKey);
|
|
|
|
await AssertHttpError(404, async () => await unrestricted.RevokeAPIKey(apiKey.ApiKey));
|
2020-03-27 06:17:31 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-18 15:10:15 +01:00
|
|
|
|
|
|
|
[Fact(Timeout = TestTimeout)]
|
|
|
|
[Trait("Integration", "Integration")]
|
|
|
|
public async Task CanCreateUsersViaAPI()
|
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create(newDb: true))
|
|
|
|
{
|
2020-03-19 05:30:53 +01:00
|
|
|
tester.PayTester.DisableRegistration = true;
|
2020-03-18 15:10:15 +01:00
|
|
|
await tester.StartAsync();
|
|
|
|
var unauthClient = new BTCPayServerClient(tester.PayTester.ServerUri);
|
2020-05-19 19:59:23 +02:00
|
|
|
await AssertHttpError(400,
|
|
|
|
async () => await unauthClient.CreateUser(new CreateApplicationUserRequest()));
|
|
|
|
await AssertHttpError(400,
|
|
|
|
async () => await unauthClient.CreateUser(
|
|
|
|
new CreateApplicationUserRequest() {Email = "test@gmail.com"}));
|
2020-03-18 15:10:15 +01:00
|
|
|
// Pass too simple
|
2020-05-19 19:59:23 +02:00
|
|
|
await AssertHttpError(400,
|
|
|
|
async () => await unauthClient.CreateUser(
|
|
|
|
new CreateApplicationUserRequest() {Email = "test3@gmail.com", Password = "a"}));
|
2020-03-18 15:10:15 +01:00
|
|
|
|
|
|
|
// We have no admin, so it should work
|
2020-05-19 19:59:23 +02:00
|
|
|
var user1 = await unauthClient.CreateUser(
|
|
|
|
new CreateApplicationUserRequest() {Email = "test@gmail.com", Password = "abceudhqw"});
|
2020-03-18 15:10:15 +01:00
|
|
|
// We have no admin, so it should work
|
2020-05-19 19:59:23 +02:00
|
|
|
var user2 = await unauthClient.CreateUser(
|
|
|
|
new CreateApplicationUserRequest() {Email = "test2@gmail.com", Password = "abceudhqw"});
|
2020-03-18 15:10:15 +01:00
|
|
|
|
|
|
|
// Duplicate email
|
2020-05-19 19:59:23 +02:00
|
|
|
await AssertHttpError(400,
|
|
|
|
async () => await unauthClient.CreateUser(
|
|
|
|
new CreateApplicationUserRequest() {Email = "test2@gmail.com", Password = "abceudhqw"}));
|
2020-03-18 15:10:15 +01:00
|
|
|
|
|
|
|
// Let's make an admin
|
2020-05-19 19:59:23 +02:00
|
|
|
var admin = await unauthClient.CreateUser(new CreateApplicationUserRequest()
|
|
|
|
{
|
|
|
|
Email = "admin@gmail.com", Password = "abceudhqw", IsAdministrator = true
|
|
|
|
});
|
2020-03-18 15:10:15 +01:00
|
|
|
|
|
|
|
// Creating a new user without proper creds is now impossible (unauthorized)
|
|
|
|
// Because if registration are locked and that an admin exists, we don't accept unauthenticated connection
|
2020-05-19 19:59:23 +02:00
|
|
|
await AssertHttpError(401,
|
|
|
|
async () => await unauthClient.CreateUser(
|
|
|
|
new CreateApplicationUserRequest() {Email = "test3@gmail.com", Password = "afewfoiewiou"}));
|
2020-03-18 15:10:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
// But should be ok with subscriptions unlocked
|
|
|
|
var settings = tester.PayTester.GetService<SettingsRepository>();
|
2020-05-19 19:59:23 +02:00
|
|
|
await settings.UpdateSetting<PoliciesSettings>(new PoliciesSettings() {LockSubscription = false});
|
|
|
|
await unauthClient.CreateUser(
|
|
|
|
new CreateApplicationUserRequest() {Email = "test3@gmail.com", Password = "afewfoiewiou"});
|
2020-03-18 15:10:15 +01:00
|
|
|
|
|
|
|
// But it should be forbidden to create an admin without being authenticated
|
2020-05-19 19:59:23 +02:00
|
|
|
await AssertHttpError(403,
|
|
|
|
async () => await unauthClient.CreateUser(new CreateApplicationUserRequest()
|
|
|
|
{
|
|
|
|
Email = "admin2@gmail.com", Password = "afewfoiewiou", IsAdministrator = true
|
|
|
|
}));
|
|
|
|
await settings.UpdateSetting<PoliciesSettings>(new PoliciesSettings() {LockSubscription = true});
|
2020-03-18 15:10:15 +01:00
|
|
|
|
|
|
|
var adminAcc = tester.NewAccount();
|
|
|
|
adminAcc.UserId = admin.Id;
|
|
|
|
adminAcc.IsAdmin = true;
|
2020-03-20 05:41:47 +01:00
|
|
|
var adminClient = await adminAcc.CreateClient(Policies.CanModifyProfile);
|
2020-03-18 15:10:15 +01:00
|
|
|
|
|
|
|
// We should be forbidden to create a new user without proper admin permissions
|
2020-05-19 19:59:23 +02:00
|
|
|
await AssertHttpError(403,
|
|
|
|
async () => await adminClient.CreateUser(
|
|
|
|
new CreateApplicationUserRequest() {Email = "test4@gmail.com", Password = "afewfoiewiou"}));
|
|
|
|
await AssertHttpError(403,
|
|
|
|
async () => await adminClient.CreateUser(new CreateApplicationUserRequest()
|
|
|
|
{
|
|
|
|
Email = "test4@gmail.com", Password = "afewfoiewiou", IsAdministrator = true
|
|
|
|
}));
|
2020-03-18 15:10:15 +01:00
|
|
|
|
2020-03-20 10:38:21 +01:00
|
|
|
// However, should be ok with the unrestricted permissions of an admin
|
|
|
|
adminClient = await adminAcc.CreateClient(Policies.Unrestricted);
|
2020-05-19 19:59:23 +02:00
|
|
|
await adminClient.CreateUser(
|
|
|
|
new CreateApplicationUserRequest() {Email = "test4@gmail.com", Password = "afewfoiewiou"});
|
2020-03-18 15:10:15 +01:00
|
|
|
// Even creating new admin should be ok
|
2020-05-19 19:59:23 +02:00
|
|
|
await adminClient.CreateUser(new CreateApplicationUserRequest()
|
|
|
|
{
|
|
|
|
Email = "admin4@gmail.com", Password = "afewfoiewiou", IsAdministrator = true
|
|
|
|
});
|
2020-03-18 15:10:15 +01:00
|
|
|
|
|
|
|
var user1Acc = tester.NewAccount();
|
|
|
|
user1Acc.UserId = user1.Id;
|
|
|
|
user1Acc.IsAdmin = false;
|
2020-03-20 05:41:47 +01:00
|
|
|
var user1Client = await user1Acc.CreateClient(Policies.CanModifyServerSettings);
|
2020-05-19 19:59:23 +02:00
|
|
|
|
2020-03-18 15:10:15 +01:00
|
|
|
// User1 trying to get server management would still fail to create user
|
2020-05-19 19:59:23 +02:00
|
|
|
await AssertHttpError(403,
|
|
|
|
async () => await user1Client.CreateUser(
|
|
|
|
new CreateApplicationUserRequest() {Email = "test8@gmail.com", Password = "afewfoiewiou"}));
|
2020-03-18 15:10:15 +01:00
|
|
|
|
|
|
|
// User1 should be able to create user if subscription unlocked
|
2020-05-19 19:59:23 +02:00
|
|
|
await settings.UpdateSetting<PoliciesSettings>(new PoliciesSettings() {LockSubscription = false});
|
|
|
|
await user1Client.CreateUser(
|
|
|
|
new CreateApplicationUserRequest() {Email = "test8@gmail.com", Password = "afewfoiewiou"});
|
2020-03-20 17:59:14 +01:00
|
|
|
|
2020-03-18 15:10:15 +01:00
|
|
|
// But not an admin
|
2020-05-19 19:59:23 +02:00
|
|
|
await AssertHttpError(403,
|
|
|
|
async () => await user1Client.CreateUser(new CreateApplicationUserRequest()
|
|
|
|
{
|
|
|
|
Email = "admin8@gmail.com", Password = "afewfoiewiou", IsAdministrator = true
|
|
|
|
}));
|
2020-03-18 15:10:15 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 19:59:23 +02:00
|
|
|
|
2020-03-24 16:18:43 +01:00
|
|
|
[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);
|
2020-05-19 19:59:23 +02:00
|
|
|
|
2020-04-01 08:21:54 +02:00
|
|
|
//create store
|
|
|
|
var newStore = await client.CreateStore(new CreateStoreRequest() {Name = "A"});
|
2020-05-19 19:59:23 +02:00
|
|
|
|
2020-04-27 13:13:20 +02:00
|
|
|
//update store
|
|
|
|
var updatedStore = await client.UpdateStore(newStore.Id, new UpdateStoreRequest() {Name = "B"});
|
|
|
|
Assert.Equal("B", updatedStore.Name);
|
|
|
|
Assert.Equal("B", (await client.GetStore(newStore.Id)).Name);
|
2020-05-19 19:59:23 +02:00
|
|
|
|
2020-03-31 10:47:13 +02:00
|
|
|
//list stores
|
2020-03-24 16:18:43 +01:00
|
|
|
var stores = await client.GetStores();
|
2020-04-01 08:21:54 +02:00
|
|
|
var storeIds = stores.Select(data => data.Id);
|
|
|
|
var storeNames = stores.Select(data => data.Name);
|
2020-03-24 16:18:43 +01:00
|
|
|
Assert.NotNull(stores);
|
2020-04-01 08:21:54 +02:00
|
|
|
Assert.Equal(2, stores.Count());
|
|
|
|
Assert.Contains(newStore.Id, storeIds);
|
|
|
|
Assert.Contains(user.StoreId, storeIds);
|
2020-03-31 10:47:13 +02:00
|
|
|
|
|
|
|
//get store
|
|
|
|
var store = await client.GetStore(user.StoreId);
|
2020-05-19 19:59:23 +02:00
|
|
|
Assert.Equal(user.StoreId, store.Id);
|
|
|
|
Assert.Contains(store.Name, storeNames);
|
|
|
|
|
2020-03-31 14:43:35 +02:00
|
|
|
//remove store
|
2020-04-01 08:21:54 +02:00
|
|
|
await client.RemoveStore(newStore.Id);
|
2020-03-31 14:43:35 +02:00
|
|
|
await AssertHttpError(403, async () =>
|
|
|
|
{
|
2020-04-01 08:21:54 +02:00
|
|
|
await client.GetStore(newStore.Id);
|
2020-03-31 14:43:35 +02:00
|
|
|
});
|
2020-04-01 08:21:54 +02:00
|
|
|
Assert.Single(await client.GetStores());
|
2020-05-19 19:59:23 +02:00
|
|
|
|
2020-04-22 15:04:56 +02:00
|
|
|
newStore = await client.CreateStore(new CreateStoreRequest() {Name = "A"});
|
2020-05-19 19:59:23 +02:00
|
|
|
var scopedClient =
|
|
|
|
await user.CreateClient(Permission.Create(Policies.CanViewStoreSettings, user.StoreId).ToString());
|
2020-04-22 15:04:56 +02:00
|
|
|
Assert.Single(await scopedClient.GetStores());
|
2020-03-24 16:18:43 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 19:59:23 +02:00
|
|
|
|
2020-03-18 15:10:15 +01:00
|
|
|
private async Task AssertHttpError(int code, Func<Task> act)
|
|
|
|
{
|
|
|
|
var ex = await Assert.ThrowsAsync<HttpRequestException>(act);
|
|
|
|
Assert.Contains(code.ToString(), ex.Message);
|
|
|
|
}
|
|
|
|
|
2020-03-12 14:59:24 +01:00
|
|
|
[Fact(Timeout = TestTimeout)]
|
|
|
|
[Trait("Integration", "Integration")]
|
|
|
|
public async Task UsersControllerTests()
|
|
|
|
{
|
2020-03-19 05:30:53 +01:00
|
|
|
using (var tester = ServerTester.Create(newDb: true))
|
2020-03-12 14:59:24 +01:00
|
|
|
{
|
2020-03-19 05:30:53 +01:00
|
|
|
tester.PayTester.DisableRegistration = true;
|
2020-03-12 14:59:24 +01:00
|
|
|
await tester.StartAsync();
|
|
|
|
var user = tester.NewAccount();
|
|
|
|
user.GrantAccess();
|
|
|
|
await user.MakeAdmin();
|
2020-03-20 05:41:47 +01:00
|
|
|
var clientProfile = await user.CreateClient(Policies.CanModifyProfile);
|
2020-03-20 10:56:30 +01:00
|
|
|
var clientServer = await user.CreateClient(Policies.CanCreateUser, Policies.CanViewProfile);
|
2020-03-20 05:41:47 +01:00
|
|
|
var clientInsufficient = await user.CreateClient(Policies.CanModifyStoreSettings);
|
2020-03-20 17:14:47 +01:00
|
|
|
var clientBasic = await user.CreateClient();
|
2020-03-16 08:36:55 +01:00
|
|
|
|
|
|
|
|
2020-03-12 14:59:24 +01:00
|
|
|
var apiKeyProfileUserData = await clientProfile.GetCurrentUser();
|
|
|
|
Assert.NotNull(apiKeyProfileUserData);
|
|
|
|
Assert.Equal(apiKeyProfileUserData.Id, user.UserId);
|
|
|
|
Assert.Equal(apiKeyProfileUserData.Email, user.RegisterDetails.Email);
|
|
|
|
|
|
|
|
await Assert.ThrowsAsync<HttpRequestException>(async () => await clientInsufficient.GetCurrentUser());
|
2020-03-12 18:43:57 +01:00
|
|
|
await clientServer.GetCurrentUser();
|
2020-03-19 11:11:15 +01:00
|
|
|
await clientProfile.GetCurrentUser();
|
2020-03-20 17:14:47 +01:00
|
|
|
await clientBasic.GetCurrentUser();
|
2020-03-18 15:10:15 +01:00
|
|
|
|
2020-05-19 19:59:23 +02:00
|
|
|
await Assert.ThrowsAsync<HttpRequestException>(async () =>
|
|
|
|
await clientInsufficient.CreateUser(new CreateApplicationUserRequest()
|
|
|
|
{
|
|
|
|
Email = $"{Guid.NewGuid()}@g.com", Password = Guid.NewGuid().ToString()
|
|
|
|
}));
|
2020-03-13 11:47:22 +01:00
|
|
|
|
|
|
|
var newUser = await clientServer.CreateUser(new CreateApplicationUserRequest()
|
|
|
|
{
|
2020-05-19 19:59:23 +02:00
|
|
|
Email = $"{Guid.NewGuid()}@g.com", Password = Guid.NewGuid().ToString()
|
2020-03-13 11:47:22 +01:00
|
|
|
});
|
|
|
|
Assert.NotNull(newUser);
|
2020-03-18 15:10:15 +01:00
|
|
|
|
2020-03-20 17:14:47 +01:00
|
|
|
var newUser2 = await clientBasic.CreateUser(new CreateApplicationUserRequest()
|
|
|
|
{
|
2020-05-19 19:59:23 +02:00
|
|
|
Email = $"{Guid.NewGuid()}@g.com", Password = Guid.NewGuid().ToString()
|
2020-03-20 17:14:47 +01:00
|
|
|
});
|
|
|
|
Assert.NotNull(newUser2);
|
|
|
|
|
2020-05-19 19:59:23 +02:00
|
|
|
await Assert.ThrowsAsync<HttpRequestException>(async () =>
|
|
|
|
await clientServer.CreateUser(new CreateApplicationUserRequest()
|
|
|
|
{
|
|
|
|
Email = $"{Guid.NewGuid()}", Password = Guid.NewGuid().ToString()
|
|
|
|
}));
|
2020-03-18 15:10:15 +01:00
|
|
|
|
2020-05-19 19:59:23 +02:00
|
|
|
await Assert.ThrowsAsync<HttpRequestException>(async () =>
|
|
|
|
await clientServer.CreateUser(
|
|
|
|
new CreateApplicationUserRequest() {Email = $"{Guid.NewGuid()}@g.com",}));
|
2020-03-13 11:47:22 +01:00
|
|
|
|
2020-05-19 19:59:23 +02:00
|
|
|
await Assert.ThrowsAsync<HttpRequestException>(async () =>
|
|
|
|
await clientServer.CreateUser(
|
|
|
|
new CreateApplicationUserRequest() {Password = Guid.NewGuid().ToString()}));
|
2020-03-12 14:59:24 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 19:59:23 +02:00
|
|
|
|
2020-04-16 15:39:08 +02:00
|
|
|
[Fact(Timeout = TestTimeout)]
|
|
|
|
[Trait("Integration", "Integration")]
|
|
|
|
public async Task HealthControllerTests()
|
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
|
|
|
await tester.StartAsync();
|
|
|
|
var unauthClient = new BTCPayServerClient(tester.PayTester.ServerUri);
|
|
|
|
|
2020-04-21 10:39:04 +02:00
|
|
|
var apiHealthData = await unauthClient.GetHealth();
|
|
|
|
Assert.NotNull(apiHealthData);
|
|
|
|
Assert.True(apiHealthData.Synchronized);
|
2020-04-16 15:39:08 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 19:59:23 +02:00
|
|
|
|
2020-05-16 23:57:49 +02:00
|
|
|
[Fact(Timeout = TestTimeout)]
|
|
|
|
[Trait("Integration", "Integration")]
|
|
|
|
public async Task ServerInfoControllerTests()
|
|
|
|
{
|
|
|
|
using (var tester = ServerTester.Create())
|
|
|
|
{
|
|
|
|
await tester.StartAsync();
|
|
|
|
var unauthClient = new BTCPayServerClient(tester.PayTester.ServerUri);
|
|
|
|
await AssertHttpError(401, async () => await unauthClient.GetServerInfo());
|
|
|
|
|
|
|
|
var user = tester.NewAccount();
|
|
|
|
user.GrantAccess();
|
|
|
|
var clientBasic = await user.CreateClient();
|
|
|
|
var serverInfoData = await clientBasic.GetServerInfo();
|
|
|
|
Assert.NotNull(serverInfoData);
|
|
|
|
Assert.NotNull(serverInfoData.Status);
|
|
|
|
Assert.True(serverInfoData.Status.FullySynched);
|
|
|
|
Assert.Contains("BTC", serverInfoData.SupportedPaymentMethods);
|
|
|
|
Assert.Contains("BTC_LightningLike", serverInfoData.SupportedPaymentMethods);
|
|
|
|
}
|
|
|
|
}
|
2020-05-19 19:59:23 +02:00
|
|
|
|
|
|
|
[Fact(Timeout = TestTimeout)]
|
|
|
|
[Trait("Integration", "Integration")]
|
|
|
|
public async Task PaymentControllerTests()
|
|
|
|
{
|
|
|
|
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 viewOnly = await user.CreateClient(Policies.CanViewPaymentRequests);
|
|
|
|
|
|
|
|
//create payment request
|
|
|
|
|
|
|
|
//validation errors
|
|
|
|
await AssertHttpError(400, async () =>
|
|
|
|
{
|
|
|
|
await client.CreatePaymentRequest(user.StoreId, new CreatePaymentRequestRequest() {Title = "A"});
|
|
|
|
});
|
|
|
|
await AssertHttpError(400, async () =>
|
|
|
|
{
|
|
|
|
await client.CreatePaymentRequest(user.StoreId,
|
|
|
|
new CreatePaymentRequestRequest() {Title = "A", Currency = "BTC", Amount = 0});
|
|
|
|
});
|
|
|
|
await AssertHttpError(400, async () =>
|
|
|
|
{
|
|
|
|
await client.CreatePaymentRequest(user.StoreId,
|
|
|
|
new CreatePaymentRequestRequest() {Title = "A", Currency = "helloinvalid", Amount = 1});
|
|
|
|
});
|
|
|
|
await AssertHttpError(403, async () =>
|
|
|
|
{
|
|
|
|
await viewOnly.CreatePaymentRequest(user.StoreId,
|
|
|
|
new CreatePaymentRequestRequest() {Title = "A", Currency = "helloinvalid", Amount = 1});
|
|
|
|
});
|
|
|
|
var newPaymentRequest = await client.CreatePaymentRequest(user.StoreId,
|
|
|
|
new CreatePaymentRequestRequest() {Title = "A", Currency = "USD", Amount = 1});
|
|
|
|
|
|
|
|
//list payment request
|
|
|
|
var paymentRequests = await viewOnly.GetPaymentRequests(user.StoreId);
|
|
|
|
|
|
|
|
Assert.NotNull(paymentRequests);
|
|
|
|
Assert.Single(paymentRequests);
|
|
|
|
Assert.Equal(newPaymentRequest.Id, paymentRequests.First().Id);
|
|
|
|
|
|
|
|
//get payment request
|
|
|
|
var paymentRequest = await viewOnly.GetPaymentRequest(user.StoreId, newPaymentRequest.Id);
|
|
|
|
Assert.Equal(newPaymentRequest.Title, paymentRequest.Title);
|
|
|
|
|
|
|
|
//update payment request
|
|
|
|
var updateRequest = JObject.FromObject(paymentRequest).ToObject<UpdatePaymentRequestRequest>();
|
|
|
|
updateRequest.Title = "B";
|
|
|
|
await AssertHttpError(403, async () =>
|
|
|
|
{
|
|
|
|
await viewOnly.UpdatePaymentRequest(user.StoreId, paymentRequest.Id, updateRequest);
|
|
|
|
});
|
|
|
|
await client.UpdatePaymentRequest(user.StoreId, paymentRequest.Id, updateRequest);
|
|
|
|
paymentRequest = await client.GetPaymentRequest(user.StoreId, newPaymentRequest.Id);
|
|
|
|
Assert.Equal(updateRequest.Title, paymentRequest.Title);
|
|
|
|
|
|
|
|
//archive payment request
|
|
|
|
await AssertHttpError(403, async () =>
|
|
|
|
{
|
|
|
|
await viewOnly.ArchivePaymentRequest(user.StoreId, paymentRequest.Id);
|
|
|
|
});
|
|
|
|
|
|
|
|
await client.ArchivePaymentRequest(user.StoreId, paymentRequest.Id);
|
|
|
|
Assert.DoesNotContain(paymentRequest.Id,
|
|
|
|
(await client.GetPaymentRequests(user.StoreId)).Select(data => data.Id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact(Timeout = TestTimeout)]
|
|
|
|
[Trait("Fast", "Fast")]
|
|
|
|
public async Task DecimalStringJsonConverterTests()
|
|
|
|
{
|
|
|
|
JsonReader Get(string val)
|
|
|
|
{
|
|
|
|
return new JsonTextReader(new StringReader(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonConverter = new DecimalStringJsonConverter();
|
|
|
|
Assert.True(jsonConverter.CanConvert(typeof(decimal)));
|
|
|
|
Assert.True(jsonConverter.CanConvert(typeof(decimal?)));
|
|
|
|
Assert.False(jsonConverter.CanConvert(typeof(double)));
|
|
|
|
Assert.False(jsonConverter.CanConvert(typeof(float)));
|
|
|
|
Assert.False(jsonConverter.CanConvert(typeof(int)));
|
|
|
|
Assert.False(jsonConverter.CanConvert(typeof(string)));
|
|
|
|
|
|
|
|
var numberJson = "1";
|
|
|
|
var numberDecimalJson = "1.2";
|
|
|
|
var stringJson = "\"1.2\"";
|
|
|
|
Assert.Equal(1m, jsonConverter.ReadJson(Get(numberJson), typeof(decimal), null, null));
|
|
|
|
Assert.Equal(1.2m, jsonConverter.ReadJson(Get(numberDecimalJson), typeof(decimal), null, null));
|
|
|
|
Assert.Null(jsonConverter.ReadJson(Get("null"), typeof(decimal?), null, null));
|
|
|
|
Assert.Throws<JsonSerializationException>(() =>
|
|
|
|
{
|
|
|
|
jsonConverter.ReadJson(Get("null"), typeof(decimal), null, null);
|
|
|
|
});
|
|
|
|
Assert.Equal(1.2m, jsonConverter.ReadJson(Get(stringJson), typeof(decimal), null, null));
|
|
|
|
Assert.Equal(1.2m, jsonConverter.ReadJson(Get(stringJson), typeof(decimal?), null, null));
|
|
|
|
}
|
2020-02-24 18:43:28 +01:00
|
|
|
}
|
|
|
|
}
|