コード例 #1
0
    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);
    }
コード例 #2
0
    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);
    }
コード例 #3
0
    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));
    }