1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-01 08:02:49 -05:00

Run formatting (#2230)

This commit is contained in:
Justin Baur
2022-08-29 16:06:55 -04:00
committed by GitHub
parent 9b7aef0763
commit 7f5f010e1e
1205 changed files with 73813 additions and 75022 deletions

View File

@ -5,96 +5,95 @@ using Bit.Core.Models;
using Bit.Test.Common.Helpers;
using Xunit;
namespace Bit.Core.Test.Entities
namespace Bit.Core.Test.Entities;
public class OrganizationTests
{
public class OrganizationTests
private static readonly Dictionary<TwoFactorProviderType, TwoFactorProvider> _testConfig = new Dictionary<TwoFactorProviderType, TwoFactorProvider>()
{
private static readonly Dictionary<TwoFactorProviderType, TwoFactorProvider> _testConfig = new Dictionary<TwoFactorProviderType, TwoFactorProvider>()
[TwoFactorProviderType.OrganizationDuo] = new TwoFactorProvider
{
[TwoFactorProviderType.OrganizationDuo] = new TwoFactorProvider
Enabled = true,
MetaData = new Dictionary<string, object>
{
Enabled = true,
MetaData = new Dictionary<string, object>
{
["IKey"] = "IKey_value",
["SKey"] = "SKey_value",
["Host"] = "Host_value",
},
}
["IKey"] = "IKey_value",
["SKey"] = "SKey_value",
["Host"] = "Host_value",
},
}
};
[Fact]
public void SetTwoFactorProviders_Success()
{
var organization = new Organization();
organization.SetTwoFactorProviders(_testConfig);
using var jsonDocument = JsonDocument.Parse(organization.TwoFactorProviders);
var root = jsonDocument.RootElement;
var duo = AssertHelper.AssertJsonProperty(root, "6", JsonValueKind.Object);
AssertHelper.AssertJsonProperty(duo, "Enabled", JsonValueKind.True);
var duoMetaData = AssertHelper.AssertJsonProperty(duo, "MetaData", JsonValueKind.Object);
var iKey = AssertHelper.AssertJsonProperty(duoMetaData, "IKey", JsonValueKind.String).GetString();
Assert.Equal("IKey_value", iKey);
var sKey = AssertHelper.AssertJsonProperty(duoMetaData, "SKey", JsonValueKind.String).GetString();
Assert.Equal("SKey_value", sKey);
var host = AssertHelper.AssertJsonProperty(duoMetaData, "Host", JsonValueKind.String).GetString();
Assert.Equal("Host_value", host);
}
[Fact]
public void GetTwoFactorProviders_Success()
{
// This is to get rid of the cached dictionary the SetTwoFactorProviders keeps so we can fully test the JSON reading
// It intent is to mimic a storing of the entity in the database and it being read later
var tempOrganization = new Organization();
tempOrganization.SetTwoFactorProviders(_testConfig);
var organization = new Organization
{
TwoFactorProviders = tempOrganization.TwoFactorProviders,
};
var twoFactorProviders = organization.GetTwoFactorProviders();
[Fact]
public void SetTwoFactorProviders_Success()
{
var organization = new Organization();
organization.SetTwoFactorProviders(_testConfig);
var duo = Assert.Contains(TwoFactorProviderType.OrganizationDuo, (IDictionary<TwoFactorProviderType, TwoFactorProvider>)twoFactorProviders);
Assert.True(duo.Enabled);
Assert.NotNull(duo.MetaData);
var iKey = Assert.Contains("IKey", (IDictionary<string, object>)duo.MetaData);
Assert.Equal("IKey_value", iKey);
var sKey = Assert.Contains("SKey", (IDictionary<string, object>)duo.MetaData);
Assert.Equal("SKey_value", sKey);
var host = Assert.Contains("Host", (IDictionary<string, object>)duo.MetaData);
Assert.Equal("Host_value", host);
}
using var jsonDocument = JsonDocument.Parse(organization.TwoFactorProviders);
var root = jsonDocument.RootElement;
[Fact]
public void GetTwoFactorProviders_SavedWithName_Success()
{
var organization = new Organization();
// This should save items with the string name of the enum and we will validate that we can read
// from that just incase some organizations have it saved that way.
organization.TwoFactorProviders = JsonSerializer.Serialize(_testConfig);
var duo = AssertHelper.AssertJsonProperty(root, "6", JsonValueKind.Object);
AssertHelper.AssertJsonProperty(duo, "Enabled", JsonValueKind.True);
var duoMetaData = AssertHelper.AssertJsonProperty(duo, "MetaData", JsonValueKind.Object);
var iKey = AssertHelper.AssertJsonProperty(duoMetaData, "IKey", JsonValueKind.String).GetString();
Assert.Equal("IKey_value", iKey);
var sKey = AssertHelper.AssertJsonProperty(duoMetaData, "SKey", JsonValueKind.String).GetString();
Assert.Equal("SKey_value", sKey);
var host = AssertHelper.AssertJsonProperty(duoMetaData, "Host", JsonValueKind.String).GetString();
Assert.Equal("Host_value", host);
}
// Preliminary Asserts to make sure we are testing what we want to be testing
using var jsonDocument = JsonDocument.Parse(organization.TwoFactorProviders);
var root = jsonDocument.RootElement;
// This means it saved the enum as its string name
AssertHelper.AssertJsonProperty(root, "OrganizationDuo", JsonValueKind.Object);
[Fact]
public void GetTwoFactorProviders_Success()
{
// This is to get rid of the cached dictionary the SetTwoFactorProviders keeps so we can fully test the JSON reading
// It intent is to mimic a storing of the entity in the database and it being read later
var tempOrganization = new Organization();
tempOrganization.SetTwoFactorProviders(_testConfig);
var organization = new Organization
{
TwoFactorProviders = tempOrganization.TwoFactorProviders,
};
// Actual checks
var twoFactorProviders = organization.GetTwoFactorProviders();
var twoFactorProviders = organization.GetTwoFactorProviders();
var duo = Assert.Contains(TwoFactorProviderType.OrganizationDuo, (IDictionary<TwoFactorProviderType, TwoFactorProvider>)twoFactorProviders);
Assert.True(duo.Enabled);
Assert.NotNull(duo.MetaData);
var iKey = Assert.Contains("IKey", (IDictionary<string, object>)duo.MetaData);
Assert.Equal("IKey_value", iKey);
var sKey = Assert.Contains("SKey", (IDictionary<string, object>)duo.MetaData);
Assert.Equal("SKey_value", sKey);
var host = Assert.Contains("Host", (IDictionary<string, object>)duo.MetaData);
Assert.Equal("Host_value", host);
}
[Fact]
public void GetTwoFactorProviders_SavedWithName_Success()
{
var organization = new Organization();
// This should save items with the string name of the enum and we will validate that we can read
// from that just incase some organizations have it saved that way.
organization.TwoFactorProviders = JsonSerializer.Serialize(_testConfig);
// Preliminary Asserts to make sure we are testing what we want to be testing
using var jsonDocument = JsonDocument.Parse(organization.TwoFactorProviders);
var root = jsonDocument.RootElement;
// This means it saved the enum as its string name
AssertHelper.AssertJsonProperty(root, "OrganizationDuo", JsonValueKind.Object);
// Actual checks
var twoFactorProviders = organization.GetTwoFactorProviders();
var duo = Assert.Contains(TwoFactorProviderType.OrganizationDuo, (IDictionary<TwoFactorProviderType, TwoFactorProvider>)twoFactorProviders);
Assert.True(duo.Enabled);
Assert.NotNull(duo.MetaData);
var iKey = Assert.Contains("IKey", (IDictionary<string, object>)duo.MetaData);
Assert.Equal("IKey_value", iKey);
var sKey = Assert.Contains("SKey", (IDictionary<string, object>)duo.MetaData);
Assert.Equal("SKey_value", sKey);
var host = Assert.Contains("Host", (IDictionary<string, object>)duo.MetaData);
Assert.Equal("Host_value", host);
}
var duo = Assert.Contains(TwoFactorProviderType.OrganizationDuo, (IDictionary<TwoFactorProviderType, TwoFactorProvider>)twoFactorProviders);
Assert.True(duo.Enabled);
Assert.NotNull(duo.MetaData);
var iKey = Assert.Contains("IKey", (IDictionary<string, object>)duo.MetaData);
Assert.Equal("IKey_value", iKey);
var sKey = Assert.Contains("SKey", (IDictionary<string, object>)duo.MetaData);
Assert.Equal("SKey_value", sKey);
var host = Assert.Contains("Host", (IDictionary<string, object>)duo.MetaData);
Assert.Equal("Host_value", host);
}
}

View File

@ -5,141 +5,140 @@ using Bit.Core.Models;
using Bit.Test.Common.Helpers;
using Xunit;
namespace Bit.Core.Test.Entities
namespace Bit.Core.Test.Entities;
public class UserTests
{
public class UserTests
// KB MB GB
public const long Multiplier = 1024 * 1024 * 1024;
[Fact]
public void StorageBytesRemaining_HasMax_DoesNotHaveStorage_ReturnsMaxAsBytes()
{
// KB MB GB
public const long Multiplier = 1024 * 1024 * 1024;
short maxStorageGb = 1;
[Fact]
public void StorageBytesRemaining_HasMax_DoesNotHaveStorage_ReturnsMaxAsBytes()
var user = new User
{
short maxStorageGb = 1;
var user = new User
{
MaxStorageGb = maxStorageGb,
Storage = null,
};
var bytesRemaining = user.StorageBytesRemaining();
Assert.Equal(bytesRemaining, maxStorageGb * Multiplier);
}
[Theory]
[InlineData(2, 1 * Multiplier, 1 * Multiplier)]
public void StorageBytesRemaining_HasMax_HasStorage_ReturnRemainingStorage(short maxStorageGb, long storageBytes, long expectedRemainingBytes)
{
var user = new User
{
MaxStorageGb = maxStorageGb,
Storage = storageBytes,
};
var bytesRemaining = user.StorageBytesRemaining();
Assert.Equal(expectedRemainingBytes, bytesRemaining);
}
private static readonly Dictionary<TwoFactorProviderType, TwoFactorProvider> _testTwoFactorConfig = new Dictionary<TwoFactorProviderType, TwoFactorProvider>
{
[TwoFactorProviderType.WebAuthn] = new TwoFactorProvider
{
Enabled = true,
MetaData = new Dictionary<string, object>
{
["Item"] = "thing",
},
},
[TwoFactorProviderType.Email] = new TwoFactorProvider
{
Enabled = false,
MetaData = new Dictionary<string, object>
{
["Email"] = "test@email.com",
},
},
MaxStorageGb = maxStorageGb,
Storage = null,
};
[Fact]
public void SetTwoFactorProviders_Success()
var bytesRemaining = user.StorageBytesRemaining();
Assert.Equal(bytesRemaining, maxStorageGb * Multiplier);
}
[Theory]
[InlineData(2, 1 * Multiplier, 1 * Multiplier)]
public void StorageBytesRemaining_HasMax_HasStorage_ReturnRemainingStorage(short maxStorageGb, long storageBytes, long expectedRemainingBytes)
{
var user = new User
{
var user = new User();
user.SetTwoFactorProviders(_testTwoFactorConfig);
MaxStorageGb = maxStorageGb,
Storage = storageBytes,
};
using var jsonDocument = JsonDocument.Parse(user.TwoFactorProviders);
var root = jsonDocument.RootElement;
var bytesRemaining = user.StorageBytesRemaining();
var webAuthn = AssertHelper.AssertJsonProperty(root, "7", JsonValueKind.Object);
AssertHelper.AssertJsonProperty(webAuthn, "Enabled", JsonValueKind.True);
var webMetaData = AssertHelper.AssertJsonProperty(webAuthn, "MetaData", JsonValueKind.Object);
AssertHelper.AssertJsonProperty(webMetaData, "Item", JsonValueKind.String);
Assert.Equal(expectedRemainingBytes, bytesRemaining);
}
var email = AssertHelper.AssertJsonProperty(root, "1", JsonValueKind.Object);
AssertHelper.AssertJsonProperty(email, "Enabled", JsonValueKind.False);
var emailMetaData = AssertHelper.AssertJsonProperty(email, "MetaData", JsonValueKind.Object);
AssertHelper.AssertJsonProperty(emailMetaData, "Email", JsonValueKind.String);
}
[Fact]
public void GetTwoFactorProviders_Success()
private static readonly Dictionary<TwoFactorProviderType, TwoFactorProvider> _testTwoFactorConfig = new Dictionary<TwoFactorProviderType, TwoFactorProvider>
{
[TwoFactorProviderType.WebAuthn] = new TwoFactorProvider
{
// This is to get rid of the cached dictionary the SetTwoFactorProviders keeps so we can fully test the JSON reading
// It intent is to mimic a storing of the entity in the database and it being read later
var tempUser = new User();
tempUser.SetTwoFactorProviders(_testTwoFactorConfig);
var user = new User
Enabled = true,
MetaData = new Dictionary<string, object>
{
TwoFactorProviders = tempUser.TwoFactorProviders,
};
var twoFactorProviders = user.GetTwoFactorProviders();
var webAuthn = Assert.Contains(TwoFactorProviderType.WebAuthn, (IDictionary<TwoFactorProviderType, TwoFactorProvider>)twoFactorProviders);
Assert.True(webAuthn.Enabled);
Assert.NotNull(webAuthn.MetaData);
var webAuthnMetaDataItem = Assert.Contains("Item", (IDictionary<string, object>)webAuthn.MetaData);
Assert.Equal("thing", webAuthnMetaDataItem);
var email = Assert.Contains(TwoFactorProviderType.Email, (IDictionary<TwoFactorProviderType, TwoFactorProvider>)twoFactorProviders);
Assert.False(email.Enabled);
Assert.NotNull(email.MetaData);
var emailMetaDataEmail = Assert.Contains("Email", (IDictionary<string, object>)email.MetaData);
Assert.Equal("test@email.com", emailMetaDataEmail);
}
[Fact]
public void GetTwoFactorProviders_SavedWithName_Success()
["Item"] = "thing",
},
},
[TwoFactorProviderType.Email] = new TwoFactorProvider
{
var user = new User();
// This should save items with the string name of the enum and we will validate that we can read
// from that just incase some users have it saved that way.
user.TwoFactorProviders = JsonSerializer.Serialize(_testTwoFactorConfig);
Enabled = false,
MetaData = new Dictionary<string, object>
{
["Email"] = "test@email.com",
},
},
};
// Preliminary Asserts to make sure we are testing what we want to be testing
using var jsonDocument = JsonDocument.Parse(user.TwoFactorProviders);
var root = jsonDocument.RootElement;
// This means it saved the enum as its string name
AssertHelper.AssertJsonProperty(root, "WebAuthn", JsonValueKind.Object);
AssertHelper.AssertJsonProperty(root, "Email", JsonValueKind.Object);
[Fact]
public void SetTwoFactorProviders_Success()
{
var user = new User();
user.SetTwoFactorProviders(_testTwoFactorConfig);
// Actual checks
var twoFactorProviders = user.GetTwoFactorProviders();
using var jsonDocument = JsonDocument.Parse(user.TwoFactorProviders);
var root = jsonDocument.RootElement;
var webAuthn = Assert.Contains(TwoFactorProviderType.WebAuthn, (IDictionary<TwoFactorProviderType, TwoFactorProvider>)twoFactorProviders);
Assert.True(webAuthn.Enabled);
Assert.NotNull(webAuthn.MetaData);
var webAuthnMetaDataItem = Assert.Contains("Item", (IDictionary<string, object>)webAuthn.MetaData);
Assert.Equal("thing", webAuthnMetaDataItem);
var webAuthn = AssertHelper.AssertJsonProperty(root, "7", JsonValueKind.Object);
AssertHelper.AssertJsonProperty(webAuthn, "Enabled", JsonValueKind.True);
var webMetaData = AssertHelper.AssertJsonProperty(webAuthn, "MetaData", JsonValueKind.Object);
AssertHelper.AssertJsonProperty(webMetaData, "Item", JsonValueKind.String);
var email = Assert.Contains(TwoFactorProviderType.Email, (IDictionary<TwoFactorProviderType, TwoFactorProvider>)twoFactorProviders);
Assert.False(email.Enabled);
Assert.NotNull(email.MetaData);
var emailMetaDataEmail = Assert.Contains("Email", (IDictionary<string, object>)email.MetaData);
Assert.Equal("test@email.com", emailMetaDataEmail);
}
var email = AssertHelper.AssertJsonProperty(root, "1", JsonValueKind.Object);
AssertHelper.AssertJsonProperty(email, "Enabled", JsonValueKind.False);
var emailMetaData = AssertHelper.AssertJsonProperty(email, "MetaData", JsonValueKind.Object);
AssertHelper.AssertJsonProperty(emailMetaData, "Email", JsonValueKind.String);
}
[Fact]
public void GetTwoFactorProviders_Success()
{
// This is to get rid of the cached dictionary the SetTwoFactorProviders keeps so we can fully test the JSON reading
// It intent is to mimic a storing of the entity in the database and it being read later
var tempUser = new User();
tempUser.SetTwoFactorProviders(_testTwoFactorConfig);
var user = new User
{
TwoFactorProviders = tempUser.TwoFactorProviders,
};
var twoFactorProviders = user.GetTwoFactorProviders();
var webAuthn = Assert.Contains(TwoFactorProviderType.WebAuthn, (IDictionary<TwoFactorProviderType, TwoFactorProvider>)twoFactorProviders);
Assert.True(webAuthn.Enabled);
Assert.NotNull(webAuthn.MetaData);
var webAuthnMetaDataItem = Assert.Contains("Item", (IDictionary<string, object>)webAuthn.MetaData);
Assert.Equal("thing", webAuthnMetaDataItem);
var email = Assert.Contains(TwoFactorProviderType.Email, (IDictionary<TwoFactorProviderType, TwoFactorProvider>)twoFactorProviders);
Assert.False(email.Enabled);
Assert.NotNull(email.MetaData);
var emailMetaDataEmail = Assert.Contains("Email", (IDictionary<string, object>)email.MetaData);
Assert.Equal("test@email.com", emailMetaDataEmail);
}
[Fact]
public void GetTwoFactorProviders_SavedWithName_Success()
{
var user = new User();
// This should save items with the string name of the enum and we will validate that we can read
// from that just incase some users have it saved that way.
user.TwoFactorProviders = JsonSerializer.Serialize(_testTwoFactorConfig);
// Preliminary Asserts to make sure we are testing what we want to be testing
using var jsonDocument = JsonDocument.Parse(user.TwoFactorProviders);
var root = jsonDocument.RootElement;
// This means it saved the enum as its string name
AssertHelper.AssertJsonProperty(root, "WebAuthn", JsonValueKind.Object);
AssertHelper.AssertJsonProperty(root, "Email", JsonValueKind.Object);
// Actual checks
var twoFactorProviders = user.GetTwoFactorProviders();
var webAuthn = Assert.Contains(TwoFactorProviderType.WebAuthn, (IDictionary<TwoFactorProviderType, TwoFactorProvider>)twoFactorProviders);
Assert.True(webAuthn.Enabled);
Assert.NotNull(webAuthn.MetaData);
var webAuthnMetaDataItem = Assert.Contains("Item", (IDictionary<string, object>)webAuthn.MetaData);
Assert.Equal("thing", webAuthnMetaDataItem);
var email = Assert.Contains(TwoFactorProviderType.Email, (IDictionary<TwoFactorProviderType, TwoFactorProvider>)twoFactorProviders);
Assert.False(email.Enabled);
Assert.NotNull(email.MetaData);
var emailMetaDataEmail = Assert.Contains("Email", (IDictionary<string, object>)email.MetaData);
Assert.Equal("test@email.com", emailMetaDataEmail);
}
}