using Xunit; using Bit.Test.Common.AutoFixture.Attributes; using System.Threading.Tasks; using System; using Bit.Core.Enums; using System.Linq; using System.Collections.Generic; using Bit.Core.Models.Table; using Bit.Test.Common.AutoFixture; using Bit.Api.Controllers; using Bit.Core.Context; using NSubstitute; using Bit.Core.Exceptions; using Bit.Api.Test.AutoFixture.Attributes; using Bit.Core.Repositories; using Bit.Core.Models.Api.Request; using Bit.Core.Services; using Bit.Core.Models.Api; namespace Bit.Api.Test.Controllers { [ControllerCustomize(typeof(OrganizationSponsorshipsController))] [SutProviderCustomize] public class OrganizationSponsorshipsControllerTests { public static IEnumerable EnterprisePlanTypes => Enum.GetValues().Where(p => PlanTypeHelper.IsEnterprise(p)).Select(p => new object[] { p }); public static IEnumerable NonEnterprisePlanTypes => Enum.GetValues().Where(p => !PlanTypeHelper.IsEnterprise(p)).Select(p => new object[] { p }); public static IEnumerable NonFamiliesPlanTypes => Enum.GetValues().Where(p => !PlanTypeHelper.IsFamilies(p)).Select(p => new object[] { p }); [Theory] [BitMemberAutoData(nameof(NonEnterprisePlanTypes))] public async Task CreateSponsorship_BadSponsoringOrgPlan_ThrowsBadRequest(PlanType sponsoringOrgPlan, Organization org, SutProvider sutProvider) { org.PlanType = sponsoringOrgPlan; sutProvider.GetDependency().GetByIdAsync(org.Id).Returns(org); var exception = await Assert.ThrowsAsync(() => sutProvider.Sut.CreateSponsorship(org.Id.ToString(), null)); Assert.Contains("Specified Organization cannot sponsor other organizations.", exception.Message); await sutProvider.GetDependency() .DidNotReceiveWithAnyArgs() .OfferSponsorshipAsync(default, default, default, default); } public static IEnumerable NonConfirmedOrganizationUsersStatuses => Enum.GetValues() .Where(s => s != OrganizationUserStatusType.Confirmed) .Select(s => new object[] { s }); [Theory] [BitMemberAutoData(nameof(NonConfirmedOrganizationUsersStatuses))] public async Task CreateSponsorship_BadSponsoringUserStatus_ThrowsBadRequest( OrganizationUserStatusType statusType, Guid userId, Organization org, OrganizationUser orgUser, OrganizationSponsorshipRequestModel model, SutProvider sutProvider) { org.PlanType = PlanType.EnterpriseAnnually; orgUser.Status = statusType; orgUser.UserId = userId; model.OrganizationUserId = orgUser.Id; sutProvider.GetDependency().GetByIdAsync(org.Id).Returns(org); sutProvider.GetDependency().UserId.Returns(userId); sutProvider.GetDependency().GetByIdAsync(orgUser.Id).Returns(orgUser); var exception = await Assert.ThrowsAsync(() => sutProvider.Sut.CreateSponsorship(org.Id.ToString(), model)); Assert.Contains("Only confirm users can sponsor other organizations.", exception.Message); await sutProvider.GetDependency() .DidNotReceiveWithAnyArgs() .OfferSponsorshipAsync(default, default, default, default); } [Theory] [BitAutoData("c56c7ab4-a174-412a-a822-abe53ea71d50")] public async Task CreateSponsorship_CreateSponsorshipAsDifferentUser_ThrowsBadRequest(Guid userId, Organization org, OrganizationUser orgUser, OrganizationSponsorshipRequestModel model, SutProvider sutProvider) { org.PlanType = PlanType.EnterpriseAnnually; orgUser.Status = OrganizationUserStatusType.Confirmed; model.OrganizationUserId = orgUser.Id; sutProvider.GetDependency().GetByIdAsync(org.Id).Returns(org); sutProvider.GetDependency().UserId.Returns(userId); sutProvider.GetDependency().GetByIdAsync(orgUser.Id).Returns(orgUser); var exception = await Assert.ThrowsAsync(() => sutProvider.Sut.CreateSponsorship(org.Id.ToString(), model)); Assert.Contains("Can only create organization sponsorships for yourself.", exception.Message); await sutProvider.GetDependency() .DidNotReceiveWithAnyArgs() .OfferSponsorshipAsync(default, default, default, default); } [Theory] [BitAutoData] public async Task CreateSponsorship_AlreadySponsoring_ThrowsBadRequest(Organization org, OrganizationUser orgUser, OrganizationSponsorship sponsorship, OrganizationSponsorshipRequestModel model, SutProvider sutProvider) { org.PlanType = PlanType.EnterpriseAnnually; orgUser.Status = OrganizationUserStatusType.Confirmed; model.OrganizationUserId = orgUser.Id; sutProvider.GetDependency().GetByIdAsync(org.Id).Returns(org); sutProvider.GetDependency().UserId.Returns(orgUser.UserId); sutProvider.GetDependency().GetByIdAsync(orgUser.Id).Returns(orgUser); sutProvider.GetDependency() .GetBySponsoringOrganizationUserIdAsync(orgUser.Id).Returns(sponsorship); var exception = await Assert.ThrowsAsync(() => sutProvider.Sut.CreateSponsorship(org.Id.ToString(), model)); Assert.Contains("Can only sponsor one organization per Organization User.", exception.Message); await sutProvider.GetDependency() .DidNotReceiveWithAnyArgs() .OfferSponsorshipAsync(default, default, default, default); } [Theory] [BitAutoData] public async Task RedeemSponsorship_BadToken_ThrowsBadRequest(string sponsorshipToken, OrganizationSponsorshipRedeemRequestModel model, SutProvider sutProvider) { sutProvider.GetDependency().ValidateRedemptionTokenAsync(sponsorshipToken) .Returns(false); var exception = await Assert.ThrowsAsync(() => sutProvider.Sut.RedeemSponsorship(sponsorshipToken, model)); Assert.Contains("Failed to parse sponsorship token.", exception.Message); await sutProvider.GetDependency() .DidNotReceiveWithAnyArgs() .SetUpSponsorshipAsync(default, default); } [Theory] [BitAutoData] public async Task RedeemSponsorship_NotSponsoredOrgOwner_ThrowsBadRequest(string sponsorshipToken, OrganizationSponsorshipRedeemRequestModel model, SutProvider sutProvider) { sutProvider.GetDependency().ValidateRedemptionTokenAsync(sponsorshipToken) .Returns(true); sutProvider.GetDependency().OrganizationOwner(model.SponsoredOrganizationId).Returns(false); var exception = await Assert.ThrowsAsync(() => sutProvider.Sut.RedeemSponsorship(sponsorshipToken, model)); Assert.Contains("Can only redeem sponsorship for an organization you own.", exception.Message); await sutProvider.GetDependency() .DidNotReceiveWithAnyArgs() .SetUpSponsorshipAsync(default, default); } [Theory] [BitAutoData] public async Task RedeemSponsorship_SponsorshipNotFound_ThrowsBadRequest(string sponsorshipToken, OrganizationSponsorshipRedeemRequestModel model, User user, SutProvider sutProvider) { sutProvider.GetDependency().ValidateRedemptionTokenAsync(sponsorshipToken) .Returns(true); sutProvider.GetDependency().OrganizationOwner(model.SponsoredOrganizationId).Returns(true); sutProvider.GetDependency().User.Returns(user); sutProvider.GetDependency().GetByOfferedToEmailAsync(user.Email) .Returns((OrganizationSponsorship)null); var exception = await Assert.ThrowsAsync(() => sutProvider.Sut.RedeemSponsorship(sponsorshipToken, model)); Assert.Contains("No unredeemed sponsorship offer exists for you.", exception.Message); await sutProvider.GetDependency() .DidNotReceiveWithAnyArgs() .SetUpSponsorshipAsync(default, default); } [Theory] [BitAutoData] public async Task RedeemSponsorship_OfferedToDifferentEmail_ThrowsBadRequest(string sponsorshipToken, OrganizationSponsorshipRedeemRequestModel model, User user, OrganizationSponsorship sponsorship, SutProvider sutProvider) { sutProvider.GetDependency().ValidateRedemptionTokenAsync(sponsorshipToken) .Returns(true); sutProvider.GetDependency().OrganizationOwner(model.SponsoredOrganizationId).Returns(true); sutProvider.GetDependency().User.Returns(user); sutProvider.GetDependency().GetByOfferedToEmailAsync(user.Email) .Returns(sponsorship); var exception = await Assert.ThrowsAsync(() => sutProvider.Sut.RedeemSponsorship(sponsorshipToken, model)); Assert.Contains("This sponsorship offer was issued to a different user email address.", exception.Message); await sutProvider.GetDependency() .DidNotReceiveWithAnyArgs() .SetUpSponsorshipAsync(default, default); } [Theory] [BitAutoData] public async Task RedeemSponsorship_OrgAlreadySponsored_ThrowsBadRequest(string sponsorshipToken, OrganizationSponsorshipRedeemRequestModel model, User user, OrganizationSponsorship sponsorship, OrganizationSponsorship existingSponsorship, SutProvider sutProvider) { user.Email = sponsorship.OfferedToEmail; sutProvider.GetDependency().ValidateRedemptionTokenAsync(sponsorshipToken) .Returns(true); sutProvider.GetDependency().OrganizationOwner(model.SponsoredOrganizationId).Returns(true); sutProvider.GetDependency().User.Returns(user); sutProvider.GetDependency() .GetByOfferedToEmailAsync(sponsorship.OfferedToEmail).Returns(sponsorship); sutProvider.GetDependency() .GetBySponsoredOrganizationIdAsync(model.SponsoredOrganizationId).Returns(existingSponsorship); var exception = await Assert.ThrowsAsync(() => sutProvider.Sut.RedeemSponsorship(sponsorshipToken, model)); Assert.Contains("Cannot redeem a sponsorship offer for an organization that is already sponsored. Revoke existing sponsorship first.", exception.Message); await sutProvider.GetDependency() .DidNotReceiveWithAnyArgs() .SetUpSponsorshipAsync(default, default); } [Theory] [BitAutoData] public async Task RedeemSponsorship_OrgNotFamiles_ThrowsBadRequest(PlanType planType, string sponsorshipToken, OrganizationSponsorshipRedeemRequestModel model, User user, OrganizationSponsorship sponsorship, Organization org, SutProvider sutProvider) { user.Email = sponsorship.OfferedToEmail; org.PlanType = planType; sutProvider.GetDependency().ValidateRedemptionTokenAsync(sponsorshipToken) .Returns(true); sutProvider.GetDependency().OrganizationOwner(model.SponsoredOrganizationId).Returns(true); sutProvider.GetDependency().User.Returns(user); sutProvider.GetDependency() .GetByOfferedToEmailAsync(sponsorship.OfferedToEmail).Returns(sponsorship); sutProvider.GetDependency() .GetBySponsoredOrganizationIdAsync(model.SponsoredOrganizationId).Returns((OrganizationSponsorship)null); sutProvider.GetDependency().GetByIdAsync(model.SponsoredOrganizationId).Returns(org); var exception = await Assert.ThrowsAsync(() => sutProvider.Sut.RedeemSponsorship(sponsorshipToken, model)); Assert.Contains("Can only redeem sponsorship offer on families organizations.", exception.Message); await sutProvider.GetDependency() .DidNotReceiveWithAnyArgs() .SetUpSponsorshipAsync(default, default); } [Theory] [BitAutoData] public async Task RevokeSponsorship_WrongSponsoringUser_ThrowsBadRequest(OrganizationUser sponsoringOrgUser, Guid currentUserId, SutProvider sutProvider) { sutProvider.GetDependency().UserId.Returns(currentUserId); sutProvider.GetDependency().GetByIdAsync(sponsoringOrgUser.Id) .Returns(sponsoringOrgUser); var exception = await Assert.ThrowsAsync(() => sutProvider.Sut.RevokeSponsorship(sponsoringOrgUser.Id.ToString())); Assert.Contains("Can only revoke a sponsorship you granted.", exception.Message); await sutProvider.GetDependency() .DidNotReceiveWithAnyArgs() .RemoveSponsorshipAsync(default); } [Theory] [BitAutoData] public async Task RevokeSponsorship_NoExistingSponsorship_ThrowsBadRequest(OrganizationUser sponsoringOrgUser, OrganizationSponsorship sponsorship, SutProvider sutProvider) { sutProvider.GetDependency().UserId.Returns(sponsoringOrgUser.UserId); sutProvider.GetDependency().GetByIdAsync(sponsoringOrgUser.Id) .Returns(sponsoringOrgUser); sutProvider.GetDependency() .GetBySponsoringOrganizationUserIdAsync(Arg.Is(v => v != sponsoringOrgUser.Id)) .Returns(sponsorship); sutProvider.GetDependency() .GetBySponsoringOrganizationUserIdAsync(sponsoringOrgUser.Id) .Returns((OrganizationSponsorship)null); var exception = await Assert.ThrowsAsync(() => sutProvider.Sut.RevokeSponsorship(sponsoringOrgUser.Id.ToString())); Assert.Contains("You are not currently sponsoring and organization.", exception.Message); await sutProvider.GetDependency() .DidNotReceiveWithAnyArgs() .RemoveSponsorshipAsync(default); } [Theory] [BitAutoData] public async Task RemoveSponsorship_WrongOrgUserType_ThrowsBadRequest(Organization sponsoredOrg, SutProvider sutProvider) { sutProvider.GetDependency().OrganizationOwner(Arg.Any()).Returns(false); var exception = await Assert.ThrowsAsync(() => sutProvider.Sut.RemoveSponsorship(sponsoredOrg.Id.ToString())); Assert.Contains("Only the owner of an organization can remove sponsorship.", exception.Message); await sutProvider.GetDependency() .DidNotReceiveWithAnyArgs() .RemoveSponsorshipAsync(default); } [Theory] [BitAutoData] public async Task RemoveSponsorship_NotSponsored_ThrowsBadRequest(Organization sponsoredOrg, OrganizationSponsorship sponsorship, SutProvider sutProvider) { sutProvider.GetDependency().OrganizationOwner(Arg.Any()).Returns(true); sutProvider.GetDependency() .GetBySponsoredOrganizationIdAsync(sponsoredOrg.Id) .Returns((OrganizationSponsorship)null); sutProvider.GetDependency() .GetBySponsoredOrganizationIdAsync(Arg.Is(v => v != sponsoredOrg.Id)) .Returns(sponsorship); var exception = await Assert.ThrowsAsync(() => sutProvider.Sut.RemoveSponsorship(sponsoredOrg.Id.ToString())); Assert.Contains("The requested organization is not currently being sponsored.", exception.Message); await sutProvider.GetDependency() .DidNotReceiveWithAnyArgs() .RemoveSponsorshipAsync(default); } } }