Skip to content

IntegrationTestBase

Namespace: IntegrationTests.Infrastructure

Base class for integration tests providing common HTTP client functionality, authentication helpers, and test data
management.

public abstract class IntegrationTestBase

Inheritance ObjectIntegrationTestBase

Methods

CleanupDatabaseAsync()

Cleans up test data by removing all test users from the database.

protected Task CleanupDatabaseAsync()

Returns

Task

CreateJsonContent<T>(T)

Creates JSON content for HTTP requests from the specified object using camelCase property naming.

protected StringContent CreateJsonContent<T>(T obj)

Type Parameters

T

The type of object to serialize.

Parameters

obj T

The object to serialize to JSON.

Returns

StringContent containing the JSON representation of the object.

CreateTestUserAsync(String, String, String)

Creates a test user with the specified credentials and role for use in integration tests.

public Task CreateTestUserAsync(string username, string password, string role)

Parameters

username String

The username for the test user.

password String

The password for the test user.

role String

The role to assign to the test user.

Returns

Task

GenerateUniqueUsername(String)

Generates a unique username by appending timestamp and counter to the base username to avoid conflicts.

protected string GenerateUniqueUsername(string baseUsername)

Parameters

baseUsername String

The base username to make unique.

Returns

A unique username string.

GetJwtTokenAsync(String, String)

Gets a valid JWT token by creating a test user and performing authentication.

protected Task<String> GetJwtTokenAsync(string baseUsername, string password)

Parameters

baseUsername String

The base username for the test user (default: "admin").

password String

The password for the test user (default: "Admin123!").

Returns

A valid JWT token string for API authentication.

GetValidJwtTokenAsync(String, String)

Authenticates with the specified credentials and returns a valid JWT token for API requests.

public Task<String> GetValidJwtTokenAsync(string username, string password)

Parameters

username String

The username for authentication.

password String

The password for authentication.

Returns

A valid JWT token string.

OneTimeSetUp()

One-time setup to initialize the test web application factory and HTTP client for all tests in the fixture.

public Task OneTimeSetUp()

Returns

Task

OneTimeTearDown()

One-time cleanup to dispose of the HTTP client and web application factory after all tests complete.

public void OneTimeTearDown()

SendAsync<T>(HttpRequestMessage)

Sends an HTTP request and deserializes the response content to the specified type.

protected Task<T> SendAsync<T>(HttpRequestMessage request)

Type Parameters

T

The type to deserialize the response content to.

Parameters

request HttpRequestMessage

The HTTP request message to send.

Returns

The deserialized response content or default value if content is empty.

SetAuthorizationHeader(String)

Sets the Authorization header on the HTTP client with the provided JWT token.

protected void SetAuthorizationHeader(string token)

Parameters

token String

The JWT token to use for authorization.

SetUp()

Setup method run before each test to reset the HTTP client authorization header.

public void SetUp()