2020-02-20 14:36:31 -05:00
|
|
|
|
using System;
|
2019-07-05 22:35:54 -05:00
|
|
|
|
using System.Collections.Generic;
|
2022-01-21 09:36:25 -05:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2021-02-04 12:54:21 -06:00
|
|
|
|
using Bit.Core.Context;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Entities;
|
2022-01-21 09:36:25 -05:00
|
|
|
|
using Bit.Core.Models.Business;
|
2019-07-05 22:35:54 -05:00
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
|
using Bit.Core.Services;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
2022-01-21 09:36:25 -05:00
|
|
|
|
using Bit.Test.Common.AutoFixture;
|
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
|
|
|
|
using Bit.Test.Common.Helpers;
|
2021-03-22 23:21:43 +01:00
|
|
|
|
using Fido2NetLib;
|
2019-07-05 22:35:54 -05:00
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Test.Services
|
|
|
|
|
|
{
|
|
|
|
|
|
public class UserServiceTests
|
|
|
|
|
|
{
|
2022-01-21 09:36:25 -05:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
|
public async Task UpdateLicenseAsync_Success(SutProvider<UserService> sutProvider,
|
|
|
|
|
|
User user, UserLicense userLicense)
|
|
|
|
|
|
{
|
|
|
|
|
|
using var tempDir = new TempDirectory();
|
2019-07-05 22:35:54 -05:00
|
|
|
|
|
2022-01-21 09:36:25 -05:00
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
|
userLicense.Issued = now.AddDays(-10);
|
|
|
|
|
|
userLicense.Expires = now.AddDays(10);
|
|
|
|
|
|
userLicense.Version = 1;
|
|
|
|
|
|
userLicense.Premium = true;
|
2019-07-05 22:35:54 -05:00
|
|
|
|
|
2022-01-21 09:36:25 -05:00
|
|
|
|
user.EmailVerified = true;
|
|
|
|
|
|
user.Email = userLicense.Email;
|
2019-07-05 22:35:54 -05:00
|
|
|
|
|
2022-01-21 09:36:25 -05:00
|
|
|
|
sutProvider.GetDependency<Settings.GlobalSettings>().SelfHosted = true;
|
|
|
|
|
|
sutProvider.GetDependency<Settings.GlobalSettings>().LicenseDirectory = tempDir.Directory;
|
|
|
|
|
|
sutProvider.GetDependency<ILicensingService>()
|
|
|
|
|
|
.VerifyLicense(userLicense)
|
|
|
|
|
|
.Returns(true);
|
2019-07-05 22:35:54 -05:00
|
|
|
|
|
2022-01-21 09:36:25 -05:00
|
|
|
|
await sutProvider.Sut.UpdateLicenseAsync(user, userLicense);
|
|
|
|
|
|
|
|
|
|
|
|
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-05 22:35:54 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|