mirror of
https://github.com/bitwarden/server.git
synced 2024-11-29 13:25:17 +01:00
Test org sponsorship service
This commit is contained in:
parent
1d11dac2cc
commit
f1832e899f
@ -128,6 +128,11 @@ namespace Bit.Core.Services
|
||||
public async Task<bool> ValidateSponsorshipAsync(Guid sponsoredOrganizationId)
|
||||
{
|
||||
var sponsoredOrganization = await _organizationRepository.GetByIdAsync(sponsoredOrganizationId);
|
||||
if (sponsoredOrganization == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var existingSponsorship = await _organizationSponsorshipRepository
|
||||
.GetBySponsoredOrganizationIdAsync(sponsoredOrganizationId);
|
||||
|
||||
@ -164,12 +169,15 @@ namespace Bit.Core.Services
|
||||
|
||||
public async Task RemoveSponsorshipAsync(Organization sponsoredOrganization, OrganizationSponsorship sponsorship = null)
|
||||
{
|
||||
await _paymentService.RemoveOrganizationSponsorshipAsync(sponsoredOrganization, sponsorship);
|
||||
await _organizationRepository.UpsertAsync(sponsoredOrganization);
|
||||
if (sponsoredOrganization != null)
|
||||
{
|
||||
await _paymentService.RemoveOrganizationSponsorshipAsync(sponsoredOrganization, sponsorship);
|
||||
await _organizationRepository.UpsertAsync(sponsoredOrganization);
|
||||
|
||||
await _mailService.SendFamiliesForEnterpriseSponsorshipRevertingEmailAsync(
|
||||
sponsoredOrganization.BillingEmailAddress(),
|
||||
sponsoredOrganization.Name);
|
||||
await _mailService.SendFamiliesForEnterpriseSponsorshipRevertingEmailAsync(
|
||||
sponsoredOrganization.BillingEmailAddress(),
|
||||
sponsoredOrganization.Name);
|
||||
}
|
||||
|
||||
if (sponsorship == null)
|
||||
{
|
||||
|
@ -11,18 +11,18 @@ namespace Bit.Test.Common.Helpers
|
||||
{
|
||||
public static class BitAutoDataAttributeHelpers
|
||||
{
|
||||
public static IEnumerable<object[]> GetData(MethodInfo testMethod, IFixture fixture, object[] fixedTestParamters)
|
||||
public static IEnumerable<object[]> GetData(MethodInfo testMethod, IFixture fixture, object[] fixedTestParameters)
|
||||
{
|
||||
var methodParameters = testMethod.GetParameters();
|
||||
var classCustomizations = testMethod.DeclaringType.GetCustomAttributes<BitCustomizeAttribute>().Select(attr => attr.GetCustomization());
|
||||
var methodCustomizations = testMethod.GetCustomAttributes<BitCustomizeAttribute>().Select(attr => attr.GetCustomization());
|
||||
|
||||
fixedTestParamters = fixedTestParamters ?? Array.Empty<object>();
|
||||
fixedTestParameters = fixedTestParameters ?? Array.Empty<object>();
|
||||
|
||||
fixture = ApplyCustomizations(ApplyCustomizations(fixture, classCustomizations), methodCustomizations);
|
||||
var missingParameters = methodParameters.Skip(fixedTestParamters.Length).Select(p => CustomizeAndCreate(p, fixture));
|
||||
var missingParameters = methodParameters.Skip(fixedTestParameters.Length).Select(p => CustomizeAndCreate(p, fixture));
|
||||
|
||||
return new object[1][] { fixedTestParamters.Concat(missingParameters).ToArray() };
|
||||
return new object[1][] { fixedTestParameters.Concat(missingParameters).ToArray() };
|
||||
}
|
||||
|
||||
public static object CustomizeAndCreate(ParameterInfo p, IFixture fixture)
|
||||
|
@ -1,18 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.Table;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Core.Utilities;
|
||||
using Bit.Test.Common.AutoFixture;
|
||||
using Bit.Test.Common.AutoFixture.Attributes;
|
||||
using Bit.Test.Common.Helpers;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using NSubstitute;
|
||||
using NSubstitute.ExceptionExtensions;
|
||||
using NSubstitute.Extensions;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Core.Test.Services
|
||||
@ -33,6 +33,12 @@ namespace Bit.Core.Test.Services
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> EnterprisePlanTypes =>
|
||||
Enum.GetValues<PlanType>().Where(p => StaticStore.GetPlan(p).Product == ProductType.Enterprise).Select(p => new object[] { p });
|
||||
|
||||
public static IEnumerable<object[]> NonEnterprisePlanTypes =>
|
||||
Enum.GetValues<PlanType>().Where(p => StaticStore.GetPlan(p).Product != ProductType.Enterprise).Select(p => new object[] { p });
|
||||
|
||||
[Theory]
|
||||
[BitAutoData]
|
||||
public async Task OfferSponsorship_CreatesSponsorship(Organization sponsoringOrg, OrganizationUser sponsoringOrgUser,
|
||||
@ -104,8 +110,254 @@ namespace Bit.Core.Test.Services
|
||||
.SendFamiliesForEnterpriseOfferEmailAsync(sponsorship.OfferedToEmail, org.Name, Arg.Any<string>());
|
||||
}
|
||||
|
||||
// TODO: test validateSponsorshipAsync
|
||||
private async Task AssertRemovedSponsoredPaymentAsync(Organization sponsoredOrg,
|
||||
OrganizationSponsorship sponsorship, SutProvider<OrganizationSponsorshipService> sutProvider)
|
||||
{
|
||||
await sutProvider.GetDependency<IPaymentService>().Received(1)
|
||||
.RemoveOrganizationSponsorshipAsync(sponsoredOrg, sponsorship);
|
||||
await sutProvider.GetDependency<IOrganizationRepository>().Received(1).UpsertAsync(sponsoredOrg);
|
||||
await sutProvider.GetDependency<IMailService>().Received(1)
|
||||
.SendFamiliesForEnterpriseSponsorshipRevertingEmailAsync(sponsoredOrg.BillingEmailAddress(), sponsoredOrg.Name);
|
||||
}
|
||||
|
||||
// TODO: test RemoveSponsorshipAsync
|
||||
private async Task AssertRemovedSponsorshipAsync(OrganizationSponsorship sponsorship,
|
||||
SutProvider<OrganizationSponsorshipService> sutProvider)
|
||||
{
|
||||
if (sponsorship.CloudSponsor || sponsorship.SponsorshipLapsedDate.HasValue)
|
||||
{
|
||||
await sutProvider.GetDependency<IOrganizationSponsorshipRepository>().Received(1)
|
||||
.DeleteAsync(sponsorship);
|
||||
}
|
||||
else
|
||||
{
|
||||
await sutProvider.GetDependency<IOrganizationSponsorshipRepository>().Received(1)
|
||||
.UpsertAsync(sponsorship);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task AssertDidNotRemoveSponsoredPaymentAsync(SutProvider<OrganizationSponsorshipService> sutProvider)
|
||||
{
|
||||
await sutProvider.GetDependency<IPaymentService>().DidNotReceiveWithAnyArgs()
|
||||
.RemoveOrganizationSponsorshipAsync(default, default);
|
||||
await sutProvider.GetDependency<IOrganizationRepository>().DidNotReceiveWithAnyArgs()
|
||||
.UpsertAsync(default);
|
||||
await sutProvider.GetDependency<IMailService>().DidNotReceiveWithAnyArgs()
|
||||
.SendFamiliesForEnterpriseSponsorshipRevertingEmailAsync(default, default);
|
||||
}
|
||||
|
||||
private static async Task AssertDidNotRemoveSponsorshipAsync(SutProvider<OrganizationSponsorshipService> sutProvider)
|
||||
{
|
||||
await sutProvider.GetDependency<IOrganizationSponsorshipRepository>().DidNotReceiveWithAnyArgs()
|
||||
.DeleteAsync(default);
|
||||
await sutProvider.GetDependency<IOrganizationSponsorshipRepository>().DidNotReceiveWithAnyArgs()
|
||||
.UpsertAsync(default);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[BitAutoData]
|
||||
public async Task ValidateSponsorshipAsync_NoSponsoredOrg_EarlyReturn(Guid sponsoredOrgId,
|
||||
SutProvider<OrganizationSponsorshipService> sutProvider)
|
||||
{
|
||||
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(sponsoredOrgId).Returns((Organization)null);
|
||||
|
||||
var result = await sutProvider.Sut.ValidateSponsorshipAsync(sponsoredOrgId);
|
||||
|
||||
Assert.False(result);
|
||||
await AssertDidNotRemoveSponsoredPaymentAsync(sutProvider);
|
||||
await AssertDidNotRemoveSponsorshipAsync(sutProvider);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[BitAutoData]
|
||||
public async Task ValidateSponsorshipAsync_NoExistingSponsorship_UpdatesStripePlan(Organization sponsoredOrg,
|
||||
SutProvider<OrganizationSponsorshipService> sutProvider)
|
||||
{
|
||||
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(sponsoredOrg.Id).Returns(sponsoredOrg);
|
||||
|
||||
var result = await sutProvider.Sut.ValidateSponsorshipAsync(sponsoredOrg.Id);
|
||||
|
||||
Assert.False(result);
|
||||
await AssertRemovedSponsoredPaymentAsync(sponsoredOrg, null, sutProvider);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[BitAutoData]
|
||||
public async Task ValidateSponsorshipAsync_SponsoringOrgNull_UpdatesStripePlan(Organization sponsoredOrg,
|
||||
OrganizationSponsorship existingSponsorship, SutProvider<OrganizationSponsorshipService> sutProvider)
|
||||
{
|
||||
existingSponsorship.SponsoringOrganizationId = null;
|
||||
|
||||
sutProvider.GetDependency<IOrganizationSponsorshipRepository>()
|
||||
.GetBySponsoredOrganizationIdAsync(sponsoredOrg.Id).Returns(existingSponsorship);
|
||||
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(sponsoredOrg.Id).Returns(sponsoredOrg);
|
||||
|
||||
var result = await sutProvider.Sut.ValidateSponsorshipAsync(sponsoredOrg.Id);
|
||||
|
||||
Assert.False(result);
|
||||
await AssertRemovedSponsoredPaymentAsync(sponsoredOrg, existingSponsorship, sutProvider);
|
||||
await AssertRemovedSponsorshipAsync(existingSponsorship, sutProvider);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[BitAutoData]
|
||||
public async Task ValidateSponsorshipAsync_SponsoringOrgUserNull_UpdatesStripePlan(Organization sponsoredOrg,
|
||||
OrganizationSponsorship existingSponsorship, SutProvider<OrganizationSponsorshipService> sutProvider)
|
||||
{
|
||||
existingSponsorship.SponsoringOrganizationUserId = null;
|
||||
|
||||
sutProvider.GetDependency<IOrganizationSponsorshipRepository>()
|
||||
.GetBySponsoredOrganizationIdAsync(sponsoredOrg.Id).Returns(existingSponsorship);
|
||||
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(sponsoredOrg.Id).Returns(sponsoredOrg);
|
||||
|
||||
var result = await sutProvider.Sut.ValidateSponsorshipAsync(sponsoredOrg.Id);
|
||||
|
||||
Assert.False(result);
|
||||
await AssertRemovedSponsoredPaymentAsync(sponsoredOrg, existingSponsorship, sutProvider);
|
||||
await AssertRemovedSponsorshipAsync(existingSponsorship, sutProvider);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[BitAutoData]
|
||||
public async Task ValidateSponsorshipAsync_SponsorshipTypeNull_UpdatesStripePlan(Organization sponsoredOrg,
|
||||
OrganizationSponsorship existingSponsorship, SutProvider<OrganizationSponsorshipService> sutProvider)
|
||||
{
|
||||
existingSponsorship.PlanSponsorshipType = null;
|
||||
|
||||
sutProvider.GetDependency<IOrganizationSponsorshipRepository>()
|
||||
.GetBySponsoredOrganizationIdAsync(sponsoredOrg.Id).Returns(existingSponsorship);
|
||||
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(sponsoredOrg.Id).Returns(sponsoredOrg);
|
||||
|
||||
var result = await sutProvider.Sut.ValidateSponsorshipAsync(sponsoredOrg.Id);
|
||||
|
||||
Assert.False(result);
|
||||
await AssertRemovedSponsoredPaymentAsync(sponsoredOrg, existingSponsorship, sutProvider);
|
||||
await AssertRemovedSponsorshipAsync(existingSponsorship, sutProvider);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[BitAutoData]
|
||||
public async Task ValidateSponsorshipAsync_SponsoringOrgNotFound_UpdatesStripePlan(Organization sponsoredOrg,
|
||||
OrganizationSponsorship existingSponsorship, SutProvider<OrganizationSponsorshipService> sutProvider)
|
||||
{
|
||||
sutProvider.GetDependency<IOrganizationSponsorshipRepository>()
|
||||
.GetBySponsoredOrganizationIdAsync(sponsoredOrg.Id).Returns(existingSponsorship);
|
||||
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(sponsoredOrg.Id).Returns(sponsoredOrg);
|
||||
|
||||
var result = await sutProvider.Sut.ValidateSponsorshipAsync(sponsoredOrg.Id);
|
||||
|
||||
Assert.False(result);
|
||||
await AssertRemovedSponsoredPaymentAsync(sponsoredOrg, existingSponsorship, sutProvider);
|
||||
await AssertRemovedSponsorshipAsync(existingSponsorship, sutProvider);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[BitMemberAutoData(nameof(NonEnterprisePlanTypes))]
|
||||
public async Task ValidateSponsorshipAsync_SponsoringOrgNotEnterprise_UpdatesStripePlan(PlanType planType,
|
||||
Organization sponsoredOrg, OrganizationSponsorship existingSponsorship, Organization sponsoringOrg,
|
||||
SutProvider<OrganizationSponsorshipService> sutProvider)
|
||||
{
|
||||
sponsoringOrg.PlanType = planType;
|
||||
existingSponsorship.SponsoringOrganizationId = sponsoringOrg.Id;
|
||||
|
||||
sutProvider.GetDependency<IOrganizationSponsorshipRepository>()
|
||||
.GetBySponsoredOrganizationIdAsync(sponsoredOrg.Id).Returns(existingSponsorship);
|
||||
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(sponsoredOrg.Id).Returns(sponsoredOrg);
|
||||
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(sponsoringOrg.Id).Returns(sponsoringOrg);
|
||||
|
||||
var result = await sutProvider.Sut.ValidateSponsorshipAsync(sponsoredOrg.Id);
|
||||
|
||||
Assert.False(result);
|
||||
await AssertRemovedSponsoredPaymentAsync(sponsoredOrg, existingSponsorship, sutProvider);
|
||||
await AssertRemovedSponsorshipAsync(existingSponsorship, sutProvider);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[BitMemberAutoData(nameof(EnterprisePlanTypes))]
|
||||
public async Task ValidateSponsorshipAsync_SponsoringOrgDisabled_UpdatesStripePlan(PlanType planType,
|
||||
Organization sponsoredOrg, OrganizationSponsorship existingSponsorship, Organization sponsoringOrg,
|
||||
SutProvider<OrganizationSponsorshipService> sutProvider)
|
||||
{
|
||||
sponsoringOrg.PlanType = planType;
|
||||
sponsoringOrg.Enabled = false;
|
||||
existingSponsorship.SponsoringOrganizationId = sponsoringOrg.Id;
|
||||
|
||||
sutProvider.GetDependency<IOrganizationSponsorshipRepository>()
|
||||
.GetBySponsoredOrganizationIdAsync(sponsoredOrg.Id).Returns(existingSponsorship);
|
||||
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(sponsoredOrg.Id).Returns(sponsoredOrg);
|
||||
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(sponsoringOrg.Id).Returns(sponsoringOrg);
|
||||
|
||||
var result = await sutProvider.Sut.ValidateSponsorshipAsync(sponsoredOrg.Id);
|
||||
|
||||
Assert.False(result);
|
||||
await AssertRemovedSponsoredPaymentAsync(sponsoredOrg, existingSponsorship, sutProvider);
|
||||
await AssertRemovedSponsorshipAsync(existingSponsorship, sutProvider);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[BitMemberAutoData(nameof(EnterprisePlanTypes))]
|
||||
public async Task ValidateSponsorshipAsync_Valid(PlanType planType,
|
||||
Organization sponsoredOrg, OrganizationSponsorship existingSponsorship, Organization sponsoringOrg,
|
||||
SutProvider<OrganizationSponsorshipService> sutProvider)
|
||||
{
|
||||
sponsoringOrg.PlanType = planType;
|
||||
sponsoringOrg.Enabled = true;
|
||||
existingSponsorship.SponsoringOrganizationId = sponsoringOrg.Id;
|
||||
|
||||
sutProvider.GetDependency<IOrganizationSponsorshipRepository>()
|
||||
.GetBySponsoredOrganizationIdAsync(sponsoredOrg.Id).Returns(existingSponsorship);
|
||||
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(sponsoredOrg.Id).Returns(sponsoredOrg);
|
||||
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(sponsoringOrg.Id).Returns(sponsoringOrg);
|
||||
|
||||
var result = await sutProvider.Sut.ValidateSponsorshipAsync(sponsoredOrg.Id);
|
||||
|
||||
Assert.True(result);
|
||||
|
||||
await AssertDidNotRemoveSponsoredPaymentAsync(sutProvider);
|
||||
await AssertDidNotRemoveSponsorshipAsync(sutProvider);
|
||||
}
|
||||
|
||||
|
||||
[Theory]
|
||||
[BitAutoData]
|
||||
public async Task RemoveSponsorshipAsync_NullDoNothing(SutProvider<OrganizationSponsorshipService> sutProvider)
|
||||
{
|
||||
await sutProvider.Sut.RemoveSponsorshipAsync(null, null);
|
||||
|
||||
await AssertDidNotRemoveSponsoredPaymentAsync(sutProvider);
|
||||
await AssertDidNotRemoveSponsorshipAsync(sutProvider);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[BitAutoData]
|
||||
public async Task RemoveSponsorshipAsync_NullSponsoredOrg(OrganizationSponsorship sponsorship,
|
||||
SutProvider<OrganizationSponsorshipService> sutProvider)
|
||||
{
|
||||
await sutProvider.Sut.RemoveSponsorshipAsync(null, sponsorship);
|
||||
|
||||
await AssertDidNotRemoveSponsoredPaymentAsync(sutProvider);
|
||||
await AssertRemovedSponsorshipAsync(sponsorship, sutProvider);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[BitAutoData]
|
||||
public async Task RemoveSponsorshipAsync_NullSponsorship(Organization sponsoredOrg,
|
||||
SutProvider<OrganizationSponsorshipService> sutProvider)
|
||||
{
|
||||
await sutProvider.Sut.RemoveSponsorshipAsync(sponsoredOrg, null);
|
||||
|
||||
await AssertRemovedSponsoredPaymentAsync(sponsoredOrg, null, sutProvider);
|
||||
await AssertDidNotRemoveSponsorshipAsync(sutProvider);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[BitAutoData]
|
||||
public async Task RemoveSponsorshipAsync_RemoveBoth(Organization sponsoredOrg,
|
||||
OrganizationSponsorship sponsorship, SutProvider<OrganizationSponsorshipService> sutProvider)
|
||||
{
|
||||
await sutProvider.Sut.RemoveSponsorshipAsync(sponsoredOrg, sponsorship);
|
||||
|
||||
await AssertRemovedSponsoredPaymentAsync(sponsoredOrg, sponsorship, sutProvider);
|
||||
await AssertRemovedSponsorshipAsync(sponsorship, sutProvider);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user