From ba7b9073ac01ac668239daac757124059fe9b959 Mon Sep 17 00:00:00 2001 From: Jonas Hendrickx Date: Wed, 26 Mar 2025 11:50:15 +0100 Subject: [PATCH] More tests for `CreateSponsorshipHandler` --- .../CreateSponsorshipHandlerTests.cs | 57 ++++++++++--------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/test/Core.Test/OrganizationFeatures/OrganizationSponsorships/FamiliesForEnterprise/SponsorshipCreation/CreateSponsorshipHandlerTests.cs b/test/Core.Test/OrganizationFeatures/OrganizationSponsorships/FamiliesForEnterprise/SponsorshipCreation/CreateSponsorshipHandlerTests.cs index 9f93d6358a..a2d8369d35 100644 --- a/test/Core.Test/OrganizationFeatures/OrganizationSponsorships/FamiliesForEnterprise/SponsorshipCreation/CreateSponsorshipHandlerTests.cs +++ b/test/Core.Test/OrganizationFeatures/OrganizationSponsorships/FamiliesForEnterprise/SponsorshipCreation/CreateSponsorshipHandlerTests.cs @@ -19,21 +19,8 @@ namespace Bit.Core.Test.OrganizationFeatures.OrganizationSponsorships.FamiliesFo [SutProviderCustomize] public class CreateSponsorshipHandlerTests : FamiliesForEnterpriseTestsBase { - 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 CreateSponsorship_OfferedToNotFound_ThrowsBadRequest(OrganizationUser orgUser, SutProvider sutProvider) + public async Task HandleAsync_OfferedToNotFound_ThrowsBadRequest(OrganizationUser orgUser, SutProvider sutProvider) { sutProvider.GetDependency().GetUserByIdAsync(orgUser.UserId!.Value).ReturnsNull(); var request = new CreateSponsorshipRequest(null, orgUser, PlanSponsorshipType.FamiliesForEnterprise, null, null, null); @@ -46,7 +33,7 @@ public class CreateSponsorshipHandlerTests : FamiliesForEnterpriseTestsBase } [Theory, BitAutoData] - public async Task CreateSponsorship_OfferedToSelf_ThrowsBadRequest(OrganizationUser orgUser, string sponsoredEmail, User user, SutProvider sutProvider) + public async Task HandleAsync_OfferedToSelf_ThrowsBadRequest(OrganizationUser orgUser, string sponsoredEmail, User user, SutProvider sutProvider) { user.Email = sponsoredEmail; sutProvider.GetDependency().GetUserByIdAsync(orgUser.UserId!.Value).Returns(user); @@ -59,8 +46,32 @@ public class CreateSponsorshipHandlerTests : FamiliesForEnterpriseTestsBase .CreateAsync(null!); } + public static readonly OrganizationUserStatusType[] _unconfirmedOrganizationUserStatusTypes = Enum + .GetValues() + .Where(x => x != OrganizationUserStatusType.Confirmed).ToArray(); + + [Theory, BitMemberAutoData(nameof(_unconfirmedOrganizationUserStatusTypes))] + public async Task HandleAsync_UnconfirmedSponsoringMember_ThrowsBadRequest( + OrganizationUserStatusType sponsoringMemberStatus, Organization sponsoringOrg, + OrganizationUser sponsoringOrgUser, string sponsoredEmail, User user, string friendlyName, + SutProvider sutProvider) + { + sponsoringOrg.PlanType = PlanType.EnterpriseAnnually; + sponsoringOrgUser.Status = sponsoringMemberStatus; + + sutProvider.GetDependency().GetUserByIdAsync(sponsoringOrgUser.UserId!.Value).Returns(user); + + + var request = new CreateSponsorshipRequest(sponsoringOrg, sponsoringOrgUser, + PlanSponsorshipType.FamiliesForEnterprise, sponsoredEmail, friendlyName, null); + + var exception = await Assert.ThrowsAsync(() => sutProvider.Sut.HandleAsync(request)); + + Assert.Contains("Only confirmed users can sponsor other organizations.", exception.Message); + } + [Theory, BitMemberAutoData(nameof(NonEnterprisePlanTypes))] - public async Task CreateSponsorship_BadSponsoringOrgPlan_ThrowsBadRequest(PlanType sponsoringOrgPlan, + public async Task HandleAsync_BadSponsoringOrgPlan_ThrowsBadRequest(PlanType sponsoringOrgPlan, Organization org, OrganizationUser orgUser, User user, SutProvider sutProvider) { org.PlanType = sponsoringOrgPlan; @@ -79,7 +90,7 @@ public class CreateSponsorshipHandlerTests : FamiliesForEnterpriseTestsBase [Theory] [BitMemberAutoData(nameof(NonConfirmedOrganizationUsersStatuses))] - public async Task CreateSponsorship_BadSponsoringUserStatus_ThrowsBadRequest( + public async Task HandleAsync_BadSponsoringUserStatus_ThrowsBadRequest( OrganizationUserStatusType statusType, Organization org, OrganizationUser orgUser, User user, SutProvider sutProvider) { @@ -100,7 +111,7 @@ public class CreateSponsorshipHandlerTests : FamiliesForEnterpriseTestsBase [Theory] [OrganizationSponsorshipCustomize] [BitAutoData] - public async Task CreateSponsorship_AlreadySponsoring_Throws(Organization org, + public async Task HandleAsync_AlreadySponsoring_Throws(Organization org, OrganizationUser orgUser, User user, OrganizationSponsorship sponsorship, SutProvider sutProvider) { @@ -123,7 +134,7 @@ public class CreateSponsorshipHandlerTests : FamiliesForEnterpriseTestsBase [Theory] [BitAutoData] - public async Task CreateSponsorship_ReturnsExpectedSponsorship(Organization sponsoringOrg, OrganizationUser sponsoringOrgUser, User user, + public async Task HandleAsync_ReturnsExpectedSponsorship(Organization sponsoringOrg, OrganizationUser sponsoringOrgUser, User user, string sponsoredEmail, string friendlyName, SutProvider sutProvider) { sponsoringOrg.PlanType = PlanType.EnterpriseAnnually; @@ -148,12 +159,6 @@ public class CreateSponsorshipHandlerTests : FamiliesForEnterpriseTestsBase Notes = null }; - Assert.Equal(expectedSponsorship.SponsoringOrganizationId, actual.SponsoringOrganizationId); - Assert.Equal(expectedSponsorship.SponsoringOrganizationUserId, actual.SponsoringOrganizationUserId); - Assert.Equal(expectedSponsorship.FriendlyName, actual.FriendlyName); - Assert.Equal(expectedSponsorship.OfferedToEmail, actual.OfferedToEmail); - Assert.Equal(expectedSponsorship.PlanSponsorshipType, actual.PlanSponsorshipType); - Assert.Equal(expectedSponsorship.IsAdminInitiated, actual.IsAdminInitiated); - Assert.Equal(expectedSponsorship.Notes, actual.Notes); + AssertHelper.AssertPropertyEqual(expectedSponsorship, actual); } }