Makes it so unit tests can skirt username validation

This commit is contained in:
jvyden 2022-02-23 10:12:21 -05:00
commit 5d28256c3e
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
2 changed files with 8 additions and 5 deletions

View file

@ -16,8 +16,8 @@ public class DatabaseTests : LighthouseServerTest
await using Database database = new();
int rand = new Random().Next();
User userA = await database.CreateUser("createUserTwiceTest" + rand, HashHelper.GenerateAuthToken());
User userB = await database.CreateUser("createUserTwiceTest" + rand, HashHelper.GenerateAuthToken());
User userA = await database.CreateUser("unitTestUser" + rand, HashHelper.GenerateAuthToken());
User userB = await database.CreateUser("unitTestUser" + rand, HashHelper.GenerateAuthToken());
Assert.NotNull(userA);
Assert.NotNull(userB);

View file

@ -50,11 +50,14 @@ public class Database : DbContext
if (!password.StartsWith('$')) throw new ArgumentException(nameof(password) + " is not a BCrypt hash");
// 16 is PSN max, 3 is PSN minimum
if (username.Length > 16 || username.Length < 3) throw new ArgumentException(nameof(username) + " is either too long or too short");
if (!ServerStatics.IsUnitTesting || !username.StartsWith("unitTestUser"))
{
if (username.Length > 16 || username.Length < 3) throw new ArgumentException(nameof(username) + " is either too long or too short");
Regex regex = new("^[a-zA-Z0-9_.-]*$");
Regex regex = new("^[a-zA-Z0-9_.-]*$");
if (!regex.IsMatch(username)) throw new ArgumentException(nameof(username) + " does not match the username regex");
if (!regex.IsMatch(username)) throw new ArgumentException(nameof(username) + " does not match the username regex");
}
User user;
if ((user = await this.Users.Where(u => u.Username == username).FirstOrDefaultAsync()) != null) return user;