Search Results for

    Show / Hide Table of Contents

    Class Test_AuthService

    Comprehensive test suite for the AuthService class, testing all authentication-related functionality.

    Inheritance
    object
    Test_AuthService
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: OEMS.UnitTests.Application
    Assembly: OEMS.UnitTests.dll
    Syntax
    [TestClass]
    public sealed class Test_AuthService
    Remarks

    This test class verifies the behavior of user registration, login, token refresh, token validation, and password management features.

    Methods

    GenerateTokenForOrganizationAsync_UserNotInOrganization_ReturnsError()

    Tests that GenerateTokenForOrganizationAsync handles user not in organization correctly

    Declaration
    [TestMethod]
    public Task GenerateTokenForOrganizationAsync_UserNotInOrganization_ReturnsError()
    Returns
    Type Description
    Task

    GenerateTokenForOrganizationAsync_WithAdditionalInfo_GeneratesToken()

    Tests that the GenerateTokenForOrganizationAsync method correctly generates a token with organization claims.

    Declaration
    [TestMethod]
    public Task GenerateTokenForOrganizationAsync_WithAdditionalInfo_GeneratesToken()
    Returns
    Type Description
    Task
    Remarks

    This test verifies token creation with additional org information

    GenerateTokenForOrganizationAsync_WithBoundaryCases_HandlesCorrectly()

    Tests token generation with boundary cases.

    Declaration
    [TestMethod]
    public Task GenerateTokenForOrganizationAsync_WithBoundaryCases_HandlesCorrectly()
    Returns
    Type Description
    Task

    GenerateTokenForOrganizationAsync_WithDifferentRoles_ReturnsSuccess()

    Tests that a token can be generated for a user with specific organization roles.

    Declaration
    [TestMethod]
    public Task GenerateTokenForOrganizationAsync_WithDifferentRoles_ReturnsSuccess()
    Returns
    Type Description
    Task

    GenerateTokenForOrganizationAsync_WithMissingUser_ReturnsError()

    Tests that GenerateTokenForOrganizationAsync handles missing user correctly

    Declaration
    [TestMethod]
    public Task GenerateTokenForOrganizationAsync_WithMissingUser_ReturnsError()
    Returns
    Type Description
    Task

    GenerateTokenForOrganizationAsync_WithNonExistentUser_ReturnsError()

    Tests that the token generation correctly fails when the user doesn't exist.

    Declaration
    [TestMethod]
    public Task GenerateTokenForOrganizationAsync_WithNonExistentUser_ReturnsError()
    Returns
    Type Description
    Task

    HasAllowedDomain_WithBoundaryCases_HandlesCorrectly()

    Tests the HasAllowedDomain method with boundary case emails.

    Declaration
    [TestMethod]
    public void HasAllowedDomain_WithBoundaryCases_HandlesCorrectly()

    HasAllowedDomain_WithInvalidEmails_ReturnsFalse()

    Tests the HasAllowedDomain method with invalid email formats.

    Declaration
    [TestMethod]
    public void HasAllowedDomain_WithInvalidEmails_ReturnsFalse()

    HasAllowedDomain_WithValidEmails_ReturnsCorrectResult()

    Tests the HasAllowedDomain method with various email formats.

    Declaration
    [TestMethod]
    public void HasAllowedDomain_WithValidEmails_ReturnsCorrectResult()

    HashPassword_WithBoundaryCasePasswords_HandlesCorrectly()

    Tests that the password hashing function handles boundary cases correctly. This includes empty passwords, very long passwords, and null values.

    Declaration
    [TestMethod]
    public void HashPassword_WithBoundaryCasePasswords_HandlesCorrectly()

    HashPassword_WithNullPassword_ThrowsException()

    Tests that the password hashing function handles invalid parameter - null password.

    Declaration
    [TestMethod]
    [ExpectedException(typeof(ArgumentNullException))]
    public void HashPassword_WithNullPassword_ThrowsException()

    HashPassword_WithValidPassword_CreatesUniqueHashes()

    Tests that the password hashing function generates unique hashes for the same password.

    Declaration
    [TestMethod]
    public void HashPassword_WithValidPassword_CreatesUniqueHashes()
    Remarks

    This test verifies:

    1. Each call to HashPassword generates a unique salt
    2. The same password results in different hashes when using different salts
    3. The hashing function produces consistent results

    IsValidPassword_WithBoundaryCases_BehavesCorrectly()

    Tests the password validation function with boundary cases that are right at the edge of the requirements.

    Declaration
    [TestMethod]
    public void IsValidPassword_WithBoundaryCases_BehavesCorrectly()

    IsValidPassword_WithInvalidPasswords_ReturnsFalse()

    Tests that the password validation function correctly rejects passwords that do not meet the requirements.

    Declaration
    [TestMethod]
    public void IsValidPassword_WithInvalidPasswords_ReturnsFalse()

    IsValidPassword_WithValidPasswords_ReturnsTrue()

    Tests that the password validation function correctly validates passwords that meet all requirements.

    Declaration
    [TestMethod]
    public void IsValidPassword_WithValidPasswords_ReturnsTrue()

    LoginAsync_UpdatesLastLoginDate()

    Tests that login updates the user's last login date.

    Declaration
    [TestMethod]
    public Task LoginAsync_UpdatesLastLoginDate()
    Returns
    Type Description
    Task

    LoginAsync_WithInactiveUser_ReturnsError()

    Tests that login fails with inactive user account.

    Declaration
    [TestMethod]
    public Task LoginAsync_WithInactiveUser_ReturnsError()
    Returns
    Type Description
    Task

    LoginAsync_WithIncorrectPassword_ReturnsError()

    Tests that login fails with incorrect password.

    Declaration
    [TestMethod]
    public Task LoginAsync_WithIncorrectPassword_ReturnsError()
    Returns
    Type Description
    Task

    LoginAsync_WithNonExistentEmail_ReturnsError()

    Tests that login fails with non-existent email.

    Declaration
    [TestMethod]
    public Task LoginAsync_WithNonExistentEmail_ReturnsError()
    Returns
    Type Description
    Task

    LoginAsync_WithNonExistentUsername_ReturnsError()

    Tests that login fails with non-existent username.

    Declaration
    [TestMethod]
    public Task LoginAsync_WithNonExistentUsername_ReturnsError()
    Returns
    Type Description
    Task

    LoginAsync_WithRefreshTokenError_StillSucceeds()

    Tests login with database error on refresh token creation.

    Declaration
    [TestMethod]
    public Task LoginAsync_WithRefreshTokenError_StillSucceeds()
    Returns
    Type Description
    Task

    LoginAsync_WithRememberMe_SetsLongerExpiry()

    Tests that login with remember me flag sets longer token expiration.

    Declaration
    [TestMethod]
    public Task LoginAsync_WithRememberMe_SetsLongerExpiry()
    Returns
    Type Description
    Task

    LoginAsync_WithUpdateUserError_StillSucceeds()

    Tests login with database error on UpdateUserAsync.

    Declaration
    [TestMethod]
    public Task LoginAsync_WithUpdateUserError_StillSucceeds()
    Returns
    Type Description
    Task

    LoginAsync_WithValidEmail_ReturnsSuccess()

    Tests that a user can successfully login with valid email and password.

    Declaration
    [TestMethod]
    public Task LoginAsync_WithValidEmail_ReturnsSuccess()
    Returns
    Type Description
    Task

    LoginAsync_WithValidUsername_ReturnsSuccess()

    Tests that a user can successfully login with valid username and password.

    Declaration
    [TestMethod]
    public Task LoginAsync_WithValidUsername_ReturnsSuccess()
    Returns
    Type Description
    Task
    Remarks

    This test verifies:

    1. The login operation completes successfully with a username
    2. A valid JWT token and refresh token are returned
    3. The JWT token contains the expected user claims
    4. The correct user object is returned with the authentication result

    LogoutAsync_ClearsAuthenticationAndTokens()

    Declaration
    [TestMethod]
    public Task LogoutAsync_ClearsAuthenticationAndTokens()
    Returns
    Type Description
    Task

    RefreshTokenAsync_WithAlmostExpiredToken_ReturnsSuccess()

    Declaration
    [TestMethod]
    public Task RefreshTokenAsync_WithAlmostExpiredToken_ReturnsSuccess()
    Returns
    Type Description
    Task

    RefreshTokenAsync_WithExpiredToken_ReturnsError()

    Declaration
    [TestMethod]
    public Task RefreshTokenAsync_WithExpiredToken_ReturnsError()
    Returns
    Type Description
    Task

    RefreshTokenAsync_WithInactiveUser_ReturnsError()

    Declaration
    [TestMethod]
    public Task RefreshTokenAsync_WithInactiveUser_ReturnsError()
    Returns
    Type Description
    Task

    RefreshTokenAsync_WithJustExpiredToken_ReturnsError()

    Declaration
    [TestMethod]
    public Task RefreshTokenAsync_WithJustExpiredToken_ReturnsError()
    Returns
    Type Description
    Task

    RefreshTokenAsync_WithNullUser_ReturnsError()

    Declaration
    [TestMethod]
    public Task RefreshTokenAsync_WithNullUser_ReturnsError()
    Returns
    Type Description
    Task

    RefreshTokenAsync_WithRevokedToken_ReturnsError()

    Declaration
    [TestMethod]
    public Task RefreshTokenAsync_WithRevokedToken_ReturnsError()
    Returns
    Type Description
    Task

    RefreshTokenAsync_WithValidToken_ReturnsNewTokenPair()

    Tests that a valid refresh token can be exchanged for a new token pair.

    Declaration
    [TestMethod]
    public Task RefreshTokenAsync_WithValidToken_ReturnsNewTokenPair()
    Returns
    Type Description
    Task
    Remarks

    This test verifies:

    1. A valid refresh token can be used to obtain a new JWT token
    2. The old refresh token is revoked and a new one is issued
    3. The user's last login date is updated
    4. The new refresh token is different from the original one
    5. All repository interactions occur as expected

    RegisterUserAsync_DomainValidationTests()

    Declaration
    [TestMethod]
    public Task RegisterUserAsync_DomainValidationTests()
    Returns
    Type Description
    Task

    RegisterUserAsync_WithBorderlinePassword_TestsPasswordPolicy()

    Tests that registration handles password policy enforcement correctly. Tests exactly at boundary of minimum length requirement.

    Declaration
    [TestMethod]
    public Task RegisterUserAsync_WithBorderlinePassword_TestsPasswordPolicy()
    Returns
    Type Description
    Task

    RegisterUserAsync_WithComplexPassword_ReturnsSuccess()

    Tests the registration with very complex but valid password.

    Declaration
    [TestMethod]
    public Task RegisterUserAsync_WithComplexPassword_ReturnsSuccess()
    Returns
    Type Description
    Task

    RegisterUserAsync_WithDatabaseError_ReturnsError()

    Tests that registration fails when there's a database error.

    Declaration
    [TestMethod]
    public Task RegisterUserAsync_WithDatabaseError_ReturnsError()
    Returns
    Type Description
    Task

    RegisterUserAsync_WithDifferentEmailDomain_ReturnsSuccess()

    Tests that registration works with valid different email domains.

    Declaration
    [TestMethod]
    public Task RegisterUserAsync_WithDifferentEmailDomain_ReturnsSuccess()
    Returns
    Type Description
    Task

    RegisterUserAsync_WithExistingEmail_ReturnsError()

    Declaration
    [TestMethod]
    public Task RegisterUserAsync_WithExistingEmail_ReturnsError()
    Returns
    Type Description
    Task

    RegisterUserAsync_WithExistingUsername_ReturnsError()

    Declaration
    [TestMethod]
    public Task RegisterUserAsync_WithExistingUsername_ReturnsError()
    Returns
    Type Description
    Task

    RegisterUserAsync_WithInvalidDomain_ReturnsError()

    Declaration
    [TestMethod]
    public Task RegisterUserAsync_WithInvalidDomain_ReturnsError()
    Returns
    Type Description
    Task

    RegisterUserAsync_WithInvalidEmailDomain_ReturnsError()

    Tests that registration fails with invalid email domain.

    Declaration
    [TestMethod]
    public Task RegisterUserAsync_WithInvalidEmailDomain_ReturnsError()
    Returns
    Type Description
    Task

    RegisterUserAsync_WithInvalidPassword_ReturnsError()

    Declaration
    [TestMethod]
    public Task RegisterUserAsync_WithInvalidPassword_ReturnsError()
    Returns
    Type Description
    Task

    RegisterUserAsync_WithValidStandardData_ReturnsSuccess()

    Tests that a user can successfully register with valid data.

    Declaration
    [TestMethod]
    public Task RegisterUserAsync_WithValidStandardData_ReturnsSuccess()
    Returns
    Type Description
    Task
    Remarks

    This test verifies:

    1. The registration operation completes successfully with standard valid data
    2. A valid JWT token and refresh token are created for the new user
    3. The user object is created with the correct properties
    4. New users are created with active status and non-admin role
    5. Organization user is created correctly

    RegisterUserAsync_WithVariousInvalidPasswords_ReturnsError()

    Tests that registration fails with various invalid password patterns.

    Declaration
    [TestMethod]
    public Task RegisterUserAsync_WithVariousInvalidPasswords_ReturnsError()
    Returns
    Type Description
    Task

    SetUp()

    Initializes test dependencies before each test method execution.

    Declaration
    [TestInitialize]
    public void SetUp()
    Remarks

    Sets up mock repositories and configuration with test values to isolate the authentication service for unit testing.

    ValidateTokenAsync_WithEmptyToken_ReturnsFalse()

    Declaration
    [TestMethod]
    public Task ValidateTokenAsync_WithEmptyToken_ReturnsFalse()
    Returns
    Type Description
    Task

    ValidateTokenAsync_WithInactiveUser_ReturnsFalse()

    Declaration
    [TestMethod]
    public Task ValidateTokenAsync_WithInactiveUser_ReturnsFalse()
    Returns
    Type Description
    Task

    ValidateTokenAsync_WithInvalidSignature_ReturnsFalse()

    Declaration
    [TestMethod]
    public Task ValidateTokenAsync_WithInvalidSignature_ReturnsFalse()
    Returns
    Type Description
    Task

    ValidateTokenAsync_WithInvalidToken_ReturnsFalse()

    Declaration
    [TestMethod]
    public Task ValidateTokenAsync_WithInvalidToken_ReturnsFalse()
    Returns
    Type Description
    Task

    ValidateTokenAsync_WithManipulatedClaims_ReturnsFalse()

    Declaration
    [TestMethod]
    public Task ValidateTokenAsync_WithManipulatedClaims_ReturnsFalse()
    Returns
    Type Description
    Task

    ValidateTokenAsync_WithNonExistentUser_ReturnsFalse()

    Declaration
    [TestMethod]
    public Task ValidateTokenAsync_WithNonExistentUser_ReturnsFalse()
    Returns
    Type Description
    Task

    ValidateTokenAsync_WithValidToken_ReturnsTrue()

    Tests that a valid JWT token is properly validated.

    Declaration
    [TestMethod]
    public Task ValidateTokenAsync_WithValidToken_ReturnsTrue()
    Returns
    Type Description
    Task
    Remarks

    This test verifies:

    1. A newly generated valid token passes validation
    2. The user ID is correctly extracted from the token
    3. The user's active status is checked during validation

    VerifyPassword_WithBoundaryCases_HandlesCorrectly()

    Tests that the password verification function handles boundary cases correctly.

    Declaration
    [TestMethod]
    public void VerifyPassword_WithBoundaryCases_HandlesCorrectly()

    VerifyPassword_WithCorrectPassword_ReturnsTrue()

    Tests that the password verification function correctly validates a matching password. This is a test for valid parameters.

    Declaration
    [TestMethod]
    public void VerifyPassword_WithCorrectPassword_ReturnsTrue()

    VerifyPassword_WithIncorrectPassword_ReturnsFalse()

    Tests that the password verification function correctly rejects a non-matching password. This is a test for valid but non-matching parameters.

    Declaration
    [TestMethod]
    public void VerifyPassword_WithIncorrectPassword_ReturnsFalse()

    VerifyPassword_WithInvalidParameters_ReturnsFalse()

    Tests that the password verification function handles invalid parameters.

    Declaration
    [TestMethod]
    public void VerifyPassword_WithInvalidParameters_ReturnsFalse()
    In this article
    Back to top Generated by DocFX