From 5d28256c3e96f2f817bed385ed9ab54c8a8bb165 Mon Sep 17 00:00:00 2001 From: jvyden Date: Wed, 23 Feb 2022 10:12:21 -0500 Subject: [PATCH] Makes it so unit tests can skirt username validation --- ProjectLighthouse.Tests.GameApiTests/DatabaseTests.cs | 4 ++-- ProjectLighthouse/Database.cs | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/ProjectLighthouse.Tests.GameApiTests/DatabaseTests.cs b/ProjectLighthouse.Tests.GameApiTests/DatabaseTests.cs index 5aa82220..33da6dcb 100644 --- a/ProjectLighthouse.Tests.GameApiTests/DatabaseTests.cs +++ b/ProjectLighthouse.Tests.GameApiTests/DatabaseTests.cs @@ -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); diff --git a/ProjectLighthouse/Database.cs b/ProjectLighthouse/Database.cs index afa0614a..0ae12fde 100644 --- a/ProjectLighthouse/Database.cs +++ b/ProjectLighthouse/Database.cs @@ -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;