using System; using BasicDotnetTemplate.MainProject.Models.Settings; using Microsoft.AspNetCore.Builder; using BasicDotnetTemplate.MainProject.Utils; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Configuration; namespace BasicDotnetTemplate.MainProject.Tests; [TestClass] public class CryptoUtils_Tests { [TestMethod] public void Decrypt_Success() { try { string encryptedData = "d2ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A="; WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty()); AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData"); CryptUtils cryptoUtils = new CryptUtils(appSettings); var decryptedData = cryptoUtils.Decrypt(encryptedData); var isEqual = decryptedData == "ThisIsASuccessfullTest"; Assert.IsTrue(isEqual); } catch (Exception ex) { Console.WriteLine(ex.InnerException); Assert.Fail($"An exception was thrown: {ex.Message}"); } } [TestMethod] public void Decrypt_Error() { try { string encryptedData = "d1ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A="; WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty()); AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData"); CryptUtils cryptoUtils = new CryptUtils(appSettings); var decryptedData = cryptoUtils.Decrypt(encryptedData); var isEqual = decryptedData == "ThisIsASuccessfullTest"; Assert.IsFalse(isEqual); } catch (Exception ex) { Console.WriteLine(ex.InnerException); Assert.Fail($"An exception was thrown: {ex.Message}"); } } [TestMethod] public void Decrypt_ArgumentException() { try { string encryptedData = "d1ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A="; WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty()); AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData", "invalidCryptAppsettings.json"); CryptUtils cryptoUtils = new CryptUtils(appSettings); try { var decryptedData = cryptoUtils.Decrypt(encryptedData); Assert.Fail($"Expected exception instead of response: {decryptedData}"); } catch (ArgumentException argumentException) { Assert.IsInstanceOfType(argumentException, typeof(ArgumentException)); } catch (Exception exception) { Assert.IsInstanceOfType(exception, typeof(ArgumentException)); } } catch (Exception ex) { Console.WriteLine(ex.InnerException); Assert.Fail($"An exception was thrown: {ex.Message}"); } } [TestMethod] public void Decrypt_Empty() { try { string encryptedData = "WGHWRBE5mVWaV="; WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty()); AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData"); CryptUtils cryptoUtils = new CryptUtils(appSettings); var decryptedData = cryptoUtils.Decrypt(encryptedData); var isEqual = decryptedData == String.Empty; Assert.IsTrue(isEqual); } catch (Exception ex) { Console.WriteLine(ex.InnerException); Assert.Fail($"An exception was thrown: {ex.Message}"); } } }