2020-03-13 11:47:22 +01:00
|
|
|
using System;
|
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-02-24 18:43:28 +01:00
|
|
|
using BTCPayServer.Controllers;
|
2020-03-13 11:47:22 +01:00
|
|
|
using BTCPayServer.Controllers.RestApi.Users;
|
2020-03-18 15:10:15 +01:00
|
|
|
using BTCPayServer.Services;
|
2020-02-24 18:43:28 +01:00
|
|
|
using BTCPayServer.Tests.Logging;
|
2020-03-02 16:50:28 +01:00
|
|
|
using Microsoft.AspNet.SignalR.Client;
|
2020-02-24 18:43:28 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Xunit;
|
|
|
|
using Xunit.Abstractions;
|
2020-03-13 11:47:22 +01:00
|
|
|
using CreateApplicationUserRequest = BTCPayServer.Client.Models.CreateApplicationUserRequest;
|
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-03-18 15:10:15 +01: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-20 05:41:47 +01:00
|
|
|
var client = await user.CreateClient(Policies.CanModifyServerSettings, Policies.CanModifyStoreSettings);
|
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-02-24 18:43:28 +01:00
|
|
|
Assert.Equal(user.UserId, apiKeyData.UserId);
|
|
|
|
Assert.Equal(2, apiKeyData.Permissions.Length);
|
2020-03-18 15:10:15 +01:00
|
|
|
|
2020-03-02 16:50:28 +01:00
|
|
|
//revoke current api key
|
|
|
|
await client.RevokeCurrentAPIKeyInfo();
|
|
|
|
await Assert.ThrowsAsync<HttpRequestException>(async () =>
|
|
|
|
{
|
|
|
|
await client.GetCurrentAPIKeyInfo();
|
|
|
|
});
|
2020-02-24 18:43:28 +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);
|
|
|
|
await AssertHttpError(400, async () => await unauthClient.CreateUser(new CreateApplicationUserRequest()));
|
|
|
|
await AssertHttpError(400, async () => await unauthClient.CreateUser(new CreateApplicationUserRequest() { Email = "test@gmail.com" }));
|
|
|
|
// Pass too simple
|
|
|
|
await AssertHttpError(400, async () => await unauthClient.CreateUser(new CreateApplicationUserRequest() { Email = "test3@gmail.com", Password = "a" }));
|
|
|
|
|
|
|
|
// We have no admin, so it should work
|
|
|
|
var user1 = await unauthClient.CreateUser(new CreateApplicationUserRequest() { Email = "test@gmail.com", Password = "abceudhqw" });
|
|
|
|
// We have no admin, so it should work
|
|
|
|
var user2 = await unauthClient.CreateUser(new CreateApplicationUserRequest() { Email = "test2@gmail.com", Password = "abceudhqw" });
|
|
|
|
|
|
|
|
// Duplicate email
|
|
|
|
await AssertHttpError(400, async () => await unauthClient.CreateUser(new CreateApplicationUserRequest() { Email = "test2@gmail.com", Password = "abceudhqw" }));
|
|
|
|
|
|
|
|
// Let's make an admin
|
|
|
|
var admin = await unauthClient.CreateUser(new CreateApplicationUserRequest() { Email = "admin@gmail.com", Password = "abceudhqw", IsAdministrator = true });
|
|
|
|
|
|
|
|
// 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
|
|
|
|
await AssertHttpError(401, async () => await unauthClient.CreateUser(new CreateApplicationUserRequest() { Email = "test3@gmail.com", Password = "afewfoiewiou" }));
|
|
|
|
|
|
|
|
|
|
|
|
// But should be ok with subscriptions unlocked
|
|
|
|
var settings = tester.PayTester.GetService<SettingsRepository>();
|
|
|
|
await settings.UpdateSetting<PoliciesSettings>(new PoliciesSettings() { LockSubscription = false });
|
|
|
|
await unauthClient.CreateUser(new CreateApplicationUserRequest() { Email = "test3@gmail.com", Password = "afewfoiewiou" });
|
|
|
|
|
|
|
|
// But it should be forbidden to create an admin without being authenticated
|
|
|
|
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 });
|
|
|
|
|
|
|
|
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
|
|
|
|
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 }));
|
|
|
|
|
|
|
|
// However, should be ok with the server management permissions
|
2020-03-20 05:41:47 +01:00
|
|
|
adminClient = await adminAcc.CreateClient(Policies.CanModifyServerSettings);
|
2020-03-18 15:10:15 +01:00
|
|
|
await adminClient.CreateUser(new CreateApplicationUserRequest() { Email = "test4@gmail.com", Password = "afewfoiewiou" });
|
|
|
|
// Even creating new admin should be ok
|
|
|
|
await adminClient.CreateUser(new CreateApplicationUserRequest() { Email = "admin4@gmail.com", Password = "afewfoiewiou", IsAdministrator = true });
|
|
|
|
|
|
|
|
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-03-18 15:10:15 +01:00
|
|
|
// User1 trying to get server management would still fail to create user
|
|
|
|
await AssertHttpError(403, async () => await user1Client.CreateUser(new CreateApplicationUserRequest() { Email = "test8@gmail.com", Password = "afewfoiewiou" }));
|
|
|
|
|
|
|
|
// User1 should be able to create user if subscription unlocked
|
|
|
|
await settings.UpdateSetting<PoliciesSettings>(new PoliciesSettings() { LockSubscription = false });
|
|
|
|
await user1Client.CreateUser(new CreateApplicationUserRequest() { Email = "test8@gmail.com", Password = "afewfoiewiou" });
|
|
|
|
// But not an admin
|
|
|
|
await AssertHttpError(403, async () => await user1Client.CreateUser(new CreateApplicationUserRequest() { Email = "admin8@gmail.com", Password = "afewfoiewiou", IsAdministrator = true }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
var clientServer = await user.CreateClient(Policies.CanModifyServerSettings, Policies.CanViewProfile);
|
|
|
|
var clientInsufficient = await user.CreateClient(Policies.CanModifyStoreSettings);
|
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-18 15:10:15 +01:00
|
|
|
|
2020-03-19 05:30:53 +01: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-03-18 15:10:15 +01: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-13 11:47:22 +01: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-03-13 11:47:22 +01:00
|
|
|
await Assert.ThrowsAsync<HttpRequestException>(async () => await clientServer.CreateUser(new CreateApplicationUserRequest()
|
|
|
|
{
|
|
|
|
Email = $"{Guid.NewGuid()}@g.com",
|
2020-03-18 15:10:15 +01:00
|
|
|
}));
|
|
|
|
|
2020-03-13 11:47:22 +01:00
|
|
|
await Assert.ThrowsAsync<HttpRequestException>(async () => await clientServer.CreateUser(new CreateApplicationUserRequest()
|
|
|
|
{
|
|
|
|
Password = Guid.NewGuid().ToString()
|
2020-03-18 15:10:15 +01:00
|
|
|
}));
|
2020-03-13 11:47:22 +01:00
|
|
|
|
2020-03-12 14:59:24 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-24 18:43:28 +01:00
|
|
|
}
|
|
|
|
}
|