Skip to content

Commit

Permalink
Even more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
svrooij committed Aug 14, 2023
1 parent 3e093a9 commit 73181cb
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/WingetIntune/Internal/Msal/PublicClientAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ public async Task<AuthenticationResult> AcquireTokenInteractiveAsync(IEnumerable

public class PublicClientOptions
{
public string ClientId { get; set; }
public string? ClientId { get; set; }
public bool UseBroker { get; set; } = true;
}
26 changes: 13 additions & 13 deletions src/WingetIntune/Intune/IntuneManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public partial class IntuneManager
private readonly Mapper mapper = new Mapper();
private readonly IAzureFileUploader azureFileUploader;

private const string IntuneWinAppUtil = "IntuneWinAppUtil.exe";
private const string IntuneWinAppUtilUrl = "https://github.com/microsoft/Microsoft-Win32-Content-Prep-Tool/raw/master/IntuneWinAppUtil.exe";
internal const string IntuneWinAppUtil = "IntuneWinAppUtil.exe";
internal const string IntuneWinAppUtilUrl = "https://github.com/microsoft/Microsoft-Win32-Content-Prep-Tool/raw/master/IntuneWinAppUtil.exe";

public IntuneManager(ILogger<IntuneManager> logger, IFileManager fileManager, IProcessManager processManager, HttpClient httpClient, IAzureFileUploader azureFileUploader)
{
Expand All @@ -43,8 +43,8 @@ public async Task GenerateMsiPackage(string tempFolder, string outputFolder, Mod
LogGeneratePackage(packageInfo.PackageIdentifier!, packageInfo.Version!, outputFolder);
var packageTempFolder = fileManager.CreateFolderForPackage(tempFolder, packageInfo.PackageIdentifier!, packageInfo.Version!);
var packageFolder = fileManager.CreateFolderForPackage(outputFolder, packageInfo.PackageIdentifier!, packageInfo.Version!);
var contentPrepToolLocation = await DownloadContentPrepTool(tempFolder, contentPrepUri, cancellationToken);
var installerPath = await DownloadInstaller(packageTempFolder, packageInfo, cancellationToken);
var contentPrepToolLocation = await DownloadContentPrepToolAsync(tempFolder, contentPrepUri, cancellationToken);
var installerPath = await DownloadInstallerAsync(packageTempFolder, packageInfo, cancellationToken);
LoadMsiDetails(installerPath, ref packageInfo);
await GenerateIntuneWinFile(contentPrepToolLocation, packageTempFolder, packageFolder, packageInfo.InstallerFilename!, cancellationToken);
await DownloadLogoAsync(packageFolder, packageInfo.PackageIdentifier!, cancellationToken);
Expand All @@ -66,8 +66,8 @@ public async Task GenerateInstallerPackage(string tempFolder, string outputFolde
LogGeneratePackage(packageInfo.PackageIdentifier!, packageInfo.Version!, outputFolder);
var packageTempFolder = fileManager.CreateFolderForPackage(tempFolder, packageInfo.PackageIdentifier!, packageInfo.Version!);
var packageFolder = fileManager.CreateFolderForPackage(outputFolder, packageInfo.PackageIdentifier!, packageInfo.Version!);
var contentPrepToolLocation = await DownloadContentPrepTool(tempFolder, contentPrepUri, cancellationToken);
var installerPath = await DownloadInstaller(packageTempFolder, packageInfo, cancellationToken);
var contentPrepToolLocation = await DownloadContentPrepToolAsync(tempFolder, contentPrepUri, cancellationToken);
var installerPath = await DownloadInstallerAsync(packageTempFolder, packageInfo, cancellationToken);
await GenerateIntuneWinFile(contentPrepToolLocation, packageTempFolder, packageFolder, packageInfo.InstallerFilename!, cancellationToken);
await DownloadLogoAsync(packageFolder, packageInfo.PackageIdentifier!, cancellationToken);
//await GenerateMsiDetails(packageFolder, packageInfo, installerPath, cancellationToken);
Expand Down Expand Up @@ -196,29 +196,29 @@ await graphServiceClient.Intune_CommitWin32LobAppContentVersionFileAsync(app.Id!
}
}

private Task DownloadLogoAsync(string packageFolder, string packageId, CancellationToken cancellationToken)
internal Task DownloadLogoAsync(string packageFolder, string packageId, CancellationToken cancellationToken)
{
var logoPath = Path.Combine(packageFolder, "..", "logo.png");
var logoPath = Path.GetFullPath(Path.Combine(packageFolder, "..", "logo.png"));
var logoUri = $"https://api.winstall.app/icons/{packageId}.png";//new Uri($"https://winget.azureedge.net/cache/icons/48x48/{packageId}.png");
LogDownloadLogo(logoUri);
return fileManager.DownloadFileAsync(logoPath, logoUri, throwOnFailure: false, overrideFile: false, cancellationToken);
return fileManager.DownloadFileAsync(logoUri, logoPath, throwOnFailure: false, overrideFile: false, cancellationToken);
}

private async Task<string> DownloadContentPrepTool(string tempFolder, Uri contentPrepUri, CancellationToken cancellationToken)
internal async Task<string> DownloadContentPrepToolAsync(string tempFolder, Uri contentPrepUri, CancellationToken cancellationToken)
{
LogDownloadContentPrepTool(contentPrepUri);
fileManager.CreateFolder(tempFolder);

var contentPrepToolPath = Path.Combine(tempFolder, IntuneWinAppUtil);
await fileManager.DownloadFileAsync(contentPrepToolPath, contentPrepUri.ToString(), throwOnFailure: true, overrideFile: false, cancellationToken);
await fileManager.DownloadFileAsync(contentPrepUri.ToString(), contentPrepToolPath, throwOnFailure: true, overrideFile: false, cancellationToken);
return contentPrepToolPath;
}

private async Task<string> DownloadInstaller(string tempPackageFolder, PackageInfo packageInfo, CancellationToken cancellationToken)
internal async Task<string> DownloadInstallerAsync(string tempPackageFolder, PackageInfo packageInfo, CancellationToken cancellationToken)
{
var installerPath = Path.Combine(tempPackageFolder, packageInfo.InstallerFilename!);
LogDownloadInstaller(packageInfo.InstallerUrl!, installerPath);
await fileManager.DownloadFileAsync(installerPath, packageInfo.InstallerUrl!.ToString(), throwOnFailure: true, overrideFile: false, cancellationToken);
await fileManager.DownloadFileAsync(packageInfo.InstallerUrl!.ToString(), installerPath, throwOnFailure: true, overrideFile: false, cancellationToken);
return installerPath;
}

Expand Down
15 changes: 5 additions & 10 deletions tests/WingetIntune.Tests/AzCopyAzureUploaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using Microsoft.Extensions.Logging.Abstractions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WingetIntune.Tests;

public class AzCopyAzureUploaderTests
{
static string azCopyPath = Path.Combine(Path.GetTempPath(), "intunewin", "azcopy.exe");
private static string azCopyPath = Path.Combine(Path.GetTempPath(), "intunewin", "azcopy.exe");

[Fact]
public async Task UploadAsync_CallsProcessManager()
{
Expand All @@ -32,7 +29,6 @@ public async Task UploadAsync_CallsProcessManager()

fileManagerMock.VerifyAll();
processManagerMock.VerifyAll();

}

[Fact]
Expand All @@ -52,7 +48,7 @@ public async Task UploadAsync_DownloadsAzCopy()
.Verifiable();

fileManagerMock.Setup(fileManagerMock => fileManagerMock.ExtractFileToFolder(It.IsAny<string>(), It.IsAny<string>())).Verifiable();

fileManagerMock.Setup(fileManagerMock => fileManagerMock.FindFile(It.IsAny<string>(), "azcopy.exe")).Returns(azCopyPath).Verifiable();

fileManagerMock.Setup(fileManagerMock => fileManagerMock.CopyFile(azCopyPath, azCopyPath, false)).Verifiable();
Expand All @@ -70,6 +66,5 @@ public async Task UploadAsync_DownloadsAzCopy()

fileManagerMock.VerifyAll();
processManagerMock.VerifyAll();

}
}
}
3 changes: 0 additions & 3 deletions tests/WingetIntune.Tests/GraphServiceExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ public async Task Intune_CommitWin32LobAppContentVersionFileAsync_MakesCorrectRe
};

await graphServiceClient.Intune_CommitWin32LobAppContentVersionFileAsync(appId, contentVersionId.ToString(), fileId, body, CancellationToken.None)!;

}

[Fact]
Expand Down Expand Up @@ -238,7 +237,6 @@ public async Task Intune_WaitForFinalCommitStateAsync_ThrowsOnFailure()
var httpClient = new HttpClient(handlerMock.Object);
var graphServiceClient = new GraphServiceClient(httpClient, new Internal.Msal.StaticAuthenticationProvider(token));
await Assert.ThrowsAsync<Exception>(async () => await graphServiceClient.Intune_WaitForFinalCommitStateAsync(appId, contentVersionId.ToString(), fileId, CancellationToken.None)!);

}

[Fact]
Expand Down Expand Up @@ -276,6 +274,5 @@ public async Task Intune_WaitForFinalCommitStateAsync_ThrowsOnTimedOut()
var httpClient = new HttpClient(handlerMock.Object);
var graphServiceClient = new GraphServiceClient(httpClient, new Internal.Msal.StaticAuthenticationProvider(token));
await Assert.ThrowsAsync<Exception>(async () => await graphServiceClient.Intune_WaitForFinalCommitStateAsync(appId, contentVersionId.ToString(), fileId, CancellationToken.None)!);

}
}
71 changes: 71 additions & 0 deletions tests/WingetIntune.Tests/IntuneManagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Microsoft.Extensions.Logging.Abstractions;
using WingetIntune.Models;

namespace WingetIntune.Tests;

public class IntuneManagerTests
{
[Fact]
public async Task DownloadLogoAsync_CallsFilemanager()
{
var packageId = "Microsoft.AzureCLI";
var version = "2.26.1";
var folder = Path.Combine(Path.GetTempPath(), "intunewin", packageId, version);

var logoPath = Path.GetFullPath(Path.Combine(folder, "..", "logo.png"));

var fileManagerMock = new Mock<IFileManager>();
fileManagerMock.Setup(x => x.DownloadFileAsync($"https://api.winstall.app/icons/{packageId}.png", logoPath, false, false, It.IsAny<CancellationToken>()))
.Returns(Task.CompletedTask)
.Verifiable();

var intuneManager = new IntuneManager(new NullLogger<IntuneManager>(), fileManagerMock.Object, null, null, null);

Check warning on line 22 in tests/WingetIntune.Tests/IntuneManagerTests.cs

View workflow job for this annotation

GitHub Actions / Build and Test

Cannot convert null literal to non-nullable reference type. [/home/runner/work/WingetIntune/WingetIntune/tests/WingetIntune.Tests/WingetIntune.Tests.csproj]

Check warning on line 22 in tests/WingetIntune.Tests/IntuneManagerTests.cs

View workflow job for this annotation

GitHub Actions / Build and Test

Cannot convert null literal to non-nullable reference type. [/home/runner/work/WingetIntune/WingetIntune/tests/WingetIntune.Tests/WingetIntune.Tests.csproj]

Check warning on line 22 in tests/WingetIntune.Tests/IntuneManagerTests.cs

View workflow job for this annotation

GitHub Actions / Build and Test

Cannot convert null literal to non-nullable reference type. [/home/runner/work/WingetIntune/WingetIntune/tests/WingetIntune.Tests/WingetIntune.Tests.csproj]

Check warning on line 22 in tests/WingetIntune.Tests/IntuneManagerTests.cs

View workflow job for this annotation

GitHub Actions / Build and Test

Cannot convert null literal to non-nullable reference type. [/home/runner/work/WingetIntune/WingetIntune/tests/WingetIntune.Tests/WingetIntune.Tests.csproj]
await intuneManager.DownloadLogoAsync(folder, packageId, CancellationToken.None);

fileManagerMock.Verify();
}

[Fact]
public async Task DownloadInstallerAsync_CallsFilemanager()
{
var packageId = "Microsoft.AzureCLI";
var version = "2.26.1";
var folder = Path.Combine(Path.GetTempPath(), "intunewin", packageId, version);

var packageInfo = new PackageInfo
{
InstallerFilename = "testpackage.exe",
InstallerUrl = new Uri("https://localhost/testpackage.exe")
};

var installerPath = Path.GetFullPath(Path.Combine(folder, packageInfo.InstallerFilename));

var fileManagerMock = new Mock<IFileManager>();
fileManagerMock.Setup(x => x.DownloadFileAsync(packageInfo.InstallerUrl.ToString(), installerPath, true, false, It.IsAny<CancellationToken>()))
.Returns(Task.CompletedTask)
.Verifiable();

var intuneManager = new IntuneManager(new NullLogger<IntuneManager>(), fileManagerMock.Object, null, null, null);

Check warning on line 48 in tests/WingetIntune.Tests/IntuneManagerTests.cs

View workflow job for this annotation

GitHub Actions / Build and Test

Cannot convert null literal to non-nullable reference type. [/home/runner/work/WingetIntune/WingetIntune/tests/WingetIntune.Tests/WingetIntune.Tests.csproj]

Check warning on line 48 in tests/WingetIntune.Tests/IntuneManagerTests.cs

View workflow job for this annotation

GitHub Actions / Build and Test

Cannot convert null literal to non-nullable reference type. [/home/runner/work/WingetIntune/WingetIntune/tests/WingetIntune.Tests/WingetIntune.Tests.csproj]

Check warning on line 48 in tests/WingetIntune.Tests/IntuneManagerTests.cs

View workflow job for this annotation

GitHub Actions / Build and Test

Cannot convert null literal to non-nullable reference type. [/home/runner/work/WingetIntune/WingetIntune/tests/WingetIntune.Tests/WingetIntune.Tests.csproj]
await intuneManager.DownloadInstallerAsync(folder, packageInfo, CancellationToken.None);

fileManagerMock.Verify();
}

[Fact]
public async Task DownloadContentPrepToolAsync_CallsFilemanager()
{
var tempFolder = Path.GetTempPath();
var contentPrepToolPath = Path.Combine(tempFolder, IntuneManager.IntuneWinAppUtil);

var fileManagerMock = new Mock<IFileManager>();
fileManagerMock.Setup(x => x.CreateFolder(tempFolder));
fileManagerMock.Setup(x => x.DownloadFileAsync(IntuneManager.IntuneWinAppUtilUrl, contentPrepToolPath, true, false, It.IsAny<CancellationToken>()))
.Returns(Task.CompletedTask)
.Verifiable();

var intuneManager = new IntuneManager(new NullLogger<IntuneManager>(), fileManagerMock.Object, null, null, null);

Check warning on line 66 in tests/WingetIntune.Tests/IntuneManagerTests.cs

View workflow job for this annotation

GitHub Actions / Build and Test

Cannot convert null literal to non-nullable reference type. [/home/runner/work/WingetIntune/WingetIntune/tests/WingetIntune.Tests/WingetIntune.Tests.csproj]

Check warning on line 66 in tests/WingetIntune.Tests/IntuneManagerTests.cs

View workflow job for this annotation

GitHub Actions / Build and Test

Cannot convert null literal to non-nullable reference type. [/home/runner/work/WingetIntune/WingetIntune/tests/WingetIntune.Tests/WingetIntune.Tests.csproj]

Check warning on line 66 in tests/WingetIntune.Tests/IntuneManagerTests.cs

View workflow job for this annotation

GitHub Actions / Build and Test

Cannot convert null literal to non-nullable reference type. [/home/runner/work/WingetIntune/WingetIntune/tests/WingetIntune.Tests/WingetIntune.Tests.csproj]
await intuneManager.DownloadContentPrepToolAsync(tempFolder, IntuneManager.DefaultIntuneWinAppUrl, CancellationToken.None);

fileManagerMock.Verify();
}
}

0 comments on commit 73181cb

Please sign in to comment.