mirror of
https://github.com/bitwarden/server.git
synced 2024-12-02 13:53:23 +01:00
6357514064
* Add PlanSponsorshipType to static store * Add sponsorship type to token and creates sponsorship * PascalCase properties * Require sponsorship for remove * Create subscription sponsorship helper class * Handle Sponsored subscription changes * Add sponsorship id to subscription metadata * Make sponsoring references nullable This state indicates that a sponsorship has lapsed, but was not able to be reverted for billing reasons * WIP: Validate and remove subscriptions * Update sponsorships on organization and org user delete * Add friendly name to organization sponsorship
80 lines
3.2 KiB
C#
80 lines
3.2 KiB
C#
using System;
|
|
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.Test.Common.AutoFixture;
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
using Bit.Test.Common.Helpers;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using NSubstitute;
|
|
using NSubstitute.ExceptionExtensions;
|
|
using Xunit;
|
|
|
|
namespace Bit.Core.Test.Services
|
|
{
|
|
[SutProviderCustomize]
|
|
public class OrganizationSponsorshipServiceTests
|
|
{
|
|
private bool sponsorshipValidator(OrganizationSponsorship sponsorship, OrganizationSponsorship expectedSponsorship)
|
|
{
|
|
try
|
|
{
|
|
AssertHelper.AssertPropertyEqual(sponsorship, expectedSponsorship, nameof(OrganizationSponsorship.Id));
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[BitAutoData]
|
|
public async Task OfferSponsorship_CreatesSponsorship(Organization sponsoringOrg, OrganizationUser sponsoringOrgUser,
|
|
string sponsoredEmail, string friendlyName, SutProvider<OrganizationSponsorshipService> sutProvider)
|
|
{
|
|
await sutProvider.Sut.OfferSponsorshipAsync(sponsoringOrg, sponsoringOrgUser,
|
|
PlanSponsorshipType.FamiliesForEnterprise, sponsoredEmail, friendlyName);
|
|
|
|
var expectedSponsorship = new OrganizationSponsorship
|
|
{
|
|
SponsoringOrganizationId = sponsoringOrg.Id,
|
|
SponsoringOrganizationUserId = sponsoringOrgUser.Id,
|
|
FriendlyName = friendlyName,
|
|
OfferedToEmail = sponsoredEmail,
|
|
CloudSponsor = true,
|
|
};
|
|
|
|
await sutProvider.GetDependency<IOrganizationSponsorshipRepository>().Received(1)
|
|
.CreateAsync(Arg.Is<OrganizationSponsorship>(s => sponsorshipValidator(s, expectedSponsorship)));
|
|
// TODO: Validate email called with appropriate token.s
|
|
}
|
|
|
|
[Theory]
|
|
[BitAutoData]
|
|
public async Task OfferSponsorship_CreateSponsorshipThrows_RevertsDatabase(Organization sponsoringOrg, OrganizationUser sponsoringOrgUser,
|
|
string sponsoredEmail, string friendlyName, SutProvider<OrganizationSponsorshipService> sutProvider)
|
|
{
|
|
var expectedException = new Exception();
|
|
OrganizationSponsorship createdSponsorship = null;
|
|
sutProvider.GetDependency<IOrganizationSponsorshipRepository>().CreateAsync(default).ThrowsForAnyArgs(callInfo =>
|
|
{
|
|
createdSponsorship = callInfo.ArgAt<OrganizationSponsorship>(0);
|
|
createdSponsorship.Id = Guid.NewGuid();
|
|
return expectedException;
|
|
});
|
|
|
|
var actualException = await Assert.ThrowsAsync<Exception>(() =>
|
|
sutProvider.Sut.OfferSponsorshipAsync(sponsoringOrg, sponsoringOrgUser,
|
|
PlanSponsorshipType.FamiliesForEnterprise, sponsoredEmail, friendlyName));
|
|
Assert.Same(expectedException, actualException);
|
|
|
|
await sutProvider.GetDependency<IOrganizationSponsorshipRepository>().Received(1)
|
|
.DeleteAsync(createdSponsorship);
|
|
}
|
|
}
|
|
}
|