Fixed the way we will be using custom messages

pull/470/head
tidusjar 8 years ago
parent 8953d4eaa8
commit fea4dd6309

@ -24,94 +24,36 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using NUnit.Framework;
using PlexRequests.Core.Models;
using PlexRequests.Core.SettingModels;
namespace PlexRequests.Core.Tests
{
[TestFixture]
public class NotificationMessageResolverTests
public class AuthenticationSettingsTests
{
[TestCaseSource(nameof(MessageResolver))]
[Ignore("WIP")]
public NotificationMessageResolution Resolve(Dictionary<NotificationType, string> body, Dictionary<string,string> param)
[Test, TestCaseSource(nameof(UserData))]
public void DeniedUserListTest(string users, string[] expected)
{
var n = new NotificationMessageResolver();
var s = new NotificationSettings
{
Message = body,
CustomParamaters = param
};
var model = new AuthenticationSettings { DeniedUsers = users };
return n.ParseMessage(s, NotificationType.NewRequest);
}
var result = model.DeniedUserList;
private static IEnumerable<TestCaseData> MessageResolver
{
get
Assert.That(result.Count, Is.EqualTo(expected.Length));
for (var i = 0; i < expected.Length; i++)
{
yield return new TestCaseData(
new Dictionary<NotificationType, string> { { NotificationType.NewRequest, "There has been a new request from {Username}, Title: {Title} for {Type}" } },
new Dictionary<string, string>{{"Username", "Jamie" },{"Title", "Finding Dory" },{"Type", "Movie" }})
.Returns("There has been a new request from Jamie, Title: Finding Dory for Movie")
.SetName("FindingDory");
yield return new TestCaseData(
new Dictionary<NotificationType, string> { { NotificationType.NewRequest, string.Empty } },
new Dictionary<string, string>())
.Returns(string.Empty)
.SetName("Empty Message");
yield return new TestCaseData(
new Dictionary<NotificationType, string> { { NotificationType.NewRequest, "{{Wowwzer}} Damn}{{son}}}}" } },
new Dictionary<string, string> { {"son","HEY!"} })
.Returns("{{Wowwzer}} Damn}{HEY!}}}")
.SetName("Multiple Curlys");
yield return new TestCaseData(
new Dictionary<NotificationType, string> { { NotificationType.NewRequest, "This is a message with no curlys" } },
new Dictionary<string, string> { { "son", "HEY!" } })
.Returns("This is a message with no curlys")
.SetName("No Curlys");
yield return new TestCaseData(
new Dictionary<NotificationType, string> { { NotificationType.NewRequest, new string(')', 5000)} },
new Dictionary<string, string> { { "son", "HEY!" } })
.Returns(new string(')', 5000))
.SetName("Long String");
yield return new TestCaseData(
new Dictionary<NotificationType, string> { { NotificationType.NewRequest, "This is a {Username} and {Username} Because {Curly}{Curly}" } },
new Dictionary<string, string> { { "Username", "HEY!" }, {"Curly","Bob"} })
.Returns("This is a HEY! and HEY! Because BobBob")
.SetName("Double Curly");
yield return new TestCaseData(
new Dictionary<NotificationType, string> { { NotificationType.NewRequest, "This is a {Username} and {Username} Because {Curly}{Curly}" } },
new Dictionary<string, string> { { "username", "HEY!" }, { "Curly", "Bob" } })
.Returns("This is a {Username} and {Username} Because BobBob")
.SetName("Case sensitive");
yield return new TestCaseData(
new Dictionary<NotificationType, string> { { NotificationType.NewRequest, "{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}" } },
new Dictionary<string, string> { { "a", "b" } })
.Returns("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
.SetName("Lots of curlys");
yield return new TestCaseData(
new Dictionary<NotificationType, string> { { NotificationType.NewRequest, $"{{{new string('b', 10000)}}}" } },
new Dictionary<string, string> { { new string('b', 10000), "Hello" } })
.Returns("Hello")
.SetName("Very long Curly");
Assert.That(result[i], Is.EqualTo(expected[i]));
}
}
static readonly object[] UserData =
{
new object[] { "john", new [] {"john"} },
new object[] { "john , abc ,", new [] {"john", "abc"} },
new object[] { "john,, cde", new [] {"john", "cde"} },
new object[] { "john,,, aaa , baaa , ", new [] {"john","aaa","baaa"} },
new object[] { "john, aaa , baaa , maaa, caaa", new [] {"john","aaa","baaa", "maaa", "caaa"} },
};
}
}

@ -24,36 +24,123 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using NUnit.Framework;
using PlexRequests.Core.Models;
using PlexRequests.Core.SettingModels;
namespace PlexRequests.Core.Tests
{
[TestFixture]
public class AuthenticationSettingsTests
public class NotificationMessageResolverTests
{
[Test, TestCaseSource(nameof(UserData))]
public void DeniedUserListTest(string users, string[] expected)
[TestCaseSource(nameof(MessageBodyResolver))]
public string ResolveBody(string body, NotificationMessageCurlys param)
{
var model = new AuthenticationSettings { DeniedUsers = users };
var n = new NotificationMessageResolver();
var s = new NotificationSettings
{
Message = new Dictionary<NotificationType, NotificationMessageContent> { { NotificationType.NewRequest, new NotificationMessageContent { Body = body } } }
};
var result = model.DeniedUserList;
var result = n.ParseMessage(s, NotificationType.NewRequest, param);
return result.Body;
}
Assert.That(result.Count, Is.EqualTo(expected.Length));
for (var i = 0; i < expected.Length; i++)
[TestCaseSource(nameof(MessageSubjectResolver))]
public string ResolveSubject(string subject, NotificationMessageCurlys param)
{
var n = new NotificationMessageResolver();
var s = new NotificationSettings
{
Assert.That(result[i], Is.EqualTo(expected[i]));
}
Message = new Dictionary<NotificationType, NotificationMessageContent> { { NotificationType.NewRequest, new NotificationMessageContent {Subject = subject} } }
};
var result = n.ParseMessage(s, NotificationType.NewRequest, param);
return result.Subject;
}
static readonly object[] UserData =
private static IEnumerable<TestCaseData> MessageSubjectResolver
{
get
{
yield return new TestCaseData(
"{Username} has requested a {Type}",
new NotificationMessageCurlys("Jamie", "Finding Dory", DateTime.Now.ToString(), "Movie", string.Empty))
.Returns("Jamie has requested a Movie").SetName("Subject Curlys");
yield return new TestCaseData(
null,
new NotificationMessageCurlys("Jamie", "Finding Dory", DateTime.Now.ToString(), "Movie", string.Empty))
.Returns(string.Empty).SetName("Empty Subject");
yield return new TestCaseData(
"New Request Incoming!",
new NotificationMessageCurlys("Jamie", "Finding Dory", DateTime.Now.ToString(), "Movie", string.Empty))
.Returns("New Request Incoming!").SetName("No curlys");
yield return new TestCaseData(
"%$R£%$£^%$&{Username}@{}:§",
new NotificationMessageCurlys("Jamie", "Finding Dory", DateTime.Now.ToString(), "Movie", string.Empty))
.Returns("%$R£%$£^%$&Jamie@{}:§").SetName("Special Chars");
}
}
private static IEnumerable<TestCaseData> MessageBodyResolver
{
new object[] { "john", new [] {"john"} },
new object[] { "john , abc ,", new [] {"john", "abc"} },
new object[] { "john,, cde", new [] {"john", "cde"} },
new object[] { "john,,, aaa , baaa , ", new [] {"john","aaa","baaa"} },
new object[] { "john, aaa , baaa , maaa, caaa", new [] {"john","aaa","baaa", "maaa", "caaa"} },
};
get
{
yield return new TestCaseData(
"There has been a new request from {Username}, Title: {Title} for {Type}",
new NotificationMessageCurlys("Jamie", "Finding Dory", DateTime.Now.ToString(), "Movie", string.Empty))
.Returns("There has been a new request from Jamie, Title: Finding Dory for Movie").SetName("FindingDory");
yield return new TestCaseData(
null,
new NotificationMessageCurlys(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty))
.Returns(string.Empty)
.SetName("Empty Message");
yield return new TestCaseData(
"{{Wowwzer}} Damn}{{Username}}}}",
new NotificationMessageCurlys("HEY!", string.Empty, string.Empty, string.Empty, string.Empty))
.Returns("{{Wowwzer}} Damn}{HEY!}}}")
.SetName("Multiple Curlys");
yield return new TestCaseData(
"This is a message with no curlys",
new NotificationMessageCurlys("Jamie", "Finding Dory", DateTime.Now.ToString(), "Movie", string.Empty))
.Returns("This is a message with no curlys")
.SetName("No Curlys");
yield return new TestCaseData(
new string(')', 5000),
new NotificationMessageCurlys(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty))
.Returns(new string(')', 5000))
.SetName("Long String");
yield return new TestCaseData(
"This is a {Username} and {Username} Because {Issue}{Issue}",
new NotificationMessageCurlys("HEY!", string.Empty, string.Empty, string.Empty, "Bob"))
.Returns("This is a HEY! and HEY! Because BobBob")
.SetName("Double Curly");
yield return new TestCaseData(
"This is a {username} and {username} Because {Issue}{Issue}",
new NotificationMessageCurlys("HEY!", string.Empty, string.Empty, string.Empty, "Bob"))
.Returns("This is a {username} and {username} Because BobBob")
.SetName("Case sensitive");
yield return new TestCaseData(
"{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}{Date}",
new NotificationMessageCurlys("HEY!", string.Empty, "b", string.Empty, "Bob"))
.Returns("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
.SetName("Lots of curlys");
}
}
}
}

@ -59,9 +59,9 @@
<Otherwise />
</Choose>
<ItemGroup>
<Compile Include="NotificationMessageResolverTests.cs" />
<Compile Include="StatusCheckerTests.cs" />
<Compile Include="AuthenticationSettingsTests.cs" />
<Compile Include="StatusCheckerTests.cs" />
<Compile Include="NotificationMessageResolverTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>

@ -1,7 +1,7 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: NotificationMessageResolution.cs
// File: NotificationMessageContent.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
@ -26,7 +26,7 @@
#endregion
namespace PlexRequests.Core
{
public class NotificationMessageResolution
public class NotificationMessageContent
{
public string Subject { get; set; }
public string Body { get; set; }

@ -0,0 +1,57 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: NotificationMessageCurlys.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System;
using System.Collections.Generic;
namespace PlexRequests.Core
{
public class NotificationMessageCurlys
{
public NotificationMessageCurlys(string username, string title, string dateTime, string type, string issue)
{
Username = username;
Title = title;
Date = dateTime;
Type = type;
Issue = issue;
}
private string Username { get; }
private string Title { get; }
private string Date { get; }
private string Type { get; }
private string Issue { get; }
public Dictionary<string, string> Curlys => new Dictionary<string, string>
{
{nameof(Username), Username },
{nameof(Title), Title },
{nameof(Date), Date },
{nameof(Type), Type },
{nameof(Issue), Issue }
};
}
}

@ -36,17 +36,17 @@ namespace PlexRequests.Core
{
private const char StartChar = (char)123;
private const char EndChar = (char)125;
public NotificationMessageResolution ParseMessage<T>(T notification, NotificationType type) where T : NotificationSettings
public NotificationMessageContent ParseMessage<T>(T notification, NotificationType type, NotificationMessageCurlys c) where T : NotificationSettings
{
var bodyToParse = notification.Message.FirstOrDefault(x => x.Key == type).Value;
var subjectToParse = notification.Subject.FirstOrDefault(x => x.Key == type).Value;
var content = notification.Message.FirstOrDefault(x => x.Key == type).Value;
//if (string.IsNullOrEmpty(notificationToParse))
// return string.Empty;
return Resolve(bodyToParse, subjectToParse, notification.CustomParamaters);
return Resolve(content.Body, content.Subject, c.Curlys);
}
private NotificationMessageResolution Resolve(string body, string subject, Dictionary<string, string> paramaters)
private NotificationMessageContent Resolve(string body, string subject, Dictionary<string, string> paramaters)
{
var bodyFields = FindCurlyFields(body);
@ -70,11 +70,15 @@ namespace PlexRequests.Core
}
}
return new NotificationMessageResolution { Body = body, Subject = subject };
return new NotificationMessageContent { Body = body ?? string.Empty, Subject = subject ?? string.Empty };
}
private IEnumerable<string> FindCurlyFields(string message)
{
if (string.IsNullOrEmpty(message))
{
return new List<string>();
}
var insideCurly = false;
var fields = new List<string>();
var currentWord = string.Empty;

@ -71,7 +71,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CacheKeys.cs" />
<Compile Include="NotificationMessageResolution.cs" />
<Compile Include="NotificationMessageContent.cs" />
<Compile Include="NotificationMessageCurlys.cs" />
<Compile Include="NotificationMessageResolver.cs" />
<Compile Include="IIssueService.cs" />
<Compile Include="IRequestService.cs" />

@ -34,18 +34,28 @@ namespace PlexRequests.Core.SettingModels
{
public NotificationSettings()
{
CustomParamaters = new Dictionary<string, string>
Message = new Dictionary<NotificationType, NotificationMessageContent>
{
{"Username", string.Empty },
{"Date", string.Empty },
{"Title", string.Empty },
{"RequestType", string.Empty },
{"Issue", string.Empty },
{NotificationType.NewRequest, new NotificationMessageContent() },
{NotificationType.Issue, new NotificationMessageContent() },
{NotificationType.AdminNote, new NotificationMessageContent() },
{NotificationType.RequestApproved, new NotificationMessageContent() },
{NotificationType.RequestAvailable, new NotificationMessageContent() },
{NotificationType.Test, new NotificationMessageContent() },
};
}
public Dictionary<NotificationType, string> Message { get; set; }
public Dictionary<NotificationType, string> Subject { get; set; }
public Dictionary<string,string> CustomParamaters { get; set; }
public Dictionary<NotificationType, NotificationMessageContent> Message { get; set; }
}
public static class NotificationCurly
{
public static readonly List<string> Curlys = new List<string>
{
"Username",
"Title",
"Date",
"Issue",
"Type"
};
}
}

@ -25,6 +25,9 @@
// ************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using MailKit.Security;
@ -119,6 +122,10 @@ namespace PlexRequests.Services.Notification
private async Task EmailNewRequest(NotificationModel model, EmailNotificationSettings settings)
{
//var r = new NotificationMessageCurlys(model.User, model.Title, DateTime.Now.ToString(), model.RequestType.ToString(), string.Empty);
//var resolver = new NotificationMessageResolver();
//var bodyResult = resolver.ParseMessage(settings, NotificationType.NewRequest, r);
var message = new MimeMessage
{
Body = new TextPart("plain") { Text = $"Hello! The user '{model.User}' has requested the {model.RequestType.GetString()?.ToLower()} '{model.Title}'! Please log in to approve this request. Request Date: {model.DateTime.ToString("f")}" },

Loading…
Cancel
Save