2022-06-30 01:46:41 +02:00
|
|
|
|
using System.Text.Json;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Entities;
|
2022-04-01 22:08:47 +02:00
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
using Bit.Core.Models;
|
2022-01-21 15:36:25 +01:00
|
|
|
|
using Bit.Core.Models.Business;
|
2022-06-16 22:30:50 +02:00
|
|
|
|
using Bit.Core.Models.Data.Organizations;
|
2022-04-28 18:14:09 +02:00
|
|
|
|
using Bit.Core.Repositories;
|
2019-07-06 05:35:54 +02:00
|
|
|
|
using Bit.Core.Services;
|
2022-01-21 15:36:25 +01:00
|
|
|
|
using Bit.Test.Common.AutoFixture;
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
|
|
|
using Bit.Test.Common.Helpers;
|
2019-07-06 05:35:54 +02:00
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using NSubstitute;
|
2022-04-01 22:08:47 +02:00
|
|
|
|
using NSubstitute.ReceivedExtensions;
|
2019-07-06 05:35:54 +02:00
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Test.Services;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[SutProviderCustomize]
|
2019-07-06 05:35:54 +02:00
|
|
|
|
public class UserServiceTests
|
|
|
|
|
{
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2019-07-06 05:35:54 +02:00
|
|
|
|
public async Task UpdateLicenseAsync_Success(SutProvider<UserService> sutProvider,
|
2022-01-21 15:36:25 +01:00
|
|
|
|
User user, UserLicense userLicense)
|
2019-07-06 05:35:54 +02:00
|
|
|
|
{
|
2022-01-21 15:36:25 +01:00
|
|
|
|
using var tempDir = new TempDirectory();
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-01-21 15:36:25 +01:00
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
userLicense.Issued = now.AddDays(-10);
|
|
|
|
|
userLicense.Expires = now.AddDays(10);
|
|
|
|
|
userLicense.Version = 1;
|
|
|
|
|
userLicense.Premium = true;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-01-21 15:36:25 +01:00
|
|
|
|
user.EmailVerified = true;
|
|
|
|
|
user.Email = userLicense.Email;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-05-13 15:48:48 +02:00
|
|
|
|
sutProvider.GetDependency<Settings.IGlobalSettings>().SelfHosted = true;
|
|
|
|
|
sutProvider.GetDependency<Settings.IGlobalSettings>().LicenseDirectory = tempDir.Directory;
|
2022-01-21 15:36:25 +01:00
|
|
|
|
sutProvider.GetDependency<ILicensingService>()
|
|
|
|
|
.VerifyLicense(userLicense)
|
|
|
|
|
.Returns(true);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-01-21 15:36:25 +01:00
|
|
|
|
await sutProvider.Sut.UpdateLicenseAsync(user, userLicense);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-01-21 15:36:25 +01:00
|
|
|
|
var filePath = Path.Combine(tempDir.Directory, "user", $"{user.Id}.json");
|
|
|
|
|
Assert.True(File.Exists(filePath));
|
|
|
|
|
var document = JsonDocument.Parse(File.OpenRead(filePath));
|
|
|
|
|
var root = document.RootElement;
|
|
|
|
|
Assert.Equal(JsonValueKind.Object, root.ValueKind);
|
|
|
|
|
// Sort of a lazy way to test that it is indented but not sure of a better way
|
|
|
|
|
Assert.Contains('\n', root.GetRawText());
|
|
|
|
|
AssertHelper.AssertJsonProperty(root, "LicenseKey", JsonValueKind.String);
|
|
|
|
|
AssertHelper.AssertJsonProperty(root, "Id", JsonValueKind.String);
|
|
|
|
|
AssertHelper.AssertJsonProperty(root, "Premium", JsonValueKind.True);
|
|
|
|
|
var versionProp = AssertHelper.AssertJsonProperty(root, "Version", JsonValueKind.Number);
|
|
|
|
|
Assert.Equal(1, versionProp.GetInt32());
|
2019-07-06 05:35:54 +02:00
|
|
|
|
}
|
2022-04-01 22:08:47 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-04-01 22:08:47 +02:00
|
|
|
|
public async Task SendTwoFactorEmailAsync_Success(SutProvider<UserService> sutProvider, User user)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-04-01 22:08:47 +02:00
|
|
|
|
var email = user.Email.ToLowerInvariant();
|
|
|
|
|
var token = "thisisatokentocompare";
|
|
|
|
|
|
|
|
|
|
var userTwoFactorTokenProvider = Substitute.For<IUserTwoFactorTokenProvider<User>>();
|
|
|
|
|
userTwoFactorTokenProvider
|
|
|
|
|
.CanGenerateTwoFactorTokenAsync(Arg.Any<UserManager<User>>(), user)
|
|
|
|
|
.Returns(Task.FromResult(true));
|
|
|
|
|
userTwoFactorTokenProvider
|
|
|
|
|
.GenerateAsync("2faEmail:" + email, Arg.Any<UserManager<User>>(), user)
|
|
|
|
|
.Returns(Task.FromResult(token));
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-04-01 22:08:47 +02:00
|
|
|
|
sutProvider.Sut.RegisterTokenProvider("Email", userTwoFactorTokenProvider);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-04-01 22:08:47 +02:00
|
|
|
|
user.SetTwoFactorProviders(new Dictionary<TwoFactorProviderType, TwoFactorProvider>
|
2022-08-29 20:53:16 +02:00
|
|
|
|
{
|
2022-04-01 22:08:47 +02:00
|
|
|
|
[TwoFactorProviderType.Email] = new TwoFactorProvider
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-04-01 22:08:47 +02:00
|
|
|
|
MetaData = new Dictionary<string, object> { ["Email"] = email },
|
|
|
|
|
Enabled = true
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
2022-04-01 22:08:47 +02:00
|
|
|
|
await sutProvider.Sut.SendTwoFactorEmailAsync(user);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-04-01 22:08:47 +02:00
|
|
|
|
await sutProvider.GetDependency<IMailService>()
|
|
|
|
|
.Received(1)
|
|
|
|
|
.SendTwoFactorEmailAsync(email, token);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-04-01 22:08:47 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-04-01 22:08:47 +02:00
|
|
|
|
public async Task SendTwoFactorEmailBecauseNewDeviceLoginAsync_Success(SutProvider<UserService> sutProvider, User user)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-04-01 22:08:47 +02:00
|
|
|
|
var email = user.Email.ToLowerInvariant();
|
|
|
|
|
var token = "thisisatokentocompare";
|
|
|
|
|
|
|
|
|
|
var userTwoFactorTokenProvider = Substitute.For<IUserTwoFactorTokenProvider<User>>();
|
|
|
|
|
userTwoFactorTokenProvider
|
|
|
|
|
.CanGenerateTwoFactorTokenAsync(Arg.Any<UserManager<User>>(), user)
|
|
|
|
|
.Returns(Task.FromResult(true));
|
|
|
|
|
userTwoFactorTokenProvider
|
|
|
|
|
.GenerateAsync("2faEmail:" + email, Arg.Any<UserManager<User>>(), user)
|
|
|
|
|
.Returns(Task.FromResult(token));
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-04-01 22:08:47 +02:00
|
|
|
|
sutProvider.Sut.RegisterTokenProvider("Email", userTwoFactorTokenProvider);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-04-01 22:08:47 +02:00
|
|
|
|
user.SetTwoFactorProviders(new Dictionary<TwoFactorProviderType, TwoFactorProvider>
|
2022-08-29 20:53:16 +02:00
|
|
|
|
{
|
2022-06-06 19:52:50 +02:00
|
|
|
|
[TwoFactorProviderType.Email] = new TwoFactorProvider
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-04-01 22:08:47 +02:00
|
|
|
|
MetaData = new Dictionary<string, object> { ["Email"] = email },
|
|
|
|
|
Enabled = true
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
2022-06-06 19:52:50 +02:00
|
|
|
|
await sutProvider.Sut.SendTwoFactorEmailAsync(user, true);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-04-01 22:08:47 +02:00
|
|
|
|
await sutProvider.GetDependency<IMailService>()
|
|
|
|
|
.Received(1)
|
|
|
|
|
.SendNewDeviceLoginTwoFactorEmailAsync(email, token);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-04-01 22:08:47 +02:00
|
|
|
|
public async Task SendTwoFactorEmailAsync_ExceptionBecauseNoProviderOnUser(SutProvider<UserService> sutProvider, User user)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-06-06 19:52:50 +02:00
|
|
|
|
user.TwoFactorProviders = null;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-04-01 22:08:47 +02:00
|
|
|
|
await Assert.ThrowsAsync<ArgumentNullException>("No email.", () => sutProvider.Sut.SendTwoFactorEmailAsync(user));
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-04-01 22:08:47 +02:00
|
|
|
|
public async Task SendTwoFactorEmailAsync_ExceptionBecauseNoProviderMetadataOnUser(SutProvider<UserService> sutProvider, User user)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-04-01 22:08:47 +02:00
|
|
|
|
user.SetTwoFactorProviders(new Dictionary<TwoFactorProviderType, TwoFactorProvider>
|
|
|
|
|
{
|
|
|
|
|
[TwoFactorProviderType.Email] = new TwoFactorProvider
|
|
|
|
|
{
|
|
|
|
|
MetaData = null,
|
|
|
|
|
Enabled = true
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-04-01 22:08:47 +02:00
|
|
|
|
await Assert.ThrowsAsync<ArgumentNullException>("No email.", () => sutProvider.Sut.SendTwoFactorEmailAsync(user));
|
|
|
|
|
}
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-04-01 22:08:47 +02:00
|
|
|
|
public async Task SendTwoFactorEmailAsync_ExceptionBecauseNoProviderEmailMetadataOnUser(SutProvider<UserService> sutProvider, User user)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-04-01 22:08:47 +02:00
|
|
|
|
user.SetTwoFactorProviders(new Dictionary<TwoFactorProviderType, TwoFactorProvider>
|
|
|
|
|
{
|
|
|
|
|
[TwoFactorProviderType.Email] = new TwoFactorProvider
|
|
|
|
|
{
|
|
|
|
|
MetaData = new Dictionary<string, object> { ["qweqwe"] = user.Email.ToLowerInvariant() },
|
|
|
|
|
Enabled = true
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-04-28 18:14:09 +02:00
|
|
|
|
|
|
|
|
|
await Assert.ThrowsAsync<ArgumentNullException>("No email.", () => sutProvider.Sut.SendTwoFactorEmailAsync(user));
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-04-28 18:14:09 +02:00
|
|
|
|
public async Task Needs2FABecauseNewDeviceAsync_ReturnsTrue(SutProvider<UserService> sutProvider, User user)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-04-28 18:14:09 +02:00
|
|
|
|
user.EmailVerified = true;
|
2022-06-06 19:52:50 +02:00
|
|
|
|
user.TwoFactorProviders = null;
|
|
|
|
|
user.UnknownDeviceVerificationEnabled = true;
|
2022-04-28 18:14:09 +02:00
|
|
|
|
const string deviceIdToCheck = "7b01b586-b210-499f-8d52-0c3fdaa646fc";
|
|
|
|
|
const string deviceIdInRepo = "ea29126c-91b7-4cc4-8ce6-00105b37f64a";
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-05-13 15:48:48 +02:00
|
|
|
|
sutProvider.GetDependency<IDeviceRepository>()
|
|
|
|
|
.GetManyByUserIdAsync(user.Id)
|
|
|
|
|
.Returns(Task.FromResult<ICollection<Device>>(new List<Device>
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-05-13 15:48:48 +02:00
|
|
|
|
new Device { Identifier = deviceIdInRepo }
|
|
|
|
|
}));
|
2022-04-28 18:14:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<Settings.IGlobalSettings>().TwoFactorAuth.EmailOnNewDeviceLogin.Returns(true);
|
|
|
|
|
|
|
|
|
|
Assert.True(await sutProvider.Sut.Needs2FABecauseNewDeviceAsync(user, deviceIdToCheck, "password"));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-06-06 19:52:50 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-06-06 19:52:50 +02:00
|
|
|
|
public async Task Needs2FABecauseNewDeviceAsync_ReturnsFalse_When_GranType_Is_AuthorizationCode(SutProvider<UserService> sutProvider, User user)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-04-28 18:14:09 +02:00
|
|
|
|
user.EmailVerified = true;
|
2022-06-06 19:52:50 +02:00
|
|
|
|
user.TwoFactorProviders = null;
|
2022-04-28 18:14:09 +02:00
|
|
|
|
const string deviceIdToCheck = "7b01b586-b210-499f-8d52-0c3fdaa646fc";
|
|
|
|
|
const string deviceIdInRepo = "ea29126c-91b7-4cc4-8ce6-00105b37f64a";
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-04-28 18:14:09 +02:00
|
|
|
|
sutProvider.GetDependency<IDeviceRepository>()
|
|
|
|
|
.GetManyByUserIdAsync(user.Id)
|
|
|
|
|
.Returns(Task.FromResult<ICollection<Device>>(new List<Device>
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-04-28 18:14:09 +02:00
|
|
|
|
new Device { Identifier = deviceIdInRepo }
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}));
|
|
|
|
|
|
2022-06-06 19:52:50 +02:00
|
|
|
|
Assert.False(await sutProvider.Sut.Needs2FABecauseNewDeviceAsync(user, deviceIdToCheck, "authorization_code"));
|
2022-08-29 21:53:48 +02:00
|
|
|
|
}
|
2022-06-06 19:52:50 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-06-06 19:52:50 +02:00
|
|
|
|
public async Task Needs2FABecauseNewDeviceAsync_ReturnsFalse_When_Email_Is_Not_Verified(SutProvider<UserService> sutProvider, User user)
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2022-04-28 18:14:09 +02:00
|
|
|
|
user.EmailVerified = false;
|
2022-06-06 19:52:50 +02:00
|
|
|
|
user.TwoFactorProviders = null;
|
2022-04-28 18:14:09 +02:00
|
|
|
|
const string deviceIdToCheck = "7b01b586-b210-499f-8d52-0c3fdaa646fc";
|
|
|
|
|
const string deviceIdInRepo = "ea29126c-91b7-4cc4-8ce6-00105b37f64a";
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-06-06 19:52:50 +02:00
|
|
|
|
sutProvider.GetDependency<IDeviceRepository>()
|
|
|
|
|
.GetManyByUserIdAsync(user.Id)
|
|
|
|
|
.Returns(Task.FromResult<ICollection<Device>>(new List<Device>
|
2022-08-29 20:53:16 +02:00
|
|
|
|
{
|
2022-06-06 19:52:50 +02:00
|
|
|
|
new Device { Identifier = deviceIdInRepo }
|
2022-08-29 21:53:48 +02:00
|
|
|
|
}));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-04-28 18:14:09 +02:00
|
|
|
|
Assert.False(await sutProvider.Sut.Needs2FABecauseNewDeviceAsync(user, deviceIdToCheck, "password"));
|
2022-08-29 21:53:48 +02:00
|
|
|
|
}
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-06-06 19:52:50 +02:00
|
|
|
|
public async Task Needs2FABecauseNewDeviceAsync_ReturnsFalse_When_Is_The_First_Device(SutProvider<UserService> sutProvider, User user)
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2022-06-06 19:52:50 +02:00
|
|
|
|
user.EmailVerified = true;
|
|
|
|
|
user.TwoFactorProviders = null;
|
|
|
|
|
const string deviceIdToCheck = "7b01b586-b210-499f-8d52-0c3fdaa646fc";
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IDeviceRepository>()
|
|
|
|
|
.GetManyByUserIdAsync(user.Id)
|
|
|
|
|
.Returns(Task.FromResult<ICollection<Device>>(new List<Device>()));
|
|
|
|
|
|
|
|
|
|
Assert.False(await sutProvider.Sut.Needs2FABecauseNewDeviceAsync(user, deviceIdToCheck, "password"));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-06-06 19:52:50 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-04-28 18:14:09 +02:00
|
|
|
|
public async Task Needs2FABecauseNewDeviceAsync_ReturnsFalse_When_DeviceId_Is_Already_In_Repo(SutProvider<UserService> sutProvider, User user)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-04-28 18:14:09 +02:00
|
|
|
|
user.EmailVerified = true;
|
2022-06-06 19:52:50 +02:00
|
|
|
|
user.TwoFactorProviders = null;
|
2022-04-28 18:14:09 +02:00
|
|
|
|
const string deviceIdToCheck = "7b01b586-b210-499f-8d52-0c3fdaa646fc";
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-04-28 18:14:09 +02:00
|
|
|
|
sutProvider.GetDependency<IDeviceRepository>()
|
|
|
|
|
.GetManyByUserIdAsync(user.Id)
|
|
|
|
|
.Returns(Task.FromResult<ICollection<Device>>(new List<Device>
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-06-06 19:52:50 +02:00
|
|
|
|
new Device { Identifier = deviceIdToCheck }
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}));
|
|
|
|
|
|
2022-06-06 19:52:50 +02:00
|
|
|
|
Assert.False(await sutProvider.Sut.Needs2FABecauseNewDeviceAsync(user, deviceIdToCheck, "password"));
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-04-28 18:14:09 +02:00
|
|
|
|
public async Task Needs2FABecauseNewDeviceAsync_ReturnsFalse_When_GlobalSettings_2FA_EmailOnNewDeviceLogin_Is_Disabled(SutProvider<UserService> sutProvider, User user)
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2022-06-06 19:52:50 +02:00
|
|
|
|
user.EmailVerified = true;
|
|
|
|
|
user.TwoFactorProviders = null;
|
|
|
|
|
const string deviceIdToCheck = "7b01b586-b210-499f-8d52-0c3fdaa646fc";
|
|
|
|
|
const string deviceIdInRepo = "ea29126c-91b7-4cc4-8ce6-00105b37f64a";
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IDeviceRepository>()
|
|
|
|
|
.GetManyByUserIdAsync(user.Id)
|
|
|
|
|
.Returns(Task.FromResult<ICollection<Device>>(new List<Device>
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-06-06 19:52:50 +02:00
|
|
|
|
new Device { Identifier = deviceIdInRepo }
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<Settings.IGlobalSettings>().TwoFactorAuth.EmailOnNewDeviceLogin.Returns(false);
|
|
|
|
|
|
|
|
|
|
Assert.False(await sutProvider.Sut.Needs2FABecauseNewDeviceAsync(user, deviceIdToCheck, "password"));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-06-06 19:52:50 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-06-06 19:52:50 +02:00
|
|
|
|
public async Task Needs2FABecauseNewDeviceAsync_ReturnsFalse_When_UnknownDeviceVerification_Is_Disabled(SutProvider<UserService> sutProvider, User user)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-06-06 19:52:50 +02:00
|
|
|
|
user.EmailVerified = true;
|
|
|
|
|
user.TwoFactorProviders = null;
|
|
|
|
|
user.UnknownDeviceVerificationEnabled = false;
|
|
|
|
|
const string deviceIdToCheck = "7b01b586-b210-499f-8d52-0c3fdaa646fc";
|
|
|
|
|
const string deviceIdInRepo = "ea29126c-91b7-4cc4-8ce6-00105b37f64a";
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-06-06 19:52:50 +02:00
|
|
|
|
sutProvider.GetDependency<IDeviceRepository>()
|
|
|
|
|
.GetManyByUserIdAsync(user.Id)
|
|
|
|
|
.Returns(Task.FromResult<ICollection<Device>>(new List<Device>
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-04-28 18:14:09 +02:00
|
|
|
|
new Device { Identifier = deviceIdInRepo }
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}));
|
|
|
|
|
|
2022-06-06 19:52:50 +02:00
|
|
|
|
sutProvider.GetDependency<Settings.IGlobalSettings>().TwoFactorAuth.EmailOnNewDeviceLogin.Returns(true);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-06-06 19:52:50 +02:00
|
|
|
|
Assert.False(await sutProvider.Sut.Needs2FABecauseNewDeviceAsync(user, deviceIdToCheck, "password"));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-06-06 19:52:50 +02:00
|
|
|
|
public void CanEditDeviceVerificationSettings_ReturnsTrue(SutProvider<UserService> sutProvider, User user)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-06-06 19:52:50 +02:00
|
|
|
|
user.EmailVerified = true;
|
|
|
|
|
user.TwoFactorProviders = null;
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<Settings.IGlobalSettings>().TwoFactorAuth.EmailOnNewDeviceLogin.Returns(true);
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-06-06 19:52:50 +02:00
|
|
|
|
Assert.True(sutProvider.Sut.CanEditDeviceVerificationSettings(user));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-06-06 19:52:50 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-06-06 19:52:50 +02:00
|
|
|
|
public void CanEditDeviceVerificationSettings_ReturnsFalse_When_GlobalSettings_2FA_EmailOnNewDeviceLogin_Is_Disabled(SutProvider<UserService> sutProvider, User user)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-06-06 19:52:50 +02:00
|
|
|
|
user.EmailVerified = true;
|
|
|
|
|
user.TwoFactorProviders = null;
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-06-06 19:52:50 +02:00
|
|
|
|
sutProvider.GetDependency<Settings.IGlobalSettings>().TwoFactorAuth.EmailOnNewDeviceLogin.Returns(false);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-06-06 19:52:50 +02:00
|
|
|
|
Assert.False(sutProvider.Sut.CanEditDeviceVerificationSettings(user));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-06-06 19:52:50 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-06-06 19:52:50 +02:00
|
|
|
|
public void CanEditDeviceVerificationSettings_ReturnsFalse_When_Email_Is_Not_Verified(SutProvider<UserService> sutProvider, User user)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-06-06 19:52:50 +02:00
|
|
|
|
user.EmailVerified = false;
|
|
|
|
|
user.TwoFactorProviders = null;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-06-06 19:52:50 +02:00
|
|
|
|
sutProvider.GetDependency<Settings.IGlobalSettings>().TwoFactorAuth.EmailOnNewDeviceLogin.Returns(true);
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-06-06 19:52:50 +02:00
|
|
|
|
Assert.False(sutProvider.Sut.CanEditDeviceVerificationSettings(user));
|
2022-08-29 20:53:16 +02:00
|
|
|
|
}
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-06-06 19:52:50 +02:00
|
|
|
|
public void CanEditDeviceVerificationSettings_ReturnsFalse_When_User_Uses_Key_Connector(SutProvider<UserService> sutProvider, User user)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-06-06 19:52:50 +02:00
|
|
|
|
user.EmailVerified = true;
|
|
|
|
|
user.TwoFactorProviders = null;
|
|
|
|
|
user.UsesKeyConnector = true;
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-06-06 19:52:50 +02:00
|
|
|
|
sutProvider.GetDependency<Settings.IGlobalSettings>().TwoFactorAuth.EmailOnNewDeviceLogin.Returns(true);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-06-06 19:52:50 +02:00
|
|
|
|
Assert.False(sutProvider.Sut.CanEditDeviceVerificationSettings(user));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-06-06 19:52:50 +02:00
|
|
|
|
public void CanEditDeviceVerificationSettings_ReturnsFalse_When_User_Has_A_2FA_Already_Set_Up(SutProvider<UserService> sutProvider, User user)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-06-06 19:52:50 +02:00
|
|
|
|
user.EmailVerified = true;
|
|
|
|
|
user.SetTwoFactorProviders(new Dictionary<TwoFactorProviderType, TwoFactorProvider>
|
|
|
|
|
{
|
|
|
|
|
[TwoFactorProviderType.Email] = new TwoFactorProvider
|
|
|
|
|
{
|
|
|
|
|
MetaData = new Dictionary<string, object> { ["Email"] = "asdfasf" },
|
|
|
|
|
Enabled = true
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<Settings.IGlobalSettings>().TwoFactorAuth.EmailOnNewDeviceLogin.Returns(true);
|
|
|
|
|
|
|
|
|
|
Assert.False(sutProvider.Sut.CanEditDeviceVerificationSettings(user));
|
|
|
|
|
}
|
2022-06-16 22:30:50 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-06-16 22:30:50 +02:00
|
|
|
|
public async void HasPremiumFromOrganization_Returns_False_If_No_Orgs(SutProvider<UserService> sutProvider, User user)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>().GetManyByUserAsync(user.Id).Returns(new List<OrganizationUser>());
|
|
|
|
|
Assert.False(await sutProvider.Sut.HasPremiumFromOrganization(user));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[BitAutoData(false, true)]
|
|
|
|
|
[BitAutoData(true, false)]
|
2022-06-16 22:30:50 +02:00
|
|
|
|
public async void HasPremiumFromOrganization_Returns_False_If_Org_Not_Eligible(bool orgEnabled, bool orgUsersGetPremium, SutProvider<UserService> sutProvider, User user, OrganizationUser orgUser, Organization organization)
|
|
|
|
|
{
|
|
|
|
|
orgUser.OrganizationId = organization.Id;
|
|
|
|
|
organization.Enabled = orgEnabled;
|
|
|
|
|
organization.UsersGetPremium = orgUsersGetPremium;
|
|
|
|
|
var orgAbilities = new Dictionary<Guid, OrganizationAbility>() { { organization.Id, new OrganizationAbility(organization) } };
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>().GetManyByUserAsync(user.Id).Returns(new List<OrganizationUser>() { orgUser });
|
|
|
|
|
sutProvider.GetDependency<IApplicationCacheService>().GetOrganizationAbilitiesAsync().Returns(orgAbilities);
|
|
|
|
|
|
|
|
|
|
Assert.False(await sutProvider.Sut.HasPremiumFromOrganization(user));
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-06-16 22:30:50 +02:00
|
|
|
|
public async void HasPremiumFromOrganization_Returns_True_If_Org_Eligible(SutProvider<UserService> sutProvider, User user, OrganizationUser orgUser, Organization organization)
|
|
|
|
|
{
|
|
|
|
|
orgUser.OrganizationId = organization.Id;
|
|
|
|
|
organization.Enabled = true;
|
|
|
|
|
organization.UsersGetPremium = true;
|
|
|
|
|
var orgAbilities = new Dictionary<Guid, OrganizationAbility>() { { organization.Id, new OrganizationAbility(organization) } };
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>().GetManyByUserAsync(user.Id).Returns(new List<OrganizationUser>() { orgUser });
|
|
|
|
|
sutProvider.GetDependency<IApplicationCacheService>().GetOrganizationAbilitiesAsync().Returns(orgAbilities);
|
|
|
|
|
|
|
|
|
|
Assert.True(await sutProvider.Sut.HasPremiumFromOrganization(user));
|
2019-07-06 05:35:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|