Example #1
0
    public void SetStoreInfo()
    {
        var services = new ServiceCollection();

        services.AddMultiTenant().WithInMemoryStore().WithStaticStrategy("initech");
        var sp = services.BuildServiceProvider();
        var ti = new TenantInfo("initech", "initech", null, null, null);

        sp.GetService <IMultiTenantStore>().TryAddAsync(ti).Wait();

        var context = CreateHttpContextMock(sp).Object;

        var mw = new MultiTenantMiddleware(null);

        mw.Invoke(context).Wait();

        var resolvedContext = (MultiTenantContext)context.Items[Finbuckle.MultiTenant.AspNetCore.Constants.HttpContextMultiTenantContext];

        Assert.NotNull(resolvedContext.StoreInfo);
        Assert.NotNull(resolvedContext.StoreInfo.Store);

        // Test that the wrapper store was "unwrapped"
        Assert.Equal(typeof(InMemoryStore), resolvedContext.StoreInfo.StoreType);
        Assert.IsType <InMemoryStore>(resolvedContext.StoreInfo.Store);

        Assert.NotNull(resolvedContext.StoreInfo.MultiTenantContext);
    }
Example #2
0
    async void SetMultiTenantContextAccessor()
    {
        var services = new ServiceCollection();

        services.AddMultiTenant <TenantInfo>().
        WithStaticStrategy("initech").
        WithInMemoryStore();
        var sp    = services.BuildServiceProvider();
        var store = sp.GetService <IMultiTenantStore <TenantInfo> >();

        store.TryAddAsync(new TenantInfo {
            Id = "initech", Identifier = "initech"
        }).Wait();

        var context = new Mock <HttpContext>();

        context.Setup(c => c.RequestServices).Returns(sp);

        var mw = new MultiTenantMiddleware(c => {
            var accessor = c.RequestServices.GetRequiredService <IMultiTenantContextAccessor <TenantInfo> >();
            var resolver = c.RequestServices.GetRequiredService <ITenantResolver <TenantInfo> >();
            Assert.NotNull(accessor.MultiTenantContext);
            return(Task.CompletedTask);
        });

        await mw.Invoke(context.Object);
    }
Example #3
0
    public void HandleRemoteAuthenticationResolutionIfUsingWithRemoteAuthentication()
    {
        // Create remote strategy mock
        var remoteResolverMock = new Mock <RemoteAuthenticationStrategy>();

        remoteResolverMock.Setup <Task <string> >(o => o.GetIdentifierAsync(It.IsAny <object>())).Returns(Task.FromResult("initech"));

        var services = new ServiceCollection();

        services.AddAuthentication();
        services.AddSingleton <RemoteAuthenticationStrategy>(remoteResolverMock.Object);
        services.AddMultiTenant().WithInMemoryStore().WithDelegateStrategy(o => Task.FromResult <string>(null)).WithRemoteAuthentication();

        // Substitute in the mocks...
        var removed = services.Remove(ServiceDescriptor.Singleton <RemoteAuthenticationStrategy, RemoteAuthenticationStrategy>());

        services.AddSingleton <RemoteAuthenticationStrategy>(_sp => remoteResolverMock.Object);

        var sp      = services.BuildServiceProvider();
        var mock    = CreateHttpContextMock(sp);
        var context = mock.Object;

        var mw = new MultiTenantMiddleware(null);

        mw.Invoke(context).Wait();
        remoteResolverMock.Verify(r => r.GetIdentifierAsync(context));

        // Check that the remote strategy was set in the multitenant context.
        Assert.IsAssignableFrom <RemoteAuthenticationStrategy>(context.GetMultiTenantContext().StrategyInfo.Strategy);
    }
Example #4
0
    public void HandleRemoteAuthenticationResolutionIfUsingWithRemoteAuthentication()
    {
        var services = new ServiceCollection();

        services.AddAuthentication();
        services.AddMultiTenant().WithInMemoryStore().WithStrategy <NullStrategy>(ServiceLifetime.Singleton).WithRemoteAuthentication();

        // Substitute in the mock...
        services.Remove(ServiceDescriptor.Singleton <IRemoteAuthenticationStrategy, RemoteAuthenticationStrategy>());
        var remoteResolverMock = new Mock <RemoteAuthenticationStrategy>();

        services.AddSingleton <IRemoteAuthenticationStrategy>(_sp => remoteResolverMock.Object);
        var sp = services.BuildServiceProvider();

        var mock    = CreateHttpContextMock(sp);
        var context = mock.Object;

        var mw = new MultiTenantMiddleware(null);

        mw.Invoke(context).Wait();
        remoteResolverMock.Verify(r => r.GetIdentifierAsync(context));

        // Check that the remote strategy was set in the multitenant context.
        Assert.IsAssignableFrom <RemoteAuthenticationStrategy>(context.GetMultiTenantContext().StrategyInfo.Strategy);
    }
Example #5
0
    public void CycleThroughStrategies()
    {
        var      services = new ServiceCollection();
        int      count1   = 0;
        int      count2   = 0;
        int      count3   = 0;
        DateTime time1    = DateTime.Now;
        DateTime time2    = DateTime.Now;
        DateTime time3    = DateTime.Now;

        services.AddMultiTenant().WithInMemoryStore()
        .WithStrategy <TestStrat1>(ServiceLifetime.Singleton, _ =>
                                   new TestStrat1(c =>
        {
            count1++;
            time1 = DateTime.Now;
            Thread.Sleep(100);
            return(Task.FromResult <string>(null));
        }))
        .WithStrategy <TestStrat2>(ServiceLifetime.Singleton, _ =>
                                   new TestStrat2(c =>
        {
            count2++;
            time2 = DateTime.Now;
            Thread.Sleep(100);
            return(Task.FromResult <string>(null));
        }))
        .WithStrategy <TestStrat3>(ServiceLifetime.Singleton, _ =>
                                   new TestStrat3(c =>
        {
            count3++;
            time3 = DateTime.Now;
            Thread.Sleep(100);
            return(Task.FromResult <string>(null));
        }));

        var sp      = services.BuildServiceProvider();
        var context = CreateHttpContextMock(sp).Object;

        var mw = new MultiTenantMiddleware(null);

        mw.Invoke(context).Wait();

        Assert.Equal(1, count1);
        Assert.Equal(1, count2);
        Assert.Equal(1, count3);
        Assert.True(time1 < time2 && time2 < time3);
    }
Example #6
0
    public void SetContextItemToNullIfNoTenant()
    {
        var services = new ServiceCollection();

        services.AddAuthentication();
        services.AddMultiTenant().WithInMemoryStore().WithStaticStrategy("initech");
        var sp = services.BuildServiceProvider();

        var mock    = CreateHttpContextMock(sp);
        var context = mock.Object;

        var mw = new MultiTenantMiddleware(null);

        mw.Invoke(context).Wait();

        var resolveTenantContext = (TenantContext)context.Items[Finbuckle.MultiTenant.AspNetCore.Constants.HttpContextTenantContext];

        Assert.Null(resolveTenantContext);
    }
Example #7
0
    public void ResolveTenantRequest()
    {
        var services = new ServiceCollection();

        services.AddMultiTenant().WithInMemoryStore().WithStaticStrategy("initech");
        var sp = services.BuildServiceProvider();
        var tc = new TenantContext("initech", "initech", null, null, null, null);

        sp.GetService <IMultiTenantStore>().TryAdd(tc);

        var context = CreateHttpContextMock(sp).Object;

        var mw = new MultiTenantMiddleware(null);

        mw.Invoke(context).Wait();

        var resolveTenantContext = (TenantContext)context.Items[Finbuckle.MultiTenant.AspNetCore.Constants.HttpContextTenantContext];

        Assert.Equal("initech", resolveTenantContext.Id);
    }
Example #8
0
    public void HandleTenantIdentifierNotFoundInStore()
    {
        var services = new ServiceCollection();

        services.AddAuthentication();
        services.AddMultiTenant().WithInMemoryStore().WithStaticStrategy("initech");
        var sp = services.BuildServiceProvider();

        var mock    = CreateHttpContextMock(sp);
        var context = mock.Object;

        var mw = new MultiTenantMiddleware(null);

        mw.Invoke(context).Wait();

        var multiTenantContext = (MultiTenantContext)context.Items[Finbuckle.MultiTenant.AspNetCore.Constants.HttpContextMultiTenantContext];

        Assert.Null(multiTenantContext.TenantInfo);
        Assert.Null(multiTenantContext.StoreInfo);
        Assert.NotNull(multiTenantContext.StrategyInfo);
    }
Example #9
0
    public void SkipRemoteAuthenticationResolutionIfNotUsingWithRemoteAuthentication()
    {
        var services = new ServiceCollection();

        services.AddAuthentication();
        services.AddMultiTenant().WithInMemoryStore().WithStrategy <NullStrategy>(ServiceLifetime.Singleton);

        // Add in the mock...
        var remoteResolverMock = new Mock <RemoteAuthenticationStrategy>();

        services.AddSingleton <IRemoteAuthenticationStrategy>(_sp => remoteResolverMock.Object);
        var sp = services.BuildServiceProvider();

        var mock    = CreateHttpContextMock(sp);
        var context = mock.Object;

        var mw = new MultiTenantMiddleware(null);

        mw.Invoke(context).Wait();
        remoteResolverMock.Verify(r => r.GetIdentifierAsync(context), Times.Never);
    }
Example #10
0
    public void HandleRemoteAuthenticationResolutionIfUsingWithRemoteAuthentication()
    {
        var services = new ServiceCollection();

        services.AddAuthentication();
        services.AddMultiTenant().WithInMemoryStore().WithStrategy <NullStrategy>().WithRemoteAuthentication();

        // Substitute in the mock...
        services.Remove(ServiceDescriptor.Singleton <IRemoteAuthenticationStrategy, RemoteAuthenticationStrategy>());
        var remoteResolverMock = new Mock <RemoteAuthenticationStrategy>();

        services.AddSingleton <IRemoteAuthenticationStrategy>(_sp => remoteResolverMock.Object);
        var sp = services.BuildServiceProvider();

        var mock    = CreateHttpContextMock(sp);
        var context = mock.Object;

        var mw = new MultiTenantMiddleware(null);

        mw.Invoke(context).Wait();
        remoteResolverMock.Verify(r => r.GetIdentifier(context));
    }
Example #11
0
    public void AlwaysHandleFallbackStrategyLast()
    {
        var services = new ServiceCollection();

        services.AddAuthentication();
        services.AddMultiTenant().WithInMemoryStore()
        .WithFallbackStrategy("default")
        .WithDelegateStrategy(async o => await Task.FromResult <string>(null));
        var sp = services.BuildServiceProvider();

        var mock    = CreateHttpContextMock(sp);
        var context = mock.Object;

        var mw = new MultiTenantMiddleware(null);

        mw.Invoke(context).Wait();

        var multiTenantContext = (MultiTenantContext)context.Items[Finbuckle.MultiTenant.AspNetCore.Constants.HttpContextMultiTenantContext];

        Assert.Null(multiTenantContext.TenantInfo);
        Assert.Null(multiTenantContext.StoreInfo);
        Assert.NotNull(multiTenantContext.StrategyInfo);
        Assert.IsType <FallbackStrategy>(multiTenantContext.StrategyInfo.Strategy);
    }