1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-29 13:25:17 +01:00

Add resend sponsorship offer api endpoint

This commit is contained in:
Matt Gibson 2021-11-10 17:00:48 -05:00 committed by Justin Baur
parent a605dc7286
commit 32333048b4
5 changed files with 172 additions and 24 deletions

View File

@ -72,6 +72,33 @@ namespace Bit.Api.Controllers
model.PlanSponsorshipType, model.SponsoredEmail, model.FriendlyName);
}
[HttpPost("{sponsoringOrgId}/families-for-enterprise/resend")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task ResendSponsorshipOffer(string sponsoringOrgId)
{
// TODO: validate has right to sponsor, send sponsorship email
var sponsoringOrgIdGuid = new Guid(sponsoringOrgId);
var sponsoringOrg = await _organizationRepository.GetByIdAsync(sponsoringOrgIdGuid);
if (sponsoringOrg == null)
{
throw new BadRequestException("Cannot find the requested sponsoring organization.");
}
var sponsoringOrgUser = await _organizationUserRepository.GetByOrganizationAsync(sponsoringOrgIdGuid, _currentContext.UserId ?? default);
if (sponsoringOrgUser == null || sponsoringOrgUser.Status != OrganizationUserStatusType.Confirmed)
{
throw new BadRequestException("Only confirmed users can sponsor other organizations.");
}
var existingOrgSponsorship = await _organizationSponsorshipRepository.GetBySponsoringOrganizationUserIdAsync(sponsoringOrgUser.Id);
if (existingOrgSponsorship == null || existingOrgSponsorship.OfferedToEmail == null)
{
throw new BadRequestException("Cannot find an outstanding sponsorship offer for this organization.");
}
await _organizationsSponsorshipService.SendSponsorshipOfferAsync(sponsoringOrg, existingOrgSponsorship);
}
[HttpPost("redeem")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task RedeemSponsorship([FromQuery] string sponsorshipToken, [FromBody] OrganizationSponsorshipRedeemRequestModel model)

View File

@ -10,6 +10,7 @@ namespace Bit.Core.Services
Task<bool> ValidateRedemptionTokenAsync(string encryptedToken);
Task OfferSponsorshipAsync(Organization sponsoringOrg, OrganizationUser sponsoringOrgUser,
PlanSponsorshipType sponsorshipType, string sponsoredEmail, string friendlyName);
Task SendSponsorshipOfferAsync(Organization sponsoringOrg, OrganizationSponsorship sponsorship);
Task SetUpSponsorshipAsync(OrganizationSponsorship sponsorship, Organization sponsoredOrganization);
Task<bool> ValidateSponsorshipAsync(Guid sponsoredOrganizationId);
Task RemoveSponsorshipAsync(Organization sponsoredOrganization, OrganizationSponsorship sponsorship);

View File

@ -91,6 +91,7 @@ namespace Bit.Core.Services
{
sponsorship = await _organizationSponsorshipRepository.CreateAsync(sponsorship);
await SendSponsorshipOfferAsync(sponsoringOrg, sponsorship);
await _mailService.SendFamiliesForEnterpriseOfferEmailAsync(sponsoredEmail, sponsoringOrg.Name,
RedemptionToken(sponsorship.Id, sponsorshipType));
}
@ -104,6 +105,12 @@ namespace Bit.Core.Services
}
}
public async Task SendSponsorshipOfferAsync(Organization sponsoringOrg, OrganizationSponsorship sponsorship)
{
await _mailService.SendFamiliesForEnterpriseOfferEmailAsync(sponsorship.OfferedToEmail, sponsoringOrg.Name,
RedemptionToken(sponsorship.Id, sponsorship.PlanSponsorshipType.Value));
}
public async Task SetUpSponsorshipAsync(OrganizationSponsorship sponsorship, Organization sponsoredOrganization)
{
if (sponsorship.PlanSponsorshipType == null)

View File

@ -103,6 +103,100 @@ namespace Bit.Api.Test.Controllers
.OfferSponsorshipAsync(default, default, default, default, default);
}
[Theory]
[BitAutoData]
public async Task ResendSponsorshipOffer_SponsoringOrgNotFound_ThrowsBadRequest(Guid sponsoringOrgId,
SutProvider<OrganizationSponsorshipsController> sutProvider)
{
var exception = await Assert.ThrowsAsync<BadRequestException>(() => sutProvider.Sut.ResendSponsorshipOffer(sponsoringOrgId.ToString()));
Assert.Contains("Cannot find the requested sponsoring organization.", exception.Message);
await sutProvider.GetDependency<IOrganizationSponsorshipService>()
.DidNotReceiveWithAnyArgs()
.SendSponsorshipOfferAsync(default, default);
}
[Theory]
[BitAutoData]
public async Task ResendSponsorshipOffer_SponsoringOrgUserNotFound_ThrowsBadRequest(Organization org,
SutProvider<OrganizationSponsorshipsController> sutProvider)
{
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(org.Id).Returns(org);
var exception = await Assert.ThrowsAsync<BadRequestException>(() => sutProvider.Sut.ResendSponsorshipOffer(org.Id.ToString()));
Assert.Contains("Only confirmed users can sponsor other organizations.", exception.Message);
await sutProvider.GetDependency<IOrganizationSponsorshipService>()
.DidNotReceiveWithAnyArgs()
.SendSponsorshipOfferAsync(default, default);
}
[Theory]
[BitAutoData]
[BitMemberAutoData(nameof(NonConfirmedOrganizationUsersStatuses))]
public async Task ResendSponsorshipOffer_SponsoringOrgUserNotConfirmed_ThrowsBadRequest(OrganizationUserStatusType status,
Organization org, OrganizationUser orgUser,
SutProvider<OrganizationSponsorshipsController> sutProvider)
{
orgUser.Status = status;
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(org.Id).Returns(org);
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(orgUser.UserId);
sutProvider.GetDependency<IOrganizationUserRepository>().GetByOrganizationAsync(org.Id, orgUser.UserId.Value)
.Returns(orgUser);
var exception = await Assert.ThrowsAsync<BadRequestException>(() => sutProvider.Sut.ResendSponsorshipOffer(org.Id.ToString()));
Assert.Contains("Only confirmed users can sponsor other organizations.", exception.Message);
await sutProvider.GetDependency<IOrganizationSponsorshipService>()
.DidNotReceiveWithAnyArgs()
.SendSponsorshipOfferAsync(default, default);
}
[Theory]
[BitAutoData]
public async Task ResendSponsorshipOffer_SponsorshipNotFound_ThrowsBadRequest(Organization org,
OrganizationUser orgUser, SutProvider<OrganizationSponsorshipsController> sutProvider)
{
orgUser.Status = OrganizationUserStatusType.Confirmed;
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(org.Id).Returns(org);
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(orgUser.UserId);
sutProvider.GetDependency<IOrganizationUserRepository>().GetByOrganizationAsync(org.Id, orgUser.UserId.Value)
.Returns(orgUser);
var exception = await Assert.ThrowsAsync<BadRequestException>(() => sutProvider.Sut.ResendSponsorshipOffer(org.Id.ToString()));
Assert.Contains("Cannot find an outstanding sponsorship offer for this organization.", exception.Message);
await sutProvider.GetDependency<IOrganizationSponsorshipService>()
.DidNotReceiveWithAnyArgs()
.SendSponsorshipOfferAsync(default, default);
}
[Theory]
[BitAutoData]
public async Task ResendSponsorshipOffer_NoOfferToEmail_ThrowsBadRequest(Organization org,
OrganizationUser orgUser, OrganizationSponsorship sponsorship,
SutProvider<OrganizationSponsorshipsController> sutProvider)
{
orgUser.Status = OrganizationUserStatusType.Confirmed;
sponsorship.OfferedToEmail = null;
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(org.Id).Returns(org);
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(orgUser.UserId);
sutProvider.GetDependency<IOrganizationUserRepository>().GetByOrganizationAsync(org.Id, orgUser.UserId.Value)
.Returns(orgUser);
sutProvider.GetDependency<IOrganizationSponsorshipRepository>().GetBySponsoringOrganizationUserIdAsync(orgUser.Id)
.Returns(sponsorship);
var exception = await Assert.ThrowsAsync<BadRequestException>(() => sutProvider.Sut.ResendSponsorshipOffer(org.Id.ToString()));
Assert.Contains("Cannot find an outstanding sponsorship offer for this organization.", exception.Message);
await sutProvider.GetDependency<IOrganizationSponsorshipService>()
.DidNotReceiveWithAnyArgs()
.SendSponsorshipOfferAsync(default, default);
}
[Theory]
[BitAutoData]
public async Task RedeemSponsorship_BadToken_ThrowsBadRequest(string sponsorshipToken,
@ -147,7 +241,8 @@ namespace Bit.Api.Test.Controllers
sutProvider.GetDependency<IOrganizationSponsorshipService>().ValidateRedemptionTokenAsync(sponsorshipToken)
.Returns(true);
sutProvider.GetDependency<ICurrentContext>().OrganizationOwner(model.SponsoredOrganizationId).Returns(true);
sutProvider.GetDependency<ICurrentContext>().User.Returns(user);
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(user.Id);
sutProvider.GetDependency<IUserService>().GetUserByIdAsync(user.Id).Returns(user);
sutProvider.GetDependency<IOrganizationSponsorshipRepository>().GetByOfferedToEmailAsync(user.Email)
.Returns((OrganizationSponsorship)null);
@ -169,7 +264,8 @@ namespace Bit.Api.Test.Controllers
sutProvider.GetDependency<IOrganizationSponsorshipService>().ValidateRedemptionTokenAsync(sponsorshipToken)
.Returns(true);
sutProvider.GetDependency<ICurrentContext>().OrganizationOwner(model.SponsoredOrganizationId).Returns(true);
sutProvider.GetDependency<ICurrentContext>().User.Returns(user);
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(user.Id);
sutProvider.GetDependency<IUserService>().GetUserByIdAsync(user.Id).Returns(user);
sutProvider.GetDependency<IOrganizationSponsorshipRepository>().GetByOfferedToEmailAsync(user.Email)
.Returns(sponsorship);
@ -193,7 +289,8 @@ namespace Bit.Api.Test.Controllers
sutProvider.GetDependency<IOrganizationSponsorshipService>().ValidateRedemptionTokenAsync(sponsorshipToken)
.Returns(true);
sutProvider.GetDependency<ICurrentContext>().OrganizationOwner(model.SponsoredOrganizationId).Returns(true);
sutProvider.GetDependency<ICurrentContext>().User.Returns(user);
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(user.Id);
sutProvider.GetDependency<IUserService>().GetUserByIdAsync(user.Id).Returns(user);
sutProvider.GetDependency<IOrganizationSponsorshipRepository>()
.GetByOfferedToEmailAsync(sponsorship.OfferedToEmail).Returns(sponsorship);
sutProvider.GetDependency<IOrganizationSponsorshipRepository>()
@ -220,7 +317,8 @@ namespace Bit.Api.Test.Controllers
sutProvider.GetDependency<IOrganizationSponsorshipService>().ValidateRedemptionTokenAsync(sponsorshipToken)
.Returns(true);
sutProvider.GetDependency<ICurrentContext>().OrganizationOwner(model.SponsoredOrganizationId).Returns(true);
sutProvider.GetDependency<ICurrentContext>().User.Returns(user);
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(user.Id);
sutProvider.GetDependency<IUserService>().GetUserByIdAsync(user.Id).Returns(user);
sutProvider.GetDependency<IOrganizationSponsorshipRepository>()
.GetByOfferedToEmailAsync(sponsorship.OfferedToEmail).Returns(sponsorship);
sutProvider.GetDependency<IOrganizationSponsorshipRepository>()
@ -256,21 +354,21 @@ namespace Bit.Api.Test.Controllers
[Theory]
[BitAutoData]
public async Task RevokeSponsorship_NoExistingSponsorship_ThrowsBadRequest(OrganizationUser sponsoringOrgUser,
public async Task RevokeSponsorship_NoExistingSponsorship_ThrowsBadRequest(OrganizationUser orgUser,
OrganizationSponsorship sponsorship, SutProvider<OrganizationSponsorshipsController> sutProvider)
{
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(sponsoringOrgUser.UserId);
sutProvider.GetDependency<IOrganizationUserRepository>().GetByIdAsync(sponsoringOrgUser.Id)
.Returns(sponsoringOrgUser);
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(orgUser.UserId);
sutProvider.GetDependency<IOrganizationUserRepository>().GetByOrganizationAsync(orgUser.OrganizationId, orgUser.UserId.Value)
.Returns(orgUser);
sutProvider.GetDependency<IOrganizationSponsorshipRepository>()
.GetBySponsoringOrganizationUserIdAsync(Arg.Is<Guid>(v => v != sponsoringOrgUser.Id))
.GetBySponsoringOrganizationUserIdAsync(Arg.Is<Guid>(v => v != orgUser.Id))
.Returns(sponsorship);
sutProvider.GetDependency<IOrganizationSponsorshipRepository>()
.GetBySponsoringOrganizationUserIdAsync(sponsoringOrgUser.Id)
.GetBySponsoringOrganizationUserIdAsync(orgUser.Id)
.Returns((OrganizationSponsorship)null);
var exception = await Assert.ThrowsAsync<BadRequestException>(() =>
sutProvider.Sut.RevokeSponsorship(sponsoringOrgUser.Id.ToString()));
sutProvider.Sut.RevokeSponsorship(orgUser.OrganizationId.ToString()));
Assert.Contains("You are not currently sponsoring an organization.", exception.Message);
await sutProvider.GetDependency<IOrganizationSponsorshipService>()
@ -280,23 +378,23 @@ namespace Bit.Api.Test.Controllers
[Theory]
[BitAutoData]
public async Task RevokeSponsorship_SponsorshipNotRedeemed_ThrowsBadRequest(OrganizationUser sponsoringOrgUser,
public async Task RevokeSponsorship_SponsorshipNotRedeemed_ThrowsBadRequest(OrganizationUser orgUser,
OrganizationSponsorship sponsorship, SutProvider<OrganizationSponsorshipsController> sutProvider)
{
sponsorship.SponsoredOrganizationId = null;
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(sponsoringOrgUser.UserId);
sutProvider.GetDependency<IOrganizationUserRepository>().GetByIdAsync(sponsoringOrgUser.Id)
.Returns(sponsoringOrgUser);
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(orgUser.UserId);
sutProvider.GetDependency<IOrganizationUserRepository>().GetByOrganizationAsync(orgUser.OrganizationId, orgUser.UserId.Value)
.Returns(orgUser);
sutProvider.GetDependency<IOrganizationSponsorshipRepository>()
.GetBySponsoringOrganizationUserIdAsync(Arg.Is<Guid>(v => v != sponsoringOrgUser.Id))
.GetBySponsoringOrganizationUserIdAsync(Arg.Is<Guid>(v => v != orgUser.Id))
.Returns(sponsorship);
sutProvider.GetDependency<IOrganizationSponsorshipRepository>()
.GetBySponsoringOrganizationUserIdAsync(sponsoringOrgUser.Id)
.GetBySponsoringOrganizationUserIdAsync(orgUser.Id)
.Returns((OrganizationSponsorship)sponsorship);
var exception = await Assert.ThrowsAsync<BadRequestException>(() =>
sutProvider.Sut.RevokeSponsorship(sponsoringOrgUser.Id.ToString()));
sutProvider.Sut.RevokeSponsorship(orgUser.OrganizationId.ToString()));
Assert.Contains("You are not currently sponsoring an organization.", exception.Message);
await sutProvider.GetDependency<IOrganizationSponsorshipService>()
@ -306,19 +404,19 @@ namespace Bit.Api.Test.Controllers
[Theory]
[BitAutoData]
public async Task RevokeSponsorship_SponsoredOrgNotFound_ThrowsBadRequest(OrganizationUser sponsoringOrgUser,
public async Task RevokeSponsorship_SponsoredOrgNotFound_ThrowsBadRequest(OrganizationUser orgUser,
OrganizationSponsorship sponsorship, SutProvider<OrganizationSponsorshipsController> sutProvider)
{
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(sponsoringOrgUser.UserId);
sutProvider.GetDependency<IOrganizationUserRepository>().GetByIdAsync(sponsoringOrgUser.Id)
.Returns(sponsoringOrgUser);
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(orgUser.UserId);
sutProvider.GetDependency<IOrganizationUserRepository>().GetByOrganizationAsync(orgUser.OrganizationId, orgUser.UserId.Value)
.Returns(orgUser);
sutProvider.GetDependency<IOrganizationSponsorshipRepository>()
.GetBySponsoringOrganizationUserIdAsync(sponsoringOrgUser.Id)
.GetBySponsoringOrganizationUserIdAsync(orgUser.Id)
.Returns(sponsorship);
var exception = await Assert.ThrowsAsync<BadRequestException>(() =>
sutProvider.Sut.RevokeSponsorship(sponsoringOrgUser.Id.ToString()));
sutProvider.Sut.RevokeSponsorship(orgUser.OrganizationId.ToString()));
Assert.Contains("Unable to find the sponsored Organization.", exception.Message);
await sutProvider.GetDependency<IOrganizationSponsorshipService>()

View File

@ -92,5 +92,20 @@ namespace Bit.Core.Test.Services
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
}
}