Skip to content

Commit

Permalink
Version 1.0.6
Browse files Browse the repository at this point in the history
* Fix for dependency injeciton in Multilanguage package
* Unit tests added (to be continued)
  • Loading branch information
milons committed Nov 22, 2021
1 parent 1bfb138 commit 81470f2
Show file tree
Hide file tree
Showing 17 changed files with 147 additions and 20 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Release notes

### 1.0.6

* Fix for dependency injection in Multilanguage package.
* Unit tests added (to be continued).

### 1.0.4

* Swedish (sv-SE) translations added.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using HttpContextMoq;
using HttpContextMoq.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives;
using Moq;
using PiotrTrojan.AspNetCore.IdentityErrorLocalization.de_DE;
using PiotrTrojan.AspNetCore.IdentityErrorLocalization.es_ES;
using PiotrTrojan.AspNetCore.IdentityErrorLocalization.fa_IR;
using PiotrTrojan.AspNetCore.IdentityErrorLocalization.fr_FR;
using PiotrTrojan.AspNetCore.IdentityErrorLocalization.pl_PL;
using PiotrTrojan.AspNetCore.IdentityErrorLocalization.pt_PT;
using PiotrTrojan.AspNetCore.IdentityErrorLocalization.ru_RU;
using PiotrTrojan.AspNetCore.IdentityErrorLocalization.sv_SE;
using PiotrTrojan.AspNetCore.IdentityErrorLocalization.tr_TR;
using PiotrTrojan.AspNetCore.IdentityErrorLocalization.uk_UA;
using System;
using System.Collections.Generic;
using Xunit;

namespace PiotrTrojan.AspNetCore.IdentityErrorLocalization.Test
{
public class MultilangTest
{
[Fact]
public void DiTest()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddMultilangIdentityErrorDescriberFactory();
var sp = serviceCollection.BuildServiceProvider();
var factory = sp.GetService<IdentityErrorDescriberFactory>();
Assert.NotNull(factory);
}

[Theory]
[InlineData("de", typeof(GermanIdentityErrorDescriber), typeof(IdentityErrorDescriber))]
[InlineData("es", typeof(SpanishIdentityErrorDescriber), typeof(IdentityErrorDescriber))]
[InlineData("fa", typeof(PersianIdentityErrorDescriber), typeof(IdentityErrorDescriber))]
[InlineData("fr", typeof(FrenchIdentityErrorDescriber), typeof(IdentityErrorDescriber))]
[InlineData("pl", typeof(PolishIdentityErrorDescriber), typeof(IdentityErrorDescriber))]
[InlineData("pt", typeof(PortugueseIdentityErrorDescriber), typeof(IdentityErrorDescriber))]
[InlineData("ru", typeof(RussianIdentityErrorDescriber), typeof(IdentityErrorDescriber))]
[InlineData("sv", typeof(SwedishIdentityErrorDescriber), typeof(IdentityErrorDescriber))]
[InlineData("tr", typeof(TurkishIdentityErrorDescriber), typeof(IdentityErrorDescriber))]
[InlineData("uk", typeof(UkrainianIdentityErrorDescriber), typeof(IdentityErrorDescriber))]
public void MultilanguageFactoryTest<T>(string lang, Type validDescriberType, Type invalidDescriberType)
{
var serviceCollection = new ServiceCollection();
var httpContextMock = new HttpContextMock();
httpContextMock.SetupRequestHeaders(new Dictionary<string, StringValues>
{
{ "Accept-Language", lang }
});
var mockHttpContextAccessor = new Mock<IHttpContextAccessor>();
mockHttpContextAccessor.Setup(_ => _.HttpContext).Returns(httpContextMock);
serviceCollection.AddTransient<IHttpContextAccessor>(sp =>
{
return mockHttpContextAccessor.Object;
});

var validErrorDescriber = (IdentityErrorDescriber)Activator.CreateInstance(validDescriberType);
var invalidErrorDescriber = (IdentityErrorDescriber)Activator.CreateInstance(invalidDescriberType);

serviceCollection.AddMultilangIdentityErrorDescriberFactory();
var factory = serviceCollection.BuildServiceProvider().GetService<IdentityErrorDescriberFactory>();

Assert.NotNull(factory);
Assert.Equal(factory.GetDescriber().DefaultError().Description, validErrorDescriber.DefaultError().Description);
Assert.NotEqual(factory.GetDescriber().DefaultError().Description, invalidErrorDescriber.DefaultError().Description);
}

// TODO: Additional tests of dependency injection should be done here (e.g. AddTurkishIdentityErrorDescriber)

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="HttpContextMoq" Version="1.2.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<!--<ProjectReference Include="..\PiotrTrojan.AspNetCore.IdentityErrorLocalization.de-DE\PiotrTrojan.AspNetCore.IdentityErrorLocalization.de-DE.csproj" />
<ProjectReference Include="..\PiotrTrojan.AspNetCore.IdentityErrorLocalization.es-ES\PiotrTrojan.AspNetCore.IdentityErrorLocalization.es-ES.csproj" />
<ProjectReference Include="..\PiotrTrojan.AspNetCore.IdentityErrorLocalization.fa-IR\PiotrTrojan.AspNetCore.IdentityErrorLocalization.fa-IR.csproj" />
<ProjectReference Include="..\PiotrTrojan.AspNetCore.IdentityErrorLocalization.fr-FR\PiotrTrojan.AspNetCore.IdentityErrorLocalization.fr-FR.csproj" />
<ProjectReference Include="..\PiotrTrojan.AspNetCore.IdentityErrorLocalization.pl-PL\PiotrTrojan.AspNetCore.IdentityErrorLocalization.pl-PL.csproj" />
<ProjectReference Include="..\PiotrTrojan.AspNetCore.IdentityErrorLocalization.pt-PT\PiotrTrojan.AspNetCore.IdentityErrorLocalization.pt-PT.csproj" />
<ProjectReference Include="..\PiotrTrojan.AspNetCore.IdentityErrorLocalization.ru-RU\PiotrTrojan.AspNetCore.IdentityErrorLocalization.ru-RU.csproj" />
<ProjectReference Include="..\PiotrTrojan.AspNetCore.IdentityErrorLocalization.sv-SE\PiotrTrojan.AspNetCore.IdentityErrorLocalization.sv-SE.csproj" />
<ProjectReference Include="..\PiotrTrojan.AspNetCore.IdentityErrorLocalization.tr-TR\PiotrTrojan.AspNetCore.IdentityErrorLocalization.tr-TR.csproj" />
<ProjectReference Include="..\PiotrTrojan.AspNetCore.IdentityErrorLocalization.uk-UA\PiotrTrojan.AspNetCore.IdentityErrorLocalization.uk-UA.csproj" />-->
<ProjectReference Include="..\PiotrTrojan.AspNetCore.IdentityErrorLocalization\PiotrTrojan.AspNetCore.IdentityErrorLocalization.Multilang.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>PiotrTrojan.AspNetCore.IdentityErrorLocalization.de_DE</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.4</Version>
<Version>1.0.6</Version>
<Description>German localization for IdentityErrorDescriber (Microsoft.AspNetCore.Identity) for .net 5.
Contains both IdentityErrorDescriber and IdentityBuilderExtension to register the Describer.
For multilanguage support based on HTTP headers consider using PiotrTrojan.AspNetCore.IdentityErrorLocalization.Multilang nuget with proper factory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>PiotrTrojan.AspNetCore.IdentityErrorLocalization.es_ES</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.4</Version>
<Version>1.0.6</Version>
<Description>Spanish localization for IdentityErrorDescriber (Microsoft.AspNetCore.Identity) for .net 5.
Contains both IdentityErrorDescriber and IdentityBuilderExtension to register the Describer.
For multilanguage support based on HTTP headers consider using PiotrTrojan.AspNetCore.IdentityErrorLocalization.Multilang nuget with proper factory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>PiotrTrojan.AspNetCore.IdentityErrorLocalization.fa_IR</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.4</Version>
<Version>1.0.6</Version>
<Description>Persian localization for IdentityErrorDescriber (Microsoft.AspNetCore.Identity) for .net 5.
Contains both IdentityErrorDescriber and IdentityBuilderExtension to register the Describer.
For multilanguage support based on HTTP headers consider using PiotrTrojan.AspNetCore.IdentityErrorLocalization.Multilang nuget with proper factory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>PiotrTrojan.AspNetCore.IdentityErrorLocalization.fr_FR</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.4</Version>
<Version>1.0.6</Version>
<Description>French localization for IdentityErrorDescriber (Microsoft.AspNetCore.Identity) for .net 5.
Contains both IdentityErrorDescriber and IdentityBuilderExtension to register the Describer.
For multilanguage support based on HTTP headers consider using PiotrTrojan.AspNetCore.IdentityErrorLocalization.Multilang nuget with proper factory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>PiotrTrojan.AspNetCore.IdentityErrorLocalization.PL_pl</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.4</Version>
<Version>1.0.6</Version>
<Description>Polish localization for IdentityErrorDescriber (Microsoft.AspNetCore.Identity) for .net 5.
Contains both IdentityErrorDescriber and IdentityBuilderExtension to register the Describer.
For multilanguage support based on HTTP headers consider using PiotrTrojan.AspNetCore.IdentityErrorLocalization.Multilang nuget with proper factory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>PiotrTrojan.AspNetCore.IdentityErrorLocalization.pt_PT</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.4</Version>
<Version>1.0.6</Version>
<Description>Portuguese localization for IdentityErrorDescriber (Microsoft.AspNetCore.Identity) for .net 5.
Contains both IdentityErrorDescriber and IdentityBuilderExtension to register the Describer.
For multilanguage support based on HTTP headers consider using PiotrTrojan.AspNetCore.IdentityErrorLocalization.Multilang nuget with proper factory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>PiotrTrojan.AspNetCore.IdentityErrorLocalization.ru_RU</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.4</Version>
<Version>1.0.6</Version>
<Description>Russian localization for IdentityErrorDescriber (Microsoft.AspNetCore.Identity) for .net 5.
Contains both IdentityErrorDescriber and IdentityBuilderExtension to register the Describer.
For multilanguage support based on HTTP headers consider using PiotrTrojan.AspNetCore.IdentityErrorLocalization.Multilang nuget with proper factory.
Expand Down
6 changes: 6 additions & 0 deletions source/PiotrTrojan.AspNetCore.IdentityErrorLocalization.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PiotrTrojan.AspNetCore.Iden
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PiotrTrojan.AspNetCore.IdentityErrorLocalization.uk-UA", "PiotrTrojan.AspNetCore.IdentityErrorLocalization.uk-UA\PiotrTrojan.AspNetCore.IdentityErrorLocalization.uk-UA.csproj", "{987099E2-4CE2-41ED-953F-C9C89B1949F9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PiotrTrojan.AspNetCore.IdentityErrorLocalization.Test", "PiotrTrojan.AspNetCore.IdentityErrorLocalization.Test\PiotrTrojan.AspNetCore.IdentityErrorLocalization.Test.csproj", "{79342E03-8581-4D29-A445-639066B626F1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -75,6 +77,10 @@ Global
{987099E2-4CE2-41ED-953F-C9C89B1949F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{987099E2-4CE2-41ED-953F-C9C89B1949F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{987099E2-4CE2-41ED-953F-C9C89B1949F9}.Release|Any CPU.Build.0 = Release|Any CPU
{79342E03-8581-4D29-A445-639066B626F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{79342E03-8581-4D29-A445-639066B626F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{79342E03-8581-4D29-A445-639066B626F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{79342E03-8581-4D29-A445-639066B626F1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>PiotrTrojan.AspNetCore.IdentityErrorLocalization.sv_SE</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.4</Version>
<Version>1.0.6</Version>
<Description>Swedish localization for IdentityErrorDescriber (Microsoft.AspNetCore.Identity) for .net 5.
Contains both IdentityErrorDescriber and IdentityBuilderExtension to register the Describer.
For multilanguage support based on HTTP headers consider using PiotrTrojan.AspNetCore.IdentityErrorLocalization.Multilang nuget with proper factory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>PiotrTrojan.AspNetCore.IdentityErrorLocalization.tr_TR</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.4</Version>
<Version>1.0.6</Version>
<Description>Turkish localization for IdentityErrorDescriber (Microsoft.AspNetCore.Identity) for .net 5.
Contains both IdentityErrorDescriber and IdentityBuilderExtension to register the Describer.
For multilanguage support based on HTTP headers consider using PiotrTrojan.AspNetCore.IdentityErrorLocalization.Multilang nuget with proper factory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>PiotrTrojan.AspNetCore.IdentityErrorLocalization.uk_UA</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.4</Version>
<Version>1.0.6</Version>
<Description>Ukrainian localization for IdentityErrorDescriber (Microsoft.AspNetCore.Identity) for .net 5.
Contains both IdentityErrorDescriber and IdentityBuilderExtension to register the Describer.
For multilanguage support based on HTTP headers consider using PiotrTrojan.AspNetCore.IdentityErrorLocalization.Multilang nuget with proper factory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ public static IServiceCollection AddMultilangIdentityErrorDescriberFactory(this
{
serviceCollection.AddScoped<IdentityErrorDescriberFactory>(sp =>
{
var httpContext = sp.GetRequiredService<IHttpContextAccessor>().HttpContext;
var culture = httpContext.Request
.GetTypedHeaders()
.AcceptLanguage
.OrderByDescending(q => q.Quality)
.FirstOrDefault()?.Value.Value ?? string.Empty;
var httpContext = sp.GetService<IHttpContextAccessor>()?.HttpContext;
var culture = httpContext?.Request
.GetTypedHeaders()
.AcceptLanguage
.OrderByDescending(q => q.Quality)
.FirstOrDefault()?.Value.Value ?? string.Empty;
return new IdentityErrorDescriberFactory(culture);

});
return serviceCollection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ public class MultilangIdentityErrorDescriber : IdentityErrorDescriber
{
private readonly IdentityErrorDescriber describer;

public MultilangIdentityErrorDescriber(Lazy<IdentityErrorDescriberFactory> factory)
public MultilangIdentityErrorDescriber(IdentityErrorDescriberFactory factory = null)
{
describer = factory?.Value.GetDescriber() ??
throw new Exception(
describer = factory?.GetDescriber() ?? throw new Exception(
"IdentityErrorDescriberFactory not registered." +
"Call AddMultilangIdentityErrorDescriberFactory() on IServiceCollection during services registration.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>PiotrTrojan.AspNetCore.IdentityErrorLocalization.Multilang</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.4</Version>
<Version>1.0.6</Version>
<Description>Localizations for IdentityErrorDescriber (Microsoft.AspNetCore.Identity) for .net 5.
Resolved describer is based on "accept-language" HTTP request header.
Contains localizations French, German, Persian, Polish, Portuguese, Russian, Spanish and Turkish.
Expand Down

0 comments on commit 81470f2

Please sign in to comment.