Skip to content

Commit 82201c0

Browse files
committed
Add rudimentary auto generation for 5000 users and ciphers
1 parent 6b03d9f commit 82201c0

File tree

1 file changed

+78
-42
lines changed

1 file changed

+78
-42
lines changed

util/DBSeeder/EFDBSeeder.cs

Lines changed: 78 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Bit.Core.Vault.Enums; // Change this line
33
using Bit.Infrastructure.EntityFramework.Repositories;
44
using Bit.Infrastructure.EntityFramework.Vault.Models;
5+
using Bogus;
56
using Microsoft.Extensions.Logging;
67

78

@@ -38,7 +39,7 @@ public bool SeedDatabase()
3839

3940
// Seed the database
4041
SeedUsers(context);
41-
SeedCipher(context);
42+
SeedCiphers(context);
4243

4344
Console.WriteLine("Database seeded successfully!");
4445
}
@@ -51,8 +52,12 @@ public bool SeedDatabase()
5152
}
5253
catch (Exception ex)
5354
{
54-
Console.WriteLine($"An error occurred: {ex.Message}");
55-
return false;
55+
Console.WriteLine($"Error adding users: {ex.Message}");
56+
if (ex.InnerException != null)
57+
{
58+
Console.WriteLine($"Inner exception: {ex.InnerException.Message}");
59+
}
60+
throw; // Re-throw the exception to stop the seeding process
5661
}
5762

5863

@@ -64,66 +69,97 @@ private void SeedUsers(DatabaseContext context)
6469
{
6570
if (!context.Users.Any())
6671
{
67-
context.Users.AddRange(
68-
new Bit.Infrastructure.EntityFramework.Models.User // Specify the full namespace
72+
Console.WriteLine("Generating 5000 users...");
73+
74+
var faker = new Faker<Bit.Infrastructure.EntityFramework.Models.User>()
75+
.RuleFor(u => u.Id, f => Guid.NewGuid())
76+
.RuleFor(u => u.Name, f => f.Name.FullName())
77+
.RuleFor(u => u.Email, (f, u) => f.Internet.Email(u.Name))
78+
.RuleFor(u => u.EmailVerified, f => f.Random.Bool(0.9f))
79+
.RuleFor(u => u.SecurityStamp, f => Guid.NewGuid().ToString())
80+
.RuleFor(u => u.ApiKey, f => Guid.NewGuid().ToString("N").Substring(0, 30))
81+
.RuleFor(u => u.CreationDate, f => f.Date.Past(2))
82+
.RuleFor(u => u.RevisionDate, (f, u) => f.Date.Between(u.CreationDate, DateTime.UtcNow));
83+
84+
var users = faker.Generate(5000);
85+
86+
const int batchSize = 100;
87+
for (int i = 0; i < users.Count; i += batchSize)
6988
{
70-
Id = Guid.NewGuid(),
71-
Name = "Test User",
72-
Email = "[email protected]",
73-
EmailVerified = true,
74-
SecurityStamp = Guid.NewGuid().ToString(),
75-
ApiKey = "TestApiKey"
89+
context.Users.AddRange(users.Skip(i).Take(batchSize));
90+
context.SaveChanges();
91+
Console.WriteLine($"Added {Math.Min(i + batchSize, users.Count)} users");
7692
}
77-
);
78-
context.SaveChanges();
79-
Console.WriteLine("Test user added to the database.");
93+
94+
Console.WriteLine("5000 test users added to the database.");
8095
}
8196
else
8297
{
8398
Console.WriteLine("Users table is not empty. Skipping user seeding.");
8499
}
85100
}
86101

87-
private void SeedCipher(DatabaseContext context)
102+
private void SeedCiphers(DatabaseContext context)
88103
{
89104
if (!context.Ciphers.Any())
90105
{
91-
var testUser = context.Users.FirstOrDefault();
92-
if (testUser == null)
106+
var users = context.Users.ToList();
107+
if (!users.Any())
93108
{
94109
Console.WriteLine("No users found. Please seed users first.");
95110
return;
96111
}
97112

98-
var cipher = new Cipher
99-
{
100-
Id = Guid.NewGuid(),
101-
UserId = testUser.Id,
102-
OrganizationId = null, // Set this if needed
103-
Type = CipherType.Login,
104-
Data = JsonSerializer.Serialize(new
113+
Console.WriteLine($"Generating ciphers for {users.Count} users...");
114+
115+
var faker = new Faker<Cipher>()
116+
.RuleFor(c => c.Id, f => Guid.NewGuid())
117+
.RuleFor(c => c.Type, f => CipherType.Login)
118+
.RuleFor(c => c.Data, f => JsonSerializer.Serialize(new
105119
{
106-
Name = "Test Login",
107-
Notes = "This is a test login cipher",
120+
Name = f.Internet.DomainName(),
121+
Notes = f.Lorem.Sentence(),
108122
Login = new
109123
{
110-
Username = "testuser",
111-
Password = "testpassword",
112-
Uri = "https://example.com"
124+
Username = f.Internet.UserName(),
125+
Password = f.Internet.Password(),
126+
Uri = f.Internet.Url()
127+
}
128+
}))
129+
.RuleFor(c => c.CreationDate, f => f.Date.Past(1))
130+
.RuleFor(c => c.RevisionDate, (f, c) => f.Date.Between(c.CreationDate, DateTime.UtcNow))
131+
.RuleFor(c => c.DeletedDate, f => null)
132+
.RuleFor(c => c.Reprompt, f => CipherRepromptType.None);
133+
134+
const int batchSize = 100;
135+
for (int i = 0; i < users.Count; i += batchSize)
136+
{
137+
var userBatch = users.Skip(i).Take(batchSize);
138+
var ciphers = userBatch.Select(user =>
139+
{
140+
var cipher = faker.Generate();
141+
cipher.UserId = user.Id;
142+
return cipher;
143+
}).ToList();
144+
145+
try
146+
{
147+
context.Ciphers.AddRange(ciphers);
148+
context.SaveChanges();
149+
Console.WriteLine($"Added ciphers for users {i + 1} to {Math.Min(i + batchSize, users.Count)}");
150+
}
151+
catch (Exception ex)
152+
{
153+
Console.WriteLine($"Error adding ciphers: {ex.Message}");
154+
if (ex.InnerException != null)
155+
{
156+
Console.WriteLine($"Inner exception: {ex.InnerException.Message}");
113157
}
114-
}),
115-
Favorites = null,
116-
Folders = null,
117-
Attachments = null,
118-
CreationDate = DateTime.UtcNow,
119-
RevisionDate = DateTime.UtcNow,
120-
DeletedDate = null,
121-
Reprompt = CipherRepromptType.None
122-
};
123-
124-
context.Ciphers.Add(cipher);
125-
context.SaveChanges();
126-
Console.WriteLine("Test cipher added to the database.");
158+
throw; // Re-throw the exception to stop the seeding process
159+
}
160+
}
161+
162+
Console.WriteLine($"Ciphers added for all {users.Count} users.");
127163
}
128164
else
129165
{

0 commit comments

Comments
 (0)