1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-05 14:23:30 +01:00
bitwarden-server/test/Core.Test/Services/OrganizationSponsorshipServiceTests.cs

112 lines
4.6 KiB
C#
Raw Normal View History

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;
2021-11-09 23:51:36 +01:00
using Microsoft.AspNetCore.DataProtection;
2021-11-04 14:25:40 +01:00
using Microsoft.IdentityModel.Tokens;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
2021-11-09 23:51:36 +01:00
using NSubstitute.Extensions;
2021-11-04 14:25:40 +01:00
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 23:51:36 +01:00
string sponsoredEmail, string friendlyName, Guid sponsorshipId,
SutProvider<OrganizationSponsorshipService> sutProvider)
2021-11-04 14:25:40 +01:00
{
2021-11-09 23:51:36 +01:00
var dataProtector = Substitute.For<IDataProtector>();
sutProvider.GetDependency<IDataProtectionProvider>().CreateProtector(default).ReturnsForAnyArgs(dataProtector);
sutProvider.GetDependency<IOrganizationSponsorshipRepository>().CreateAsync(default).ReturnsForAnyArgs(callInfo =>
{
var sponsorship = callInfo.Arg<OrganizationSponsorship>();
sponsorship.Id = sponsorshipId;
return sponsorship;
});
2021-11-04 17:51:22 +01:00
await sutProvider.Sut.OfferSponsorshipAsync(sponsoringOrg, sponsoringOrgUser,
PlanSponsorshipType.FamiliesForEnterprise, sponsoredEmail, friendlyName);
2021-11-04 14:25:40 +01:00
var expectedSponsorship = new OrganizationSponsorship
{
2021-11-09 23:51:36 +01:00
Id = sponsorshipId,
2021-11-04 14:25:40 +01:00
SponsoringOrganizationId = sponsoringOrg.Id,
SponsoringOrganizationUserId = sponsoringOrgUser.Id,
FriendlyName = friendlyName,
2021-11-04 14:25:40 +01:00
OfferedToEmail = sponsoredEmail,
2021-11-09 16:40:31 +01:00
PlanSponsorshipType = PlanSponsorshipType.FamiliesForEnterprise,
2021-11-04 14:25:40 +01:00
CloudSponsor = true,
};
await sutProvider.GetDependency<IOrganizationSponsorshipRepository>().Received(1)
.CreateAsync(Arg.Is<OrganizationSponsorship>(s => sponsorshipValidator(s, expectedSponsorship)));
2021-11-09 23:51:36 +01:00
await sutProvider.GetDependency<IMailService>().Received(1).
SendFamiliesForEnterpriseOfferEmailAsync(sponsoredEmail, sponsoringOrg.Name,
Arg.Any<string>());
2021-11-04 14:25:40 +01:00
}
[Theory]
[BitAutoData]
public async Task OfferSponsorship_CreateSponsorshipThrows_RevertsDatabase(Organization sponsoringOrg, OrganizationUser sponsoringOrgUser,
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,
PlanSponsorshipType.FamiliesForEnterprise, sponsoredEmail, friendlyName));
2021-11-04 14:25:40 +01:00
Assert.Same(expectedException, actualException);
await sutProvider.GetDependency<IOrganizationSponsorshipRepository>().Received(1)
.DeleteAsync(createdSponsorship);
}
[Theory]
[BitAutoData]
public async Task SendSponsorshipOfferAsync(Organization org, OrganizationSponsorship sponsorship,
SutProvider<OrganizationSponsorshipService> sutProvider)
{
await sutProvider.Sut.SendSponsorshipOfferAsync(org, sponsorship);
await sutProvider.GetDependency<IMailService>().Received(1)
.SendFamiliesForEnterpriseOfferEmailAsync(sponsorship.OfferedToEmail, org.Name, Arg.Any<string>());
}
// TODO: test validateSponsorshipAsync
// TODO: test RemoveSponsorshipAsync
2021-11-04 14:25:40 +01:00
}
}