diff --git a/PlexRequests.Core.Tests/AuthenticationSettingsTests.cs b/PlexRequests.Core.Tests/AuthenticationSettingsTests.cs index fd7987131..62b03470c 100644 --- a/PlexRequests.Core.Tests/AuthenticationSettingsTests.cs +++ b/PlexRequests.Core.Tests/AuthenticationSettingsTests.cs @@ -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 body, Dictionary 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 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.NewRequest, "There has been a new request from {Username}, Title: {Title} for {Type}" } }, - new Dictionary{{"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.NewRequest, string.Empty } }, - new Dictionary()) - .Returns(string.Empty) - .SetName("Empty Message"); - - yield return new TestCaseData( - new Dictionary { { NotificationType.NewRequest, "{{Wowwzer}} Damn}{{son}}}}" } }, - new Dictionary { {"son","HEY!"} }) - .Returns("{{Wowwzer}} Damn}{HEY!}}}") - .SetName("Multiple Curlys"); - - - yield return new TestCaseData( - new Dictionary { { NotificationType.NewRequest, "This is a message with no curlys" } }, - new Dictionary { { "son", "HEY!" } }) - .Returns("This is a message with no curlys") - .SetName("No Curlys"); - - yield return new TestCaseData( - new Dictionary { { NotificationType.NewRequest, new string(')', 5000)} }, - new Dictionary { { "son", "HEY!" } }) - .Returns(new string(')', 5000)) - .SetName("Long String"); - - - yield return new TestCaseData( - new Dictionary { { NotificationType.NewRequest, "This is a {Username} and {Username} Because {Curly}{Curly}" } }, - new Dictionary { { "Username", "HEY!" }, {"Curly","Bob"} }) - .Returns("This is a HEY! and HEY! Because BobBob") - .SetName("Double Curly"); - - yield return new TestCaseData( - new Dictionary { { NotificationType.NewRequest, "This is a {Username} and {Username} Because {Curly}{Curly}" } }, - new Dictionary { { "username", "HEY!" }, { "Curly", "Bob" } }) - .Returns("This is a {Username} and {Username} Because BobBob") - .SetName("Case sensitive"); - - yield return new TestCaseData( - new Dictionary { { 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 { { "a", "b" } }) - .Returns("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") - .SetName("Lots of curlys"); - - yield return new TestCaseData( - new Dictionary { { NotificationType.NewRequest, $"{{{new string('b', 10000)}}}" } }, - new Dictionary { { 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"} }, + }; } } \ No newline at end of file diff --git a/PlexRequests.Core.Tests/NotificationMessageResolverTests.cs b/PlexRequests.Core.Tests/NotificationMessageResolverTests.cs index 62b03470c..fc52768e0 100644 --- a/PlexRequests.Core.Tests/NotificationMessageResolverTests.cs +++ b/PlexRequests.Core.Tests/NotificationMessageResolverTests.cs @@ -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.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.NewRequest, new NotificationMessageContent {Subject = subject} } } + }; + + var result = n.ParseMessage(s, NotificationType.NewRequest, param); + return result.Subject; } - static readonly object[] UserData = + private static IEnumerable 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 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"); + } + } } } \ No newline at end of file diff --git a/PlexRequests.Core.Tests/PlexRequests.Core.Tests.csproj b/PlexRequests.Core.Tests/PlexRequests.Core.Tests.csproj index 3a456e243..aba30f7ce 100644 --- a/PlexRequests.Core.Tests/PlexRequests.Core.Tests.csproj +++ b/PlexRequests.Core.Tests/PlexRequests.Core.Tests.csproj @@ -59,9 +59,9 @@ - - + + diff --git a/PlexRequests.Core/NotificationMessageResolution.cs b/PlexRequests.Core/NotificationMessageContent.cs similarity index 94% rename from PlexRequests.Core/NotificationMessageResolution.cs rename to PlexRequests.Core/NotificationMessageContent.cs index 70979bc60..3a89eda6c 100644 --- a/PlexRequests.Core/NotificationMessageResolution.cs +++ b/PlexRequests.Core/NotificationMessageContent.cs @@ -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; } diff --git a/PlexRequests.Core/NotificationMessageCurlys.cs b/PlexRequests.Core/NotificationMessageCurlys.cs new file mode 100644 index 000000000..ea10494a4 --- /dev/null +++ b/PlexRequests.Core/NotificationMessageCurlys.cs @@ -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 Curlys => new Dictionary + { + {nameof(Username), Username }, + {nameof(Title), Title }, + {nameof(Date), Date }, + {nameof(Type), Type }, + {nameof(Issue), Issue } + }; + } +} \ No newline at end of file diff --git a/PlexRequests.Core/NotificationMessageResolver.cs b/PlexRequests.Core/NotificationMessageResolver.cs index 1178ec607..8d0734cd1 100644 --- a/PlexRequests.Core/NotificationMessageResolver.cs +++ b/PlexRequests.Core/NotificationMessageResolver.cs @@ -36,17 +36,17 @@ namespace PlexRequests.Core { private const char StartChar = (char)123; private const char EndChar = (char)125; - public NotificationMessageResolution ParseMessage(T notification, NotificationType type) where T : NotificationSettings + public NotificationMessageContent ParseMessage(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 paramaters) + private NotificationMessageContent Resolve(string body, string subject, Dictionary 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 FindCurlyFields(string message) { + if (string.IsNullOrEmpty(message)) + { + return new List(); + } var insideCurly = false; var fields = new List(); var currentWord = string.Empty; diff --git a/PlexRequests.Core/PlexRequests.Core.csproj b/PlexRequests.Core/PlexRequests.Core.csproj index 2ff688521..15424b811 100644 --- a/PlexRequests.Core/PlexRequests.Core.csproj +++ b/PlexRequests.Core/PlexRequests.Core.csproj @@ -71,7 +71,8 @@ - + + diff --git a/PlexRequests.Core/SettingModels/NotificationSettings.cs b/PlexRequests.Core/SettingModels/NotificationSettings.cs index 9eae41072..88b0f7081 100644 --- a/PlexRequests.Core/SettingModels/NotificationSettings.cs +++ b/PlexRequests.Core/SettingModels/NotificationSettings.cs @@ -34,18 +34,28 @@ namespace PlexRequests.Core.SettingModels { public NotificationSettings() { - CustomParamaters = new Dictionary + Message = new Dictionary { - {"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 Message { get; set; } - public Dictionary Subject { get; set; } - public Dictionary CustomParamaters { get; set; } + public Dictionary Message { get; set; } + } + + public static class NotificationCurly + { + public static readonly List Curlys = new List + { + "Username", + "Title", + "Date", + "Issue", + "Type" + }; } } \ No newline at end of file diff --git a/PlexRequests.Services/Notification/EmailMessageNotification.cs b/PlexRequests.Services/Notification/EmailMessageNotification.cs index e67e42e5f..dc30815c8 100644 --- a/PlexRequests.Services/Notification/EmailMessageNotification.cs +++ b/PlexRequests.Services/Notification/EmailMessageNotification.cs @@ -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")}" },