Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions src/dvx.Tests/ConfigLoaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text.Json;
using dvx.Config;
using dvx.Models;
using Shouldly;
using Xunit;

Expand Down Expand Up @@ -406,6 +407,99 @@ public void ResolveEnvironmentConfig_NoEnvAndNoConfig_ThrowsClearMessage()
ex.Message.ShouldContain("--env");
}

// ── auth type ──────────────────────────────────────────────────────────

[Fact]
public void ResolveEnvironmentConfig_InteractiveFlag_WinsOverConfigAuthType()
{
var path = WriteConfig("""
{
"environments": [
{ "name": "dev", "url": "https://dev.crm.dynamics.com",
"clientId": "c1", "clientSecret": "s1" }
]
}
""");
var config = ConfigLoader.TryLoad(path);

var env = ConfigLoader.ResolveEnvironmentConfig(
"dev", config, null, null, null, cliInteractiveAuth: true);

env.AuthType.ShouldBe(DataverseAuthType.Interactive);
}

[Fact]
public void ResolveEnvironmentConfig_Interactive_RequiresOnlyUrl()
{
var env = ConfigLoader.ResolveEnvironmentConfig(
envName: null, config: null,
cliUrl: "https://org.crm.dynamics.com",
cliClientId: null, cliClientSecret: null,
cliInteractiveAuth: true);

env.AuthType.ShouldBe(DataverseAuthType.Interactive);
env.Url.ShouldBe("https://org.crm.dynamics.com");
}

[Fact]
public void ResolveEnvironmentConfig_Interactive_NoUrl_ThrowsNamingUrl()
{
var ex = Should.Throw<InvalidOperationException>(() =>
ConfigLoader.ResolveEnvironmentConfig(
envName: null, config: null,
cliUrl: null, cliClientId: null, cliClientSecret: null,
cliInteractiveAuth: true));

ex.Message.ShouldContain("--url");
}

[Fact]
public void ResolveEnvironmentConfig_Interactive_FromConfigAuthType_NeedsNoSecret()
{
var path = WriteConfig("""
{
"environments": [
{ "name": "dev", "url": "https://dev.crm.dynamics.com", "authType": "interactive" }
]
}
""");
var config = ConfigLoader.TryLoad(path);

var env = ConfigLoader.ResolveEnvironmentConfig("dev", config, null, null, null);

env.AuthType.ShouldBe(DataverseAuthType.Interactive);
env.Url.ShouldBe("https://dev.crm.dynamics.com");
}

[Fact]
public void Load_AuthType_ParsesCaseInsensitively()
{
var path = WriteConfig("""
{
"environments": [
{ "name": "dev", "url": "https://dev.crm.dynamics.com", "authType": "Interactive" }
]
}
""");

ConfigLoader.TryLoad(path)!.Environments[0].AuthType.ShouldBe(DataverseAuthType.Interactive);
}

[Fact]
public void Load_AuthType_DefaultsToClientSecret_WhenOmitted()
{
var path = WriteConfig("""
{
"environments": [
{ "name": "dev", "url": "https://dev.crm.dynamics.com",
"clientId": "c1", "clientSecret": "s1" }
]
}
""");

ConfigLoader.TryLoad(path)!.Environments[0].AuthType.ShouldBe(DataverseAuthType.ClientSecret);
}

// ── webResources ───────────────────────────────────────────────────────

[Fact]
Expand Down
75 changes: 75 additions & 0 deletions src/dvx.Tests/DataverseClientFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using dvx.Models;
using dvx.Services;
using Shouldly;
using Xunit;

namespace dvx.Tests
{
public class DataverseClientFactoryTests
{
[Fact]
public void ClientSecretConnectionString_ReturnsCorrectFormat()
{
var env = new EnvironmentConfig
{
Url = "https://test.crm.dynamics.com",
ClientId = "my-client-id",
ClientSecret = "my-secret"
};

var connStr = DataverseClientFactory.ClientSecretConnectionString(env);

connStr.ShouldContain("AuthType=ClientSecret");
connStr.ShouldContain("Url=https://test.crm.dynamics.com");
connStr.ShouldContain("ClientId=my-client-id");
connStr.ShouldContain("ClientSecret=my-secret");
}

[Fact]
public void InteractiveConnectionString_ReturnsCorrectFormat()
{
var url = "https://test.crm.dynamics.com";

var connStr = DataverseClientFactory.InteractiveConnectionString(url);

connStr.ShouldContain("AuthType=OAuth");
connStr.ShouldContain($"Url={url}");
connStr.ShouldContain("AppId=51f81489-12ee-4a9e-aaae-a2591f45987d");
connStr.ShouldContain("RedirectUri=http://localhost");
connStr.ShouldContain("LoginPrompt=Auto");
}

[Fact]
public void GetConnectionString_UsesInteractive_WhenAuthTypeIsInteractive()
{
var env = new EnvironmentConfig
{
Url = "https://test.crm.dynamics.com",
AuthType = DataverseAuthType.Interactive
};

var connStr = DataverseClientFactory.GetConnectionString(env);

connStr.ShouldContain("AuthType=OAuth");
connStr.ShouldContain("Url=https://test.crm.dynamics.com");
}

[Fact]
public void GetConnectionString_UsesClientSecret_WhenAuthTypeIsClientSecret()
{
var env = new EnvironmentConfig
{
Url = "https://test.crm.dynamics.com",
AuthType = DataverseAuthType.ClientSecret,
ClientId = "cid",
ClientSecret = "sec"
};

var connStr = DataverseClientFactory.GetConnectionString(env);

connStr.ShouldContain("AuthType=ClientSecret");
connStr.ShouldContain("ClientId=cid");
connStr.ShouldContain("ClientSecret=sec");
}
}
}
6 changes: 4 additions & 2 deletions src/dvx/Commands/AdoptCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/// </summary>
public static class AdoptCommand
{
public static Command Build(ILoggerFactory loggerFactory)

Check warning on line 19 in src/dvx/Commands/AdoptCommand.cs

View workflow job for this annotation

GitHub Actions / build

Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed.
{
var cmd = new Command("adopt",
"Read existing plugin steps from Dataverse and scaffold [PluginStep] attributes onto the source.");
Expand All @@ -32,9 +32,10 @@
"Defaults to the project's assembly name.");
var dryRun = CommandOptions.DryRun();
var verbose = CommandOptions.Verbose();
var interactiveAuth = CommandOptions.InteractiveAuth();

cmd.AddOptions(env, config, url, clientId, clientSecret, project, assemblyName,
dryRun, verbose);
dryRun, verbose, interactiveAuth);

cmd.SetHandler((InvocationContext ctx) =>
{
Expand All @@ -47,12 +48,13 @@
var asmName = ctx.ParseResult.GetValueForOption(assemblyName);
var isDryRun = ctx.ParseResult.GetValueForOption(dryRun);
var isVerbose = ctx.ParseResult.GetValueForOption(verbose);
var cliInteractive = ctx.ParseResult.GetValueForOption(interactiveAuth);

try
{
var appConfig = ConfigLoader.TryLoad(configPath);
var envConfig = ConfigLoader.ResolveEnvironmentConfig(
envName, appConfig, cliUrl, cliClientId, cliSecret);
envName, appConfig, cliUrl, cliClientId, cliSecret, cliInteractive);
var resolvedProject = ConfigLoader.ResolveProject(appConfig, projectPath);
using var svc = DataverseClientFactory.Create(envConfig);

Expand Down
1 change: 1 addition & 0 deletions src/dvx/Commands/CreateConfigCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static class CreateConfigCommand
{
"name": "dev",
"url": "https://your-dev-org.crm4.dynamics.com",
"authType": "clientSecret",
"clientId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"clientSecret": "your-secret"
}
Expand Down
6 changes: 4 additions & 2 deletions src/dvx/Commands/DeployCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ public static Command Build()
var project = CommandOptions.Project();
var publisherPrefix = CommandOptions.PublisherPrefix();
var solutionUniqueName = CommandOptions.SolutionUniqueName();
var interactiveAuth = CommandOptions.InteractiveAuth();
var verbose = CommandOptions.Verbose();

cmd.AddOptions(env, config, url, clientId, clientSecret, project, publisherPrefix,
solutionUniqueName, verbose);
solutionUniqueName, interactiveAuth, verbose);

cmd.SetHandler((InvocationContext ctx) =>
{
Expand All @@ -35,13 +36,14 @@ public static Command Build()
var projectPath = ctx.ParseResult.GetValueForOption(project)!;
var pubPrefix = ctx.ParseResult.GetValueForOption(publisherPrefix);
var cliSolution = ctx.ParseResult.GetValueForOption(solutionUniqueName);
var cliInteractive = ctx.ParseResult.GetValueForOption(interactiveAuth);
var isVerbose = ctx.ParseResult.GetValueForOption(verbose);

try
{
var appConfig = ConfigLoader.TryLoad(configPath);
var envConfig = ConfigLoader.ResolveEnvironmentConfig(
envName, appConfig, cliUrl, cliClientId, cliSecret);
envName, appConfig, cliUrl, cliClientId, cliSecret, cliInteractive);
var configured = ConfigLoader.ResolveConfiguredPublisherPrefix(appConfig, pubPrefix);
var solution = ConfigLoader.ResolveSolutionUniqueName(appConfig, cliSolution);
var resolvedProject = ConfigLoader.ResolveProject(appConfig, projectPath);
Expand Down
6 changes: 4 additions & 2 deletions src/dvx/Commands/RegisterCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{
public static class RegisterCommand
{
public static Command Build(ILoggerFactory loggerFactory)

Check warning on line 13 in src/dvx/Commands/RegisterCommand.cs

View workflow job for this annotation

GitHub Actions / build

Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed.
{
var cmd = new Command("register",
"Reflect the plugin DLL for [PluginStep] attributes and sync step registrations in Dataverse.");
Expand All @@ -25,11 +25,12 @@
var verbose = CommandOptions.Verbose();
var solutionUniqueName = CommandOptions.SolutionUniqueName();
var deleteOrphaned = CommandOptions.DeleteOrphanedSteps();
var interactiveAuth = CommandOptions.InteractiveAuth();

// --project and --assembly-name are mutually exclusive; both optional — validated at runtime

cmd.AddOptions(env, config, url, clientId, clientSecret, project, assemblyName,
dryRun, verbose, solutionUniqueName, deleteOrphaned);
dryRun, verbose, solutionUniqueName, deleteOrphaned, interactiveAuth);

cmd.SetHandler((InvocationContext ctx) =>
{
Expand All @@ -44,6 +45,7 @@
var isVerbose = ctx.ParseResult.GetValueForOption(verbose);
var cliSolution = ctx.ParseResult.GetValueForOption(solutionUniqueName);
var delOrphaned = ctx.ParseResult.GetValueForOption(deleteOrphaned);
var cliInteractive = ctx.ParseResult.GetValueForOption(interactiveAuth);

if (!string.IsNullOrEmpty(projectPath) && !string.IsNullOrEmpty(asmName))
{
Expand All @@ -57,7 +59,7 @@
{
var appConfig = ConfigLoader.TryLoad(configPath);
var envConfig = ConfigLoader.ResolveEnvironmentConfig(
envName, appConfig, cliUrl, cliClientId, cliSecret);
envName, appConfig, cliUrl, cliClientId, cliSecret, cliInteractive);
var solution = ConfigLoader.ResolveSolutionUniqueName(appConfig, cliSolution);
using var svc = DataverseClientFactory.Create(envConfig);

Expand Down
5 changes: 5 additions & 0 deletions src/dvx/Commands/Shared/CommandOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public static class CommandOptions
"Service principal client secret. " +
"Overrides config. Env var: DVX_CLIENT_SECRET.");

public static Option<bool> InteractiveAuth() => new Option<bool>(
"--interactive-auth",
"Sign in interactively via the browser instead of a client secret (local dev). " +
"Token is cached securely. Equivalent to authType=interactive in config.");

public static Option<string?> Project() => new Option<string?>(
"--project",
"Path to the plugin .csproj file. The tool will run 'dotnet build' to produce the .nupkg and .dll. " +
Expand Down
6 changes: 4 additions & 2 deletions src/dvx/Commands/SyncCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ public static Command Build(ILoggerFactory loggerFactory)
var verbose = CommandOptions.Verbose();
var solutionUniqueName = CommandOptions.SolutionUniqueName();
var deleteOrphaned = CommandOptions.DeleteOrphanedSteps();
var interactiveAuth = CommandOptions.InteractiveAuth();

cmd.AddOptions(env, config, url, clientId, clientSecret, project, publisherPrefix,
dryRun, verbose, solutionUniqueName, deleteOrphaned);
dryRun, verbose, solutionUniqueName, deleteOrphaned, interactiveAuth);

cmd.SetHandler((InvocationContext ctx) =>
{
Expand All @@ -41,12 +42,13 @@ public static Command Build(ILoggerFactory loggerFactory)
var isVerbose = ctx.ParseResult.GetValueForOption(verbose);
var cliSolution = ctx.ParseResult.GetValueForOption(solutionUniqueName);
var delOrphaned = ctx.ParseResult.GetValueForOption(deleteOrphaned);
var cliInteractive = ctx.ParseResult.GetValueForOption(interactiveAuth);

try
{
var appConfig = ConfigLoader.TryLoad(configPath);
var envConfig = ConfigLoader.ResolveEnvironmentConfig(
envName, appConfig, cliUrl, cliClientId, cliSecret);
envName, appConfig, cliUrl, cliClientId, cliSecret, cliInteractive);
var configured = ConfigLoader.ResolveConfiguredPublisherPrefix(appConfig, cliPrefix);
var solution = ConfigLoader.ResolveSolutionUniqueName(appConfig, cliSolution);
var resolvedProject = ConfigLoader.ResolveProject(appConfig, projectPath);
Expand Down
6 changes: 4 additions & 2 deletions src/dvx/Commands/WebResourceSyncCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{
public static class WebResourceSyncCommand
{
public static Command Build(ILoggerFactory loggerFactory)

Check warning on line 15 in src/dvx/Commands/WebResourceSyncCommand.cs

View workflow job for this annotation

GitHub Actions / build

Refactor this method to reduce its Cognitive Complexity from 42 to the 15 allowed.
{
var cmd = new Command("sync",
"Upsert and publish Dataverse web resources from a folder and/or a manifest file.");
Expand All @@ -29,9 +29,10 @@
var deleteOrphaned = CommandOptions.DeleteOrphaned();
var dryRun = CommandOptions.DryRun();
var verbose = CommandOptions.Verbose();
var interactiveAuth = CommandOptions.InteractiveAuth();

cmd.AddOptions(env, config, url, clientId, clientSecret, folder, manifest, publisherPrefix,
solutionUniqueName, noPublish, deleteOrphaned, dryRun, verbose);
solutionUniqueName, noPublish, deleteOrphaned, dryRun, verbose, interactiveAuth);

cmd.SetHandler((InvocationContext ctx) =>
{
Expand All @@ -49,6 +50,7 @@
var delOrphaned = p.GetValueForOption(deleteOrphaned);
var isDryRun = p.GetValueForOption(dryRun);
var isVerbose = p.GetValueForOption(verbose);
var cliInteractive = p.GetValueForOption(interactiveAuth);

try
{
Expand Down Expand Up @@ -84,7 +86,7 @@
}

var envConfig = ConfigLoader.ResolveEnvironmentConfig(
envName, appConfig, cliUrl, cliClientId, cliSecret);
envName, appConfig, cliUrl, cliClientId, cliSecret, cliInteractive);
using var svc = DataverseClientFactory.Create(envConfig);

var desired = new List<WebResourceDefinition>();
Expand Down
Loading
Loading