unit tests

This commit is contained in:
John Andrews
2024-08-28 09:49:12 +12:00
parent 549b2d2b4f
commit 85b9a72183
6 changed files with 387 additions and 96 deletions

View File

@@ -109,28 +109,13 @@ File shrunk in size by: {{ difference | file_size }} / {{ percent }}%
return 2;
}
using var httpClient = new HttpClient();
string title = args.ReplaceVariables(this.Title, stripMissing: true);
// Set the authorization header with the Access Token
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", settings.ApiToken);
var result = GetWebRequest(settings.ApiToken, title, body);
// Create the request content
var content = new StringContent(JsonSerializer.Serialize(
new {
type = "note",
title,
body
}), Encoding.UTF8, "application/json");
var response = httpClient.PostAsync("https://api.pushbullet.com/v2/pushes", content).Result;
if (response.IsSuccessStatusCode)
if (result.success)
return 1;
string error = response.Content.ReadAsStringAsync().Result;
args.Logger?.WLog("Error from Pushbullet: " + error);
args.Logger?.WLog("Error from Pushbullet: " + result.body);
return 2;
}
catch (Exception ex)
@@ -139,4 +124,57 @@ File shrunk in size by: {{ difference | file_size }} / {{ percent }}%
return 2;
}
}
/// <summary>
/// The method used to send the request
/// </summary>
private Func<string, string, string, (bool success, string body)>? _GetWebRequest;
/// <summary>
/// Gets the method used to send a request
/// </summary>
internal Func<string, string, string, (bool success, string body)> GetWebRequest
{
get
{
if(_GetWebRequest == null)
{
_GetWebRequest = (apiToken, title, body) =>
{
try
{
using var httpClient = new HttpClient();
// Set the authorization header with the Access Token
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", apiToken);
// Create the request content
var content = new StringContent(JsonSerializer.Serialize(
new {
type = "note",
title,
body
}), Encoding.UTF8, "application/json");
var response = httpClient.PostAsync("https://api.pushbullet.com/v2/pushes", content).Result;
string responseBody = response.Content.ReadAsStringAsync().Result;
return (response.IsSuccessStatusCode, responseBody);
}
catch(Exception ex)
{
return (false, ex.Message);
}
};
}
return _GetWebRequest;
}
#if(DEBUG)
set
{
_GetWebRequest = value;
}
#endif
}
}

View File

@@ -5,21 +5,61 @@ using PluginTestLibrary;
namespace FileFlows.Pushbullet.Tests;
/// <summary>
/// Push bullet tests
/// </summary>
[TestClass]
public class PushbulletTests : TestBase
{
/// <summary>
/// Tests a basic success
/// </summary>
[TestMethod]
public void Pushbullet_Basic_Message()
public void Success()
{
var args = new NodeParameters("test.file", Logger, false, string.Empty, new LocalFileService());
args.GetPluginSettingsJson = (string input) =>
{
return File.ReadAllText("../../../../../Pushbullet.json");
};
var args = GetNodeParameters(TempFile);
args.GetPluginSettingsJson = _ => """{"ApiToken": "api-token" }""";
args.RenderTemplate = template => template;
var node = new FileFlows.Pushbullet.Communication.Pushbullet();
node.Message = "a message";
Assert.AreEqual(1, node.Execute(args));
var element = new FileFlows.Pushbullet.Communication.Pushbullet();
element.GetWebRequest = (accessToken, message, title)
=> (true, "sent");
element.Message = "a message";
Assert.AreEqual(1, element.Execute(args));
}
/// <summary>
/// Tests a basic success
/// </summary>
[TestMethod]
public void Fail()
{
var args = GetNodeParameters(TempFile);
args.GetPluginSettingsJson = _ => """{"ApiToken": "api-token" }""";
args.RenderTemplate = template => template;
var element = new FileFlows.Pushbullet.Communication.Pushbullet();
element.GetWebRequest = (accessToken, message, title)
=> (false, "sent");
element.Message = "a message";
Assert.AreEqual(2, element.Execute(args));
}
/// <summary>
/// Tests a basic success
/// </summary>
[TestMethod]
public void NoSettings()
{
var args = GetNodeParameters(TempFile);
args.GetPluginSettingsJson = _ => string.Empty;
args.RenderTemplate = template => template;
var element = new FileFlows.Pushbullet.Communication.Pushbullet();
element.GetWebRequest = (accessToken, message, title)
=> (true, "sent");
element.Message = "a message";
Assert.AreEqual(2, element.Execute(args));
}
}

View File

@@ -155,36 +155,11 @@ File shrunk in size by: {{ difference | file_size }} / {{ percent }}%
args.Logger?.WLog("No message to send");
return 2;
}
List<KeyValuePair<string, string>> parameters = new ()
{
new ("token", settings.ApiToken),
new ("user", settings.UserKey),
new ("message", message),
new ("priority", Priority.ToString())
};
if (Priority == 2)
{
int expire = Expire < 1 ? 1 : (Expire > 86400 ? 86400 : Expire);
parameters.Add(new("expire", expire.ToString()));
int retry = Retry < 30 ? 30 : (Retry > 86400 ? 86400 : Retry);
parameters.Add(new("retry", retry.ToString()));
}
var content = new FormUrlEncodedContent(parameters);
using var httpClient = new HttpClient();
var response = httpClient.PostAsync("https://api.pushover.net/1/messages.json", content).Result;
if (response.IsSuccessStatusCode)
var result = GetWebRequest(settings.ApiToken, settings.UserKey, message, Priority.ToString());
if (result.success)
return 1;
string error = response.Content.ReadAsStringAsync().Result;
args.Logger?.WLog("Error from Pushover: " + error);
args.Logger?.WLog("Error from Pushover: " + result.body);
return 2;
}
catch (Exception ex)
@@ -193,4 +168,67 @@ File shrunk in size by: {{ difference | file_size }} / {{ percent }}%
return 2;
}
}
/// <summary>
/// The method used to send the request
/// </summary>
private Func<string, string, string, string, (bool success, string body)>? _GetWebRequest;
/// <summary>
/// Gets the method used to send a request
/// </summary>
internal Func<string, string, string, string, (bool success, string body)> GetWebRequest
{
get
{
if(_GetWebRequest == null)
{
_GetWebRequest = (token, user, message, priority) =>
{
try
{
List<KeyValuePair<string, string>> parameters = new ()
{
new ("token", token),
new ("user", user),
new ("message", message),
new ("priority", Priority.ToString())
};
if (Priority == 2)
{
int expire = Expire < 1 ? 1 : (Expire > 86400 ? 86400 : Expire);
parameters.Add(new("expire", expire.ToString()));
int retry = Retry < 30 ? 30 : (Retry > 86400 ? 86400 : Retry);
parameters.Add(new("retry", retry.ToString()));
}
var content = new FormUrlEncodedContent(parameters);
using var httpClient = new HttpClient();
var response = httpClient.PostAsync("https://api.pushover.net/1/messages.json", content).Result;
string responseBody = response.Content.ReadAsStringAsync().Result;
return (response.IsSuccessStatusCode, responseBody);
}
catch(Exception ex)
{
return (false, ex.Message);
}
};
}
return _GetWebRequest;
}
#if(DEBUG)
set
{
_GetWebRequest = value;
}
#endif
}
}

View File

@@ -9,18 +9,89 @@ namespace FileFlows.Pushover.Tests;
[TestClass]
public class PushoverTests : TestBase
{
/// <summary>
/// Tests a basic success
/// </summary>
[TestMethod]
public void Pushover_Basic_Message()
public void Success()
{
var args = new NodeParameters("test.file", Logger, false, string.Empty, new LocalFileService());
args.GetPluginSettingsJson = (string input) =>
{
return File.ReadAllText("../../../../../pushover.json");
};
var args = GetNodeParameters(TempFile);
args.GetPluginSettingsJson = _ => """{"ApiToken": "api-token", "UserKey":"user" }""";
args.RenderTemplate = template => template;
var node = new FileFlows.Pushover.Communication.Pushover();
node.Message = "a message";
Assert.AreEqual(1, node.Execute(args));
var element = new Communication.Pushover();
element.GetWebRequest = (token, user,message, priority)
=> (true, "sent");
element.Message = "a message";
Assert.AreEqual(1, element.Execute(args));
}
/// <summary>
/// Tests a basic failure
/// </summary>
[TestMethod]
public void Fail()
{
var args = GetNodeParameters(TempFile);
args.GetPluginSettingsJson = _ => """{"ApiToken": "api-token", "UserKey":"user" }""";
args.RenderTemplate = template => template;
var element = new Communication.Pushover();
element.GetWebRequest = (token, user,message, priority)
=> (false, "sent");
element.Message = "a message";
Assert.AreEqual(2, element.Execute(args));
}
/// <summary>
/// Tests a no settings fails
/// </summary>
[TestMethod]
public void NoSettings()
{
var args = GetNodeParameters(TempFile);
args.GetPluginSettingsJson = _ => string.Empty;
args.RenderTemplate = template => template;
var element = new Communication.Pushover();
element.GetWebRequest = (token, user,message, priority)
=> (true, "sent");
element.Message = "a message";
Assert.AreEqual(2, element.Execute(args));
}
/// <summary>
/// Tests a no token fails
/// </summary>
[TestMethod]
public void NoToken()
{
var args = GetNodeParameters(TempFile);
args.GetPluginSettingsJson = _ => """{ "UserKey":"user" }""";
args.RenderTemplate = template => template;
var element = new Communication.Pushover();
element.GetWebRequest = (token, user,message, priority)
=> (true, "sent");
element.Message = "a message";
Assert.AreEqual(2, element.Execute(args));
}
/// <summary>
/// Tests a no user fails success
/// </summary>
[TestMethod]
public void NoUser()
{
var args = GetNodeParameters(TempFile);
args.GetPluginSettingsJson = _ => """{"ApiToken": "api-token" }""";
args.RenderTemplate = template => template;
var element = new Communication.Pushover();
element.GetWebRequest = (token, user,message, priority)
=> (true, "sent");
element.Message = "a message";
Assert.AreEqual(2, element.Execute(args));
}
}

View File

@@ -72,29 +72,6 @@ File shrunk in size by: {{ difference | file_size }} / {{ percent }}%
}
}
/// <summary>
/// Sends a telegram message
/// </summary>
/// <param name="botToken">the bot token</param>
/// <param name="chatId">the chat id</param>
/// <param name="message">the message to send</param>
/// <returns>true if successful, otherwise false</returns>
internal static bool SendMessage(string botToken, string chatId, string message)
{
using (HttpClient client = new HttpClient())
{
string apiUrl = $"https://api.telegram.org/bot{botToken}/sendMessage";
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("chat_id", chatId),
new KeyValuePair<string, string>("text", message)
});
var response = client.PostAsync(apiUrl, content).Result;
return response.IsSuccessStatusCode;
}
}
/// <summary>
/// Executes the flow element
@@ -129,7 +106,11 @@ File shrunk in size by: {{ difference | file_size }} / {{ percent }}%
var result = SendMessage(settings.BotToken, settings.ChatId, message);
return result ? 1 : 2;
if (result.success)
return 1;
args.Logger?.WLog("Error from Telegram: " + result.body);
return 2;
}
catch (Exception ex)
{
@@ -137,4 +118,54 @@ File shrunk in size by: {{ difference | file_size }} / {{ percent }}%
return 2;
}
}
/// <summary>
/// The method used to send the request
/// </summary>
private Func<string, string, string, (bool success, string body)>? _SendMessage;
/// <summary>
/// Gets the method used to send a request
/// </summary>
internal Func<string, string, string, (bool success, string body)> SendMessage
{
get
{
if(_SendMessage == null)
{
_SendMessage = (string botToken, string chatId, string message) =>
{
try
{
using HttpClient client = new HttpClient();
string apiUrl = $"https://api.telegram.org/bot{botToken}/sendMessage";
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("chat_id", chatId),
new KeyValuePair<string, string>("text", message)
});
var response = client.PostAsync(apiUrl, content).Result;
string responseBody = response.Content.ReadAsStringAsync().Result;
return (response.IsSuccessStatusCode, responseBody);
}
catch(Exception ex)
{
return (false, ex.Message);
}
};
}
return _SendMessage;
}
#if(DEBUG)
set
{
_SendMessage = value;
}
#endif
}
}

View File

@@ -8,16 +8,89 @@ namespace FileFlows.Telegram.Tests;
[TestClass]
public class TelegramTests : TestBase
{
/// <summary>
/// Tests a basic success
/// </summary>
[TestMethod]
public void SendMessage()
public void Success()
{
string botToken = "sometoken";
string chatId = "somechat";
var result =
Communication.Telegram.SendMessage(botToken, chatId, "this is a test");
Assert.IsTrue(result);
var args = GetNodeParameters(TempFile);
args.GetPluginSettingsJson = _ => """{"BotToken": "bot-token", "ChatId": "chat-id" }""";
args.RenderTemplate = template => template;
var element = new Communication.Telegram();
element.SendMessage = (botToken, chatId,message)
=> (true, "sent");
element.Message = "a message";
Assert.AreEqual(1, element.Execute(args));
}
/// <summary>
/// Tests a basic failure
/// </summary>
[TestMethod]
public void Fail()
{
var args = GetNodeParameters(TempFile);
args.GetPluginSettingsJson = _ => """{"BotToken": "bot-token", "ChatId": "chat-id" }""";
args.RenderTemplate = template => template;
var element = new Communication.Telegram();
element.SendMessage = (botToken, chatId,message)
=> (false, "sent");
element.Message = "a message";
Assert.AreEqual(2, element.Execute(args));
}
/// <summary>
/// Tests a no settings fails
/// </summary>
[TestMethod]
public void NoSettings()
{
var args = GetNodeParameters(TempFile);
args.GetPluginSettingsJson = _ => string.Empty;
args.RenderTemplate = template => template;
var element = new Communication.Telegram();
element.SendMessage = (botToken, chatId,message)
=> (true, "sent");
element.Message = "a message";
Assert.AreEqual(2, element.Execute(args));
}
/// <summary>
/// Tests a no token fails
/// </summary>
[TestMethod]
public void NoToken()
{
var args = GetNodeParameters(TempFile);
args.GetPluginSettingsJson = _ => """{ "ChatId": "chat-id" }""";
args.RenderTemplate = template => template;
var element = new Communication.Telegram();
element.SendMessage = (botToken, chatId,message)
=> (true, "sent");
element.Message = "a message";
Assert.AreEqual(2, element.Execute(args));
}
/// <summary>
/// Tests a no chat id fails success
/// </summary>
[TestMethod]
public void NoChatId()
{
var args = GetNodeParameters(TempFile);
args.GetPluginSettingsJson = _ => """{"BotToken": "bot-token" }""";
args.RenderTemplate = template => template;
var element = new Communication.Telegram();
element.SendMessage = (botToken, chatId,message)
=> (true, "sent");
element.Message = "a message";
Assert.AreEqual(2, element.Execute(args));
}
}