Class IdentityTestingExtensions

Namespace
XBullet.EasyTesting.Authentication
Assembly
XBullet.EasyTesting.dll

Helpers for arranging persisted ASP.NET Core Identity users in integration tests.

public static class IdentityTestingExtensions
Inheritance
IdentityTestingExtensions
Inherited Members

Methods

CreateIdentityTestUserAsync<TUser>(IServiceProvider, TUser, string)

Creates a simulated identity linked to a persisted Identity user.

public static Task<TestUser> CreateIdentityTestUserAsync<TUser>(this IServiceProvider services, TUser user, string authenticationScheme = "IntegrationTest") where TUser : class

Parameters

services IServiceProvider

The service provider used to create and dispose an asynchronous scope and resolve the user manager. The provider is not owned or disposed by this method.

user TUser

The non-null persisted user whose identifier, name, roles, and claims are read. The method does not own, dispose, persist, or mutate it.

authenticationScheme string

The non-empty simulated authentication scheme stored as both the target scheme and authentication type. The default is AuthenticationScheme.

Returns

Task<TestUser>

A task whose result is a new test-user snapshot linked by the persisted user's identifier. The caller owns the returned definition.

Type Parameters

TUser

The application's reference-type Identity user registered with UserManager<TUser>.

Exceptions

InvalidOperationException

No UserManager<TUser> is registered in the created scope.

CreateTestUserAsync<TUser>(UserManager<TUser>, TUser, string)

Creates a simulated identity linked to a user managed by ASP.NET Core Identity.

public static Task<TestUser> CreateTestUserAsync<TUser>(this UserManager<TUser> userManager, TUser user, string authenticationScheme = "IntegrationTest") where TUser : class

Parameters

userManager UserManager<TUser>

The non-null manager used to read user data. The method does not own or dispose it.

user TUser

The non-null user whose identifier, name, supported roles, and supported claims are read. The method does not own, dispose, persist, or mutate it.

authenticationScheme string

The non-empty simulated authentication scheme stored as both the target scheme and authentication type. The default is AuthenticationScheme.

Returns

Task<TestUser>

A task whose result is a new test-user snapshot. Its display name is the Identity user name, or the user identifier when the name is null. Standard name, identifier, and role claims are excluded from the additional-claims collection.

Type Parameters

TUser

The reference-type Identity user managed by this user manager.

SeedIdentityUserAsync<TUser>(IServiceProvider, TUser, string?, IEnumerable<string>?)

Creates an Identity user through the application's UserManager<TUser>, optionally assigning a password and roles.

public static Task<TUser> SeedIdentityUserAsync<TUser>(this IServiceProvider services, TUser user, string? password = null, IEnumerable<string>? roles = null) where TUser : class

Parameters

services IServiceProvider

The service provider used to create and dispose an asynchronous scope and resolve the user manager. The provider is not owned or disposed by this method.

user TUser

The non-null user instance passed to the user manager for persistence. The method does not own or dispose it, but the configured Identity store may mutate it during creation.

password string

The password passed to Identity user creation, or null to use the passwordless creation overload. The default is null; non-null values, including an empty string, are validated by the configured Identity services.

roles IEnumerable<string>

Role names to materialize once and assign after successful user creation. When null or empty, no role-assignment call is made. The enumerable is read but not retained, owned, or disposed.

Returns

Task<TUser>

A task whose result is the same user instance after successful creation and optional role assignment.

Type Parameters

TUser

The application's reference-type Identity user registered with UserManager<TUser>.

Remarks

User creation and role assignment are separate store operations. If role assignment fails, this method throws without rolling back the already-created user.

Exceptions

InvalidOperationException

Identity creation or role assignment fails, or no matching user manager is registered.