Skip to content

Commit 5813cb6

Browse files
Copilotgwharris7
andauthored
Guarantee FakeHttpMessageHandler disposal in AgentBlueprintServiceTests (#297)
* Initial plan * Fix FakeHttpMessageHandler disposal to use using statements Co-authored-by: gwharris7 <96964444+gwharris7@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: gwharris7 <96964444+gwharris7@users.noreply.github.com>
1 parent 7120cdf commit 5813cb6

1 file changed

Lines changed: 49 additions & 51 deletions

File tree

src/Tests/Microsoft.Agents.A365.DevTools.Cli.Tests/Services/AgentBlueprintServiceTests.cs

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -329,87 +329,87 @@ public async Task GetAgentInstancesForBlueprintAsync_ReturnsFilteredInstances()
329329
{
330330
// Arrange
331331
var (service, handler) = CreateServiceWithFakeHandler();
332-
333-
const string blueprintId = "a1b2c3d4-e5f6-7890-abcd-ef1234567890";
334-
335-
// Response 1: GET /beta/servicePrincipals/microsoft.graph.agentIdentity?$filter=agentIdentityBlueprintId eq '...'
336-
// Server-side filtered response returns only matching SPs
337-
handler.QueueResponse(new HttpResponseMessage(HttpStatusCode.OK)
332+
using (handler)
338333
{
339-
Content = new StringContent(JsonSerializer.Serialize(new
334+
const string blueprintId = "a1b2c3d4-e5f6-7890-abcd-ef1234567890";
335+
336+
// Response 1: GET /beta/servicePrincipals/microsoft.graph.agentIdentity?$filter=agentIdentityBlueprintId eq '...'
337+
// Server-side filtered response returns only matching SPs
338+
handler.QueueResponse(new HttpResponseMessage(HttpStatusCode.OK)
340339
{
341-
value = new[]
340+
Content = new StringContent(JsonSerializer.Serialize(new
342341
{
343-
new { id = "sp-obj-1", displayName = "Instance A", agentIdentityBlueprintId = blueprintId }
344-
}
345-
}))
346-
});
342+
value = new[]
343+
{
344+
new { id = "sp-obj-1", displayName = "Instance A", agentIdentityBlueprintId = blueprintId }
345+
}
346+
}))
347+
});
347348

348-
// Response 2: GET /beta/users?$filter=identityParentId eq 'sp-obj-1'
349-
// Secondary call to resolve the agentic user for the matching SP
350-
handler.QueueResponse(new HttpResponseMessage(HttpStatusCode.OK)
351-
{
352-
Content = new StringContent(JsonSerializer.Serialize(new
349+
// Response 2: GET /beta/users?$filter=identityParentId eq 'sp-obj-1'
350+
// Secondary call to resolve the agentic user for the matching SP
351+
handler.QueueResponse(new HttpResponseMessage(HttpStatusCode.OK)
353352
{
354-
value = new[] { new { id = "user-obj-1" } }
355-
}))
356-
});
357-
358-
// Act
359-
var instances = await service.GetAgentInstancesForBlueprintAsync("tenant-id", blueprintId);
353+
Content = new StringContent(JsonSerializer.Serialize(new
354+
{
355+
value = new[] { new { id = "user-obj-1" } }
356+
}))
357+
});
360358

361-
// Assert
362-
instances.Should().HaveCount(1);
363-
instances[0].IdentitySpId.Should().Be("sp-obj-1");
364-
instances[0].DisplayName.Should().Be("Instance A");
365-
instances[0].AgentUserId.Should().Be("user-obj-1");
359+
// Act
360+
var instances = await service.GetAgentInstancesForBlueprintAsync("tenant-id", blueprintId);
366361

367-
handler.Dispose();
362+
// Assert
363+
instances.Should().HaveCount(1);
364+
instances[0].IdentitySpId.Should().Be("sp-obj-1");
365+
instances[0].DisplayName.Should().Be("Instance A");
366+
instances[0].AgentUserId.Should().Be("user-obj-1");
367+
}
368368
}
369369

370370
[Fact]
371371
public async Task GetAgentInstancesForBlueprintAsync_ReturnsEmpty_WhenNoneFound()
372372
{
373373
// Arrange
374374
var (service, handler) = CreateServiceWithFakeHandler();
375-
376-
handler.QueueResponse(new HttpResponseMessage(HttpStatusCode.OK)
375+
using (handler)
377376
{
378-
Content = new StringContent(JsonSerializer.Serialize(new { value = Array.Empty<object>() }))
379-
});
377+
handler.QueueResponse(new HttpResponseMessage(HttpStatusCode.OK)
378+
{
379+
Content = new StringContent(JsonSerializer.Serialize(new { value = Array.Empty<object>() }))
380+
});
380381

381-
// Act
382-
var instances = await service.GetAgentInstancesForBlueprintAsync("tenant-id", "b2c3d4e5-f6a7-8901-bcde-f12345678901");
382+
// Act
383+
var instances = await service.GetAgentInstancesForBlueprintAsync("tenant-id", "b2c3d4e5-f6a7-8901-bcde-f12345678901");
383384

384-
// Assert
385-
instances.Should().BeEmpty();
386-
387-
handler.Dispose();
385+
// Assert
386+
instances.Should().BeEmpty();
387+
}
388388
}
389389

390390
[Fact]
391391
public async Task DeleteAgentUserAsync_ReturnsTrue_OnSuccess()
392392
{
393393
// Arrange
394394
var (service, handler) = CreateServiceWithFakeHandler();
395+
using (handler)
396+
{
397+
// Queue HTTP response for DELETE /beta/agentUsers/{userId}
398+
handler.QueueResponse(new HttpResponseMessage(HttpStatusCode.NoContent));
395399

396-
// Queue HTTP response for DELETE /beta/agentUsers/{userId}
397-
handler.QueueResponse(new HttpResponseMessage(HttpStatusCode.NoContent));
398-
399-
// Act
400-
var result = await service.DeleteAgentUserAsync("tenant-id", "user-obj-1");
400+
// Act
401+
var result = await service.DeleteAgentUserAsync("tenant-id", "user-obj-1");
401402

402-
// Assert
403-
result.Should().BeTrue();
404-
405-
handler.Dispose();
403+
// Assert
404+
result.Should().BeTrue();
405+
}
406406
}
407407

408408
[Fact]
409409
public async Task DeleteAgentUserAsync_ReturnsFalse_OnGraphError()
410410
{
411411
// Arrange
412-
var handler = new FakeHttpMessageHandler();
412+
using var handler = new FakeHttpMessageHandler();
413413

414414
_mockTokenProvider.GetMgGraphAccessTokenAsync(
415415
Arg.Any<string>(), Arg.Any<IEnumerable<string>>(), Arg.Any<bool>(), Arg.Any<string?>(), Arg.Any<CancellationToken>())
@@ -429,8 +429,6 @@ public async Task DeleteAgentUserAsync_ReturnsFalse_OnGraphError()
429429

430430
// Assert
431431
result.Should().BeFalse();
432-
433-
handler.Dispose();
434432
}
435433

436434
private (AgentBlueprintService service, FakeHttpMessageHandler handler) CreateServiceWithFakeHandler()

0 commit comments

Comments
 (0)