2021-11-04 14:25:40 +01:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2021-11-04 17:51:22 +01:00
|
|
|
using Bit.Core.Enums;
|
2021-11-04 14:25:40 +01:00
|
|
|
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,
|
2021-11-09 00:01:09 +01:00
|
|
|
string sponsoredEmail, string friendlyName, SutProvider<OrganizationSponsorshipService> sutProvider)
|
2021-11-04 14:25:40 +01:00
|
|
|
{
|
2021-11-04 17:51:22 +01:00
|
|
|
await sutProvider.Sut.OfferSponsorshipAsync(sponsoringOrg, sponsoringOrgUser,
|
2021-11-09 00:01:09 +01:00
|
|
|
PlanSponsorshipType.FamiliesForEnterprise, sponsoredEmail, friendlyName);
|
2021-11-04 14:25:40 +01:00
|
|
|
|
|
|
|
var expectedSponsorship = new OrganizationSponsorship
|
|
|
|
{
|
|
|
|
SponsoringOrganizationId = sponsoringOrg.Id,
|
|
|
|
SponsoringOrganizationUserId = sponsoringOrgUser.Id,
|
2021-11-09 00:01:09 +01:00
|
|
|
FriendlyName = friendlyName,
|
2021-11-04 14:25:40 +01:00
|
|
|
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,
|
2021-11-09 00:01:09 +01:00
|
|
|
string sponsoredEmail, string friendlyName, SutProvider<OrganizationSponsorshipService> sutProvider)
|
2021-11-04 14:25:40 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
2021-11-04 17:51:22 +01:00
|
|
|
var actualException = await Assert.ThrowsAsync<Exception>(() =>
|
|
|
|
sutProvider.Sut.OfferSponsorshipAsync(sponsoringOrg, sponsoringOrgUser,
|
2021-11-09 00:01:09 +01:00
|
|
|
PlanSponsorshipType.FamiliesForEnterprise, sponsoredEmail, friendlyName));
|
2021-11-04 14:25:40 +01:00
|
|
|
Assert.Same(expectedException, actualException);
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IOrganizationSponsorshipRepository>().Received(1)
|
|
|
|
.DeleteAsync(createdSponsorship);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|