-
-
Notifications
You must be signed in to change notification settings - Fork 253
/
HttpContextExtensionShould.cs
147 lines (117 loc) · 5.43 KB
/
HttpContextExtensionShould.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright Finbuckle LLC, Andrew White, and Contributors.
// Refer to the solution LICENSE file for more inforation.
using System;
using Finbuckle.MultiTenant.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Xunit;
namespace Finbuckle.MultiTenant.AspNetCore.Test.Extensions
{
public class HttpContextExtensionShould
{
[Fact]
public void GetTenantContextIfExists()
{
var ti = new TenantInfo { Id = "test" };
var tc = new MultiTenantContext<TenantInfo>();
tc.TenantInfo = ti;
var services = new ServiceCollection();
services.AddScoped<IMultiTenantContextAccessor<TenantInfo>>(_ => new AsyncLocalMultiTenantContextAccessor<TenantInfo>{ MultiTenantContext = tc });
var sp = services.BuildServiceProvider();
var httpContextMock = new Mock<HttpContext>();
httpContextMock.Setup(c => c.RequestServices).Returns(sp);
var mtc = httpContextMock.Object.GetMultiTenantContext<TenantInfo>();
Assert.Same(tc, mtc);
}
[Fact]
public void ReturnNullIfNoMultiTenantContext()
{
var services = new ServiceCollection();
services.AddScoped<IMultiTenantContextAccessor<TenantInfo>>(_ => new AsyncLocalMultiTenantContextAccessor<TenantInfo>());
var sp = services.BuildServiceProvider();
var httpContextMock = new Mock<HttpContext>();
httpContextMock.Setup(c => c.RequestServices).Returns(sp);
var mtc = httpContextMock.Object.GetMultiTenantContext<TenantInfo>();
Assert.Null(mtc);
}
[Fact]
public void SetTenantInfo()
{
var services = new ServiceCollection();
services.AddMultiTenant<TenantInfo>();
var sp = services.BuildServiceProvider();
var httpContextMock = new Mock<HttpContext>();
httpContextMock.Setup(c => c.RequestServices).Returns(sp);
var context = httpContextMock.Object;
var ti2 = new TenantInfo { Id = "tenant2" };
var res = context.TrySetTenantInfo(ti2, false);
var mtc = context.GetMultiTenantContext<TenantInfo>();
Assert.True(res);
Assert.Same(ti2, mtc!.TenantInfo);
}
[Fact]
public void SetMultiTenantContextAcccessor()
{
var services = new ServiceCollection();
services.AddMultiTenant<TenantInfo>();
var sp = services.BuildServiceProvider();
var httpContextMock = new Mock<HttpContext>();
httpContextMock.Setup(c => c.RequestServices).Returns(sp);
var context = httpContextMock.Object;
var ti2 = new TenantInfo { Id = "tenant2" };
var res = context.TrySetTenantInfo(ti2, false);
var mtc = context.GetMultiTenantContext<TenantInfo>();
var accessor = context.RequestServices.GetRequiredService<IMultiTenantContextAccessor<TenantInfo>>();
Assert.True(res);
Assert.Same(mtc, accessor.MultiTenantContext);
}
[Fact]
public void SetStoreInfoAndStrategyInfoNull()
{
var services = new ServiceCollection();
services.AddMultiTenant<TenantInfo>();
var sp = services.BuildServiceProvider();
var httpContextMock = new Mock<HttpContext>();
httpContextMock.Setup(c => c.RequestServices).Returns(sp);
var context = httpContextMock.Object;
var ti2 = new TenantInfo { Id = "tenant2" };
context.TrySetTenantInfo(ti2, false);
var mtc = context.GetMultiTenantContext<TenantInfo>();
Assert.Null(mtc?.StoreInfo);
Assert.Null(mtc?.StrategyInfo);
}
[Fact]
public void ResetScopeIfApplicable()
{
var httpContextMock = new Mock<HttpContext>();
httpContextMock.SetupProperty(c => c.RequestServices);
var services = new ServiceCollection();
services.AddScoped<object>(_ => DateTime.Now);
services.AddMultiTenant<TenantInfo>();
var sp = services.BuildServiceProvider();
httpContextMock.Object.RequestServices = sp;
var ti2 = new TenantInfo { Id = "tenant2" };
httpContextMock.Object.TrySetTenantInfo(ti2, true);
Assert.NotSame(sp, httpContextMock.Object.RequestServices);
Assert.NotStrictEqual((DateTime?)sp.GetService<object>(),
(DateTime?)httpContextMock.Object.RequestServices.GetService<object>());
}
[Fact]
public void NotResetScopeIfNotApplicable()
{
var httpContextMock = new Mock<HttpContext>();
httpContextMock.SetupProperty(c => c.RequestServices);
var services = new ServiceCollection();
services.AddScoped<object>(_ => DateTime.Now);
services.AddMultiTenant<TenantInfo>();
var sp = services.BuildServiceProvider();
httpContextMock.Object.RequestServices = sp;
var ti2 = new TenantInfo { Id = "tenant2" };
httpContextMock.Object.TrySetTenantInfo(ti2, false);
Assert.Same(sp, httpContextMock.Object.RequestServices);
Assert.StrictEqual((DateTime?)sp.GetService<object>(),
(DateTime?)httpContextMock.Object.RequestServices.GetService<object>());
}
}
}