2021-10-30 00:43:45 +02:00
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 ;
2021-10-30 19:34:03 +02:00
using Bit.Core.Repositories ;
using Bit.Core.Models.Api.Request ;
using Bit.Core.Services ;
2021-11-04 14:25:40 +01:00
using Bit.Core.Models.Api ;
2021-11-09 00:01:09 +01:00
using Bit.Core.Utilities ;
2021-10-30 00:43:45 +02:00
namespace Bit.Api.Test.Controllers
{
[ControllerCustomize(typeof(OrganizationSponsorshipsController))]
[SutProviderCustomize]
public class OrganizationSponsorshipsControllerTests
{
public static IEnumerable < object [ ] > EnterprisePlanTypes = >
2021-11-09 00:01:09 +01:00
Enum . GetValues < PlanType > ( ) . Where ( p = > StaticStore . GetPlan ( p ) . Product = = ProductType . Enterprise ) . Select ( p = > new object [ ] { p } ) ;
2021-10-30 00:43:45 +02:00
public static IEnumerable < object [ ] > NonEnterprisePlanTypes = >
2021-11-09 00:01:09 +01:00
Enum . GetValues < PlanType > ( ) . Where ( p = > StaticStore . GetPlan ( p ) . Product ! = ProductType . Enterprise ) . Select ( p = > new object [ ] { p } ) ;
2021-11-04 14:25:40 +01:00
public static IEnumerable < object [ ] > NonFamiliesPlanTypes = >
2021-11-09 00:01:09 +01:00
Enum . GetValues < PlanType > ( ) . Where ( p = > StaticStore . GetPlan ( p ) . Product ! = ProductType . Families ) . Select ( p = > new object [ ] { p } ) ;
2021-10-30 00:43:45 +02:00
[Theory]
2021-10-30 19:34:03 +02:00
[BitMemberAutoData(nameof(NonEnterprisePlanTypes))]
2021-10-30 00:43:45 +02:00
public async Task CreateSponsorship_BadSponsoringOrgPlan_ThrowsBadRequest ( PlanType sponsoringOrgPlan , Organization org ,
2021-11-09 00:01:09 +01:00
OrganizationSponsorshipRequestModel model , SutProvider < OrganizationSponsorshipsController > sutProvider )
2021-10-30 00:43:45 +02:00
{
org . PlanType = sponsoringOrgPlan ;
2021-11-09 00:01:09 +01:00
model . PlanSponsorshipType = PlanSponsorshipType . FamiliesForEnterprise ;
2021-10-30 19:34:03 +02:00
sutProvider . GetDependency < IOrganizationRepository > ( ) . GetByIdAsync ( org . Id ) . Returns ( org ) ;
2021-10-30 00:43:45 +02:00
var exception = await Assert . ThrowsAsync < BadRequestException > ( ( ) = >
2021-11-09 00:01:09 +01:00
sutProvider . Sut . CreateSponsorship ( org . Id . ToString ( ) , model ) ) ;
2021-10-30 00:43:45 +02:00
Assert . Contains ( "Specified Organization cannot sponsor other organizations." , exception . Message ) ;
2021-10-30 19:34:03 +02:00
await sutProvider . GetDependency < IOrganizationSponsorshipService > ( )
2021-10-30 19:47:21 +02:00
. DidNotReceiveWithAnyArgs ( )
2021-11-09 00:01:09 +01:00
. OfferSponsorshipAsync ( default , default , default , default , default ) ;
2021-10-30 19:34:03 +02:00
}
public static IEnumerable < object [ ] > NonConfirmedOrganizationUsersStatuses = >
Enum . GetValues < OrganizationUserStatusType > ( )
. Where ( s = > s ! = OrganizationUserStatusType . Confirmed )
. Select ( s = > new object [ ] { s } ) ;
[Theory]
[BitMemberAutoData(nameof(NonConfirmedOrganizationUsersStatuses))]
public async Task CreateSponsorship_BadSponsoringUserStatus_ThrowsBadRequest (
2021-11-09 16:40:31 +01:00
OrganizationUserStatusType statusType , Organization org , OrganizationUser orgUser ,
2021-10-30 19:34:03 +02:00
OrganizationSponsorshipRequestModel model , SutProvider < OrganizationSponsorshipsController > sutProvider )
{
org . PlanType = PlanType . EnterpriseAnnually ;
orgUser . Status = statusType ;
sutProvider . GetDependency < IOrganizationRepository > ( ) . GetByIdAsync ( org . Id ) . Returns ( org ) ;
2021-11-09 16:40:31 +01:00
sutProvider . GetDependency < ICurrentContext > ( ) . UserId . Returns ( orgUser . UserId ) ;
sutProvider . GetDependency < IOrganizationUserRepository > ( ) . GetByOrganizationAsync ( org . Id , orgUser . UserId . Value )
. Returns ( orgUser ) ;
2021-10-30 19:34:03 +02:00
var exception = await Assert . ThrowsAsync < BadRequestException > ( ( ) = >
sutProvider . Sut . CreateSponsorship ( org . Id . ToString ( ) , model ) ) ;
2021-11-09 16:40:31 +01:00
Assert . Contains ( "Only confirmed users can sponsor other organizations." , exception . Message ) ;
2021-10-30 19:34:03 +02:00
await sutProvider . GetDependency < IOrganizationSponsorshipService > ( )
2021-10-30 19:47:21 +02:00
. DidNotReceiveWithAnyArgs ( )
2021-11-09 00:01:09 +01:00
. OfferSponsorshipAsync ( default , default , default , default , default ) ;
2021-10-30 19:34:03 +02:00
}
[Theory]
[BitAutoData]
public async Task CreateSponsorship_AlreadySponsoring_ThrowsBadRequest ( Organization org ,
OrganizationUser orgUser , OrganizationSponsorship sponsorship ,
OrganizationSponsorshipRequestModel model , SutProvider < OrganizationSponsorshipsController > sutProvider )
{
org . PlanType = PlanType . EnterpriseAnnually ;
orgUser . Status = OrganizationUserStatusType . Confirmed ;
sutProvider . GetDependency < IOrganizationRepository > ( ) . GetByIdAsync ( org . Id ) . Returns ( org ) ;
sutProvider . GetDependency < ICurrentContext > ( ) . UserId . Returns ( orgUser . UserId ) ;
2021-11-09 16:40:31 +01:00
sutProvider . GetDependency < IOrganizationUserRepository > ( ) . GetByOrganizationAsync ( org . Id , orgUser . UserId . Value )
. Returns ( orgUser ) ;
2021-10-30 19:34:03 +02:00
sutProvider . GetDependency < IOrganizationSponsorshipRepository > ( )
. GetBySponsoringOrganizationUserIdAsync ( orgUser . Id ) . Returns ( sponsorship ) ;
var exception = await Assert . ThrowsAsync < BadRequestException > ( ( ) = >
sutProvider . Sut . CreateSponsorship ( org . Id . ToString ( ) , model ) ) ;
Assert . Contains ( "Can only sponsor one organization per Organization User." , exception . Message ) ;
await sutProvider . GetDependency < IOrganizationSponsorshipService > ( )
2021-10-30 19:47:21 +02:00
. DidNotReceiveWithAnyArgs ( )
2021-11-09 00:01:09 +01:00
. OfferSponsorshipAsync ( default , default , default , default , default ) ;
2021-10-30 19:34:03 +02:00
}
2021-11-10 23:00:48 +01:00
[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 ) ;
}
2021-11-04 14:25:40 +01:00
[Theory]
[BitAutoData]
public async Task RedeemSponsorship_BadToken_ThrowsBadRequest ( string sponsorshipToken ,
OrganizationSponsorshipRedeemRequestModel model , SutProvider < OrganizationSponsorshipsController > sutProvider )
{
sutProvider . GetDependency < IOrganizationSponsorshipService > ( ) . ValidateRedemptionTokenAsync ( sponsorshipToken )
. Returns ( false ) ;
var exception = await Assert . ThrowsAsync < BadRequestException > ( ( ) = >
sutProvider . Sut . RedeemSponsorship ( sponsorshipToken , model ) ) ;
Assert . Contains ( "Failed to parse sponsorship token." , exception . Message ) ;
await sutProvider . GetDependency < IOrganizationSponsorshipService > ( )
. DidNotReceiveWithAnyArgs ( )
. SetUpSponsorshipAsync ( default , default ) ;
}
[Theory]
[BitAutoData]
public async Task RedeemSponsorship_NotSponsoredOrgOwner_ThrowsBadRequest ( string sponsorshipToken ,
OrganizationSponsorshipRedeemRequestModel model , SutProvider < OrganizationSponsorshipsController > sutProvider )
{
sutProvider . GetDependency < IOrganizationSponsorshipService > ( ) . ValidateRedemptionTokenAsync ( sponsorshipToken )
. Returns ( true ) ;
sutProvider . GetDependency < ICurrentContext > ( ) . OrganizationOwner ( model . SponsoredOrganizationId ) . Returns ( false ) ;
var exception = await Assert . ThrowsAsync < BadRequestException > ( ( ) = >
sutProvider . Sut . RedeemSponsorship ( sponsorshipToken , model ) ) ;
Assert . Contains ( "Can only redeem sponsorship for an organization you own." , exception . Message ) ;
await sutProvider . GetDependency < IOrganizationSponsorshipService > ( )
. DidNotReceiveWithAnyArgs ( )
. SetUpSponsorshipAsync ( default , default ) ;
}
[Theory]
[BitAutoData]
public async Task RedeemSponsorship_SponsorshipNotFound_ThrowsBadRequest ( string sponsorshipToken ,
OrganizationSponsorshipRedeemRequestModel model , User user ,
SutProvider < OrganizationSponsorshipsController > sutProvider )
{
sutProvider . GetDependency < IOrganizationSponsorshipService > ( ) . ValidateRedemptionTokenAsync ( sponsorshipToken )
. Returns ( true ) ;
sutProvider . GetDependency < ICurrentContext > ( ) . OrganizationOwner ( model . SponsoredOrganizationId ) . Returns ( true ) ;
2021-11-10 23:00:48 +01:00
sutProvider . GetDependency < ICurrentContext > ( ) . UserId . Returns ( user . Id ) ;
sutProvider . GetDependency < IUserService > ( ) . GetUserByIdAsync ( user . Id ) . Returns ( user ) ;
2021-11-04 14:25:40 +01:00
sutProvider . GetDependency < IOrganizationSponsorshipRepository > ( ) . GetByOfferedToEmailAsync ( user . Email )
. Returns ( ( OrganizationSponsorship ) null ) ;
var exception = await Assert . ThrowsAsync < BadRequestException > ( ( ) = >
sutProvider . Sut . RedeemSponsorship ( sponsorshipToken , model ) ) ;
Assert . Contains ( "No unredeemed sponsorship offer exists for you." , exception . Message ) ;
await sutProvider . GetDependency < IOrganizationSponsorshipService > ( )
. DidNotReceiveWithAnyArgs ( )
. SetUpSponsorshipAsync ( default , default ) ;
}
[Theory]
[BitAutoData]
public async Task RedeemSponsorship_OfferedToDifferentEmail_ThrowsBadRequest ( string sponsorshipToken ,
OrganizationSponsorshipRedeemRequestModel model , User user , OrganizationSponsorship sponsorship ,
SutProvider < OrganizationSponsorshipsController > sutProvider )
{
sutProvider . GetDependency < IOrganizationSponsorshipService > ( ) . ValidateRedemptionTokenAsync ( sponsorshipToken )
. Returns ( true ) ;
sutProvider . GetDependency < ICurrentContext > ( ) . OrganizationOwner ( model . SponsoredOrganizationId ) . Returns ( true ) ;
2021-11-10 23:00:48 +01:00
sutProvider . GetDependency < ICurrentContext > ( ) . UserId . Returns ( user . Id ) ;
sutProvider . GetDependency < IUserService > ( ) . GetUserByIdAsync ( user . Id ) . Returns ( user ) ;
2021-11-04 14:25:40 +01:00
sutProvider . GetDependency < IOrganizationSponsorshipRepository > ( ) . GetByOfferedToEmailAsync ( user . Email )
. Returns ( sponsorship ) ;
var exception = await Assert . ThrowsAsync < BadRequestException > ( ( ) = >
sutProvider . Sut . RedeemSponsorship ( sponsorshipToken , model ) ) ;
Assert . Contains ( "This sponsorship offer was issued to a different user email address." , exception . Message ) ;
await sutProvider . GetDependency < IOrganizationSponsorshipService > ( )
. DidNotReceiveWithAnyArgs ( )
. SetUpSponsorshipAsync ( default , default ) ;
}
[Theory]
[BitAutoData]
public async Task RedeemSponsorship_OrgAlreadySponsored_ThrowsBadRequest ( string sponsorshipToken ,
OrganizationSponsorshipRedeemRequestModel model , User user , OrganizationSponsorship sponsorship ,
OrganizationSponsorship existingSponsorship , SutProvider < OrganizationSponsorshipsController > sutProvider )
{
user . Email = sponsorship . OfferedToEmail ;
sutProvider . GetDependency < IOrganizationSponsorshipService > ( ) . ValidateRedemptionTokenAsync ( sponsorshipToken )
. Returns ( true ) ;
sutProvider . GetDependency < ICurrentContext > ( ) . OrganizationOwner ( model . SponsoredOrganizationId ) . Returns ( true ) ;
2021-11-10 23:00:48 +01:00
sutProvider . GetDependency < ICurrentContext > ( ) . UserId . Returns ( user . Id ) ;
sutProvider . GetDependency < IUserService > ( ) . GetUserByIdAsync ( user . Id ) . Returns ( user ) ;
2021-11-04 14:25:40 +01:00
sutProvider . GetDependency < IOrganizationSponsorshipRepository > ( )
. GetByOfferedToEmailAsync ( sponsorship . OfferedToEmail ) . Returns ( sponsorship ) ;
sutProvider . GetDependency < IOrganizationSponsorshipRepository > ( )
. GetBySponsoredOrganizationIdAsync ( model . SponsoredOrganizationId ) . Returns ( existingSponsorship ) ;
var exception = await Assert . ThrowsAsync < BadRequestException > ( ( ) = >
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 < IOrganizationSponsorshipService > ( )
. 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 < OrganizationSponsorshipsController > sutProvider )
{
user . Email = sponsorship . OfferedToEmail ;
org . PlanType = planType ;
sutProvider . GetDependency < IOrganizationSponsorshipService > ( ) . ValidateRedemptionTokenAsync ( sponsorshipToken )
. Returns ( true ) ;
sutProvider . GetDependency < ICurrentContext > ( ) . OrganizationOwner ( model . SponsoredOrganizationId ) . Returns ( true ) ;
2021-11-10 23:00:48 +01:00
sutProvider . GetDependency < ICurrentContext > ( ) . UserId . Returns ( user . Id ) ;
sutProvider . GetDependency < IUserService > ( ) . GetUserByIdAsync ( user . Id ) . Returns ( user ) ;
2021-11-04 14:25:40 +01:00
sutProvider . GetDependency < IOrganizationSponsorshipRepository > ( )
. GetByOfferedToEmailAsync ( sponsorship . OfferedToEmail ) . Returns ( sponsorship ) ;
sutProvider . GetDependency < IOrganizationSponsorshipRepository > ( )
. GetBySponsoredOrganizationIdAsync ( model . SponsoredOrganizationId ) . Returns ( ( OrganizationSponsorship ) null ) ;
sutProvider . GetDependency < IOrganizationRepository > ( ) . GetByIdAsync ( model . SponsoredOrganizationId ) . Returns ( org ) ;
var exception = await Assert . ThrowsAsync < BadRequestException > ( ( ) = >
sutProvider . Sut . RedeemSponsorship ( sponsorshipToken , model ) ) ;
Assert . Contains ( "Can only redeem sponsorship offer on families organizations." , exception . Message ) ;
await sutProvider . GetDependency < IOrganizationSponsorshipService > ( )
. DidNotReceiveWithAnyArgs ( )
. SetUpSponsorshipAsync ( default , default ) ;
}
2021-10-30 19:34:03 +02:00
[Theory]
[BitAutoData]
public async Task RevokeSponsorship_WrongSponsoringUser_ThrowsBadRequest ( OrganizationUser sponsoringOrgUser ,
Guid currentUserId , SutProvider < OrganizationSponsorshipsController > sutProvider )
{
sutProvider . GetDependency < ICurrentContext > ( ) . UserId . Returns ( currentUserId ) ;
sutProvider . GetDependency < IOrganizationUserRepository > ( ) . GetByIdAsync ( sponsoringOrgUser . Id )
. Returns ( sponsoringOrgUser ) ;
var exception = await Assert . ThrowsAsync < BadRequestException > ( ( ) = >
sutProvider . Sut . RevokeSponsorship ( sponsoringOrgUser . Id . ToString ( ) ) ) ;
Assert . Contains ( "Can only revoke a sponsorship you granted." , exception . Message ) ;
await sutProvider . GetDependency < IOrganizationSponsorshipService > ( )
2021-10-30 19:47:21 +02:00
. DidNotReceiveWithAnyArgs ( )
2021-11-09 00:01:09 +01:00
. RemoveSponsorshipAsync ( default , default ) ;
2021-10-30 19:34:03 +02:00
}
[Theory]
[BitAutoData]
2021-11-10 23:00:48 +01:00
public async Task RevokeSponsorship_NoExistingSponsorship_ThrowsBadRequest ( OrganizationUser orgUser ,
2021-10-30 19:34:03 +02:00
OrganizationSponsorship sponsorship , SutProvider < OrganizationSponsorshipsController > sutProvider )
{
2021-11-10 23:00:48 +01:00
sutProvider . GetDependency < ICurrentContext > ( ) . UserId . Returns ( orgUser . UserId ) ;
sutProvider . GetDependency < IOrganizationUserRepository > ( ) . GetByOrganizationAsync ( orgUser . OrganizationId , orgUser . UserId . Value )
. Returns ( orgUser ) ;
2021-10-30 19:34:03 +02:00
sutProvider . GetDependency < IOrganizationSponsorshipRepository > ( )
2021-11-10 23:00:48 +01:00
. GetBySponsoringOrganizationUserIdAsync ( Arg . Is < Guid > ( v = > v ! = orgUser . Id ) )
2021-10-30 19:34:03 +02:00
. Returns ( sponsorship ) ;
sutProvider . GetDependency < IOrganizationSponsorshipRepository > ( )
2021-11-10 23:00:48 +01:00
. GetBySponsoringOrganizationUserIdAsync ( orgUser . Id )
2021-10-30 19:34:03 +02:00
. Returns ( ( OrganizationSponsorship ) null ) ;
var exception = await Assert . ThrowsAsync < BadRequestException > ( ( ) = >
2021-11-10 23:00:48 +01:00
sutProvider . Sut . RevokeSponsorship ( orgUser . OrganizationId . ToString ( ) ) ) ;
2021-10-30 19:34:03 +02:00
2021-11-09 00:01:09 +01:00
Assert . Contains ( "You are not currently sponsoring an organization." , exception . Message ) ;
await sutProvider . GetDependency < IOrganizationSponsorshipService > ( )
. DidNotReceiveWithAnyArgs ( )
. RemoveSponsorshipAsync ( default , default ) ;
}
[Theory]
[BitAutoData]
2021-11-10 23:00:48 +01:00
public async Task RevokeSponsorship_SponsorshipNotRedeemed_ThrowsBadRequest ( OrganizationUser orgUser ,
2021-11-09 00:01:09 +01:00
OrganizationSponsorship sponsorship , SutProvider < OrganizationSponsorshipsController > sutProvider )
{
sponsorship . SponsoredOrganizationId = null ;
2021-11-10 23:00:48 +01:00
sutProvider . GetDependency < ICurrentContext > ( ) . UserId . Returns ( orgUser . UserId ) ;
sutProvider . GetDependency < IOrganizationUserRepository > ( ) . GetByOrganizationAsync ( orgUser . OrganizationId , orgUser . UserId . Value )
. Returns ( orgUser ) ;
2021-11-09 00:01:09 +01:00
sutProvider . GetDependency < IOrganizationSponsorshipRepository > ( )
2021-11-10 23:00:48 +01:00
. GetBySponsoringOrganizationUserIdAsync ( Arg . Is < Guid > ( v = > v ! = orgUser . Id ) )
2021-11-09 00:01:09 +01:00
. Returns ( sponsorship ) ;
sutProvider . GetDependency < IOrganizationSponsorshipRepository > ( )
2021-11-10 23:00:48 +01:00
. GetBySponsoringOrganizationUserIdAsync ( orgUser . Id )
2021-11-09 00:01:09 +01:00
. Returns ( ( OrganizationSponsorship ) sponsorship ) ;
2021-11-11 00:05:31 +01:00
await sutProvider . Sut . RevokeSponsorship ( orgUser . OrganizationId . ToString ( ) ) ;
await sutProvider . GetDependency < IOrganizationSponsorshipRepository > ( ) . Received ( 1 ) . DeleteAsync ( sponsorship ) ;
2021-11-09 00:01:09 +01:00
2021-10-30 19:34:03 +02:00
await sutProvider . GetDependency < IOrganizationSponsorshipService > ( )
2021-10-30 19:47:21 +02:00
. DidNotReceiveWithAnyArgs ( )
2021-11-09 00:01:09 +01:00
. RemoveSponsorshipAsync ( default , default ) ;
}
[Theory]
[BitAutoData]
2021-11-10 23:00:48 +01:00
public async Task RevokeSponsorship_SponsoredOrgNotFound_ThrowsBadRequest ( OrganizationUser orgUser ,
2021-11-09 00:01:09 +01:00
OrganizationSponsorship sponsorship , SutProvider < OrganizationSponsorshipsController > sutProvider )
{
2021-11-10 23:00:48 +01:00
sutProvider . GetDependency < ICurrentContext > ( ) . UserId . Returns ( orgUser . UserId ) ;
sutProvider . GetDependency < IOrganizationUserRepository > ( ) . GetByOrganizationAsync ( orgUser . OrganizationId , orgUser . UserId . Value )
. Returns ( orgUser ) ;
2021-11-09 00:01:09 +01:00
sutProvider . GetDependency < IOrganizationSponsorshipRepository > ( )
2021-11-10 23:00:48 +01:00
. GetBySponsoringOrganizationUserIdAsync ( orgUser . Id )
2021-11-09 00:01:09 +01:00
. Returns ( sponsorship ) ;
var exception = await Assert . ThrowsAsync < BadRequestException > ( ( ) = >
2021-11-10 23:00:48 +01:00
sutProvider . Sut . RevokeSponsorship ( orgUser . OrganizationId . ToString ( ) ) ) ;
2021-11-09 00:01:09 +01:00
Assert . Contains ( "Unable to find the sponsored Organization." , exception . Message ) ;
await sutProvider . GetDependency < IOrganizationSponsorshipService > ( )
. DidNotReceiveWithAnyArgs ( )
. RemoveSponsorshipAsync ( default , default ) ;
2021-10-30 19:34:03 +02:00
}
[Theory]
[BitAutoData]
public async Task RemoveSponsorship_WrongOrgUserType_ThrowsBadRequest ( Organization sponsoredOrg ,
SutProvider < OrganizationSponsorshipsController > sutProvider )
{
sutProvider . GetDependency < ICurrentContext > ( ) . OrganizationOwner ( Arg . Any < Guid > ( ) ) . Returns ( false ) ;
var exception = await Assert . ThrowsAsync < BadRequestException > ( ( ) = >
sutProvider . Sut . RemoveSponsorship ( sponsoredOrg . Id . ToString ( ) ) ) ;
Assert . Contains ( "Only the owner of an organization can remove sponsorship." , exception . Message ) ;
await sutProvider . GetDependency < IOrganizationSponsorshipService > ( )
2021-10-30 19:47:21 +02:00
. DidNotReceiveWithAnyArgs ( )
2021-11-09 00:01:09 +01:00
. RemoveSponsorshipAsync ( default , default ) ;
2021-10-30 19:34:03 +02:00
}
[Theory]
[BitAutoData]
public async Task RemoveSponsorship_NotSponsored_ThrowsBadRequest ( Organization sponsoredOrg ,
OrganizationSponsorship sponsorship , SutProvider < OrganizationSponsorshipsController > sutProvider )
{
sutProvider . GetDependency < ICurrentContext > ( ) . OrganizationOwner ( Arg . Any < Guid > ( ) ) . Returns ( true ) ;
sutProvider . GetDependency < IOrganizationSponsorshipRepository > ( )
. GetBySponsoredOrganizationIdAsync ( sponsoredOrg . Id )
. Returns ( ( OrganizationSponsorship ) null ) ;
sutProvider . GetDependency < IOrganizationSponsorshipRepository > ( )
. GetBySponsoredOrganizationIdAsync ( Arg . Is < Guid > ( v = > v ! = sponsoredOrg . Id ) )
. Returns ( sponsorship ) ;
var exception = await Assert . ThrowsAsync < BadRequestException > ( ( ) = >
sutProvider . Sut . RemoveSponsorship ( sponsoredOrg . Id . ToString ( ) ) ) ;
Assert . Contains ( "The requested organization is not currently being sponsored." , exception . Message ) ;
await sutProvider . GetDependency < IOrganizationSponsorshipService > ( )
2021-10-30 19:47:21 +02:00
. DidNotReceiveWithAnyArgs ( )
2021-11-09 00:01:09 +01:00
. RemoveSponsorshipAsync ( default , default ) ;
}
[Theory]
[BitAutoData]
public async Task RemoveSponsorship_SponsoredOrgNotFound_ThrowsBadRequest ( Organization sponsoredOrg ,
OrganizationSponsorship sponsorship , SutProvider < OrganizationSponsorshipsController > sutProvider )
{
sutProvider . GetDependency < ICurrentContext > ( ) . OrganizationOwner ( Arg . Any < Guid > ( ) ) . Returns ( true ) ;
sutProvider . GetDependency < IOrganizationSponsorshipRepository > ( )
. GetBySponsoredOrganizationIdAsync ( sponsoredOrg . Id )
. Returns ( sponsorship ) ;
var exception = await Assert . ThrowsAsync < BadRequestException > ( ( ) = >
sutProvider . Sut . RemoveSponsorship ( sponsoredOrg . Id . ToString ( ) ) ) ;
Assert . Contains ( "Unable to find the sponsored Organization." , exception . Message ) ;
await sutProvider . GetDependency < IOrganizationSponsorshipService > ( )
. DidNotReceiveWithAnyArgs ( )
. RemoveSponsorshipAsync ( default , default ) ;
2021-10-30 00:43:45 +02:00
}
}
}