Skip to content

Commit

Permalink
#115 test: added mock web project to test os agnostic stuff
Browse files Browse the repository at this point in the history
+ This makes possible to completely mock the output of linux commands for e2e tests
  • Loading branch information
joseantmazonsb committed Feb 27, 2022
1 parent 65797d1 commit 509b2a8
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Linguard/Core.Test/Mocks/DefaultConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Core.Test.Mocks;

public class DefaultConfiguration : Mock<IConfiguration> {
public sealed class DefaultConfiguration : Mock<IConfiguration> {

public DefaultConfiguration() {
SetupProperty(c => c.Wireguard, new Mock<IWireguardConfiguration>().
Expand All @@ -17,5 +17,6 @@ public DefaultConfiguration() {
.SetupProperty(c => c.StorageDriver, new Mock<ITrafficStorageDriver>().Object)
.Object);
SetupProperty(c => c.Web, new Mock<IWebConfiguration>().Object);
Setup(o => o.Clone()).Returns(Object);
}
}
6 changes: 6 additions & 0 deletions Linguard/Linguard.sln
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core.Test", "Core.Test\Core
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Web", "Web\Web.csproj", "{5447985B-643D-41BB-A6AC-786A3AAD87EA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebMock", "WebMock\WebMock.csproj", "{F5C5CEA6-068A-4E40-B889-04F52101A996}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -48,5 +50,9 @@ Global
{5447985B-643D-41BB-A6AC-786A3AAD87EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5447985B-643D-41BB-A6AC-786A3AAD87EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5447985B-643D-41BB-A6AC-786A3AAD87EA}.Release|Any CPU.Build.0 = Release|Any CPU
{F5C5CEA6-068A-4E40-B889-04F52101A996}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F5C5CEA6-068A-4E40-B889-04F52101A996}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F5C5CEA6-068A-4E40-B889-04F52101A996}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F5C5CEA6-068A-4E40-B889-04F52101A996}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
2 changes: 0 additions & 2 deletions Linguard/Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Net;
using FluentValidation;
using Linguard.Core.Configuration;
using Linguard.Core.Configuration.Serialization;
Expand All @@ -9,7 +8,6 @@
using Linguard.Core.Services;
using Linguard.Log;
using Linguard.Web.Services;
using Microsoft.AspNetCore.Diagnostics;
using QRCoder;
using Radzen;
using IConfiguration = Linguard.Core.Configuration.IConfiguration;
Expand Down
14 changes: 14 additions & 0 deletions Linguard/WebMock/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@using Linguard.Web.Shared

<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"/>
<FocusOnNavigate RouteData="@routeData" Selector="h1"/>
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Oops, it looks like there's nothing here.</p>
</LayoutView>
</NotFound>
</Router>
11 changes: 11 additions & 0 deletions Linguard/WebMock/CommandRunnerMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Linguard.Core.OS;
using Moq;

namespace WebMock;

public class CommandRunnerMock : Mock<ICommandRunner> {
public CommandRunnerMock() {
Setup(o => o.Run(It.IsAny<string>()))
.Returns(new CommandResult(string.Empty, string.Empty, true));
}
}
11 changes: 11 additions & 0 deletions Linguard/WebMock/LifetimeServiceMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Linguard.Core.Managers;
using Linguard.Web.Services;
using Moq;

namespace WebMock;

public class LifetimeServiceMock : Mock<ILifetimeService> {
public LifetimeServiceMock(IConfigurationManager manager) {
Setup(o => o.OnAppStarted()).Callback(manager.LoadDefaults);
}
}
79 changes: 79 additions & 0 deletions Linguard/WebMock/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Core.Test.Mocks;
using FluentValidation;
using Linguard.Core.Models.Wireguard;
using Linguard.Core.Models.Wireguard.Validators;
using Linguard.Core.Services;
using Linguard.Core.Utils;
using Linguard.Log;
using Linguard.Web.Services;
using QRCoder;
using Radzen;
using WebMock;
using ILogger = Linguard.Log.ILogger;

var root = Path.Combine(new DirectoryInfo(Directory.GetCurrentDirectory()).Parent!.FullName, "Web",
"wwwroot");
var builder = WebApplication.CreateBuilder(new WebApplicationOptions {
WebRootPath = root,
Args = args,
ApplicationName = AssemblyInfo.Product
});

//var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();

var manager = new DefaultConfigurationManager().Object;
builder.Services.AddSingleton(manager);
builder.Services.AddTransient<ILogger, NLogLogger>();
builder.Services.AddSingleton(new CommandRunnerMock().Object);
builder.Services.AddTransient<IWireguardService, WireguardService>();
builder.Services.AddTransient<IInterfaceGenerator, DefaultInterfaceGenerator>();
builder.Services.AddTransient<IClientGenerator, DefaultClientGenerator>();
builder.Services.AddTransient<AbstractValidator<Interface>, InterfaceValidator>();
builder.Services.AddTransient<AbstractValidator<Client>, ClientValidator>();

builder.Services.AddTransient<IWebService, WebService>();
builder.Services.AddTransient<QRCodeGenerator, QRCodeGenerator>();
builder.Services.AddSingleton(new LifetimeServiceMock(manager).Object);

builder.Services.AddScoped<DialogService>();
builder.Services.AddScoped<NotificationService>();
builder.Services.AddScoped<TooltipService>();
builder.Services.AddScoped<ContextMenuService>();

var app = builder.Build();

app.Lifetime.ApplicationStarted.Register(() => {
app.Services.GetService<ILifetimeService>()?.OnAppStarted();
});

app.Lifetime.ApplicationStopping.Register(() => {
app.Services.GetService<ILifetimeService>()?.OnAppStopping();
});

app.Lifetime.ApplicationStopped.Register(() => {
app.Services.GetService<ILifetimeService>()?.OnAppStopped();
});

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
else {
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();
28 changes: 28 additions & 0 deletions Linguard/WebMock/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:37000",
"sslPort": 44355
}
},
"profiles": {
"WebMock": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7167;http://localhost:5167",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
43 changes: 43 additions & 0 deletions Linguard/WebMock/WebMock.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<_ContentIncludedByDefault Remove="Pages\Error.cshtml" />
<_ContentIncludedByDefault Remove="Pages\_Host.cshtml" />
<_ContentIncludedByDefault Remove="Pages\_Layout.cshtml" />
<_ContentIncludedByDefault Remove="Pages\Counter.razor" />
<_ContentIncludedByDefault Remove="Pages\FetchData.razor" />
<_ContentIncludedByDefault Remove="Pages\Index.razor" />
<_ContentIncludedByDefault Remove="Shared\MainLayout.razor" />
<_ContentIncludedByDefault Remove="Shared\NavMenu.razor" />
<_ContentIncludedByDefault Remove="Shared\SurveyPrompt.razor" />
<_ContentIncludedByDefault Remove="wwwroot\css\bootstrap\bootstrap.min.css" />
<_ContentIncludedByDefault Remove="wwwroot\css\bootstrap\bootstrap.min.css.map" />
<_ContentIncludedByDefault Remove="wwwroot\css\open-iconic\FONT-LICENSE" />
<_ContentIncludedByDefault Remove="wwwroot\css\open-iconic\font\css\open-iconic-bootstrap.min.css" />
<_ContentIncludedByDefault Remove="wwwroot\css\open-iconic\font\fonts\open-iconic.eot" />
<_ContentIncludedByDefault Remove="wwwroot\css\open-iconic\font\fonts\open-iconic.otf" />
<_ContentIncludedByDefault Remove="wwwroot\css\open-iconic\font\fonts\open-iconic.svg" />
<_ContentIncludedByDefault Remove="wwwroot\css\open-iconic\font\fonts\open-iconic.ttf" />
<_ContentIncludedByDefault Remove="wwwroot\css\open-iconic\font\fonts\open-iconic.woff" />
<_ContentIncludedByDefault Remove="wwwroot\css\open-iconic\ICON-LICENSE" />
<_ContentIncludedByDefault Remove="wwwroot\css\open-iconic\README.md" />
<_ContentIncludedByDefault Remove="wwwroot\css\site.css" />
<_ContentIncludedByDefault Remove="wwwroot\favicon.ico" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Core.Test\Core.Test.csproj" />
<ProjectReference Include="..\Web\Web.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Moq" Version="4.16.1" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions Linguard/WebMock/_Imports.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using Linguard.Web
@using Linguard.Web.Shared
@using Radzen
@using Radzen.Blazor
9 changes: 9 additions & 0 deletions Linguard/WebMock/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"DetailedErrors": true,
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions Linguard/WebMock/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

0 comments on commit 509b2a8

Please sign in to comment.