From 7a07bcdf382304b16b73b28f876ddcaa62e61af1 Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Sun, 28 Jul 2024 12:43:29 +1000 Subject: [PATCH] Code quality - Add at least one assertion to the test cases --- GmailAPIHelper.CORE.Tests/GmailTests.cs | 25 +++++++++++++++++-------- GmailAPIHelper.NET.Tests/GmailTests.cs | 19 +++++++++++++------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/GmailAPIHelper.CORE.Tests/GmailTests.cs b/GmailAPIHelper.CORE.Tests/GmailTests.cs index 4dee61d..3591935 100644 --- a/GmailAPIHelper.CORE.Tests/GmailTests.cs +++ b/GmailAPIHelper.CORE.Tests/GmailTests.cs @@ -5,6 +5,7 @@ using System.IO; using System.Linq; using System.Runtime.InteropServices; +using System.Text.RegularExpressions; namespace GmailAPIHelper.CORE.Tests { @@ -75,7 +76,8 @@ public void Test_GetGmailService_TokenPath_Home() destPath = Environment.GetEnvironmentVariable("HOME") + "/" + "token.json"; Directory.CreateDirectory(destPath); File.Copy(sourcePath, destPath + "/Google.Apis.Auth.OAuth2.Responses.TokenResponse-user", overwrite: true); - GmailHelper.GetGmailService(ApplicationName, GmailHelper.TokenPathType.HOME); + var service = GmailHelper.GetGmailService(ApplicationName, GmailHelper.TokenPathType.HOME); + Assert.IsTrue(service.GetType() == typeof(Google.Apis.Gmail.v1.GmailService)); Directory.Delete(destPath, recursive: true); } @@ -90,7 +92,8 @@ public void Test_GetGmailService_TokenPath_Custom() credPath = Environment.CurrentDirectory + "/" + "token.json"; else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) credPath = Environment.CurrentDirectory + "/" + "token.json"; - GmailHelper.GetGmailService(ApplicationName, GmailHelper.TokenPathType.CUSTOM, credPath); + var service = GmailHelper.GetGmailService(ApplicationName, GmailHelper.TokenPathType.CUSTOM, credPath); + Assert.IsTrue(service.GetType() == typeof(Google.Apis.Gmail.v1.GmailService)); } [TestMethod] @@ -365,6 +368,7 @@ public void Test_SendMessage_PlainText() else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) path = Environment.CurrentDirectory + "/TestFiles/PlainEmail.txt"; var body = File.ReadAllText(path); + Assert.IsFalse(Regex.IsMatch(body, "<(.|\n)*?>", RegexOptions.None, TimeSpan.FromMilliseconds(100))); GmailHelper.GetGmailService(ApplicationName) .SendMessage(GmailHelper.EmailContentType.PLAIN, TestEmailId, cc: TestEmailId, bcc: TestEmailId, subject: "EMAIL WITH PLAIN TEXT", body: body); } @@ -381,6 +385,7 @@ public void Test_SendMessage_HtmlText() else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) path = Environment.CurrentDirectory + "/TestFiles/HTMLEmail.txt"; var body = File.ReadAllText(path); + Assert.IsTrue(Regex.IsMatch(body, "<(.|\n)*?>", RegexOptions.None, TimeSpan.FromMilliseconds(100))); GmailHelper.GetGmailService(ApplicationName) .SendMessage(GmailHelper.EmailContentType.HTML, TestEmailId, cc: TestEmailId, bcc: TestEmailId, subject: "EMAIL WITH HTML TEXT", body: body); } @@ -463,6 +468,7 @@ public void Test_SendMessage_Attachments_PlainText() else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) path = Environment.CurrentDirectory + "/TestFiles/PlainEmail.txt"; var body = File.ReadAllText(path); + Assert.IsFalse(Regex.IsMatch(body, "<(.|\n)*?>", RegexOptions.None, TimeSpan.FromMilliseconds(100))); var attachmentPath = ""; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) attachmentPath = Environment.CurrentDirectory + "\\TestFiles\\Attachments\\"; @@ -499,6 +505,7 @@ public void Test_SendMessage_Attachments_HtmlText() else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) path = Environment.CurrentDirectory + "/TestFiles/HTMLEmail.txt"; var body = File.ReadAllText(path); + Assert.IsTrue(Regex.IsMatch(body, "<(.|\n)*?>", RegexOptions.None, TimeSpan.FromMilliseconds(100))); var attachmentPath = ""; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) attachmentPath = Environment.CurrentDirectory + "\\TestFiles\\Attachments\\"; @@ -1484,18 +1491,20 @@ public void Test_ListUserLabels() [TestCategory("TestCleanup")] public void Inbox_CleanUp() { - GmailHelper.GetGmailService(ApplicationName) + var mesagesMoved = 0; + mesagesMoved += GmailHelper.GetGmailService(ApplicationName) .MoveMessagesToTrash(query: $"[from:{TestEmailId}]in:inbox is:unread"); - GmailHelper.GetGmailService(ApplicationName) + mesagesMoved += GmailHelper.GetGmailService(ApplicationName) .MoveMessagesToTrash(query: $"[from:{TestEmailId}]in:spam is:unread"); - GmailHelper.GetGmailService(ApplicationName) + mesagesMoved += GmailHelper.GetGmailService(ApplicationName) .MoveMessagesToTrash(query: "[subject:'MARK DOTNETCORE MESSAGE AS READ']in:inbox is:read"); - GmailHelper.GetGmailService(ApplicationName) + mesagesMoved += GmailHelper.GetGmailService(ApplicationName) .MoveMessagesToTrash(query: "[subject:'MARK DOTNETFRAMEWORK MESSAGE AS READ']in:inbox is:read"); - GmailHelper.GetGmailService(ApplicationName) + mesagesMoved += GmailHelper.GetGmailService(ApplicationName) .MoveMessagesToTrash(query: "[subject:'MARK DOTNETCORE MESSAGES AS READ']in:inbox is:read"); - GmailHelper.GetGmailService(ApplicationName) + mesagesMoved += GmailHelper.GetGmailService(ApplicationName) .MoveMessagesToTrash(query: "[subject:'MARK DOTNETFRAMEWORK MESSAGES AS READ']in:inbox is:read"); + Assert.IsTrue(mesagesMoved >= 0); } } } \ No newline at end of file diff --git a/GmailAPIHelper.NET.Tests/GmailTests.cs b/GmailAPIHelper.NET.Tests/GmailTests.cs index 1c1ae1f..e081a90 100644 --- a/GmailAPIHelper.NET.Tests/GmailTests.cs +++ b/GmailAPIHelper.NET.Tests/GmailTests.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text.RegularExpressions; namespace GmailAPIHelper.NET.Tests { @@ -284,6 +285,7 @@ public void Test_GetMessagesAttachments_NoMatchingEmail() public void Test_SendMessage_PlainText() { var body = File.ReadAllText(Environment.CurrentDirectory + "\\TestFiles\\PlainEmail.txt"); + Assert.IsFalse(Regex.IsMatch(body, "<(.|\n)*?>", RegexOptions.None, TimeSpan.FromMilliseconds(100))); GmailHelper.GetGmailService(ApplicationName) .SendMessage(GmailHelper.EmailContentType.PLAIN, TestEmailId, cc: TestEmailId, bcc: TestEmailId, subject: "EMAIL WITH PLAIN TEXT", body: body); } @@ -293,6 +295,7 @@ public void Test_SendMessage_PlainText() public void Test_SendMessage_HtmlText() { var body = File.ReadAllText(Environment.CurrentDirectory + "\\TestFiles\\HTMLEmail.txt"); + Assert.IsTrue(Regex.IsMatch(body, "<(.|\n)*?>", RegexOptions.None, TimeSpan.FromMilliseconds(100))); GmailHelper.GetGmailService(ApplicationName) .SendMessage(GmailHelper.EmailContentType.HTML, TestEmailId, cc: TestEmailId, bcc: TestEmailId, subject: "EMAIL WITH HTML TEXT", body: body); } @@ -369,6 +372,7 @@ public void Test_SendMessage_Attachments_PlainText() { var path = Environment.CurrentDirectory + "\\TestFiles\\PlainEmail.txt"; var body = File.ReadAllText(path); + Assert.IsFalse(Regex.IsMatch(body, "<(.|\n)*?>", RegexOptions.None, TimeSpan.FromMilliseconds(100))); var attachmentPath = Environment.CurrentDirectory + "\\TestFiles\\Attachments\\"; var attachments = new List { @@ -393,6 +397,7 @@ public void Test_SendMessage_Attachments_HtmlText() { var path = Environment.CurrentDirectory + "\\TestFiles\\HTMLEmail.txt"; var body = File.ReadAllText(path); + Assert.IsTrue(Regex.IsMatch(body, "<(.|\n)*?>", RegexOptions.None, TimeSpan.FromMilliseconds(100))); var attachmentPath = Environment.CurrentDirectory + "\\TestFiles\\Attachments\\"; var attachments = new List { @@ -1261,18 +1266,20 @@ public void Test_ListUserLabels() [TestCategory("TestCleanup")] public void Inbox_CleanUp() { - GmailHelper.GetGmailService(ApplicationName) + var mesagesMoved = 0; + mesagesMoved += GmailHelper.GetGmailService(ApplicationName) .MoveMessagesToTrash(query: $"[from:{TestEmailId}]in:inbox is:unread"); - GmailHelper.GetGmailService(ApplicationName) + mesagesMoved += GmailHelper.GetGmailService(ApplicationName) .MoveMessagesToTrash(query: $"[from:{TestEmailId}]in:spam is:unread"); - GmailHelper.GetGmailService(ApplicationName) + mesagesMoved += GmailHelper.GetGmailService(ApplicationName) .MoveMessagesToTrash(query: "[subject:'MARK DOTNETCORE MESSAGE AS READ']in:inbox is:read"); - GmailHelper.GetGmailService(ApplicationName) + mesagesMoved += GmailHelper.GetGmailService(ApplicationName) .MoveMessagesToTrash(query: "[subject:'MARK DOTNETFRAMEWORK MESSAGE AS READ']in:inbox is:read"); - GmailHelper.GetGmailService(ApplicationName) + mesagesMoved += GmailHelper.GetGmailService(ApplicationName) .MoveMessagesToTrash(query: "[subject:'MARK DOTNETCORE MESSAGES AS READ']in:inbox is:read"); - GmailHelper.GetGmailService(ApplicationName) + mesagesMoved += GmailHelper.GetGmailService(ApplicationName) .MoveMessagesToTrash(query: "[subject:'MARK DOTNETFRAMEWORK MESSAGES AS READ']in:inbox is:read"); + Assert.IsTrue(mesagesMoved >= 0); } } } \ No newline at end of file