mirror of
https://github.com/revenz/FileFlowsPlugins.git
synced 2026-05-12 15:48:50 -05:00
next cloud unit test
This commit is contained in:
@@ -82,7 +82,7 @@ public class UploadToNextcloud : Node
|
||||
args.Logger?.ILog("File: " + local.Value);
|
||||
args.Logger?.ILog("Destination: " + destination);
|
||||
|
||||
var uploader = new NextcloudUploader(args.Logger!, settings.Url, settings.Username, settings.Password);
|
||||
var uploader = GetUploader(args.Logger!, settings.Url, settings.Username, settings.Password);
|
||||
var result = uploader.UploadFile(local.Value, destination);
|
||||
if(result.Failed(out error))
|
||||
{
|
||||
@@ -94,4 +94,30 @@ public class UploadToNextcloud : Node
|
||||
args.Logger?.ILog("File successfully uploaded");
|
||||
return 1;
|
||||
}
|
||||
/// <summary>
|
||||
/// The function to get the nextcloud uploader
|
||||
/// </summary>
|
||||
private Func<ILogger, string, string, string, INextcloudUploader>? _GetUploader;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the function to get the next cloud uploader used to send a request
|
||||
/// </summary>
|
||||
internal Func<ILogger, string, string, string, INextcloudUploader> GetUploader
|
||||
{
|
||||
get
|
||||
{
|
||||
if(_GetUploader == null)
|
||||
{
|
||||
_GetUploader = (logger, url, username, password) =>
|
||||
new NextcloudUploader(logger, url, username, password);
|
||||
}
|
||||
return _GetUploader;
|
||||
}
|
||||
#if(DEBUG)
|
||||
set
|
||||
{
|
||||
_GetUploader = value;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,20 @@ using System.Net;
|
||||
|
||||
namespace FileFlows.Nextcloud.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// Nextcloud uploader
|
||||
/// </summary>
|
||||
public interface INextcloudUploader
|
||||
{
|
||||
/// <summary>
|
||||
/// Uploads a file to Nextcloud
|
||||
/// </summary>
|
||||
/// <param name="localFilePath">The full path to the file on disk to be uploaded.</param>
|
||||
/// <param name="remoteFilePath">The path in Nextcloud where the file should be uploaded.</param>
|
||||
/// <returns>True if the file was uploaded successfully; otherwise, false.</returns>
|
||||
Result<bool> UploadFile(string localFilePath, string remoteFilePath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Helper class for uploading files to Nextcloud
|
||||
/// </summary>
|
||||
@@ -9,7 +23,7 @@ namespace FileFlows.Nextcloud.Helpers;
|
||||
/// <param name="nextcloudUrl">The URL of the Nextcloud instance.</param>
|
||||
/// <param name="username">The username for authentication.</param>
|
||||
/// <param name="password">The password for authentication.</param>
|
||||
public class NextcloudUploader(ILogger logger, string nextcloudUrl, string username, string password)
|
||||
public class NextcloudUploader(ILogger logger, string nextcloudUrl, string username, string password) : INextcloudUploader
|
||||
{
|
||||
private static HttpClient client;
|
||||
|
||||
@@ -25,12 +39,7 @@ public class NextcloudUploader(ILogger logger, string nextcloudUrl, string usern
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Uploads a file to Nextcloud
|
||||
/// </summary>
|
||||
/// <param name="localFilePath">The full path to the file on disk to be uploaded.</param>
|
||||
/// <param name="remoteFilePath">The path in Nextcloud where the file should be uploaded.</param>
|
||||
/// <returns>True if the file was uploaded successfully; otherwise, false.</returns>
|
||||
/// <inheritdoc />
|
||||
public Result<bool> UploadFile(string localFilePath, string remoteFilePath)
|
||||
{
|
||||
logger?.ILog("Uploading file: " + localFilePath);
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#if(DEBUG)
|
||||
|
||||
using FileFlows.Nextcloud.FlowElements;
|
||||
using FileFlows.Nextcloud.Helpers;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using PluginTestLibrary;
|
||||
|
||||
namespace FileFlows.Nextcloud.Tests;
|
||||
@@ -12,15 +14,19 @@ public class UploadTest : TestBase
|
||||
[TestMethod]
|
||||
public void Test()
|
||||
{
|
||||
var args = new NodeParameters("/home/john/src/ff-files/test-files/images/heic1.heic", Logger, false, string.Empty, new LocalFileService());
|
||||
args.GetPluginSettingsJson = (string input) =>
|
||||
{
|
||||
return File.ReadAllText("../../../../../nextcloud.json");
|
||||
};
|
||||
|
||||
var ele = new UploadToNextcloud();
|
||||
ele.DestinationPath = "ff-test/" + Guid.NewGuid() + ".heic";
|
||||
Assert.AreEqual(1, ele.Execute(args));
|
||||
var args = GetNodeParameters(TempFile);
|
||||
args.GetPluginSettingsJson = _ => """{"Username": "user", "Password": "password", "Url": "http://nextcloud.test" }""";
|
||||
string destPath = "ff-test/" + Guid.NewGuid() + ".heic";
|
||||
Mock<INextcloudUploader> mockUploader = new();
|
||||
mockUploader.Setup(x => x.UploadFile(
|
||||
It.Is<string>(y => y == TempFile),
|
||||
It.Is<string>(y => y == destPath)))
|
||||
.Returns(true);
|
||||
|
||||
var element = new UploadToNextcloud();
|
||||
element.GetUploader = (_, _, _, _) => mockUploader.Object;
|
||||
element.DestinationPath = destPath;
|
||||
Assert.AreEqual(1, element.Execute(args));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user