public void GetOrAddNamedOptionForCurrentTenantOnly(string name)
    {
        var ti = new TenantInfo {
            Id = "test-id-123"
        };
        var tc = new MultiTenantContext <TenantInfo>();

        tc.TenantInfo = ti;
        var tca = new MultiTenantContextAccessor <TenantInfo>();

        tca.MultiTenantContext = tc;
        var cache = new MultiTenantOptionsCache <TestOptions, TenantInfo>(tca);

        var options  = new TestOptions();
        var options2 = new TestOptions();

        // Add new options.
        var result = cache.GetOrAdd(name, () => options);

        Assert.Same(options, result);

        // Get the existing options if exists.
        result = cache.GetOrAdd(name, () => options2);
        Assert.NotSame(options2, result);

        // Confirm different tenant on same object is an add (ie it didn't exist there).
        ti.Id  = "diff_id";
        result = cache.GetOrAdd(name, () => options2);
        Assert.Same(options2, result);
    }
    public void RemoveOptionsForAllTenants(string name)
    {
        var ti = new TenantInfo("test-id-123", null, null, null, null);
        var tc = new MultiTenantContext();

        tc.TenantInfo = ti;
        var tca   = new TestMultiTenantContextAccessor(tc);
        var cache = new MultiTenantOptionsCache <CookieAuthenticationOptions>(tca);

        var options = new CookieAuthenticationOptions();

        // Add new options.
        var result = cache.TryAdd(name, options);

        Assert.True(result);

        // Add under a different tenant.
        ti.Id  = "diff_id";
        result = cache.TryAdd(name, options);
        Assert.True(result);

        // Remove all options and assert empty.
        result = cache.TryRemove(name);
        Assert.True(result);
        Assert.Empty((IEnumerable)cache.GetType().BaseType.
                     GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance).
                     GetValue(cache));
    }
    public void AddNamedOptionsForCurrentTenantOnlyOnAdd(string name)
    {
        var ti = new TenantInfo {
            Id = "test-id-123"
        };
        var tc = new MultiTenantContext <TenantInfo>();

        tc.TenantInfo = ti;
        var tca = new MultiTenantContextAccessor <TenantInfo>();

        tca.MultiTenantContext = tc;
        var cache = new MultiTenantOptionsCache <TestOptions, TenantInfo>(tca);

        var options = new TestOptions();

        // Add new options.
        var result = cache.TryAdd(name, options);

        Assert.True(result);

        // Fail adding options under same name.
        result = cache.TryAdd(name, options);
        Assert.False(result);

        // Change the tenant id and confirm options can be added again.
        ti.Id  = "diff_id";
        result = cache.TryAdd(name, options);
        Assert.True(result);
    }
Exemplo n.º 4
0
    public void GetOrAddNamedOptionForCurrentTenantOnly(string name)
    {
        var ti = new TenantInfo("test-id-123", null, null, null, null);
        var tc = new MultiTenantContext();

        tc.TenantInfo = ti;
        var tca   = new TestMultiTenantContextAccessor(tc);
        var cache = new MultiTenantOptionsCache <CookieAuthenticationOptions>(tca);

        var options = new CookieAuthenticationOptions();

        options.Cookie.Name = "a_name";
        var options2 = new CookieAuthenticationOptions();

        options2.Cookie.Name = "diff_name";

        // Add new options.
        var result = cache.GetOrAdd(name, () => options);

        Assert.Same(options, result);

        // Get the existing options if exists.
        result = cache.GetOrAdd(name, () => options2);
        Assert.NotSame(options2, result);

        // Confirm different tenant on same object is an add (ie it didn't exist there).
        ti.Id  = "diff_id";
        result = cache.GetOrAdd(name, () => options2);
        Assert.Same(options2, result);
    }
    public void AdjustedOptionsNameOnAdd(string name)
    {
        var ti = new TenantInfo("test-id-123", null, null, null, null);
        var tc = new MultiTenantContext();

        tc.TenantInfo = ti;
        var tca   = new TestMultiTenantContextAccessor(tc);
        var cache = new MultiTenantOptionsCache <CookieAuthenticationOptions>(tca);

        var options = new CookieAuthenticationOptions();

        // Add new options.
        var result = cache.TryAdd(name, options);

        Assert.True(result);

        // Fail adding options under same name.
        result = cache.TryAdd(name, options);
        Assert.False(result);

        // Change the TC id and confirm options can be added again.
        ti.Id  = "diff_id";
        result = cache.TryAdd(name, options);
        Assert.True(result);
    }
    public void AdjustOptionsNameOnGetOrAdd(string name)
    {
        var tc    = new TenantContext("test-id-123", null, null, null, null, null);
        var tca   = new TestTenantContextAccessor(tc);
        var cache = new MultiTenantOptionsCache <CookieAuthenticationOptions>(tca, (o, context) =>
        {
            o.Cookie.Name = context.Id;
        });

        var options = new CookieAuthenticationOptions();

        options.Cookie.Name = "a_name";
        var options2 = new CookieAuthenticationOptions();

        options2.Cookie.Name = "diff_name";

        // Add new options.
        var result = cache.GetOrAdd(name, () => options);

        Assert.Equal(options.Cookie.Name, result.Cookie.Name);

        // Get the existing object.
        result = cache.GetOrAdd(name, () => options2);
        Assert.NotEqual(options2.Cookie.Name, result.Cookie.Name);

        // Confirm different tenant on same object is an add.
        tc.GetType().GetProperty("Id").SetValue(tc, "diff_id");
        result = cache.GetOrAdd(name, () => options2);
        Assert.Equal(options2.Cookie.Name, result.Cookie.Name);
    }
    public void AdjustedOptionsNameOnAdd(string name)
    {
        var tc    = new TenantContext("test-id-123", null, null, null, null, null);
        var tca   = new TestTenantContextAccessor(tc);
        var cache = new MultiTenantOptionsCache <CookieAuthenticationOptions>(tca, (o, context) =>
        {
            o.Cookie.Name = context.Id;
        });

        var options = new CookieAuthenticationOptions();

        // Add new options.
        var result = cache.TryAdd(name, options);

        Assert.True(result);

        // Fail adding options under same name.
        result = cache.TryAdd(name, options);
        Assert.False(result);

        // Change the TC id and confirm options can be added again.
        tc.GetType().GetProperty("Id").SetValue(tc, "diff_id");
        result = cache.TryAdd(name, options);
        Assert.True(result);
    }
    public void RemoveOptionsForAllTenants(string name)
    {
        var tc    = new TenantContext("test-id-123", null, null, null, null, null);
        var tca   = new TestTenantContextAccessor(tc);
        var cache = new MultiTenantOptionsCache <CookieAuthenticationOptions>(tca, (o, context) =>
        {
            o.Cookie.Name = context.Id;
        });

        var options = new CookieAuthenticationOptions();

        // Add new options.
        var result = cache.TryAdd(name, options);

        Assert.True(result);

        tc.GetType().GetProperty("Id").SetValue(tc, "diff_id");
        result = cache.TryAdd(name, options);
        Assert.True(result);

        // Remove all options.
        result = cache.TryRemove(name);
        Assert.True(result);
        Assert.Empty((IEnumerable)cache.GetType().BaseType.
                     GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance).
                     GetValue(cache));
    }
    public void AdjustOptionsNameOnGetOrAdd(string name)
    {
        var ti = new TenantInfo("test-id-123", null, null, null, null);
        var tc = new MultiTenantContext();

        tc.TenantInfo = ti;
        var tca   = new TestMultiTenantContextAccessor(tc);
        var cache = new MultiTenantOptionsCache <CookieAuthenticationOptions>(tca);

        var options = new CookieAuthenticationOptions();

        options.Cookie.Name = "a_name";
        var options2 = new CookieAuthenticationOptions();

        options2.Cookie.Name = "diff_name";

        // Add new options.
        var result = cache.GetOrAdd(name, () => options);

        Assert.Equal(options.Cookie.Name, result.Cookie.Name);

        // Get the existing object.
        result = cache.GetOrAdd(name, () => options2);
        Assert.NotEqual(options2.Cookie.Name, result.Cookie.Name);

        // Confirm different tenant on same object is an add.
        ti.Id  = "diff_id";
        result = cache.GetOrAdd(name, () => options2);
        Assert.Equal(options2.Cookie.Name, result.Cookie.Name);
    }
Exemplo n.º 10
0
    public void ThrowsIfGetOrAddFactoryIsNull()
    {
        var tc    = new MultiTenantContext();
        var tca   = new TestMultiTenantContextAccessor(tc);
        var cache = new MultiTenantOptionsCache <CookieAuthenticationOptions>(tca);

        Assert.Throws <ArgumentNullException>(() => cache.GetOrAdd("", null));
    }
    public void ThrowsIfGetOrAddFactoryIsNull()
    {
        var tc  = new MultiTenantContext <TenantInfo>();
        var tca = new MultiTenantContextAccessor <TenantInfo>();

        tca.MultiTenantContext = tc;
        var cache = new MultiTenantOptionsCache <TestOptions, TenantInfo>(tca);

        Assert.Throws <ArgumentNullException>(() => cache.GetOrAdd("", null));
    }
    public void ThrowsIfGetOtAddFactoryIsNull()
    {
        var tc    = new TenantContext("test-id-123", null, null, null, null, null);
        var tca   = new TestTenantContextAccessor(tc);
        var cache = new MultiTenantOptionsCache <CookieAuthenticationOptions>(tca, (o, context) =>
        {
            o.Cookie.Name = context.Id;
        });

        Assert.Throws <ArgumentNullException>(() => cache.GetOrAdd("", null));
    }
    public void HandleNullMultiTenantContextOnGetOrAdd()
    {
        var tca   = new MultiTenantContextAccessor <TenantInfo>();
        var cache = new MultiTenantOptionsCache <TestOptions, TenantInfo>(tca);

        var options = new TestOptions();

        // Add new options, ensure no exception caused by null MultiTenantContext.
        var result = cache.GetOrAdd("", () => options);

        Assert.NotNull(result);
    }
Exemplo n.º 14
0
    public void HandleNullMultiTenantContextOnAdd()
    {
        var tca   = new TestMultiTenantContextAccessor(null);
        var cache = new MultiTenantOptionsCache <CookieAuthenticationOptions>(tca);

        var options = new CookieAuthenticationOptions();

        // Add new options, ensure no exception caused by null MultiTenantContext.
        var result = cache.TryAdd("", options);

        Assert.True(result);
    }
    public void HandleNullMultiTenantContextWhenAdjustedOptionsNameOnGetOrAdd()
    {
        var tca   = new TestMultiTenantContextAccessor(null);
        var cache = new MultiTenantOptionsCache <CookieAuthenticationOptions>(tca);

        var options = new CookieAuthenticationOptions();

        // Add new options, ensure no exception caused by null MultiTenantContext.
        var result = cache.GetOrAdd("", () => options);

        Assert.NotNull(result);
    }
    public void RemoveNamedOptionsForCurrentTenantOnly(string name)
    {
        var ti = new TenantInfo {
            Id = "test-id-123"
        };
        var tc = new MultiTenantContext <TenantInfo>();

        tc.TenantInfo = ti;
        var tca = new MultiTenantContextAccessor <TenantInfo>();

        tca.MultiTenantContext = tc;
        var cache = new MultiTenantOptionsCache <TestOptions, TenantInfo>(tca);

        var options = new TestOptions();

        // Add new options.
        var result = cache.TryAdd(name, options);

        Assert.True(result);

        // Add under a different tenant.
        ti.Id  = "diff_id";
        result = cache.TryAdd(name, options);
        Assert.True(result);
        result = cache.TryAdd("diffname", options);
        Assert.True(result);

        // Remove named options for current tenant.
        result = cache.TryRemove(name);
        Assert.True(result);
        var tenantCache = (ConcurrentDictionary <string, IOptionsMonitorCache <TestOptions> >)cache.GetType().
                          GetField("map", BindingFlags.NonPublic | BindingFlags.Instance).
                          GetValue(cache);

        dynamic tenantInternalCache = tenantCache[ti.Id].GetType().GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance)
                                      .GetValue(tenantCache[ti.Id]);

        // Assert named options removed and other options on tenant left as-is.
        Assert.False(tenantInternalCache.Keys.Contains(name ?? ""));
        Assert.True(tenantInternalCache.Keys.Contains("diffname"));

        // Assert other tenant not affected.
        ti.Id = "test-id-123";
        tenantInternalCache = tenantCache[ti.Id].GetType().GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance)
                              .GetValue(tenantCache[ti.Id]);
        Assert.True(tenantInternalCache.ContainsKey(name ?? ""));
    }
    public void ClearOptionsForCurrentTenantOnly()
    {
        var ti = new TenantInfo {
            Id = "test-id-123"
        };
        var tc = new MultiTenantContext <TenantInfo>();

        tc.TenantInfo = ti;
        var tca = new MultiTenantContextAccessor <TenantInfo>();

        tca.MultiTenantContext = tc;
        var cache = new MultiTenantOptionsCache <TestOptions, TenantInfo>(tca);

        var options = new TestOptions();

        // Add new options.
        var result = cache.TryAdd("", options);

        Assert.True(result);

        // Add under a different tenant.
        ti.Id  = "diff_id";
        result = cache.TryAdd("", options);
        Assert.True(result);

        // Clear options on first tenant.
        ti.Id = "test-id-123";
        cache.Clear();

        // Assert options cleared on this tenant.
        var tenantCache = (ConcurrentDictionary <string, IOptionsMonitorCache <TestOptions> >)cache.GetType().
                          GetField("map", BindingFlags.NonPublic | BindingFlags.Instance).
                          GetValue(cache);

        dynamic tenantInternalCache = tenantCache[ti.Id].GetType().GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance)
                                      .GetValue(tenantCache[ti.Id]);

        Assert.True(tenantInternalCache.IsEmpty);

        // Assert options still exist on other tenant.
        ti.Id = "diff_id";
        tenantInternalCache = tenantCache[ti.Id].GetType().GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance)
                              .GetValue(tenantCache[ti.Id]);
        Assert.False(tenantInternalCache.IsEmpty);
    }
Exemplo n.º 18
0
    public void ClearAllOptionsForClearAll()
    {
        var ti = new TenantInfo("test-id-123", null, null, null, null);
        var tc = new MultiTenantContext();

        tc.TenantInfo = ti;
        var tca   = new TestMultiTenantContextAccessor(tc);
        var cache = new MultiTenantOptionsCache <CookieAuthenticationOptions>(tca);

        var options = new CookieAuthenticationOptions();

        // Add new options.
        var result = cache.TryAdd("", options);

        Assert.True(result);

        // Add under a different tenant.
        ti.Id  = "diff_id";
        result = cache.TryAdd("", options);
        Assert.True(result);

        // Clear all options.
        cache.ClearAll();

        // Assert options cleared on this tenant.
        var tenantCache = (ConcurrentDictionary <string, IOptionsMonitorCache <CookieAuthenticationOptions> >)cache.GetType().
                          GetField("map", BindingFlags.NonPublic | BindingFlags.Instance).
                          GetValue(cache);

        dynamic tenantInternalCache = tenantCache[ti.Id].GetType().GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance)
                                      .GetValue(tenantCache[ti.Id]);

        Assert.True(tenantInternalCache.IsEmpty);

        // Assert options cleared on other tenant.
        ti.Id = "diff_id";
        tenantInternalCache = tenantCache[ti.Id].GetType().GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance)
                              .GetValue(tenantCache[ti.Id]);
        Assert.True(tenantInternalCache.IsEmpty);
    }