Skip to content

Commit

Permalink
Validate body content
Browse files Browse the repository at this point in the history
  • Loading branch information
svrooij committed May 3, 2024
1 parent e70b179 commit d5b7054
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 20 deletions.
32 changes: 12 additions & 20 deletions tests/WingetIntune.Tests/Graph/GraphStoreAppUploaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Graph.Beta;
using Microsoft.IdentityModel.Tokens;
using Microsoft.Kiota.Abstractions.Authentication;
using System.Text;
using WingetIntune.Graph;
using WingetIntune.Internal.MsStore;
Expand All @@ -11,20 +8,6 @@ namespace WingetIntune.Tests.Graph;

public class GraphStoreAppUploaderTests
{
//private readonly ILogger<GraphStoreAppUploader> _logger;
//private readonly IFileManager _fileManager;
//private readonly MicrosoftStoreClient _microsoftStoreClient;
//private readonly GraphServiceClient _graphServiceClient;
//private readonly GraphStoreAppUploader _graphStoreAppUploader;

//public GraphStoreAppUploaderTests()
//{
// _logger = Substitute.For<ILogger<GraphStoreAppUploader>>();
// _fileManager = Substitute.For<IFileManager>();
// _microsoftStoreClient = new MicrosoftStoreClient(new HttpClient(), new NullLogger<MicrosoftStoreClient>());
// //_graphServiceClient = Substitute.For<GraphServiceClient>(Substitute.For<IAuthenticationProvider>(), null);
// _graphStoreAppUploader = new GraphStoreAppUploader(_logger, _fileManager, _microsoftStoreClient);
//}

[Fact]
public async Task CreateStoreAppAsync_WithValidPackageId_CreatesAppSuccessfully()
Expand Down Expand Up @@ -61,10 +44,17 @@ public async Task CreateStoreAppAsync_WithValidPackageId_CreatesAppSuccessfully(
fileManager.ReadAllBytesAsync(Arg.Any<string>(), cancellationToken)
.Returns(Encoding.UTF8.GetBytes("fake image"));

// Validate the body of the request somehow
var expectedGraphBody = new Dictionary<string, object>
{
{ "@odata.type", "#microsoft.graph.winGetApp" },
{ "displayName", "Mozilla Firefox" }
};
httpClient.SendAsync(Arg.Is<HttpRequestMessage>(req =>
req.Method == HttpMethod.Post
&& req.RequestUri.ToString().Equals("https://graph.microsoft.com/beta/deviceAppManagement/mobileApps", StringComparison.OrdinalIgnoreCase)), cancellationToken)
&& req.RequestUri.ToString().Equals("https://graph.microsoft.com/beta/deviceAppManagement/mobileApps", StringComparison.OrdinalIgnoreCase)
&& req.Content != null
&& req.Content.ValidateJsonBody(expectedGraphBody)
), cancellationToken)
.Returns(graphResponse);

var graphClient = new GraphServiceClient(httpClient, new Microsoft.Kiota.Abstractions.Authentication.AnonymousAuthenticationProvider());
Expand Down Expand Up @@ -115,7 +105,9 @@ public async Task GetStoreIdForNameAsync_Returns_ExptectedResult()
// Validate the body of the request somehow
httpClient.SendAsync(Arg.Is<HttpRequestMessage>(req =>
req.Method == HttpMethod.Post
&& req.RequestUri == new Uri("https://storeedgefd.dsx.mp.microsoft.com/v9.0/manifestSearch")), cancellationToken)
&& req.RequestUri == new Uri("https://storeedgefd.dsx.mp.microsoft.com/v9.0/manifestSearch")
&& req.Content != null
&& req.Content.IsJson()), cancellationToken)
.Returns(expectedResponse);

// Act
Expand Down
48 changes: 48 additions & 0 deletions tests/WingetIntune.Tests/HttpContentExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace WingetIntune.Tests;
internal static class HttpContentExtensions
{
public static bool IsJson(this HttpContent? content)
{
return content?.Headers.ContentType?.MediaType == "application/json";
}

public static bool ValidateJsonBody(this HttpContent? content, Dictionary<string, object> bodyValues)
{
if (content is null || !content.IsJson())
{
return false;
}

var json = content.ReadAsStringAsync().Result;
try
{
var body = JsonSerializer.Deserialize<JsonContentBody>(json);

if (body is null || body.Data is null)
{
return false;
}
foreach (var kvp in bodyValues)
{
if (!body.Data.ContainsKey(kvp.Key) || !body.Data[kvp.Key].ValueEquals(kvp.Value.ToString()))
{
return false;
}
}
return true;
}
catch (Exception ex)
{
throw new InvalidOperationException($"Failed to deserialize JSON: {json}", ex);
}
}

public class JsonContentBody
{
[JsonExtensionData]
public Dictionary<string, JsonElement>? Data { get; set; }
}
}

0 comments on commit d5b7054

Please sign in to comment.