-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add authentication integration tests (Phase 2) #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
tests/PPDS.LiveTests/Authentication/CertificateAuthenticationTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| using FluentAssertions; | ||
| using PPDS.Auth.Credentials; | ||
| using PPDS.LiveTests.Infrastructure; | ||
| using Xunit; | ||
|
|
||
| namespace PPDS.LiveTests.Authentication; | ||
|
|
||
| /// <summary> | ||
| /// Integration tests for certificate-based (service principal) authentication. | ||
| /// These tests verify that CertificateFileCredentialProvider can successfully | ||
| /// authenticate to Dataverse using a certificate file. | ||
| /// </summary> | ||
| [Trait("Category", "Integration")] | ||
| public class CertificateAuthenticationTests : LiveTestBase, IDisposable | ||
| { | ||
| [SkipIfNoCertificate] | ||
| public async Task CertificateFileCredentialProvider_CreatesWorkingServiceClient() | ||
| { | ||
| // Arrange | ||
| var certPath = Configuration.GetCertificatePath(); | ||
| using var provider = new CertificateFileCredentialProvider( | ||
| Configuration.ApplicationId!, | ||
| certPath, | ||
| Configuration.CertificatePassword, | ||
| Configuration.TenantId!); | ||
|
|
||
| // Act | ||
| using var client = await provider.CreateServiceClientAsync(Configuration.DataverseUrl!); | ||
|
|
||
| // Assert | ||
| client.Should().NotBeNull(); | ||
| client.IsReady.Should().BeTrue("ServiceClient should be ready after successful authentication"); | ||
| } | ||
|
|
||
| [SkipIfNoCertificate] | ||
| public async Task CertificateFileCredentialProvider_CanExecuteWhoAmI() | ||
| { | ||
| // Arrange | ||
| var certPath = Configuration.GetCertificatePath(); | ||
| using var provider = new CertificateFileCredentialProvider( | ||
| Configuration.ApplicationId!, | ||
| certPath, | ||
| Configuration.CertificatePassword, | ||
| Configuration.TenantId!); | ||
|
|
||
| using var client = await provider.CreateServiceClientAsync(Configuration.DataverseUrl!); | ||
|
|
||
| // Act | ||
| var response = (Microsoft.Crm.Sdk.Messages.WhoAmIResponse)client.Execute( | ||
| new Microsoft.Crm.Sdk.Messages.WhoAmIRequest()); | ||
|
|
||
| // Assert | ||
| response.Should().NotBeNull(); | ||
| response.UserId.Should().NotBeEmpty("WhoAmI should return a valid user ID"); | ||
| response.OrganizationId.Should().NotBeEmpty("WhoAmI should return a valid organization ID"); | ||
| } | ||
|
|
||
| [SkipIfNoCertificate] | ||
| public void CertificateFileCredentialProvider_SetsIdentityProperty() | ||
| { | ||
| // Arrange | ||
| var certPath = Configuration.GetCertificatePath(); | ||
| using var provider = new CertificateFileCredentialProvider( | ||
| Configuration.ApplicationId!, | ||
| certPath, | ||
| Configuration.CertificatePassword, | ||
| Configuration.TenantId!); | ||
|
|
||
| // Assert - Identity is set before authentication | ||
| provider.Identity.Should().NotBeNullOrWhiteSpace(); | ||
| provider.Identity.Should().StartWith("app:"); | ||
| provider.AuthMethod.Should().Be(PPDS.Auth.Profiles.AuthMethod.CertificateFile); | ||
| } | ||
|
|
||
| [SkipIfNoCertificate] | ||
| public async Task CertificateFileCredentialProvider_SetsTokenExpirationAfterAuth() | ||
| { | ||
| // Arrange | ||
| var certPath = Configuration.GetCertificatePath(); | ||
| using var provider = new CertificateFileCredentialProvider( | ||
| Configuration.ApplicationId!, | ||
| certPath, | ||
| Configuration.CertificatePassword, | ||
| Configuration.TenantId!); | ||
|
|
||
| // Token expiration is null before authentication | ||
| provider.TokenExpiresAt.Should().BeNull(); | ||
|
|
||
| // Act | ||
| using var client = await provider.CreateServiceClientAsync(Configuration.DataverseUrl!); | ||
|
|
||
| // Assert - Token expiration is set after authentication | ||
| provider.TokenExpiresAt.Should().NotBeNull(); | ||
| provider.TokenExpiresAt.Should().BeAfter(DateTimeOffset.UtcNow); | ||
| } | ||
|
|
||
| [SkipIfNoCertificate] | ||
| public void LiveTestConfiguration_CanLoadCertificate() | ||
| { | ||
| // Arrange & Act | ||
| using var cert = Configuration.LoadCertificate(); | ||
|
|
||
| // Assert | ||
| cert.Should().NotBeNull(); | ||
| cert.HasPrivateKey.Should().BeTrue("Certificate must have private key for authentication"); | ||
| cert.Thumbprint.Should().NotBeNullOrWhiteSpace(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CertificateFileCredentialProvider_ThrowsOnNullArguments() | ||
| { | ||
| // Act & Assert | ||
| var act1 = () => new CertificateFileCredentialProvider(null!, "path", null, "tenant"); | ||
| var act2 = () => new CertificateFileCredentialProvider("app", null!, null, "tenant"); | ||
| var act3 = () => new CertificateFileCredentialProvider("app", "path", null, null!); | ||
|
|
||
| act1.Should().Throw<ArgumentNullException>().WithParameterName("applicationId"); | ||
| act2.Should().Throw<ArgumentNullException>().WithParameterName("certificatePath"); | ||
| act3.Should().Throw<ArgumentNullException>().WithParameterName("tenantId"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CertificateFileCredentialProvider_ThrowsOnMissingFile() | ||
| { | ||
| // Arrange | ||
| using var provider = new CertificateFileCredentialProvider( | ||
| "00000000-0000-0000-0000-000000000000", | ||
| "/nonexistent/path/cert.pfx", | ||
| null, | ||
| "00000000-0000-0000-0000-000000000000"); | ||
|
|
||
| // Act & Assert | ||
| var act = () => provider.CreateServiceClientAsync("https://fake.crm.dynamics.com"); | ||
| await act.Should().ThrowAsync<AuthenticationException>() | ||
joshsmithxrm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .WithMessage("*not found*"); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| Configuration.Dispose(); | ||
| } | ||
| } | ||
120 changes: 120 additions & 0 deletions
120
tests/PPDS.LiveTests/Authentication/ClientSecretAuthenticationTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| using FluentAssertions; | ||
| using PPDS.Auth.Credentials; | ||
| using PPDS.LiveTests.Infrastructure; | ||
| using Xunit; | ||
|
|
||
| namespace PPDS.LiveTests.Authentication; | ||
|
|
||
| /// <summary> | ||
| /// Integration tests for client secret (service principal) authentication. | ||
| /// These tests verify that ClientSecretCredentialProvider can successfully | ||
| /// authenticate to Dataverse using a client ID and secret. | ||
| /// </summary> | ||
| [Trait("Category", "Integration")] | ||
| public class ClientSecretAuthenticationTests : LiveTestBase, IDisposable | ||
| { | ||
| [SkipIfNoClientSecret] | ||
| public async Task ClientSecretCredentialProvider_CreatesWorkingServiceClient() | ||
| { | ||
| // Arrange | ||
| using var provider = new ClientSecretCredentialProvider( | ||
| Configuration.ApplicationId!, | ||
| Configuration.ClientSecret!, | ||
| Configuration.TenantId!); | ||
|
|
||
| // Act | ||
| using var client = await provider.CreateServiceClientAsync(Configuration.DataverseUrl!); | ||
|
|
||
| // Assert | ||
| client.Should().NotBeNull(); | ||
| client.IsReady.Should().BeTrue("ServiceClient should be ready after successful authentication"); | ||
| } | ||
|
|
||
| [SkipIfNoClientSecret] | ||
| public async Task ClientSecretCredentialProvider_CanExecuteWhoAmI() | ||
| { | ||
| // Arrange | ||
| using var provider = new ClientSecretCredentialProvider( | ||
| Configuration.ApplicationId!, | ||
| Configuration.ClientSecret!, | ||
| Configuration.TenantId!); | ||
|
|
||
| using var client = await provider.CreateServiceClientAsync(Configuration.DataverseUrl!); | ||
|
|
||
| // Act | ||
| var response = (Microsoft.Crm.Sdk.Messages.WhoAmIResponse)client.Execute( | ||
| new Microsoft.Crm.Sdk.Messages.WhoAmIRequest()); | ||
|
|
||
| // Assert | ||
| response.Should().NotBeNull(); | ||
| response.UserId.Should().NotBeEmpty("WhoAmI should return a valid user ID"); | ||
| response.OrganizationId.Should().NotBeEmpty("WhoAmI should return a valid organization ID"); | ||
| } | ||
|
|
||
| [SkipIfNoClientSecret] | ||
| public void ClientSecretCredentialProvider_SetsIdentityProperty() | ||
| { | ||
| // Arrange | ||
| using var provider = new ClientSecretCredentialProvider( | ||
| Configuration.ApplicationId!, | ||
| Configuration.ClientSecret!, | ||
| Configuration.TenantId!); | ||
|
|
||
| // Assert - Identity is set before authentication | ||
| provider.Identity.Should().NotBeNullOrWhiteSpace(); | ||
| provider.Identity.Should().StartWith("app:"); | ||
| provider.AuthMethod.Should().Be(PPDS.Auth.Profiles.AuthMethod.ClientSecret); | ||
| } | ||
joshsmithxrm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| [SkipIfNoClientSecret] | ||
| public async Task ClientSecretCredentialProvider_SetsTokenExpirationAfterAuth() | ||
| { | ||
| // Arrange | ||
| using var provider = new ClientSecretCredentialProvider( | ||
| Configuration.ApplicationId!, | ||
| Configuration.ClientSecret!, | ||
| Configuration.TenantId!); | ||
|
|
||
| // Token expiration is null before authentication | ||
| provider.TokenExpiresAt.Should().BeNull(); | ||
|
|
||
| // Act | ||
| using var client = await provider.CreateServiceClientAsync(Configuration.DataverseUrl!); | ||
|
|
||
| // Assert - Token expiration is set after authentication | ||
| provider.TokenExpiresAt.Should().NotBeNull(); | ||
| provider.TokenExpiresAt.Should().BeAfter(DateTimeOffset.UtcNow); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ClientSecretCredentialProvider_ThrowsOnNullArguments() | ||
| { | ||
| // Act & Assert | ||
| var act1 = () => new ClientSecretCredentialProvider(null!, "secret", "tenant"); | ||
| var act2 = () => new ClientSecretCredentialProvider("app", null!, "tenant"); | ||
| var act3 = () => new ClientSecretCredentialProvider("app", "secret", null!); | ||
|
|
||
| act1.Should().Throw<ArgumentNullException>().WithParameterName("applicationId"); | ||
| act2.Should().Throw<ArgumentNullException>().WithParameterName("clientSecret"); | ||
| act3.Should().Throw<ArgumentNullException>().WithParameterName("tenantId"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ClientSecretCredentialProvider_ThrowsOnInvalidCredentials() | ||
| { | ||
| // Arrange - Use fake credentials that will fail | ||
| using var provider = new ClientSecretCredentialProvider( | ||
| "00000000-0000-0000-0000-000000000000", | ||
| "invalid-secret", | ||
| "00000000-0000-0000-0000-000000000000"); | ||
|
|
||
| // Act & Assert | ||
| var act = () => provider.CreateServiceClientAsync("https://fake.crm.dynamics.com"); | ||
| await act.Should().ThrowAsync<AuthenticationException>(); | ||
joshsmithxrm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| Configuration.Dispose(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.