mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 15:42:48 -05:00
Additional 2FA tests (#1860)
* Add 2FA tests for organization * Move user tests to be in same directory * Fixed some names and namespace qualifications * Add GetTwoFactorProviders tests * Add tests to ensure we can read from name as well
This commit is contained in:
@ -61,7 +61,7 @@ namespace Bit.Core.Test.AutoFixture.CollectionFixtures
|
||||
|
||||
internal class CollectionAutoDataAttribute : CustomAutoDataAttribute
|
||||
{
|
||||
public CollectionAutoDataAttribute() : base(new SutProviderCustomization(), new Bit.Core.Test.AutoFixture.OrganizationFixtures.Organization())
|
||||
public CollectionAutoDataAttribute() : base(new SutProviderCustomization(), new OrganizationCustomization())
|
||||
{ }
|
||||
}
|
||||
}
|
||||
|
@ -8,21 +8,20 @@ using Bit.Core.Test.AutoFixture.Relays;
|
||||
using Bit.Infrastructure.EntityFramework.Repositories;
|
||||
using Bit.Test.Common.AutoFixture;
|
||||
using Bit.Test.Common.AutoFixture.Attributes;
|
||||
using Fixtures = Bit.Core.Test.AutoFixture.OrganizationFixtures;
|
||||
|
||||
namespace Bit.Core.Test.AutoFixture.GroupFixtures
|
||||
{
|
||||
internal class GroupOrganizationAutoDataAttribute : CustomAutoDataAttribute
|
||||
{
|
||||
public GroupOrganizationAutoDataAttribute() : base(
|
||||
new SutProviderCustomization(), new Fixtures.Organization { UseGroups = true })
|
||||
new SutProviderCustomization(), new OrganizationCustomization { UseGroups = true })
|
||||
{ }
|
||||
}
|
||||
|
||||
internal class GroupOrganizationNotUseGroupsAutoDataAttribute : CustomAutoDataAttribute
|
||||
{
|
||||
public GroupOrganizationNotUseGroupsAutoDataAttribute() : base(
|
||||
new SutProviderCustomization(), new Bit.Core.Test.AutoFixture.OrganizationFixtures.Organization { UseGroups = false })
|
||||
new SutProviderCustomization(), new OrganizationCustomization { UseGroups = false })
|
||||
{ }
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,7 @@ using Bit.Test.Common.AutoFixture.Attributes;
|
||||
|
||||
namespace Bit.Core.Test.AutoFixture.OrganizationFixtures
|
||||
{
|
||||
public class Organization : ICustomization
|
||||
public class OrganizationCustomization : ICustomization
|
||||
{
|
||||
public bool UseGroups { get; set; }
|
||||
|
||||
@ -26,7 +26,7 @@ namespace Bit.Core.Test.AutoFixture.OrganizationFixtures
|
||||
var organizationId = Guid.NewGuid();
|
||||
var maxConnections = (short)new Random().Next(10, short.MaxValue);
|
||||
|
||||
fixture.Customize<Entities.Organization>(composer => composer
|
||||
fixture.Customize<Organization>(composer => composer
|
||||
.With(o => o.Id, organizationId)
|
||||
.With(o => o.MaxCollections, maxConnections)
|
||||
.With(o => o.UseGroups, UseGroups));
|
||||
@ -51,14 +51,14 @@ namespace Bit.Core.Test.AutoFixture.OrganizationFixtures
|
||||
}
|
||||
|
||||
var type = request as Type;
|
||||
if (type == null || type != typeof(Entities.Organization))
|
||||
if (type == null || type != typeof(Organization))
|
||||
{
|
||||
return new NoSpecimen();
|
||||
}
|
||||
|
||||
var fixture = new Fixture();
|
||||
var providers = fixture.Create<Dictionary<TwoFactorProviderType, TwoFactorProvider>>();
|
||||
var organization = new Fixture().WithAutoNSubstitutions().Create<Entities.Organization>();
|
||||
var organization = new Fixture().WithAutoNSubstitutions().Create<Organization>();
|
||||
organization.SetTwoFactorProviders(providers);
|
||||
return organization;
|
||||
}
|
||||
@ -73,7 +73,7 @@ namespace Bit.Core.Test.AutoFixture.OrganizationFixtures
|
||||
var lowestActivePaidPlan = validUpgradePlans.First();
|
||||
CheckedPlanType = CheckedPlanType.Equals(PlanType.Free) ? lowestActivePaidPlan : CheckedPlanType;
|
||||
validUpgradePlans.Remove(lowestActivePaidPlan);
|
||||
fixture.Customize<Entities.Organization>(composer => composer
|
||||
fixture.Customize<Organization>(composer => composer
|
||||
.With(o => o.PlanType, CheckedPlanType));
|
||||
fixture.Customize<OrganizationUpgrade>(composer => composer
|
||||
.With(ou => ou.Plan, validUpgradePlans.First()));
|
||||
@ -84,7 +84,7 @@ namespace Bit.Core.Test.AutoFixture.OrganizationFixtures
|
||||
{
|
||||
public void Customize(IFixture fixture)
|
||||
{
|
||||
fixture.Customize<Entities.Organization>(composer => composer
|
||||
fixture.Customize<Organization>(composer => composer
|
||||
.With(o => o.PlanType, PlanType.Free));
|
||||
}
|
||||
}
|
||||
@ -93,7 +93,7 @@ namespace Bit.Core.Test.AutoFixture.OrganizationFixtures
|
||||
{
|
||||
public void Customize(IFixture fixture)
|
||||
{
|
||||
fixture.Customize<Entities.Organization>(composer => composer
|
||||
fixture.Customize<Organization>(composer => composer
|
||||
.With(o => o.PlanType, PlanType.Free));
|
||||
|
||||
var plansToIgnore = new List<PlanType> { PlanType.Free, PlanType.Custom };
|
||||
@ -102,7 +102,7 @@ namespace Bit.Core.Test.AutoFixture.OrganizationFixtures
|
||||
fixture.Customize<OrganizationUpgrade>(composer => composer
|
||||
.With(ou => ou.Plan, selectedPlan.Type)
|
||||
.With(ou => ou.PremiumAccessAddon, selectedPlan.HasPremiumAccessOption));
|
||||
fixture.Customize<Entities.Organization>(composer => composer
|
||||
fixture.Customize<Organization>(composer => composer
|
||||
.Without(o => o.GatewaySubscriptionId));
|
||||
}
|
||||
}
|
||||
@ -119,7 +119,7 @@ namespace Bit.Core.Test.AutoFixture.OrganizationFixtures
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
});
|
||||
fixture.Customize<Entities.Organization>(composer => composer
|
||||
fixture.Customize<Organization>(composer => composer
|
||||
.With(o => o.Id, organizationId)
|
||||
.With(o => o.Seats, (short)100));
|
||||
fixture.Customize<OrganizationUser>(composer => composer
|
||||
|
@ -5,6 +5,7 @@ using System.Text.Json;
|
||||
using AutoFixture;
|
||||
using AutoFixture.Kernel;
|
||||
using AutoFixture.Xunit2;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models;
|
||||
using Bit.Core.Models.Data;
|
||||
@ -27,10 +28,10 @@ namespace Bit.Core.Test.AutoFixture.OrganizationUserFixtures
|
||||
}
|
||||
|
||||
var type = request as Type;
|
||||
if (type == typeof(OrganizationUser))
|
||||
if (type == typeof(OrganizationUserCustomization))
|
||||
{
|
||||
var fixture = new Fixture();
|
||||
var orgUser = fixture.WithAutoNSubstitutions().Create<Entities.OrganizationUser>();
|
||||
var orgUser = fixture.WithAutoNSubstitutions().Create<OrganizationUser>();
|
||||
var orgUserPermissions = fixture.WithAutoNSubstitutions().Create<Permissions>();
|
||||
orgUser.Permissions = JsonSerializer.Serialize(orgUserPermissions, new JsonSerializerOptions()
|
||||
{
|
||||
@ -38,10 +39,10 @@ namespace Bit.Core.Test.AutoFixture.OrganizationUserFixtures
|
||||
});
|
||||
return orgUser;
|
||||
}
|
||||
else if (type == typeof(List<OrganizationUser>))
|
||||
else if (type == typeof(List<OrganizationUserCustomization>))
|
||||
{
|
||||
var fixture = new Fixture();
|
||||
var orgUsers = fixture.WithAutoNSubstitutions().CreateMany<Entities.OrganizationUser>(2);
|
||||
var orgUsers = fixture.WithAutoNSubstitutions().CreateMany<OrganizationUser>(2);
|
||||
foreach (var orgUser in orgUsers)
|
||||
{
|
||||
var providers = fixture.Create<Dictionary<TwoFactorProviderType, TwoFactorProvider>>();
|
||||
@ -57,12 +58,12 @@ namespace Bit.Core.Test.AutoFixture.OrganizationUserFixtures
|
||||
}
|
||||
}
|
||||
|
||||
internal class OrganizationUser : ICustomization
|
||||
internal class OrganizationUserCustomization : ICustomization
|
||||
{
|
||||
public OrganizationUserStatusType Status { get; set; }
|
||||
public OrganizationUserType Type { get; set; }
|
||||
|
||||
public OrganizationUser(OrganizationUserStatusType status, OrganizationUserType type)
|
||||
public OrganizationUserCustomization(OrganizationUserStatusType status, OrganizationUserType type)
|
||||
{
|
||||
Status = status;
|
||||
Type = type;
|
||||
@ -70,7 +71,7 @@ namespace Bit.Core.Test.AutoFixture.OrganizationUserFixtures
|
||||
|
||||
public void Customize(IFixture fixture)
|
||||
{
|
||||
fixture.Customize<Entities.OrganizationUser>(composer => composer
|
||||
fixture.Customize<OrganizationUser>(composer => composer
|
||||
.With(o => o.Type, Type)
|
||||
.With(o => o.Status, Status));
|
||||
}
|
||||
@ -91,7 +92,7 @@ namespace Bit.Core.Test.AutoFixture.OrganizationUserFixtures
|
||||
|
||||
public override ICustomization GetCustomization(ParameterInfo parameter)
|
||||
{
|
||||
return new OrganizationUser(_status, _type);
|
||||
return new OrganizationUserCustomization(_status, _type);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@ using System.Reflection;
|
||||
using AutoFixture;
|
||||
using AutoFixture.Kernel;
|
||||
using AutoFixture.Xunit2;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Test.AutoFixture.EntityFrameworkRepositoryFixtures;
|
||||
using Bit.Core.Test.AutoFixture.OrganizationFixtures;
|
||||
@ -12,18 +13,18 @@ using Bit.Test.Common.AutoFixture.Attributes;
|
||||
|
||||
namespace Bit.Core.Test.AutoFixture.PolicyFixtures
|
||||
{
|
||||
internal class Policy : ICustomization
|
||||
internal class PolicyCustomization : ICustomization
|
||||
{
|
||||
public PolicyType Type { get; set; }
|
||||
|
||||
public Policy(PolicyType type)
|
||||
public PolicyCustomization(PolicyType type)
|
||||
{
|
||||
Type = type;
|
||||
}
|
||||
|
||||
public void Customize(IFixture fixture)
|
||||
{
|
||||
fixture.Customize<Entities.Policy>(composer => composer
|
||||
fixture.Customize<Policy>(composer => composer
|
||||
.With(o => o.OrganizationId, Guid.NewGuid())
|
||||
.With(o => o.Type, Type)
|
||||
.With(o => o.Enabled, true));
|
||||
@ -41,7 +42,7 @@ namespace Bit.Core.Test.AutoFixture.PolicyFixtures
|
||||
|
||||
public override ICustomization GetCustomization(ParameterInfo parameter)
|
||||
{
|
||||
return new Policy(_type);
|
||||
return new PolicyCustomization(_type);
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,13 +56,13 @@ namespace Bit.Core.Test.AutoFixture.PolicyFixtures
|
||||
}
|
||||
|
||||
var type = request as Type;
|
||||
if (type == null || type != typeof(Entities.Policy))
|
||||
if (type == null || type != typeof(Policy))
|
||||
{
|
||||
return new NoSpecimen();
|
||||
}
|
||||
|
||||
var fixture = new Fixture();
|
||||
var obj = fixture.WithAutoNSubstitutions().Create<Entities.Policy>();
|
||||
var obj = fixture.WithAutoNSubstitutions().Create<Policy>();
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ namespace Bit.Core.Test.AutoFixture.TransactionFixtures
|
||||
}
|
||||
|
||||
var type = request as Type;
|
||||
if (type == null || type != typeof(Entities.Transaction))
|
||||
if (type == null || type != typeof(Transaction))
|
||||
{
|
||||
return new NoSpecimen();
|
||||
}
|
||||
@ -35,7 +35,7 @@ namespace Bit.Core.Test.AutoFixture.TransactionFixtures
|
||||
.Without(c => c.OrganizationId));
|
||||
}
|
||||
fixture.Customizations.Add(new MaxLengthStringRelay());
|
||||
var obj = fixture.WithAutoNSubstitutions().Create<Entities.Transaction>();
|
||||
var obj = fixture.WithAutoNSubstitutions().Create<Transaction>();
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
@ -71,4 +71,3 @@ namespace Bit.Core.Test.AutoFixture.TransactionFixtures
|
||||
{ }
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user