-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87fa9b5
commit 5ead995
Showing
9 changed files
with
299 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...entity/CleanArc.Test.Infrastructure.Identity/CleanArc.Test.Infrastructure.Identity.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\CleanArc.Tests.Setup\CleanArc.Tests.Setup.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
95 changes: 95 additions & 0 deletions
95
...Arc.Test.Infrastructure.Identity/CleanArc.Test.Infrastructure.Identity/UserManagerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using CleanArc.Domain.Entities.User; | ||
using CleanArc.Tests.Setup.Setups; | ||
|
||
namespace CleanArc.Test.Infrastructure.Identity | ||
{ | ||
public class UserManagerTest: TestIdentitySetup | ||
{ | ||
[Fact] | ||
public async Task Duplicate_User_Names_Not_Allowed() | ||
{ | ||
var user = new User() | ||
{ | ||
UserName = "Test", Email = "Test@example.com" | ||
}; | ||
|
||
var duplicateUser = new User() | ||
{ | ||
UserName = "Test", | ||
Email = "Test@example.com" | ||
}; | ||
|
||
var createUserResult = await base.TestAppUserManager.CreateUser(user); | ||
|
||
var duplicateCreateUserResult = await base.TestAppUserManager.CreateUser(duplicateUser); | ||
|
||
Assert.False(duplicateCreateUserResult.Succeeded); | ||
} | ||
|
||
[Fact] | ||
public async Task Create_Phone_Confirmation_Code_And_Verify() | ||
{ | ||
var user = new User() | ||
{ | ||
UserName = "Test", | ||
Email = "test@example.com", | ||
PhoneNumber = "09123456789" | ||
}; | ||
|
||
var createUserResult = await base.TestAppUserManager.CreateUser(user); | ||
|
||
var otpCode=await base.TestAppUserManager.GeneratePhoneNumberConfirmationToken(user,user.PhoneNumber); | ||
|
||
var confirmPhoneNumberResult=await base.TestAppUserManager.ChangePhoneNumber(user,user.PhoneNumber,otpCode); | ||
|
||
|
||
Assert.NotNull(otpCode); | ||
Assert.True(confirmPhoneNumberResult.Succeeded); | ||
} | ||
|
||
[Fact] | ||
public async Task Generate_And_Verify_Otp_Code() | ||
{ | ||
var user = new User() | ||
{ | ||
UserName = "Test", | ||
Email = "test@example.com", | ||
PhoneNumber = "09123456789" | ||
}; | ||
|
||
var createUserResult = await base.TestAppUserManager.CreateUser(user); | ||
|
||
var phoneNumberConfirmationCode = await base.TestAppUserManager.GeneratePhoneNumberConfirmationToken(user, user.PhoneNumber); | ||
|
||
var confirmPhoneNumberResult = await base.TestAppUserManager.ChangePhoneNumber(user, user.PhoneNumber, phoneNumberConfirmationCode); | ||
|
||
var otpCode = await base.TestAppUserManager.GenerateOtpCode(user); | ||
|
||
var confirmOtpCode = await base.TestAppUserManager.VerifyUserCode(user, otpCode); | ||
|
||
Assert.NotNull(otpCode); | ||
Assert.True(confirmOtpCode.Succeeded); | ||
} | ||
|
||
[Fact] | ||
public async Task Generate_Access_Token() | ||
{ | ||
var user = new User() | ||
{ | ||
UserName = "Test", | ||
Email = "test@example.com", | ||
PhoneNumber = "09123456789" | ||
}; | ||
|
||
var createUserResult = await base.TestAppUserManager.CreateUser(user); | ||
|
||
var otpCode = await base.TestAppUserManager.GeneratePhoneNumberConfirmationToken(user, user.PhoneNumber); | ||
|
||
var confirmPhoneNumberResult = await base.TestAppUserManager.ChangePhoneNumber(user, user.PhoneNumber, otpCode); | ||
|
||
var token=await base.JwtService.GenerateAsync(user); | ||
|
||
Assert.NotNull(token.access_token); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...sts/CleanArc.Test.Infrastructure.Identity/CleanArc.Test.Infrastructure.Identity/Usings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Xunit; |
34 changes: 34 additions & 0 deletions
34
src/Tests/CleanArc.Tests.Setup/CleanArc.Tests.Setup.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.9" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" /> | ||
<PackageReference Include="Moq" Version="4.18.4" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Infrastructure\CleanArc.Infrastructure.Identity\CleanArc.Infrastructure.Identity.csproj" /> | ||
<ProjectReference Include="..\..\Infrastructure\CleanArc.Infrastructure.Persistence\CleanArc.Infrastructure.Persistence.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Setups\" /> | ||
</ItemGroup> | ||
|
||
</Project> |
23 changes: 23 additions & 0 deletions
23
src/Tests/CleanArc.Tests.Setup/Setups/TestApplicationDbContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using CleanArc.Infrastructure.Persistence; | ||
using Microsoft.Data.Sqlite; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace CleanArc.Tests.Setup.Setups; | ||
|
||
public abstract class TestApplicationDbContext | ||
{ | ||
protected ApplicationDbContext UnitTestDbContext { get; } | ||
|
||
protected TestApplicationDbContext() | ||
{ | ||
var connectionStringBuilder = | ||
new SqliteConnectionStringBuilder { DataSource = ":memory:" }; | ||
var connection = new SqliteConnection(connectionStringBuilder.ToString()); | ||
|
||
var options = new DbContextOptionsBuilder<ApplicationDbContext>() | ||
.UseSqlite(connection) | ||
.Options; | ||
|
||
UnitTestDbContext = new ApplicationDbContext(options); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
src/Tests/CleanArc.Tests.Setup/Setups/TestIdentitySetup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using CleanArc.Application.Contracts; | ||
using CleanArc.Application.Contracts.Identity; | ||
using CleanArc.Application.Contracts.Persistence; | ||
using CleanArc.Domain.Entities.User; | ||
using CleanArc.Infrastructure.Identity.Identity; | ||
using CleanArc.Infrastructure.Identity.Identity.Dtos; | ||
using CleanArc.Infrastructure.Identity.Identity.Extensions; | ||
using CleanArc.Infrastructure.Identity.Identity.Manager; | ||
using CleanArc.Infrastructure.Identity.Identity.Store; | ||
using CleanArc.Infrastructure.Identity.Jwt; | ||
using CleanArc.Infrastructure.Identity.UserManager; | ||
using CleanArc.Infrastructure.Persistence; | ||
using CleanArc.Infrastructure.Persistence.Repositories.Common; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; | ||
using Microsoft.Data.Sqlite; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Moq; | ||
|
||
namespace CleanArc.Tests.Setup.Setups; | ||
|
||
public abstract class TestIdentitySetup | ||
{ | ||
protected IAppUserManager TestAppUserManager { get; } | ||
protected AppRoleManager TestAppRoleManager { get; } | ||
public AppSignInManager TestSignInManager { get; } | ||
public IAppUserManager User { get; } | ||
public IJwtService JwtService { get; } | ||
|
||
protected TestIdentitySetup() | ||
{ | ||
var serviceCollection = new ServiceCollection(); | ||
|
||
var connection = new SqliteConnection("DataSource=:memory:"); | ||
|
||
serviceCollection.AddLogging(); | ||
|
||
serviceCollection.AddDbContext<ApplicationDbContext>(options => options.UseSqlite(connection)); | ||
|
||
var context = serviceCollection.BuildServiceProvider().GetService<ApplicationDbContext>(); | ||
context.Database.OpenConnection(); | ||
context.Database.EnsureCreated(); | ||
|
||
|
||
serviceCollection.AddIdentity<User, Role>(options => | ||
{ | ||
options.Stores.ProtectPersonalData = false; | ||
options.Password.RequireDigit = false; | ||
options.Password.RequireLowercase = false; | ||
options.Password.RequireNonAlphanumeric = false; | ||
options.Password.RequiredUniqueChars = 0; | ||
options.Password.RequireUppercase = false; | ||
options.SignIn.RequireConfirmedEmail = false; | ||
options.SignIn.RequireConfirmedPhoneNumber = true; | ||
options.Lockout.MaxFailedAccessAttempts = 5; | ||
options.Lockout.AllowedForNewUsers = false; | ||
options.User.RequireUniqueEmail = false; | ||
}).AddUserStore<AppUserStore>() | ||
.AddRoleStore<RoleStore>(). | ||
AddUserManager<AppUserManager>(). | ||
AddRoleManager<AppRoleManager>(). | ||
AddErrorDescriber<AppErrorDescriber>() | ||
.AddDefaultTokenProviders(). | ||
AddSignInManager<AppSignInManager>() | ||
.AddDefaultTokenProviders() | ||
.AddPasswordlessLoginTotpTokenProvider(); | ||
|
||
serviceCollection.Configure<IdentitySettings>(settings => | ||
{ | ||
settings.Audience = "CleanArc.Unit.Tests"; | ||
settings.ExpirationMinutes = 5; | ||
settings.Issuer = "CleanArc.Unit.Tests"; | ||
settings.NotBeforeMinutes = 0; | ||
settings.SecretKey = "LongerThan-16Char-SecretKey"; | ||
settings.Encryptkey = "16CharEncryptKey"; | ||
}); | ||
|
||
serviceCollection.AddScoped<IUnitOfWork, UnitOfWork>(); | ||
serviceCollection.AddScoped<IJwtService, JwtService>(); | ||
serviceCollection.AddScoped<IAppUserManager, AppUserManagerImplementation>(); | ||
|
||
TestAppUserManager = serviceCollection.BuildServiceProvider().GetRequiredService<IAppUserManager>(); | ||
TestAppRoleManager = serviceCollection.BuildServiceProvider().GetRequiredService<AppRoleManager>(); | ||
TestSignInManager = serviceCollection.BuildServiceProvider().GetRequiredService<AppSignInManager>(); | ||
JwtService = serviceCollection.BuildServiceProvider().GetRequiredService<IJwtService>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Xunit; |