mirror of
https://github.com/bitwarden/server.git
synced 2024-11-29 13:25:17 +01:00
110 lines
5.1 KiB
C#
110 lines
5.1 KiB
C#
|
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;
|
||
|
using Bit.Core.Utilities;
|
||
|
|
||
|
namespace Bit.Api.Test.Controllers
|
||
|
{
|
||
|
[ControllerCustomize(typeof(OrganizationSponsorshipsController))]
|
||
|
[SutProviderCustomize]
|
||
|
public class OrganizationSponsorshipsControllerTests
|
||
|
{
|
||
|
public static IEnumerable<object[]> EnterprisePlanTypes =>
|
||
|
Enum.GetValues<PlanType>().Where(p => StaticStore.GetPlan(p).Product == ProductType.Enterprise).Select(p => new object[] { p });
|
||
|
public static IEnumerable<object[]> NonEnterprisePlanTypes =>
|
||
|
Enum.GetValues<PlanType>().Where(p => StaticStore.GetPlan(p).Product != ProductType.Enterprise).Select(p => new object[] { p });
|
||
|
public static IEnumerable<object[]> NonFamiliesPlanTypes =>
|
||
|
Enum.GetValues<PlanType>().Where(p => StaticStore.GetPlan(p).Product != ProductType.Families).Select(p => new object[] { p });
|
||
|
|
||
|
public static IEnumerable<object[]> NonConfirmedOrganizationUsersStatuses =>
|
||
|
Enum.GetValues<OrganizationUserStatusType>()
|
||
|
.Where(s => s != OrganizationUserStatusType.Confirmed)
|
||
|
.Select(s => new object[] { s });
|
||
|
|
||
|
|
||
|
[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 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));
|
||
|
|
||
|
Assert.Contains("Can only revoke a sponsorship you granted.", exception.Message);
|
||
|
await sutProvider.GetDependency<IOrganizationSponsorshipService>()
|
||
|
.DidNotReceiveWithAnyArgs()
|
||
|
.RemoveSponsorshipAsync(default, default);
|
||
|
}
|
||
|
|
||
|
[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));
|
||
|
|
||
|
Assert.Contains("Only the owner of an organization can remove sponsorship.", exception.Message);
|
||
|
await sutProvider.GetDependency<IOrganizationSponsorshipService>()
|
||
|
.DidNotReceiveWithAnyArgs()
|
||
|
.RemoveSponsorshipAsync(default, default);
|
||
|
}
|
||
|
}
|
||
|
}
|