Skip to content

Fix up project search scenarios when no option is provided #42860

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 2 commits into from
Jul 26, 2022
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
14 changes: 13 additions & 1 deletion src/Tools/dotnet-user-jwts/src/Commands/CreateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.Json;
using Microsoft.Extensions.CommandLineUtils;
using Microsoft.Extensions.Tools.Internal;
Expand Down Expand Up @@ -117,6 +116,19 @@ private static (JwtCreatorOptions, bool, string) ValidateArguments(
var isValid = true;
var project = DevJwtCliHelpers.GetProject(projectOption.Value());

if (project == null)
{
reporter.Error(Resources.ProjectOption_ProjectNotFound);
isValid = false;
// Break out early if we haven't been able to resolve a project
// since we depend on it for the managing of JWT tokens
return (
null,
isValid,
string.Empty
);
}

var scheme = schemeNameOption.HasValue() ? schemeNameOption.Value() : "Bearer";
var optionsString = schemeNameOption.HasValue() ? $"{Resources.JwtPrint_Scheme}: {scheme}{Environment.NewLine}" : string.Empty;

Expand Down
9 changes: 6 additions & 3 deletions src/Tools/dotnet-user-jwts/src/Helpers/DevJwtCliHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ public static bool GetProjectAndSecretsId(string projectPath, IReporter reporter
userSecretsId = null;
if (project == null)
{
reporter.Error($"No project found at `-p|--project` path or current directory.");
reporter.Error(Resources.ProjectOption_ProjectNotFound);
return false;
}

userSecretsId = GetOrSetUserSecretsId(project);
if (userSecretsId == null)
{
reporter.Error($"Project does not contain a user secrets ID.");
reporter.Error(Resources.ProjectOption_SercretIdNotFound);
return false;
}
return true;
Expand Down Expand Up @@ -112,7 +112,10 @@ public static byte[] CreateSigningKeyMaterial(string userSecretsId, string schem

public static List<string> GetAudienceCandidatesFromLaunchSettings(string project)
{
ArgumentException.ThrowIfNullOrEmpty(nameof(project));
if (string.IsNullOrEmpty(project))
{
return new List<string>();
}

var launchSettingsFilePath = Path.Combine(Path.GetDirectoryName(project)!, "Properties", "launchSettings.json");
var applicationUrls = new HashSet<string>();
Expand Down
62 changes: 34 additions & 28 deletions src/Tools/dotnet-user-jwts/src/Resources.resx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
<!--
Microsoft ResX Schema

Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple

There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -294,6 +294,12 @@
<data name="PrintCommand_ShowAllOption_Description" xml:space="preserve">
<value>Whether to show all details associated with the JWT.</value>
</data>
<data name="ProjectOption_ProjectNotFound" xml:space="preserve">
<value>No project found at `-p|--project` path or current directory.</value>
</data>
<data name="ProjectOption_SercretIdNotFound" xml:space="preserve">
<value>Project does not contain a user secrets ID.</value>
</data>
<data name="ProjectOption_Description" xml:space="preserve">
<value>The path of the project to operate on. Defaults to the project in the current directory.</value>
</data>
Expand All @@ -309,4 +315,4 @@
<data name="RemoveCommand_NoJwtFound" xml:space="preserve">
<value>No JWT with ID '{0}' found.</value>
</data>
</root>
</root>
62 changes: 62 additions & 0 deletions src/Tools/dotnet-user-jwts/test/UserJwtsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,4 +480,66 @@ public void Key_CanPrintAndReset_BySchemeAndIssuer()
var resetKey = resetMatches.SingleOrDefault().Groups[1].Value;
Assert.NotEqual(key, resetKey);
}

[Fact]
public void Create_CanHandleNoProjectOptionProvided()
{
var projectPath = _fixture.CreateProject();
Directory.SetCurrentDirectory(projectPath);

var app = new Program(_console);
app.Run(new[] { "create" });

Assert.DoesNotContain("No project found at `-p|--project` path or current directory.", _console.GetOutput());
Assert.Contains("New JWT saved", _console.GetOutput());
}

[Fact]
public void Create_CanHandleNoProjectOptionProvided_WithNoProjects()
{
var path = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "userjwtstest"));
Directory.SetCurrentDirectory(path.FullName);

var app = new Program(_console);
app.Run(new[] { "create" });

Assert.Contains("No project found at `-p|--project` path or current directory.", _console.GetOutput());
Assert.DoesNotContain(Resources.CreateCommand_NoAudience_Error, _console.GetOutput());
}

[Fact]
public void Delete_CanHandleNoProjectOptionProvided_WithNoProjects()
{
var path = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "userjwtstest"));
Directory.SetCurrentDirectory(path.FullName);

var app = new Program(_console);
app.Run(new[] { "remove", "some-id" });

Assert.Contains("No project found at `-p|--project` path or current directory.", _console.GetOutput());
}

[Fact]
public void Clear_CanHandleNoProjectOptionProvided_WithNoProjects()
{
var path = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "userjwtstest"));
Directory.SetCurrentDirectory(path.FullName);

var app = new Program(_console);
app.Run(new[] { "clear" });

Assert.Contains("No project found at `-p|--project` path or current directory.", _console.GetOutput());
}

[Fact]
public void List_CanHandleNoProjectOptionProvided_WithNoProjects()
{
var path = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "userjwtstest"));
Directory.SetCurrentDirectory(path.FullName);

var app = new Program(_console);
app.Run(new[] { "list" });

Assert.Contains("No project found at `-p|--project` path or current directory.", _console.GetOutput());
}
}