Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Test for login functionality #6909

Open
Rafa3lP opened this issue Mar 13, 2024 · 1 comment
Open

[Question] Test for login functionality #6909

Rafa3lP opened this issue Mar 13, 2024 · 1 comment

Comments

@Rafa3lP
Copy link

Rafa3lP commented Mar 13, 2024

How can I create a test to verify my API login functionality? In the context of ABP, login occurs in the Web.Core module, specifically in the TokenAuthController. I am experiencing challenges when trying to use TokenAuthController in my testing and am seeking guidance on the most effective approach to accomplish this task.

public class Login_Tests : MyProjectTestBase
{
    private readonly ITokenAuthController _tokenAuthController;

    public Login_Tests()
    {
        _tokenAuthController = Resolve<ITokenAuthController>();
    }

    protected override void PreInitialize()
    {
        LocalIocManager.RegisterIfNot<IExternalAuthConfiguration, ExternalAuthConfiguration>();
        LocalIocManager.RegisterIfNot<IExternalAuthManager, ExternalAuthManager>();
        LocalIocManager.Register<TokenAuthConfiguration>();
        LocalIocManager.RegisterIfNot<ITokenAuthController, TokenAuthController>();
    }

    [Fact]
    public async Task Should_Successfully_Login_As_Administrator()
    {
        // Arrange
        var authModel = new AuthenticateModel()
        {
            Password = "123qwe",
            RememberClient = true,
            UserNameOrEmailAddress = "[email protected]",
        };

        var result = await _tokenAuthController.Authenticate(authModel);
        // Assert
        result.AccessToken.ShouldNotBe(null);

    }
}

Abp package version: 6.0.0
Base Framework: .Net Core 5

@ismcagdas
Copy link
Member

You can take a look at this project to test your Controllers https://github.com/aspnetboilerplate/aspnetboilerplate/tree/dev/test/Abp.AspNetCore.Tests. But, since this requires DB connection, you also need to use InMemory db in your tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants