Skip to content

Commit

Permalink
PR: guard against nulls
Browse files Browse the repository at this point in the history
  • Loading branch information
chenriksson committed Apr 25, 2017
1 parent 8308c39 commit 55ce5ba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public RequireMinClientVersionForPushPolicy()
/// </summary>
private NuGetVersion GetMaxOfMinClientVersions(UserSecurityPolicyContext context)
{
var policyStates = context.Policies.Select(p => JsonConvert.DeserializeObject<State>(p.Value));
var policyStates = context.Policies
.Where(p => !string.IsNullOrEmpty(p.Value))
.Select(p => JsonConvert.DeserializeObject<State>(p.Value));
return policyStates.Max(s => s.MinClientVersion);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,33 @@ public void EvaluateReturnsFailureIfNoClientHeader()
Assert.NotNull(result.ErrorMessage);
}

[Fact]
public void EvaluateReturnsSuccess_PolicyMissingMinVerAndClientVersionHeader()
{
// Arrange & Act
var result = Evaluate(minClientVersions: "", actualClientVersion: "4.1.0");

// Assert
Assert.True(result.Success);
Assert.Null(result.ErrorMessage);
}

[Fact]
public void EvaluateReturnsFailure_PolicyMissingMinVerAndNoClientVersionHeader()
{
// Arrange & Act
var result = Evaluate(minClientVersions: "", actualClientVersion: "");

// Assert
Assert.False(result.Success);
Assert.NotNull(result.ErrorMessage);
}

private static UserSecurityPolicy CreateMinClientVersionForPushPolicy(string minClientVersion)
{
return new UserSecurityPolicy("RequireMinClientVersionForPushPolicy")
{
Value = $"{{\"v\":\"{minClientVersion}\"}}"
Value = string.IsNullOrEmpty(minClientVersion) ? null : $"{{\"v\":\"{minClientVersion}\"}}"
};
}

Expand Down

0 comments on commit 55ce5ba

Please sign in to comment.