mirror of
https://github.com/bitwarden/server.git
synced 2025-07-05 18:12:48 -05:00
Add helpers to further type PlanTypes
This commit is contained in:
@ -1,4 +1,6 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.Linq;
|
||||||
|
using Bit.Core.Models.Table;
|
||||||
|
|
||||||
namespace Bit.Core.Enums
|
namespace Bit.Core.Enums
|
||||||
{
|
{
|
||||||
@ -29,4 +31,24 @@ namespace Bit.Core.Enums
|
|||||||
[Display(Name = "Enterprise (Annually)")]
|
[Display(Name = "Enterprise (Annually)")]
|
||||||
EnterpriseAnnually= 11,
|
EnterpriseAnnually= 11,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class PlanTypeHelper
|
||||||
|
{
|
||||||
|
private static readonly PlanType[] _freePlans = new[] { PlanType.Free };
|
||||||
|
private static readonly PlanType[] _familiesPlans = new[] { PlanType.FamiliesAnnually, PlanType.FamiliesAnnually2019 };
|
||||||
|
private static readonly PlanType[] _teamsPlans = new[] { PlanType.TeamsAnnually, PlanType.TeamsAnnually2019,
|
||||||
|
PlanType.TeamsMonthly, PlanType.TeamsMonthly2019};
|
||||||
|
private static readonly PlanType[] _enterprisePlans = new[] { PlanType.EnterpriseAnnually,
|
||||||
|
PlanType.EnterpriseAnnually2019, PlanType.EnterpriseMonthly, PlanType.EnterpriseMonthly2019 };
|
||||||
|
|
||||||
|
private static bool HasPlan(PlanType[] planTypes, PlanType planType) => planTypes.Any(p => p == planType);
|
||||||
|
public static bool HasFreePlan(Organization org) => IsFree(org.PlanType);
|
||||||
|
public static bool IsFree(PlanType planType) => HasPlan(_freePlans, planType);
|
||||||
|
public static bool HasFamiliesPlan(Organization org) => IsFamilies(org.PlanType);
|
||||||
|
public static bool IsFamilies(PlanType planType) => HasPlan(_familiesPlans, planType);
|
||||||
|
public static bool HasTeamsPlan(Organization org) => IsTeams(org.PlanType);
|
||||||
|
public static bool IsTeams(PlanType planType) => HasPlan(_teamsPlans, planType);
|
||||||
|
public static bool HasEnterprisePlan(Organization org) => IsEnterprise(org.PlanType);
|
||||||
|
public static bool IsEnterprise(PlanType planType) => HasPlan(_enterprisePlans, planType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
39
test/Core.Test/Enums/PlanTypeHelperTests.cs
Normal file
39
test/Core.Test/Enums/PlanTypeHelperTests.cs
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Bit.Core.Enums;
|
||||||
|
using Bit.Core.Models.Table;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Bit.Core.Test.Enums
|
||||||
|
{
|
||||||
|
public class PlanTypeHelperTests
|
||||||
|
{
|
||||||
|
private static IEnumerable<PlanType> PlanArchetypeArray(PlanType planType) => new PlanType?[] {
|
||||||
|
PlanTypeHelper.HasFreePlan(new Organization {PlanType = planType}) ? planType : null,
|
||||||
|
PlanTypeHelper.HasFamiliesPlan(new Organization {PlanType = planType}) ? planType : null,
|
||||||
|
PlanTypeHelper.HasTeamsPlan(new Organization {PlanType = planType}) ? planType : null,
|
||||||
|
PlanTypeHelper.HasEnterprisePlan(new Organization {PlanType = planType}) ? planType : null,
|
||||||
|
}.Where(v => v.HasValue).Select(v => (PlanType)v);
|
||||||
|
|
||||||
|
public static IEnumerable<object[]> PlanTypes => Enum.GetValues<PlanType>().Select(p => new object[] { p });
|
||||||
|
public static IEnumerable<object[]> PlanTypesExceptCustom =>
|
||||||
|
Enum.GetValues<PlanType>().Except(new[] { PlanType.Custom }).Select(p => new object[] { p });
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(PlanTypesExceptCustom))]
|
||||||
|
public void NonCustomPlanTypesBelongToPlanArchetype(PlanType planType)
|
||||||
|
{
|
||||||
|
Assert.Contains(planType, PlanArchetypeArray(planType));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(PlanTypesExceptCustom))]
|
||||||
|
public void PlanTypesBelongToOnlyOneArchetype(PlanType planType)
|
||||||
|
{
|
||||||
|
Console.WriteLine(planType);
|
||||||
|
Assert.Single(PlanArchetypeArray(planType));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user