plex tests

This commit is contained in:
John Andrews
2024-08-28 08:57:41 +12:00
parent 1fee7f08f5
commit 549b2d2b4f
5 changed files with 181 additions and 90 deletions

View File

@@ -54,18 +54,18 @@ public class PlexAnalyze : PlexNode
internal PlexMedia[] GetPlexMedia(HttpClient httpClient, NodeParameters args, string baseUrl, string urlPath, string token, int depth = 0)
{
if (depth > 10)
return new PlexMedia[] { };
if (urlPath.StartsWith("/") && baseUrl.EndsWith("/"))
return [];
if (urlPath.StartsWith('/') && baseUrl.EndsWith('/'))
urlPath = urlPath[1..];
string fullUrl = baseUrl + urlPath;
fullUrl += (fullUrl.IndexOf("?", StringComparison.Ordinal) > 0 ? "&" : "?") + "X-Plex-Token=" + token;
fullUrl += (fullUrl.IndexOf('?', StringComparison.Ordinal) > 0 ? "&" : "?") + "X-Plex-Token=" + token;
var updateResponse = GetWebRequest(httpClient, fullUrl);
if (updateResponse.success == false)
{
if (string.IsNullOrWhiteSpace(updateResponse.body) == false)
args.Logger?.WLog("Failed to get files from Plex:" + updateResponse.body);
return new PlexMedia[] { };
return [];
}
@@ -81,9 +81,10 @@ public class PlexAnalyze : PlexNode
catch (Exception ex)
{
args.Logger?.ELog("Failed deserializing sections json: " + ex.Message);
return new PlexMedia[] { };
return [];
}
var list = metadata ?? new List<PlexMetadata>();
var list = metadata ?? new();
var results = new List<PlexMedia>();
foreach(var item in list)
@@ -94,7 +95,6 @@ public class PlexAnalyze : PlexNode
media.RatingKey = item.RatingKey;
results.AddRange(item.Media);
continue;
}
else if(string.IsNullOrEmpty(item?.Key) == false && depth < 10)
{

View File

@@ -6,6 +6,7 @@ public class PlexUpdater: PlexNode
{
protected override int ExecuteActual(NodeParameters args, PlexDirectory directory, string url, string mappedPath, string accessToken)
{
args.Logger?.ILog("Executing Actual in Plex Updater");
url += $"library/sections/{directory.Key}/refresh?path={Uri.EscapeDataString(mappedPath)}&X-Plex-Token=" + accessToken;
using var httpClient = new HttpClient();

View File

@@ -112,6 +112,7 @@ public abstract class PlexNode:Node
args.Logger?.WLog("Failed to find Plex section for path: " + path);
return 2;
}
args.Logger?.ILog("Found section: " + (section.Key ?? "no-key"));
return ExecuteActual(args, section, serverUrl, path, accessToken);
}
@@ -142,6 +143,9 @@ public abstract class PlexNode:Node
}
return _GetWebRequest;
}
#if(DEBUG)
set => _GetWebRequest = value;
#endif
}
private Func<HttpClient, string, (bool success, string body)>? _PutWebRequest;
internal Func<HttpClient, string, (bool success, string body)> PutWebRequest
@@ -167,5 +171,8 @@ public abstract class PlexNode:Node
}
return _PutWebRequest;
}
#if(DEBUG)
set => _PutWebRequest = value;
#endif
}
}

View File

@@ -1,59 +1,98 @@
#if(DEBUG)
using FileFlows.Plex.MediaManagement;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PluginTestLibrary;
namespace FileFlows.Plex.Tests;
[TestClass]
public class PlexAnayzeTests : TestBase
{
[TestMethod]
public void PlexAnayze_Basic()
{
var args = new NodeParameters(@"/media/tv/Outrageous Fortune/Season 3/Outrageous Fotune - 3x02.mkv", Logger, false, string.Empty, new LocalFileService());
args.GetPluginSettingsJson = (string input) =>
{
return File.ReadAllText("../../../settings.json");
};
var node = new PlexAnalyze();
Assert.AreEqual(1, node.Execute(args));
}
[TestMethod]
public void PlexAnayze_Fail()
{
var args = new NodeParameters(@"/media/tv/Outrageous Fortune/Season 3/Outrageous Fotune - 3x02a.mkv",
Logger, false, string.Empty, new LocalFileService());
args.GetPluginSettingsJson = (string input) =>
{
return File.ReadAllText("../../../settings.json");
};
var node = new PlexAnalyze();
Assert.AreEqual(2, node.Execute(args));
}
[TestMethod]
public void PlexAnayze_Mapping()
{
var args = new NodeParameters(@"/mnt/movies/The Batman (2022)/The Batman (2022).mkv",
Logger, false, string.Empty, new LocalFileService());
var settings = new PluginSettings();
settings.Mapping = new List<KeyValuePair<string, string>>();
settings.Mapping.Add(new KeyValuePair<string, string>("/mnt/movies", "/media/movies"));
args.GetPluginSettingsJson = (string input) =>
{
return File.ReadAllText("../../../settings.json");
};
var node = new PlexAnalyze();
Assert.AreEqual(1, node.Execute(args));
}
}
#endif
// #if(DEBUG)
//
// using System.Text.Json;
// using FileFlows.Plex.MediaManagement;
// using FileFlows.Plex.Models;
// using Microsoft.VisualStudio.TestTools.UnitTesting;
// using PluginTestLibrary;
//
// namespace FileFlows.Plex.Tests;
//
// [TestClass]
// public class PlexAnayzeTests : TestBase
// {
// [TestMethod]
// public void PlexAnayze_Basic()
// {
// var args = GetNodeParameters(TempFile);
// args.GetPluginSettingsJson = _ => """{"AccessToken": "access-token", "ServerUrl": "http://plex.test" }""";
//
// var element = new PlexAnalyze();
// element.GetWebRequest = (_, url) =>
// {
// if (url.Contains("library/sections"))
// return (true, JsonSerializer.Serialize(new PlexSections()
// {
// MediaContainer = new()
// {
// Metadata = [
// new ()
// {
// Key = "metatdata",
// Media = [
// new ()
// {
// Id = 123,
// RatingKey = "rating-key"
// }
// ]
// }
// ],
// Directory =
// [
// new()
// {
// Location =
// [
// new()
// {
// Id = 123,
// Path = TempPath
// }
// ]
// }
// ]
// },
//
// }));
// return (true, "");
// };
//
// Assert.AreEqual(1, element.Execute(args));
// }
//
// [TestMethod]
// public void PlexAnayze_Fail()
// {
// var args = new NodeParameters(@"/media/tv/Outrageous Fortune/Season 3/Outrageous Fotune - 3x02a.mkv",
// Logger, false, string.Empty, new LocalFileService());
// args.GetPluginSettingsJson = (string input) =>
// {
// return File.ReadAllText("../../../settings.json");
// };
//
// var node = new PlexAnalyze();
// Assert.AreEqual(2, node.Execute(args));
// }
//
// [TestMethod]
// public void PlexAnayze_Mapping()
// {
// var args = new NodeParameters(@"/mnt/movies/The Batman (2022)/The Batman (2022).mkv",
// Logger, false, string.Empty, new LocalFileService());
// var settings = new PluginSettings();
// settings.Mapping = new List<KeyValuePair<string, string>>();
// settings.Mapping.Add(new KeyValuePair<string, string>("/mnt/movies", "/media/movies"));
//
// args.GetPluginSettingsJson = (string input) =>
// {
// return File.ReadAllText("../../../settings.json");
// };
//
// var node = new PlexAnalyze();
// Assert.AreEqual(1, node.Execute(args));
// }
//
// }
//
// #endif

View File

@@ -1,6 +1,8 @@
#if(DEBUG)
using System.Text.Json;
using FileFlows.Plex.MediaManagement;
using FileFlows.Plex.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PluginTestLibrary;
@@ -12,26 +14,43 @@ public class PlexUpdaterTests : TestBase
[TestMethod]
public void Plex_Basic()
{
var args = new NodeParameters(@"/media/movies/The Batman (2022)/The Batman.mkv",
Logger, false, string.Empty, new LocalFileService());
args.GetPluginSettingsJson = (string input) =>
var args = GetNodeParameters(TempFile);
args.GetPluginSettingsJson = _ => """{"AccessToken": "access-token", "ServerUrl": "http://plex.test" }""";
var element = new PlexUpdater();
element.GetWebRequest = (_, url) =>
{
return File.ReadAllText("../../../settings.json");
if (url.Contains("library/sections"))
return (true, JsonSerializer.Serialize(new PlexSections()
{
MediaContainer = new()
{
Directory =
[
new()
{
Location =
[
new()
{
Id = 123,
Path = TempPath
}
]
}
]
}
}));
return (true, "");
};
var node = new PlexUpdater();
Assert.AreEqual(1, node.Execute(args));
Assert.AreEqual(1, element.Execute(args));
}
[TestMethod]
public void Plex_Fail()
{
var args = new NodeParameters(@"/media/unknownmovies/The Batman (2022)/The Batman.mkv",
Logger, false, string.Empty, new LocalFileService());
args.GetPluginSettingsJson = (string input) =>
{
return File.ReadAllText("../../../settings.json");
};
var args = GetNodeParameters(TempFile);
args.GetPluginSettingsJson = _ => """{"AccessToken": "access-token", "ServerUrl": "http://plex.test" }""";
var node = new PlexUpdater();
Assert.AreEqual(2, node.Execute(args));
@@ -40,19 +59,44 @@ public class PlexUpdaterTests : TestBase
[TestMethod]
public void Plex_Mapping()
{
var args = new NodeParameters(@"/mnt/movies/The Batman (2022)/The Batman.mkv",
Logger, false, string.Empty, new LocalFileService());
var settings = new PluginSettings();
settings.Mapping = new List<KeyValuePair<string, string>>();
settings.Mapping.Add(new KeyValuePair<string, string>("/mnt/movies", "/media/movies"));
args.GetPluginSettingsJson = (string input) =>
var args = GetNodeParameters(TempFile);
args.GetPluginSettingsJson = _ => JsonSerializer.Serialize(new PluginSettings
{
return File.ReadAllText("../../../settings.json");
AccessToken = "access-token",
ServerUrl = "http://plex.test",
Mapping = new List<KeyValuePair<string, string>>()
{
new(TempPath, "/media/movies")
}
});
var element = new PlexUpdater();
element.GetWebRequest = (_, url) =>
{
if (url.Contains("library/sections"))
return (true, JsonSerializer.Serialize(new PlexSections()
{
MediaContainer = new()
{
Directory =
[
new()
{
Location =
[
new()
{
Id = 123,
Path = "/media/movies"
}
]
}
]
}
}));
return (true, "");
};
var node = new PlexUpdater();
Assert.AreEqual(1, node.Execute(args));
Assert.AreEqual(1, element.Execute(args));
}
}