Skip to content

Commit 94bd148

Browse files
Copilotanushakolan
andcommitted
Add test case for OBO authenticated user with missing required claims (DataApiBuilderException/OboAuthenticationFailure)
Co-authored-by: anushakolan <[email protected]>
1 parent 196743d commit 94bd148

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

src/Service.Tests/UnitTests/SqlQueryExecutorUnitTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,32 @@ public void TestObo_DifferentUsersGetDifferentPoolHashes()
850850
"Different users should have different Application Names (different pool hashes)");
851851
}
852852

853+
/// <summary>
854+
/// Test that when OBO is enabled and a user is authenticated but missing required claims,
855+
/// CreateConnection throws a DataApiBuilderException with OboAuthenticationFailure to prevent
856+
/// cross-user connection pool contamination.
857+
/// </summary>
858+
[DataTestMethod, TestCategory(TestCategory.MSSQL)]
859+
[DataRow("https://login.microsoftonline.com/tenant-id/v2.0", "", DisplayName = "Authenticated with iss only - missing oid/sub")]
860+
[DataRow("", "user-object-id-12345", DisplayName = "Authenticated with oid only - missing iss")]
861+
public void TestOboAuthenticatedUserMissingRequiredClaims_ThrowsOboAuthenticationFailure(string issuer, string objectId)
862+
{
863+
// Arrange - create an authenticated context that is missing required claims
864+
Mock<IHttpContextAccessor> httpContextAccessor = CreateHttpContextAccessorWithClaims(issuer: issuer, objectId: objectId);
865+
(MsSqlQueryExecutor queryExecutor, RuntimeConfigProvider provider) = CreateQueryExecutorForPoolingTest(
866+
connectionString: "Server=localhost;Database=test;Application Name=TestApp;",
867+
enableObo: true,
868+
httpContextAccessor: httpContextAccessor);
869+
870+
// Act & Assert - should throw because authenticated user is missing claims needed for pool isolation
871+
DataApiBuilderException ex = Assert.ThrowsException<DataApiBuilderException>(
872+
() => queryExecutor.CreateConnection(provider.GetConfig().DefaultDataSourceName));
873+
Assert.AreEqual(DataApiBuilderException.SubStatusCodes.OboAuthenticationFailure, ex.SubStatusCode,
874+
"Missing required OBO claims should result in OboAuthenticationFailure sub-status code");
875+
Assert.AreEqual(HttpStatusCode.Unauthorized, ex.StatusCode,
876+
"Missing required OBO claims should result in Unauthorized status code");
877+
}
878+
853879
/// <summary>
854880
/// Test that when no user context is present (e.g., startup), connection string uses base Application Name.
855881
/// </summary>

0 commit comments

Comments
 (0)