Base solution for your next web application

Activities of "pradippatil"

Does anyone has answer for this?

Thanks

Hi,

Can we disable data filters for whole service or module? If yes, how? I need this because I have to disable MayHaveTenant filter at many places.

Thanks

Answer

Hi,

Finally, I got rid of this issue of using custom session properties in unit testing.

However, I tried the below code as suggested:

Configuration.ReplaceService<IAbpSession, TestGPASession>();

But, didn't work.

So, I tried to use interface IGPASession instead of GPASession concrete class as property type in App Service Base class and registered it in AppTestBase's PreInitilize method and It worked!! Below is working code snippet:

public abstract class GodseyPrecisionAgAppServiceBase : ApplicationService
    {
        public TenantManager TenantManager { get; set; }

        public UserManager UserManager { get; set; }

        //public GPASession GPASession { get; set; }
        public IGPASession GPASession { get; set; }
public abstract class AppTestBase : AbpIntegratedTestBase<GodseyPrecisionAgTestModule>
    {
        private DbConnection _hostDb;
        private Dictionary<int, DbConnection> _tenantDbs; //only used for db per tenant architecture
        public TestGPASession GPASession { get; private set; }

        protected AppTestBase()
        {
            GPASession = Resolve<TestGPASession>();
protected override void PreInitialize()
        {
            LocalIocManager.Register<IGPASession, TestGPASession>();

            base.PreInitialize();

            UseSingleDatabase();
            //UseDatabasePerTenant();
        }

Thanks for the help!

Answer

Hi,

Can you please help me on above issue (using custom session in unit testing)?

Thanks

Answer

I have added a property in application services base class as below:

public abstract class GodseyPrecisionAgAppServiceBase : ApplicationService
    {
        public GPASession GPASession { get; set; }

I am using it in application services as below:

public async Task<ListResultDto<RoleListDto>> GetRoles(GetRolesInput input)
        {
            var roles = await _roleManager
                .Roles
                .Where(r => r.OrgAdminUserId == GPASession.OrgAdminUserId)
                .WhereIf(
                    !input.Permission.IsNullOrWhiteSpace(),
                    r => r.Permissions.Any(rp => rp.Name == input.Permission && rp.IsGranted)
                )
                .ToListAsync();

            return new ListResultDto<RoleListDto>(roles.MapTo<List<RoleListDto>>());
        }

Now, when I tried to write unit tests for this method, GPASession.OrgAdminUserId property is always null, because it reads the value from Claim. So, I created one TestGPASession class as below:

public class TestGPASession : ClaimsAbpSession
    {
        public ITenantIdAccessor TenantIdAccessor { get; set; }

        public TestGPASession(IMultiTenancyConfig multiTenancy)
            : base(multiTenancy)
        {

        }

        public long? ParentUserId { get; set; }

        public long? OrgAdminUserId { get; set; }

        public bool IsSuperAdmin { get; set; }
    }

And done changes to use it in AppTestBase class as below:

public abstract class AppTestBase : AbpIntegratedTestBase<GodseyPrecisionAgTestModule>
    {
        private DbConnection _hostDb;
        private Dictionary<int, DbConnection> _tenantDbs; //only used for db per tenant architecture
        public TestGPASession GPASession { get; private set; }

        protected AppTestBase()
        {
            GPASession = Resolve<TestGPASession>();

            //Seed initial data for host
            AbpSession.TenantId = null;
            UsingDbContext(context =>
            {
                context.EntityChangeEventHelper = NullEntityChangeEventHelper.Instance;
                context.EventBus = NullEventBus.Instance;

                new InitialHostDbBuilder(context).Create();
                new DefaultTenantBuilder(context).Create();
            });

            //Seed initial data for default tenant
            AbpSession.TenantId = 1;
            UsingDbContext(context =>
            {
                context.EntityChangeEventHelper = NullEntityChangeEventHelper.Instance;
                context.EventBus = NullEventBus.Instance;

                new TenantRoleAndUserBuilder(context, 1).Create();
                new TestDataBuilder(context, 1).Create();
            });

            LoginAsDefaultTenantAdmin();
        }

        protected override void PreInitialize()
        {
           LocalIocManager.Register<IAbpSession, TestGPASession>();

            base.PreInitialize();

            UseSingleDatabase();
            //UseDatabasePerTenant();
        }

Now, I am facing an issue with dependency injection, in RoleAppService when I access GPASession object, it always resolved as GPASession not with TestGPASession, though I have registered in PreInitilize method as LocalIocManager.Register<IAbpSession, TestGPASession>();. Can you please tell me what I am missing here?

Thanks

Answer

Hi,

I have added new properties in session as mentioned in my previous comment. (#2144@c5b10a0c-d398-45c3-b017-8cad6e9af9bc)

Now, I want to use this new property in Unit Tests. Can you please help me to figure out how this new property can be used in Unit Tests?

Thanks

Answer

Thanks!

Answer

Hi,

I have added a custom claim in login post method as below:

loginResult.Identity.AddClaim(new Claim("ParentUserId", Convert.ToString(user.ParentUserId)));

I have also added a property in AspNetZeroAbpSession class as below:

public long? ParentUserId
        {
            get
            {
                var parentUserIdClaim = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == "ParentUserId");
                if (string.IsNullOrEmpty(parentUserIdClaim?.Value))
                {
                    return null;
                }

                return Convert.ToInt64(parentUserIdClaim.Value);
            }
        }

Now, I want to access this new property using AbpSession like AbpSession.ParentUserId, but I am not able to access it like that because IAbpSession interface does not have this new property. Is there any way to access this new property using AbpSession like AbpSession.ParentUserId?

Thanks

Thanks!

This is what I wanted.

Thanks again!

Hi,

I think you still didn't get me.

I do not want to update the existing functionality of the AbpAuditLogs table.

I want to change the values of entities fields like "CreatorUserId", "LastModifiedUserId", and "DeletedUserId". These are the fields of all auditable entities (like AbpUsers, AbpRoles etc) and not the AbpAuditLogs table.

Currently, irrespective of who is performing operation i.e. whether actual user or impersonator, these fields are updated by AbpSession.UserId (which always contains the actual user id, not the impersonator use id).

Hence, I want to save Impersonator User Id value in fields like "CreatorUserId", "LastModifiedUserId", and "DeletedUserId" for each entity, so that I can get who has performed that particular operation on any entity without looking into AbpAuditLogs.

Thanks

Showing 11 to 20 of 29 entries