Skip to content

Commit

Permalink
Test entry point (Program)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielweyer committed Apr 20, 2022
1 parent c7330d6 commit 1522d62
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 81 deletions.
34 changes: 11 additions & 23 deletions src/dotnet-decode-jwt/ClaimsDisplayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,20 @@ public ClaimsDisplayer(IConsole console, TimeZoneInfo localTimeZone)

public void DisplayClaims(JsonElement claims)
{
try
if (claims.ValueKind == JsonValueKind.Undefined)
{
if (claims.ValueKind == JsonValueKind.Undefined)
{
_console.ForegroundColor = ConsoleColor.DarkGray;
_console.WriteLine("There was no claims in the JWT.");
}
else
{
_console.WriteLine(string.Empty);
_console.ForegroundColor = ConsoleColor.Yellow;
_console.WriteLine($"Expiration Time ({ExpirationTimeKeyName}): {FormatDateTime(claims, ExpirationTimeKeyName)}");
_console.WriteLine($"Not Before ({NotBeforeKeyName}): {FormatDateTime(claims, NotBeforeKeyName)}");
_console.WriteLine($"Issued At ({IssuedAtKeyName}): {FormatDateTime(claims, IssuedAtKeyName)}");
_console.ForegroundColor = ConsoleColor.Green;
_console.WriteLine(string.Empty);
_console.WriteLine("Claims are:");
_console.WriteLine(string.Empty);
_console.ResetColor();

_console.WriteLine(JsonSerializer.Serialize(claims, _serializationOptions));
}
_console.WriteDullLine("There was no claims in the JWT.");
}
finally
else
{
_console.ResetColor();
_console.WriteBoringLine(string.Empty);
_console.WriteInfoLine($"Expiration Time ({ExpirationTimeKeyName}): {FormatDateTime(claims, ExpirationTimeKeyName)}");
_console.WriteInfoLine($"Not Before ({NotBeforeKeyName}): {FormatDateTime(claims, NotBeforeKeyName)}");
_console.WriteInfoLine($"Issued At ({IssuedAtKeyName}): {FormatDateTime(claims, IssuedAtKeyName)}");
_console.WriteFancyLine(string.Empty);
_console.WriteFancyLine("Claims are:");
_console.WriteFancyLine(string.Empty);
_console.WriteBoringLine(JsonSerializer.Serialize(claims, _serializationOptions));
}
}

Expand Down
51 changes: 51 additions & 0 deletions src/dotnet-decode-jwt/Infrastructure/NonThreadSafeConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace Gabo.DotNet.DecodeJwt.Infrastructure;

public interface IConsole
{
void WriteBoringLine(string value);
void WriteDullLine(string value);
void WriteInfoLine(string value);
void WriteFancyLine(string value);
void WriteErrorLine(string value);
}

internal class NonThreadSafeConsole : IConsole
{
internal const ConsoleColor BoringColor = ConsoleColor.Gray;
internal const ConsoleColor DullColor = ConsoleColor.DarkGray;
internal const ConsoleColor InfoColor = ConsoleColor.Yellow;
internal const ConsoleColor FancyColor = ConsoleColor.Green;
internal const ConsoleColor ErrorColor = ConsoleColor.Red;

public void WriteBoringLine(string value)
{
WriteColoredLine(BoringColor, value);
}

public void WriteDullLine(string value)
{
WriteColoredLine(DullColor, value);
}

public void WriteInfoLine(string value)
{
WriteColoredLine(InfoColor, value);
}

public void WriteFancyLine(string value)
{
WriteColoredLine(FancyColor, value);
}

public void WriteErrorLine(string value)
{
WriteColoredLine(ErrorColor, value);
}

private static void WriteColoredLine(ConsoleColor color, string value)
{
Console.ForegroundColor = color;
Console.WriteLine(value);
Console.ResetColor();
}
}
18 changes: 8 additions & 10 deletions src/dotnet-decode-jwt/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ internal static class Program
internal const int SuccessExitCode = 0;
internal const int FailureExitCode = 1;

internal static IConsole AbstractedConsole;

internal static int Main(string[] args)
{
AbstractedConsole ??= new NonThreadSafeConsole();

if (args.Length != 1)
{
Console.WriteLine("A single argument should be provided:");
Console.WriteLine("dotnet decode-jwt eyJhbGciOiJub25lIn0.ewogICAgImlzcyI6ICJiZXN0LWlzc3VlciIsCiAgICAic3ViIjogIm5pY2Utc3ViamVjdCIsCiAgICAiYXVkIjogWyJhdWRpZW5jZS1vbmUiLCAiYXVkaWVuY2UtdHdvIl0sCiAgICAiZXhwIjogMTUyODY5MTM1MCwKICAgICJuYmYiOiAxNTI4NjkwNzUwLAogICAgImlhdCI6IDE1Mjg2OTA3NTAsCiAgICAianRpIjogImMzMTk3ZGNiLWUxMTMtNDc3OC04OTc5LWI5NTZmNjg0MDA3ZiIsCiAgICAiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvZW1haWxhZGRyZXNzIjogImhpQG1lLmNvbSIsCiAgICAic29tZS1udW1iZXIiOiAxMi41NiwKICAgICJuZXN0ZWQtY2xhaW0iOiB7CiAgICAgICAgImhpIjogIkknbSIsCiAgICAgICAgImEiOiAibmVzdGVkIGNsYWltIgogICAgfQp9Cg==.");
AbstractedConsole.WriteBoringLine("A single argument should be provided:");
AbstractedConsole.WriteBoringLine("dotnet decode-jwt eyJhbGciOiJub25lIn0.ewogICAgImlzcyI6ICJiZXN0LWlzc3VlciIsCiAgICAic3ViIjogIm5pY2Utc3ViamVjdCIsCiAgICAiYXVkIjogWyJhdWRpZW5jZS1vbmUiLCAiYXVkaWVuY2UtdHdvIl0sCiAgICAiZXhwIjogMTUyODY5MTM1MCwKICAgICJuYmYiOiAxNTI4NjkwNzUwLAogICAgImlhdCI6IDE1Mjg2OTA3NTAsCiAgICAianRpIjogImMzMTk3ZGNiLWUxMTMtNDc3OC04OTc5LWI5NTZmNjg0MDA3ZiIsCiAgICAiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvZW1haWxhZGRyZXNzIjogImhpQG1lLmNvbSIsCiAgICAic29tZS1udW1iZXIiOiAxMi41NiwKICAgICJuZXN0ZWQtY2xhaW0iOiB7CiAgICAgICAgImhpIjogIkknbSIsCiAgICAgICAgImEiOiAibmVzdGVkIGNsYWltIgogICAgfQp9Cg==.");
return FailureExitCode;
}

var console = new SimplifiedConsole();
var claimsDisplayer = new ClaimsDisplayer(console, TimeZoneInfo.Local);
var claimsDisplayer = new ClaimsDisplayer(AbstractedConsole, TimeZoneInfo.Local);

try
{
Expand All @@ -25,13 +28,8 @@ internal static int Main(string[] args)
}
catch (FormatException e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e.Message);
AbstractedConsole.WriteErrorLine(e.Message);
return FailureExitCode;
}
finally
{
Console.ResetColor();
}
}
}
26 changes: 0 additions & 26 deletions src/dotnet-decode-jwt/SimplifiedConsole.cs

This file was deleted.

1 change: 1 addition & 0 deletions src/dotnet-decode-jwt/dotnet-decode-jwt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<InternalsVisibleTo Include="Gabo.DotNet.DecodeJwt.Tests" />
</ItemGroup>
<ItemGroup>
<Using Include="Gabo.DotNet.DecodeJwt.Infrastructure" />
<Using Include="System.Text" />
<Using Include="System.Text.Json" />
</ItemGroup>
Expand Down
58 changes: 36 additions & 22 deletions tests/dotnet-decode-jwt-tests/ClaimsDisplayerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,28 @@ public void GivenAnyClaim_WhenDisplayClaims_ThenDisplayClaims()
// Assert
var expected = new List<string>
{
"SET FOREGROUND COLOR: Gray",
"WRITE: ",
"RESET COLOR",
"SET FOREGROUND COLOR: Yellow",
"WRITE: Expiration Time (exp): N/A",
"RESET COLOR",
"SET FOREGROUND COLOR: Yellow",
"WRITE: Not Before (nbf): N/A",
"RESET COLOR",
"SET FOREGROUND COLOR: Yellow",
$"WRITE: Issued At (iat): Thursday, 18 January 2018 01:30:22 UTC / Thursday, 18 January 2018 12:30:22 (UTC+10:00) {_timeZoneDisplayName}",
"RESET COLOR",
"SET FOREGROUND COLOR: Green",
"WRITE: ",
"RESET COLOR",
"SET FOREGROUND COLOR: Green",
"WRITE: Claims are:",
"RESET COLOR",
"SET FOREGROUND COLOR: Green",
"WRITE: ",
"RESET COLOR",
"SET FOREGROUND COLOR: Gray",
$"WRITE: {{{Environment.NewLine} \"iat\": 1516239022{Environment.NewLine}}}",
"RESET COLOR"
};
Expand All @@ -99,16 +111,28 @@ public void GivenIatIsNotTimestamp_WhenDisplayClaims_ThenDisplayDateAsNotAvailab
// Assert
var expected = new List<string>
{
"SET FOREGROUND COLOR: Gray",
"WRITE: ",
"RESET COLOR",
"SET FOREGROUND COLOR: Yellow",
"WRITE: Expiration Time (exp): N/A",
"RESET COLOR",
"SET FOREGROUND COLOR: Yellow",
"WRITE: Not Before (nbf): N/A",
"RESET COLOR",
"SET FOREGROUND COLOR: Yellow",
"WRITE: Issued At (iat): N/A",
"RESET COLOR",
"SET FOREGROUND COLOR: Green",
"WRITE: ",
"RESET COLOR",
"SET FOREGROUND COLOR: Green",
"WRITE: Claims are:",
"RESET COLOR",
"SET FOREGROUND COLOR: Green",
"WRITE: ",
"RESET COLOR",
"SET FOREGROUND COLOR: Gray",
$"WRITE: {{{Environment.NewLine} \"iat\": \"hello\"{Environment.NewLine}}}",
"RESET COLOR"
};
Expand All @@ -128,42 +152,32 @@ public void GivenHtmlSensitiveCharacter_WhenDisplayClaims_ThenDoNotEscape()
// Assert
var expected = new List<string>
{
"SET FOREGROUND COLOR: Gray",
"WRITE: ",
"RESET COLOR",
"SET FOREGROUND COLOR: Yellow",
"WRITE: Expiration Time (exp): N/A",
"RESET COLOR",
"SET FOREGROUND COLOR: Yellow",
"WRITE: Not Before (nbf): N/A",
"RESET COLOR",
"SET FOREGROUND COLOR: Yellow",
"WRITE: Issued At (iat): N/A",
"RESET COLOR",
"SET FOREGROUND COLOR: Green",
"WRITE: ",
"RESET COLOR",
"SET FOREGROUND COLOR: Green",
"WRITE: Claims are:",
"RESET COLOR",
"SET FOREGROUND COLOR: Green",
"WRITE: ",
"RESET COLOR",
"SET FOREGROUND COLOR: Gray",
$"WRITE: {{{Environment.NewLine} \"hi\": \"I'm\"{Environment.NewLine}}}",
"RESET COLOR"
};

_console.Actions.Should().BeEquivalentTo(expected);
}
}

internal class MockConsole : IConsole
{
private readonly List<string> _actions = new List<string>();

public IReadOnlyList<string> Actions => _actions;

public ConsoleColor ForegroundColor
{
set => _actions.Add($"SET FOREGROUND COLOR: {value}");
}

public void WriteLine(string value)
{
_actions.Add($"WRITE: {value}");
}

public void ResetColor()
{
_actions.Add("RESET COLOR");
}
}
74 changes: 74 additions & 0 deletions tests/dotnet-decode-jwt-tests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ namespace Gabo.DotNet.DecodeJwt.Tests;

public class ProgramTests
{
private readonly MockConsole _console;

private const string ValidJwtInput = "eyJhbGciOiJub25lIn0.ewogICAgImlzcyI6ICJiZXN0LWlzc3VlciIsCiAgICAic3ViIjogIm5pY2Utc3ViamVjdCIsCiAgICAiYXVkIjogWyJhdWRpZW5jZS1vbmUiLCAiYXVkaWVuY2UtdHdvIl0sCiAgICAiZXhwIjogMTUyODY5MTM1MCwKICAgICJuYmYiOiAxNTI4NjkwNzUwLAogICAgImlhdCI6IDE1Mjg2OTA3NTAsCiAgICAianRpIjogImMzMTk3ZGNiLWUxMTMtNDc3OC04OTc5LWI5NTZmNjg0MDA3ZiIsCiAgICAiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvZW1haWxhZGRyZXNzIjogImhpQG1lLmNvbSIsCiAgICAic29tZS1udW1iZXIiOiAxMi41NiwKICAgICJuZXN0ZWQtY2xhaW0iOiB7CiAgICAgICAgImhpIjogIkknbSIsCiAgICAgICAgImEiOiAibmVzdGVkIGNsYWltIgogICAgfQp9Cg==.";
private const string InvalidJwtInput = "cQ==";

public ProgramTests()
{
_console = new MockConsole();
Program.AbstractedConsole = _console;
}

[Fact]
public void GivenNoArgumentProvided_ThenFailureExitCode()
{
Expand All @@ -18,6 +26,29 @@ public void GivenNoArgumentProvided_ThenFailureExitCode()
actualExitCode.Should().Be(Program.FailureExitCode);
}

[Fact]
public void GivenNoArgumentProvided_ThenDisplayInstructions()
{
// Arrange
var input = Array.Empty<string>();

// Act
Program.Main(input);

// Assert
var expected = new List<string>
{
"SET FOREGROUND COLOR: Gray",
"WRITE: A single argument should be provided:",
"RESET COLOR",
"SET FOREGROUND COLOR: Gray",
$"WRITE: dotnet decode-jwt {ValidJwtInput}",
"RESET COLOR"
};

_console.Actions.Should().BeEquivalentTo(expected);
}

[Fact]
public void GivenMoreThanOneArgumentProvided_ThenFailureExitCode()
{
Expand All @@ -31,6 +62,29 @@ public void GivenMoreThanOneArgumentProvided_ThenFailureExitCode()
actualExitCode.Should().Be(Program.FailureExitCode);
}

[Fact]
public void GivenMoreThanOneArgumentProvided_ThenDisplayInstructions()
{
// Arrange
var input = new[] { ValidJwtInput, ValidJwtInput };

// Act
Program.Main(input);

// Assert
var expected = new List<string>
{
"SET FOREGROUND COLOR: Gray",
"WRITE: A single argument should be provided:",
"RESET COLOR",
"SET FOREGROUND COLOR: Gray",
$"WRITE: dotnet decode-jwt {ValidJwtInput}",
"RESET COLOR"
};

_console.Actions.Should().BeEquivalentTo(expected);
}

[Fact]
public void GivenInvalidJwtInput_ThenFailureExitCode()
{
Expand All @@ -44,6 +98,26 @@ public void GivenInvalidJwtInput_ThenFailureExitCode()
actualExitCode.Should().Be(Program.FailureExitCode);
}

[Fact]
public void GivenInvalidJwtInput_ThenDisplayErrorMessage()
{
// Arrange
var input = new[] { InvalidJwtInput };

// Act
Program.Main(input);

// Assert
var expected = new List<string>
{
"SET FOREGROUND COLOR: Red",
"WRITE: The JWT should contain two periods.",
"RESET COLOR"
};

_console.Actions.Should().BeEquivalentTo(expected);
}

[Fact]
public void GivenValidJwtInput_ThenSuccessExitCode()
{
Expand Down

0 comments on commit 1522d62

Please sign in to comment.