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 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 ClearAllOptionsForClearAll()
    {
        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 all options.
        cache.ClearAll();

        // 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 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);
    }
    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 ?? ""));
    }
Example #5
0
    public void ClearOptionsForTenantIdOnly()
    {
        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 options on first tenant.
        cache.Clear("test-id-123");

        // 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["test-id-123"]);

        Assert.True(tenantInternalCache.IsEmpty);

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