diff --git a/src/Tgstation.Server.Host/Jobs/JobsHubGroupMapper.cs b/src/Tgstation.Server.Host/Jobs/JobsHubGroupMapper.cs index be1b14a502..2e2a5da93d 100644 --- a/src/Tgstation.Server.Host/Jobs/JobsHubGroupMapper.cs +++ b/src/Tgstation.Server.Host/Jobs/JobsHubGroupMapper.cs @@ -126,7 +126,7 @@ namespace Tgstation.Server.Host.Jobs .InstancePermissionSets .AsQueryable() .Where(ips => ips.PermissionSetId == authenticationContext.PermissionSet.Id.Value) - .Select(ips => ips.Id) + .Select(ips => ips.InstanceId) .ToListAsync(cancellationToken)); await mappingFunc( diff --git a/tests/Tgstation.Server.Host.Tests/Jobs/TestJobsHubGroupMapper.cs b/tests/Tgstation.Server.Host.Tests/Jobs/TestJobsHubGroupMapper.cs new file mode 100644 index 0000000000..b00d2af4e8 --- /dev/null +++ b/tests/Tgstation.Server.Host.Tests/Jobs/TestJobsHubGroupMapper.cs @@ -0,0 +1,120 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using Moq; + +using Tgstation.Server.Api.Hubs; +using Tgstation.Server.Api.Rights; +using Tgstation.Server.Host.Database; +using Tgstation.Server.Host.Jobs; +using Tgstation.Server.Host.Models; +using Tgstation.Server.Host.Security; +using Tgstation.Server.Host.Utils.SignalR; + +namespace Tgstation.Server.Host.Tests.Jobs +{ + [TestClass] + public sealed class TestJobsHubGroupMapper + { + [TestMethod] + public async Task TestGroupMapping() + { + using var loggerFactory = LoggerFactory.Create(builder => + { + builder.AddConsole(); + builder.SetMinimumLevel(LogLevel.Trace); + }); + + var mockHub = new Mock>(); + var mockDcf = new Mock(); + + + using var context = Utils.CreateDatabaseContext(); + mockDcf.Setup(x => x.UseContext(It.IsNotNull>())).Returns>(func => func(context)); + + var mockPs = new PermissionSet + { + Id = 23421, + InstanceManagerRights = RightsHelper.AllRights(), + AdministrationRights = RightsHelper.AllRights(), + }; + var testIps1 = new InstancePermissionSet + { + ByondRights = RightsHelper.AllRights(), + ChatBotRights = RightsHelper.AllRights(), + ConfigurationRights = RightsHelper.AllRights(), + DreamDaemonRights = RightsHelper.AllRights(), + DreamMakerRights = RightsHelper.AllRights(), + Id = 43892849, + InstanceId = 348928, + InstancePermissionSetRights = RightsHelper.AllRights(), + RepositoryRights = RightsHelper.AllRights(), + PermissionSetId = mockPs.Id.Value, + PermissionSet = mockPs, + }; + + var testIps2 = new InstancePermissionSet + { + ByondRights = RightsHelper.AllRights(), + ChatBotRights = RightsHelper.AllRights(), + ConfigurationRights = RightsHelper.AllRights(), + DreamDaemonRights = RightsHelper.AllRights(), + DreamMakerRights = RightsHelper.AllRights(), + Id = 454354, + InstanceId = 2234, + InstancePermissionSetRights = RightsHelper.AllRights(), + RepositoryRights = RightsHelper.AllRights(), + PermissionSetId = mockPs.Id.Value, + PermissionSet = mockPs, + }; + context.InstancePermissionSets.Add(testIps1); + context.InstancePermissionSets.Add(testIps2); + + var cancellationToken = CancellationToken.None; + await context.SaveChangesAsync(cancellationToken); + + var mockUpdater = new Mock(); + + var mapper = new JobsHubGroupMapper( + mockHub.Object, + mockDcf.Object, + mockUpdater.Object, + loggerFactory.CreateLogger()); + + await mapper.StartAsync(cancellationToken); + + var mockAuthenticationContext = new Mock(); + var mockUser = new User + { + Id = 2134134, + }; + + mockAuthenticationContext.SetupGet(x => x.User).Returns(mockUser); + + mockAuthenticationContext.SetupGet(x => x.PermissionSet).Returns(mockPs); + + bool ran = false; + Task Callback(IEnumerable results) + { + ran = true; + Assert.AreEqual(2, results.Count()); + Assert.IsTrue(results.Contains(JobsHub.HubGroupName(testIps1.InstanceId))); + Assert.IsTrue(results.Contains(JobsHub.HubGroupName(testIps2.InstanceId))); + return Task.CompletedTask; + } + + await mockHub.RaiseAsync(x => x.OnConnectionMapGroups += null, mockAuthenticationContext.Object, (Func, Task>)Callback, cancellationToken); + + Assert.IsTrue(ran); + + await mapper.StopAsync(cancellationToken); + } + } +}