Skip to content

Commit

Permalink
Update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Jun 23, 2023
1 parent 593944f commit fb0bfe4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 46 deletions.
53 changes: 29 additions & 24 deletions Server/Services/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DeviceGroup> GetDeviceGroup(
Expand Down Expand Up @@ -1257,27 +1259,30 @@ public Device[] GetDevicesForUser(string userName)
return Array.Empty<Device>();
}

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<Device>();
}

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();
}

Expand Down
50 changes: 28 additions & 22 deletions Tests/Server.Tests/DataServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -179,14 +193,6 @@ public async Task TestInit()
_testData = new TestData();
await _testData.Init();
_dataService = IoCActivator.ServiceProvider.GetRequiredService<IDataService>();

var newDevice = new Device()
{
ID = _newDeviceID,
DeviceName = Environment.MachineName,
Is64Bit = Environment.Is64BitOperatingSystem,
OrganizationID = _testData.Org1Id
};
}

[TestMethod]
Expand Down

0 comments on commit fb0bfe4

Please sign in to comment.