This commit is contained in:
Martijn Scheepers
2024-07-24 14:05:31 +02:00
parent f8f573dbd0
commit 1f3555787b
11 changed files with 357 additions and 31 deletions

View File

@@ -7,7 +7,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0-preview-20200812-03" />
<PackageReference Include="bunit.core" Version="1.0.0-beta-10" />
<PackageReference Include="bunit.web" Version="1.0.0-beta-10" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0-preview-20200921-01" />
<PackageReference Include="Moq" Version="4.14.5" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />

View File

@@ -0,0 +1,145 @@
using Arrestanten_planbord.Data;
using Arrestanten_planbord.Data.Identity;
using Arrestanten_planbord.Pages.Components;
using BlazorBrowserStorage;
using Bunit;
using Bunit.TestDoubles.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Arrestanten_planbord.UnitTest
{
[TestClass]
public class BugreportTest
{
private static Mock<ISessionStorage> _sessionStorageMoq;
private static Mock<IConfiguration> _configurationMoq;
private static Mock<ILogger<CustomUserStore>> _customUserStoreLoggerMoq;
private static PasswordHasher<CustomIdentityUser> _PasswordHasher;
private static Mock<CustomUserStore> _customUserStoreMoq;
private static CustomUserStore _customUserStore;
private static readonly string TestFolder = "BugreportTest";
[ClassInitialize]
public static void MyClassInitialize(Microsoft.VisualStudio.TestTools.UnitTesting.TestContext testContext)
{
_sessionStorageMoq = new Mock<ISessionStorage>();
_sessionStorageMoq.Object.SetItem<string>("IPAddress", "11.22.33.44");
_sessionStorageMoq.Object.SetItem<string>("RequestParams", "A lot off request params");
_customUserStoreLoggerMoq = new Mock<ILogger<CustomUserStore>>();
_PasswordHasher = new PasswordHasher<CustomIdentityUser>();
_configurationMoq = new Mock<IConfiguration>();
var testAppsettings = new ConfigurationBuilder().AddJsonFile("appsettings.test.json").Build();
var DatastoreLocationSection = new Mock<IConfigurationSection>();
DatastoreLocationSection.Setup(a => a.Value).Returns(testAppsettings.GetValue<string>("UsersLocation") + "\\" + TestFolder);
_configurationMoq.Setup(a => a.GetSection("UsersLocation")).Returns(DatastoreLocationSection.Object);
string directory = _configurationMoq.Object.GetValue<string>("UsersLocation");
if (Directory.Exists(directory))
{
Directory.Delete(directory, true);
}
_customUserStoreMoq = new Mock<CustomUserStore>(_customUserStoreLoggerMoq.Object, _PasswordHasher, _configurationMoq.Object);
_customUserStore = new CustomUserStore(_customUserStoreLoggerMoq.Object, _PasswordHasher, _configurationMoq.Object);
}
[ClassCleanup]
public static void MyClassClassCleanup()
{
}
private CustomUserStore GetCustomUserStore()
{
return new CustomUserStore(_customUserStoreLoggerMoq.Object, _PasswordHasher, _configurationMoq.Object);
}
//[TestMethod]
//public void CounterShouldIncrementWhenSelected()
//{
// // Arrange
// using var ctx = new Bunit.TestContext();
// var cut = ctx.RenderComponent<Counter>();
// var paraElm = cut.Find("p");
// // Act
// cut.Find("button").Click();
// var paraElmText = paraElm.TextContent;
// // Assert
// paraElmText.MarkupMatches("Current count: 1");
//}
[TestMethod]
public void CreateBugReportIconSmall()
{
// Arrange
using var ctx = new Bunit.TestContext();
ctx.Services.AddBlazorBrowserStorage();
ctx.Services.AddScoped<BugReportService>();
//ctx.Services.AddDefaultIdentity<CustomIdentityUser>(options =>
//{
// options.SignIn.RequireConfirmedAccount = false;
// options.Password.RequireDigit = false;
// options.Password.RequiredLength = 6;
// options.Password.RequireNonAlphanumeric = false;
// options.Password.RequireUppercase = false;
// options.Password.RequireLowercase = false;
//})
// .AddUserStore<CustomUserStore>(_customUserStoreLoggerMoq.Object);
//ctx.Services.AddAuthorization(options =>
//{
// var claims = new ApplicationClaims();
// foreach (var item in claims.GetCustomClaims())
// {
// options.AddPolicy(item.Value, policy => policy.RequireClaim(item.Type, item.Value));
// }
//});
var authContext = ctx.Services.AddTestAuthorization();
authContext.SetAuthorized("Test");
// Act
var bugreport = ctx.RenderComponent<BugReport>(parameters => parameters
.Add<Boolean>(p => p.Small, true)
);
// Assert
//bugreport.MarkupMatches(@"<h1>Please log in!</h1><p>State: Not authorized</p>");
bugreport.MarkupMatches(@"<MatListItem OnClick=");
//bugreport.MarkupMatches(@"<MatListItem OnClick="@OpenDialog">Fouten/Suggesties</MatListItem>");
//bugreport.
}
}
}

View File

@@ -0,0 +1,166 @@
using Arrestanten_planbord.Data;
using Arrestanten_planbord.Pages.Admin;
using Bunit.TestDoubles.Authorization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Bunit;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Bunit.TestDoubles.JSInterop;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Arrestanten_planbord.HealthCheck;
namespace Arrestanten_planbord.UnitTest
{
[TestClass]
public class SystemOverviewTest
{
//private static Mock<ISessionStorage> _sessionStorageMoq;
private static Mock<IConfiguration> _configurationMoq;
private static Mock<HealthCheckService> _healthCheckServiceMoq;
private static Mock<IHealthCheck> _diskStorageHealthCheckMoq;
private static Mock<IHealthCheck> _memoryHealthCheckMoq;
private static Mock<IHealthCheck> _threadsHealthCheckMoq;
//private static Mock<ILogger<SystemInformation>> _systemInformationLoggerMoq;
//private static PasswordHasher<CustomIdentityUser> _PasswordHasher;
//private static Mock<CustomUserStore> _customUserStoreMoq;
//private static CustomUserStore _customUserStore;
//private static readonly string TestFolder = "BugreportTest";
[ClassInitialize]
public static void MyClassInitialize(Microsoft.VisualStudio.TestTools.UnitTesting.TestContext testContext)
{
//_sessionStorageMoq = new Mock<ISessionStorage>();
//_sessionStorageMoq.Object.SetItem<string>("IPAddress", "11.22.33.44");
//_sessionStorageMoq.Object.SetItem<string>("RequestParams", "A lot off request params");
//_systemInformationLoggerMoq = new Mock<ILogger<SystemInformation>>();
//_PasswordHasher = new PasswordHasher<CustomIdentityUser>();
_configurationMoq = new Mock<IConfiguration>();
_configurationMoq.Setup(a => a.GetValue<string>("DatastoreLocation")).Returns("This is the DataStoreLocation Moq");
_configurationMoq.Setup(a => a.GetValue<string>("UsersLocation")).Returns("This is the UsersLocation Moq");
_configurationMoq.Setup(a => a.GetValue<string>("BugReportLocation")).Returns("This is the BugReportLocation Moq");
_healthCheckServiceMoq = new Mock<HealthCheckService>();
//_diskStorageHealthCheckMoq = new Mock<IHealthCheck>(new DiskStorageHealthCheck(_configurationMoq.Object));
//_memoryHealthCheckMoq = new Mock<IHealthCheck>(new MemoryHealthCheck(1024,));
_threadsHealthCheckMoq = new Mock<IHealthCheck>(new ThreadsHealthCheck(100));
//var testAppsettings = new ConfigurationBuilder().AddJsonFile("appsettings.test.json").Build();
//var DatastoreLocationSection = new Mock<IConfigurationSection>();
//DatastoreLocationSection.Setup(a => a.Value).Returns(testAppsettings.GetValue<string>("UsersLocation") + "\\" + TestFolder);
//_configurationMoq.Setup(a => a.GetSection("UsersLocation")).Returns(DatastoreLocationSection.Object);
//string directory = _configurationMoq.Object.GetValue<string>("UsersLocation");
//if (Directory.Exists(directory))
//{
// Directory.Delete(directory, true);
//}
//_customUserStoreMoq = new Mock<CustomUserStore>(_customUserStoreLoggerMoq.Object, _PasswordHasher, _configurationMoq.Object);
//_customUserStore = new CustomUserStore(_customUserStoreLoggerMoq.Object, _PasswordHasher, _configurationMoq.Object);
}
[ClassCleanup]
public static void MyClassClassCleanup()
{
}
//private CustomUserStore GetCustomUserStore()
//{
// return new CustomUserStore(_customUserStoreLoggerMoq.Object, _PasswordHasher, _configurationMoq.Object);
//}
//[TestMethod]
//public void CounterShouldIncrementWhenSelected()
//{
// // Arrange
// using var ctx = new Bunit.TestContext();
// var cut = ctx.RenderComponent<Counter>();
// var paraElm = cut.Find("p");
// // Act
// cut.Find("button").Click();
// var paraElmText = paraElm.TextContent;
// // Assert
// paraElmText.MarkupMatches("Current count: 1");
//}
[TestMethod]
public void CreateBugReportIconSmall()
{
// Arrange
using var ctx = new Bunit.TestContext();
ctx.Services.AddLogging();
ctx.Services.AddScoped<SystemInformation>();
ctx.Services.AddSingleton<IConfiguration>(_configurationMoq.Object);
//ctx.Services.AddScoped<IHealthCheck>(_threadsHealthCheckMoq.Object);
//ctx.Services.AddMockJSRuntime();
//ctx.Services.AddDefaultIdentity<CustomIdentityUser>(options =>
//{
// options.SignIn.RequireConfirmedAccount = false;
// options.Password.RequireDigit = false;
// options.Password.RequiredLength = 6;
// options.Password.RequireNonAlphanumeric = false;
// options.Password.RequireUppercase = false;
// options.Password.RequireLowercase = false;
//})
// .AddUserStore<CustomUserStore>(_customUserStoreLoggerMoq.Object);
//ctx.Services.AddAuthorization(options =>
//{
// var claims = new ApplicationClaims();
// foreach (var item in claims.GetCustomClaims())
// {
// options.AddPolicy(item.Value, policy => policy.RequireClaim(item.Type, item.Value));
// }
//});
var authContext = ctx.Services.AddTestAuthorization();
authContext.SetAuthorized("Test");
// Act
var cut = ctx.RenderComponent<SystemOverview>();
//var bugreport = ctx.RenderComponent<SystemOverview>(parameters => parameters
//.Add<Boolean>(p => p.Small, true)
//);
// Assert
//bugreport.MarkupMatches(@"<h1>Please log in!</h1><p>State: Not authorized</p>");
cut.MarkupMatches(@"<MatListItem OnClick=");
//bugreport.MarkupMatches(@"<MatListItem OnClick="@OpenDialog">Fouten/Suggesties</MatListItem>");
//bugreport.
}
}
}

View File

@@ -52,7 +52,8 @@
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="5.0.0-rc.1.20451.7" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.0-rc.1.20453.2" />
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.4.0" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
</ItemGroup>
<ItemGroup>

View File

@@ -1,10 +1,6 @@
using Arrestanten_planbord.Models;
using Arrestanten_planbord.Pages.Components;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.IO;
using System.Linq;
@@ -12,7 +8,12 @@ using System.Threading.Tasks;
namespace Arrestanten_planbord.Data
{
public class BugReportService
public interface IBugReportService
{
}
public class BugReportService : IBugReportService
{
private readonly UserManager<CustomIdentityUser> _userManager;
private readonly ConfigFileHelper _configFileHelper;
@@ -27,7 +28,6 @@ namespace Arrestanten_planbord.Data
public async Task<ResultModel> StoreBugReportAsync(BugReportModel bugReport)
{
bugReport.CurrentIdentity = await _userManager.FindByNameAsync(bugReport.Name);
if (bugReport.CurrentIdentity == null)
{

View File

@@ -1,12 +1,9 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Arrestanten_planbord.Data
{
@@ -23,8 +20,8 @@ namespace Arrestanten_planbord.Data
private string GetLogFolder()
{
string config = _config.GetValue<string>("Serilog:WriteTo:1:Args:pathFormat");
string directory = config.Substring(0, config.LastIndexOf('\\'));
string config = _config.GetValue<string>("Serilog:WriteTo:Async:Args:configure:0:Args:path");
string directory = config.Substring(0, config.LastIndexOf('\\'));
if (Directory.Exists(directory) == false)
{
Directory.CreateDirectory(directory);
@@ -39,7 +36,7 @@ namespace Arrestanten_planbord.Data
public string ReadLogFile(string fileName)
{
string logFile = GetLogFolder() + fileName;
string logFile = GetLogFolder() + "\\" + fileName;
if (File.Exists(logFile))
{
using FileStream fs = new FileStream(logFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

View File

@@ -13,7 +13,12 @@ using System.Runtime.InteropServices;
namespace Arrestanten_planbord.Data
{
public class SystemInformation
public interface ISystemInformation
{
}
public class SystemInformation : ISystemInformation
{
private readonly ILogger _logger;
private readonly IConfiguration _config;
@@ -35,7 +40,7 @@ namespace Arrestanten_planbord.Data
public string LogLocation()
{
string config = _config.GetValue<string>("Serilog:WriteTo:1:Args:pathFormat");
string config = _config.GetValue<string>("Serilog:WriteTo:Async:Args:configure:0:Args:path");
return config.Substring(0, config.LastIndexOf('\\'));
}

View File

@@ -13,7 +13,7 @@
<MatCard>
<MatCardContent>
<MatSelectItem Items="@FileItems" TValue="string" ValueChanged="@ReadLogFile"></MatSelectItem>
<MatHeadline5>@FileName</MatHeadline5>
<MatHeadline1>@FileName</MatHeadline1>
<MatBody1>
<MatTextField @bind-Value="@FileContent" Label="@FileName" TextArea="true" FullWidth="true" rows="50"></MatTextField>
</MatBody1>

View File

@@ -2,10 +2,8 @@
@using Arrestanten_planbord.Models
@using System.Security.Claims
@using BlazorBrowserStorage;
@using Microsoft.AspNetCore.Http
@inject BugReportService bugReportService
@inject ISessionStorage sessionStorage
@inject IHttpContextAccessor httpContextAccessor
@if (Small)
{

View File

@@ -11,6 +11,7 @@ using Arrestanten_planbord.HealthCheck;
using BlazorBrowserStorage;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
namespace Arrestanten_planbord
{
@@ -63,7 +64,8 @@ namespace Arrestanten_planbord
services.AddHttpContextAccessor(); //useragent and ipaddress in bugreport
//TODO: .net 5 https://github.com/dotnet/aspnetcore/issues/25691
//TODO: .net 5 rc2 https://github.com/dotnet/aspnetcore/issues/25691
//https://gist.github.com/SteveSandersonMS/ba16f6bb6934842d78c89ab5314f4b56
services.AddBlazorBrowserStorage(); //Store useragent/ipaddress
services.AddRazorPages();

View File

@@ -38,18 +38,28 @@
"Threads": 55
},
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"MinimumLevel": "Information",
"WriteTo": [
{ "Name": "Console" },
{
"Name": "RollingFile",
"Args": {
"pathFormat": "C:\\Visual Studio Projecten\\Arrestanten_planbord\\Arrestanten_planbord\\DataStore\\Logs\\log-{Date}.txt",
"rollingInterval": "RollingInterval.Day",
"Shared": true
}
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Async" ],
"WriteTo:Async": {
"Name": "Async",
"Args": {
"configure": [
{
"Name": "File",
"Args": {
"path": "C:\\Visual Studio Projecten\\Arrestanten_planbord\\Arrestanten_planbord\\DataStore\\Logs\\log-.txt",
"rollingInterval": "Day",
"Shared": true,
"retainedFileCountLimit": 5,
"fileSizeLimitBytes": 10240,
"rollOnFileSizeLimit": true
}
},
{
"Name": "Console"
}
]
}
]
}
}
}