From fb0bfe457907b9a51365287d9d1fdf152860a764 Mon Sep 17 00:00:00 2001 From: Jared Goodwin Date: Fri, 23 Jun 2023 04:20:25 -0700 Subject: [PATCH] Update tests. --- Server/Services/DataService.cs | 53 ++++++++++++++------------ Tests/Server.Tests/DataServiceTests.cs | 50 +++++++++++++----------- 2 files changed, 57 insertions(+), 46 deletions(-) diff --git a/Server/Services/DataService.cs b/Server/Services/DataService.cs index 721b900ee..21bcb12a2 100644 --- a/Server/Services/DataService.cs +++ b/Server/Services/DataService.cs @@ -1159,16 +1159,18 @@ public int GetDeviceCount(RemotelyUser user) { using var dbContext = _appDbFactory.GetContext(); - return dbContext.Devices - .Include(x => x.DeviceGroup) - .ThenInclude(x => x.Users) - .Count(x => - x.OrganizationID == user.OrganizationID && - ( - user.IsAdministrator || - string.IsNullOrWhiteSpace(x.DeviceGroupID) || - x.DeviceGroup.Users.Any(deviceUser => deviceUser.Id == user.Id) - )); + if (user.IsAdministrator) + { + return GetDeviceCount(); + } + + return dbContext.Users + .Include(x => x.DeviceGroups) + .ThenInclude(x => x.Devices) + .Where(x => x.Id == user.Id) + .SelectMany(x => x.DeviceGroups) + .SelectMany(x => x.Devices) + .Count(); } public async Task GetDeviceGroup( @@ -1257,27 +1259,30 @@ public Device[] GetDevicesForUser(string userName) return Array.Empty(); } - var user = dbContext.Users.FirstOrDefault(x => x.UserName == userName); + var user = dbContext.Users + .AsNoTracking() + .FirstOrDefault(x => x.UserName == userName); if (user is null) { return Array.Empty(); } - var deviceIds = dbContext.Devices - .Include(x => x.DeviceGroup) - .ThenInclude(x => x.Users) - .Where(x => - x.OrganizationID == user.OrganizationID && - ( - user.IsAdministrator || - string.IsNullOrWhiteSpace(x.DeviceGroupID) || - x.DeviceGroup.Users.Any(deviceUser => deviceUser.Id == user.Id) - )) - .Select(x => x.ID); + if (user.IsAdministrator) + { + return dbContext.Devices + .AsNoTracking() + .Where(x => x.OrganizationID == user.OrganizationID) + .ToArray(); + } - return dbContext.Devices - .Where(x => deviceIds.Contains(x.ID)) + return dbContext.Users + .AsNoTracking() + .Include(x => x.DeviceGroups) + .ThenInclude(x => x.Devices) + .Where(x => x.UserName == userName) + .SelectMany(x => x.DeviceGroups) + .SelectMany(x => x.Devices) .ToArray(); } diff --git a/Tests/Server.Tests/DataServiceTests.cs b/Tests/Server.Tests/DataServiceTests.cs index 2dbd23f16..245285bc0 100644 --- a/Tests/Server.Tests/DataServiceTests.cs +++ b/Tests/Server.Tests/DataServiceTests.cs @@ -14,9 +14,9 @@ namespace Remotely.Tests [TestClass] public class DataServiceTests { + private readonly string _newDeviceID = "NewDeviceName"; private IDataService _dataService; private TestData _testData; - private string _newDeviceID = "NewDeviceName"; [TestMethod] public async Task AddAlert() @@ -75,32 +75,46 @@ public async Task CreateDevice() [TestMethod] public void DeviceGroupPermissions() { - Assert.IsTrue(_dataService.GetDevicesForUser(_testData.Org1Admin1.UserName).Count() == 2); - Assert.IsTrue(_dataService.GetDevicesForUser(_testData.Org1Admin2.UserName).Count() == 2); - Assert.IsTrue(_dataService.GetDevicesForUser(_testData.Org1User1.UserName).Count() == 2); - Assert.IsTrue(_dataService.GetDevicesForUser(_testData.Org1User2.UserName).Count() == 2); + Assert.AreEqual(2, _dataService.GetDevicesForUser(_testData.Org1Admin1.UserName).Length); + Assert.AreEqual(2, _dataService.GetDevicesForUser(_testData.Org1Admin2.UserName).Length); + Assert.AreEqual(0, _dataService.GetDevicesForUser(_testData.Org1User1.UserName).Length); + Assert.AreEqual(0, _dataService.GetDevicesForUser(_testData.Org1User2.UserName).Length); + Assert.AreEqual(0, _dataService.GetDevicesForUser(_testData.Org2User1.UserName).Length); + Assert.AreEqual(0, _dataService.GetDevicesForUser(_testData.Org2User2.UserName).Length); - var groupID = _dataService.GetDeviceGroups(_testData.Org1Admin1.UserName).First().ID; + Assert.IsTrue(_dataService.DoesUserHaveAccessToDevice(_testData.Org1Device1.ID, _testData.Org1Admin1)); + Assert.IsTrue(_dataService.DoesUserHaveAccessToDevice(_testData.Org1Device1.ID, _testData.Org1Admin2)); + Assert.IsFalse(_dataService.DoesUserHaveAccessToDevice(_testData.Org1Device1.ID, _testData.Org1User1)); + Assert.IsFalse(_dataService.DoesUserHaveAccessToDevice(_testData.Org1Device1.ID, _testData.Org1User2)); + Assert.IsFalse(_dataService.DoesUserHaveAccessToDevice(_testData.Org1Device1.ID, _testData.Org2User1)); + Assert.IsFalse(_dataService.DoesUserHaveAccessToDevice(_testData.Org1Device1.ID, _testData.Org2User2)); - _dataService.UpdateDevice(_testData.Org1Device1.ID, "", "", groupID, ""); + var groupID = _testData.Org1Group1.ID; _dataService.AddUserToDeviceGroup(_testData.Org1Id, groupID, _testData.Org1User1.UserName, out _); + _testData.Org1Device1.DeviceGroupID = groupID; + _dataService.UpdateDevice(_testData.Org1Device1.ID, "", "", groupID, ""); - Assert.IsTrue(_dataService.GetDevicesForUser(_testData.Org1Admin1.UserName).Count() == 2); - Assert.IsTrue(_dataService.GetDevicesForUser(_testData.Org1Admin2.UserName).Count() == 2); - Assert.IsTrue(_dataService.GetDevicesForUser(_testData.Org1User1.UserName).Count() == 2); - Assert.IsTrue(_dataService.GetDevicesForUser(_testData.Org1User2.UserName).Count() == 1); + Assert.AreEqual(2, _dataService.GetDevicesForUser(_testData.Org1Admin1.UserName).Length); + Assert.AreEqual(2, _dataService.GetDevicesForUser(_testData.Org1Admin2.UserName).Length); + Assert.AreEqual(1, _dataService.GetDevicesForUser(_testData.Org1User1.UserName).Length); + Assert.AreEqual(0, _dataService.GetDevicesForUser(_testData.Org1User2.UserName).Length); + Assert.AreEqual(0, _dataService.GetDevicesForUser(_testData.Org2User1.UserName).Length); + Assert.AreEqual(0, _dataService.GetDevicesForUser(_testData.Org2User2.UserName).Length); Assert.IsTrue(_dataService.DoesUserHaveAccessToDevice(_testData.Org1Device1.ID, _testData.Org1Admin1)); Assert.IsTrue(_dataService.DoesUserHaveAccessToDevice(_testData.Org1Device1.ID, _testData.Org1Admin2)); Assert.IsTrue(_dataService.DoesUserHaveAccessToDevice(_testData.Org1Device1.ID, _testData.Org1User1)); Assert.IsFalse(_dataService.DoesUserHaveAccessToDevice(_testData.Org1Device1.ID, _testData.Org1User2)); + Assert.IsFalse(_dataService.DoesUserHaveAccessToDevice(_testData.Org1Device1.ID, _testData.Org2User1)); + Assert.IsFalse(_dataService.DoesUserHaveAccessToDevice(_testData.Org1Device1.ID, _testData.Org2User2)); var allDevices = _dataService.GetAllDevices(_testData.Org1Id).Select(x => x.ID).ToArray(); - Assert.AreEqual(2, _dataService.FilterDeviceIDsByUserPermission(allDevices, _testData.Org1Admin1).Length); Assert.AreEqual(2, _dataService.FilterDeviceIDsByUserPermission(allDevices, _testData.Org1Admin2).Length); - Assert.AreEqual(2, _dataService.FilterDeviceIDsByUserPermission(allDevices, _testData.Org1User1).Length); - Assert.AreEqual(1, _dataService.FilterDeviceIDsByUserPermission(allDevices, _testData.Org1User2).Length); + Assert.AreEqual(1, _dataService.FilterDeviceIDsByUserPermission(allDevices, _testData.Org1User1).Length); + Assert.AreEqual(0, _dataService.FilterDeviceIDsByUserPermission(allDevices, _testData.Org1User2).Length); + Assert.AreEqual(0, _dataService.FilterDeviceIDsByUserPermission(allDevices, _testData.Org2User1).Length); + Assert.AreEqual(0, _dataService.FilterDeviceIDsByUserPermission(allDevices, _testData.Org2User2).Length); } [TestMethod] @@ -179,14 +193,6 @@ public async Task TestInit() _testData = new TestData(); await _testData.Init(); _dataService = IoCActivator.ServiceProvider.GetRequiredService(); - - var newDevice = new Device() - { - ID = _newDeviceID, - DeviceName = Environment.MachineName, - Is64Bit = Environment.Is64BitOperatingSystem, - OrganizationID = _testData.Org1Id - }; } [TestMethod]