Files
Alphaeus Mote e1fcdf4cea feat(scripts): Run As Each Signed-In User [skip ci]
The script editor's Run As is now a choice: Agent Account, Signed-In User, or
Each Signed-In User. Each runs the script once per signed-in user, in turn,
under one timeout; any failure fails the job and names who. Nobody signed in
runs nothing and succeeds. The server refuses it for agents before 1.14.0.
Migration for all three providers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 12:28:54 -05:00

85 lines
3.1 KiB
C#

using Agent.Configuration;
using Agent.PowerShell;
using Agent.Sessions;
using Agent.Storage;
using Microsoft.Extensions.Logging.Abstractions;
using Shared.Contracts.Messages;
using Shared.Contracts.Models;
using Xunit;
namespace Agent.Tests;
/// <summary>Each Signed-In User: the script runs once per signed-in user, and nobody means nothing to do.</summary>
public sealed class RunAsEachUserTests : IDisposable
{
private readonly string root = Path.Combine(Path.GetTempPath(), "executer-runaseach-tests", Guid.NewGuid().ToString("N"));
private readonly RecordingPublisher publisher = new(() => DateTimeOffset.UtcNow);
private readonly JobHistoryStore history;
public RunAsEachUserTests()
{
Directory.CreateDirectory(root);
history = new JobHistoryStore(Path.Combine(root, "jobs.db"));
}
public void Dispose()
{
history.Dispose();
Microsoft.Data.Sqlite.SqliteConnection.ClearAllPools();
try
{
Directory.Delete(root, recursive: true);
}
catch (IOException)
{
}
}
private sealed class FixedUsers(SignedInUserLookup lookup) : ISignedInUserResolver
{
public SignedInUserLookup Resolve(string? requested = null) => lookup;
}
private async Task<JobStatePayload> RunAsync(SignedInUserLookup lookup, bool eachUser)
{
var handler = new PowerShellJobHandler(
new PowerShellExecutionService(), publisher, history, new Agent.Files.ServerDownloads(null, false), NullLogger.Instance,
new JobRootInfo(Path.Combine(root, "jobs"), Shared.Infrastructure.Configuration.ConfigurationSource.Default),
signedInUsers: new FixedUsers(lookup));
var command = new ScriptCommandPayload(
Guid.NewGuid(), Guid.NewGuid(), "Per User", "'ran'", 60, RunAsUser: true, RunAsEachUser: eachUser);
await handler.RunJobAsync(command, Guid.NewGuid(), null, TestContext.Current.CancellationToken);
return publisher.States.Last(s => s.JobId == command.JobId);
}
[Fact]
public async Task NobodySignedInRunsForNobodyAndSucceeds()
{
var state = await RunAsync(SignedInUserLookup.Nobody(SignedInUsers.NobodyReason, []), eachUser: true);
Assert.Equal(JobState.Succeeded, state.State);
Assert.Contains("ran for nobody", state.Message);
Assert.DoesNotContain(publisher.Output, o => o.Text.Contains("ran") && !o.Text.Contains("nobody"));
}
[Fact]
public async Task SessionsThatCannotBeReadFailTheJob()
{
var state = await RunAsync(SignedInUserLookup.Nobody("The device's sessions could not be read."), eachUser: true);
Assert.Equal(JobState.Failed, state.State);
Assert.Contains("could not be read", state.Message);
}
[Fact]
public async Task OneSignedInUserStillFailsWhenNobodyIsClear()
{
var state = await RunAsync(SignedInUserLookup.Nobody("Several users are signed in", ["ann", "bob"]), eachUser: false);
Assert.Equal(JobState.Failed, state.State);
Assert.Contains("Several users", state.Message);
}
}