1
+ using System ;
2
+ using System . Net ;
3
+ using System . Threading . Tasks ;
4
+ using Microsoft . AspNetCore . TestHost ;
5
+ using Microsoft . Extensions . Hosting ;
6
+ using Microsoft . Extensions . DependencyInjection ;
7
+ using Microsoft . AspNetCore . Mvc ;
8
+ using NUnit . Framework ;
9
+ using Xtensive . Orm . Web . Tests . Filters ;
10
+ using System . Linq ;
11
+ using System . Diagnostics . CodeAnalysis ;
12
+
13
+ namespace Xtensive . Orm . Web . Tests . Filters
14
+ {
15
+ public class ActionFilterTestController : Controller
16
+ {
17
+ [ SuppressMessage ( "Style" , "IDE0060:Remove unused parameter" , Justification = "parameter required for test" ) ]
18
+ public IActionResult NoDomainAsService ( [ FromServices ] SessionAccessor accessor )
19
+ {
20
+ return Ok ( ) ;
21
+ }
22
+
23
+ public IActionResult NoAccessors ( )
24
+ {
25
+ var sessionObject = ( Session ) HttpContext . Items [ SessionAccessor . SessionIdentifier ] ;
26
+ if ( sessionObject != null )
27
+ return BadRequest ( "Session Initialized" ) ;
28
+ return Ok ( "Ok" ) ;
29
+ }
30
+
31
+ public IActionResult WithOneAccessorNoFilter ( [ FromServices ] SessionAccessor accessor )
32
+ {
33
+ var sessionObject = ( Session ) HttpContext . Items [ SessionAccessor . SessionIdentifier ] ;
34
+ if ( sessionObject == null && ! accessor . ContextIsBound )
35
+ return Ok ( ) ;
36
+ return BadRequest ( "Session initialized or context bound" ) ;
37
+ }
38
+
39
+ public IActionResult WithOneAccessorFromServices ( [ FromServices ] SessionAccessor accessor )
40
+ {
41
+ if ( ! accessor . ContextIsBound )
42
+ return BadRequest ( "ContextIsBound=false" ) ;
43
+ if ( accessor . Session == null )
44
+ return BadRequest ( "SessionIsNull" ) ;
45
+ return Ok ( ) ;
46
+ }
47
+
48
+ public IActionResult WithTwoAccessorsFromServices (
49
+ [ FromServices ] SessionAccessor accessor1 ,
50
+ [ FromServices ] SessionAccessor accessor2 )
51
+ {
52
+ if ( ! accessor1 . ContextIsBound || ! accessor2 . ContextIsBound )
53
+ return BadRequest ( "ContextIsBound=false" ) ;
54
+ if ( accessor1 . Session == null || accessor2 . Session == null )
55
+ return BadRequest ( "SessionIsNull" ) ;
56
+ return Ok ( ) ;
57
+ }
58
+
59
+ [ SuppressMessage ( "Style" , "IDE0060:Remove unused parameter" , Justification = "parameter required for test" ) ]
60
+ public IActionResult ManualSessionRemoval ( [ FromServices ] SessionAccessor accessor )
61
+ {
62
+ _ = HttpContext . Items . Remove ( SessionAccessor . SessionIdentifier ) ;
63
+ return Ok ( ) ;
64
+ }
65
+
66
+ [ SuppressMessage ( "Style" , "IDE0060:Remove unused parameter" , Justification = "parameter required for test" ) ]
67
+ public IActionResult ManualTransactionRemoval ( [ FromServices ] SessionAccessor accessor )
68
+ {
69
+ _ = HttpContext . Items . Remove ( SessionAccessor . ScopeIdentifier ) ;
70
+ return Ok ( ) ;
71
+ }
72
+
73
+ public IActionResult AutoCompleteTransaction ( [ FromServices ] SessionAccessor accessor )
74
+ {
75
+ _ = new DummyEntity ( accessor . Session ) ;
76
+ return Ok ( ) ;
77
+ }
78
+ }
79
+ }
80
+
81
+ namespace Xtensive . Orm . Web . Tests
82
+ {
83
+ public class ActionFilterTest : WebTestBase
84
+ {
85
+ [ Test ]
86
+ public async Task NoDomainAsServiceTest ( )
87
+ {
88
+ var hostBuilder = GetConfiguredHostBuilder < ActionFilterTestController > (
89
+ nameof ( ActionFilterTestController . NoDomainAsService ) ,
90
+ ( s ) => {
91
+ // no domain registration
92
+ AddTestRequiredServices ( s ) ;
93
+ AddControllers ( s ) ;
94
+ } ) ;
95
+
96
+ using ( var host = await hostBuilder . StartAsync ( ) ) {
97
+ var ex = Assert . ThrowsAsync < InvalidOperationException > ( async ( ) => await host . GetTestClient ( ) . GetAsync ( "/" ) ) ;
98
+ Assert . That ( ex . Message . Contains ( "Domain is not found" ) , Is . True ) ;
99
+ }
100
+ }
101
+
102
+ [ Test ]
103
+ public async Task NoAccessorsTest ( )
104
+ {
105
+ var hostBuilder = GetConfiguredHostBuilder < ActionFilterTestController > (
106
+ nameof ( ActionFilterTestController . NoAccessors ) ) ;
107
+
108
+ using ( var host = await hostBuilder . StartAsync ( ) ) {
109
+ var response = await host . GetTestClient ( ) . GetAsync ( "/" ) ;
110
+ Assert . That ( response . StatusCode , Is . EqualTo ( HttpStatusCode . OK ) ) ;
111
+ }
112
+ }
113
+
114
+ [ Test ]
115
+ public async Task WithOneAccessorNoFilterTest ( )
116
+ {
117
+ var hostBuilder = GetConfiguredHostBuilder < ActionFilterTestController > (
118
+ nameof ( ActionFilterTestController . WithOneAccessorNoFilter ) ,
119
+ ( s ) => {
120
+ AddTestRequiredServices ( s ) ;
121
+ AddControllersWithNoFilter ( s ) ;
122
+ } ) ;
123
+
124
+ using ( var host = await hostBuilder . StartAsync ( ) ) {
125
+ var response = await host . GetTestClient ( ) . GetAsync ( "/" ) ;
126
+ Assert . That ( response . StatusCode , Is . EqualTo ( HttpStatusCode . OK ) ) ;
127
+ }
128
+ }
129
+
130
+ [ Test ]
131
+ public async Task WithOneAccessorFromServicesTest ( )
132
+ {
133
+ var hostBuilder = GetConfiguredHostBuilder < ActionFilterTestController > (
134
+ nameof ( ActionFilterTestController . WithOneAccessorFromServices ) ) ;
135
+
136
+ using ( var host = await hostBuilder . StartAsync ( ) ) {
137
+ var response = await host . GetTestClient ( ) . GetAsync ( "/" ) ;
138
+ Assert . That ( response . StatusCode , Is . EqualTo ( HttpStatusCode . OK ) ) ;
139
+ }
140
+ }
141
+
142
+ [ Test ]
143
+ public async Task WithTwoAccessorsFromServicesTest ( )
144
+ {
145
+ var hostBuilder = GetConfiguredHostBuilder < ActionFilterTestController > (
146
+ nameof ( ActionFilterTestController . WithTwoAccessorsFromServices ) ) ;
147
+
148
+ using ( var host = await hostBuilder . StartAsync ( ) ) {
149
+ var response = await host . GetTestClient ( ) . GetAsync ( "/" ) ;
150
+ Assert . That ( response . StatusCode , Is . EqualTo ( HttpStatusCode . OK ) ) ;
151
+ }
152
+ }
153
+
154
+ [ Test ]
155
+ public async Task ManualSessionRemovalTest ( )
156
+ {
157
+ var hostBuilder = GetConfiguredHostBuilder < ActionFilterTestController > (
158
+ nameof ( ActionFilterTestController . ManualSessionRemoval ) ) ;
159
+
160
+ using ( var host = await hostBuilder . StartAsync ( ) ) {
161
+ var ex = Assert . ThrowsAsync < InvalidOperationException > ( async ( ) => await host . GetTestClient ( ) . GetAsync ( "/" ) ) ;
162
+ Assert . That ( ex . Message . Contains ( "Session or TransactionScope no longer exists in HttpContext" ) , Is . True ) ;
163
+ }
164
+ }
165
+
166
+ [ Test ]
167
+ public async Task ManualTransactionRemovalTest ( )
168
+ {
169
+ var hostBuilder = GetConfiguredHostBuilder < ActionFilterTestController > (
170
+ nameof ( ActionFilterTestController . ManualTransactionRemoval ) ) ;
171
+
172
+ using ( var host = await hostBuilder . StartAsync ( ) ) {
173
+ var ex = Assert . ThrowsAsync < InvalidOperationException > ( async ( ) => await host . GetTestClient ( ) . GetAsync ( "/" ) ) ;
174
+ Assert . That ( ex . Message . Contains ( "Session or TransactionScope no longer exists in HttpContext" ) , Is . True ) ;
175
+ }
176
+ }
177
+
178
+ [ Test ]
179
+ public async Task AutoCompleteTransactionTest ( )
180
+ {
181
+ var hostBuilder = GetConfiguredHostBuilder < ActionFilterTestController > (
182
+ nameof ( ActionFilterTestController . AutoCompleteTransaction ) ) ;
183
+
184
+ using ( var host = await hostBuilder . StartAsync ( ) ) {
185
+ var response = await host . GetTestClient ( ) . GetAsync ( "/" ) ;
186
+ Assert . That ( response . StatusCode , Is . EqualTo ( HttpStatusCode . OK ) ) ;
187
+ }
188
+
189
+ await using ( var session = await Domain . OpenSessionAsync ( ) )
190
+ await using ( var tx = await session . OpenTransactionAsync ( ) ) {
191
+ Assert . That ( session . Query . All < DummyEntity > ( ) . FirstOrDefault ( ) , Is . Not . Null ) ;
192
+ }
193
+ }
194
+
195
+ protected override void AddTestRequiredServices ( IServiceCollection services )
196
+ {
197
+ base . AddTestRequiredServices ( services ) ;
198
+ _ = services . AddScoped < SessionAccessor > ( ) ;
199
+ }
200
+
201
+ protected override void AddControllers ( IServiceCollection services )
202
+ {
203
+ _ = services . AddControllers ( options => options . Filters . AddDataObjectsSessionActionFilter ( ) )
204
+ . AddApplicationPart ( typeof ( ActionFilterTest ) . Assembly ) ;
205
+ }
206
+
207
+ private void AddControllersWithNoFilter ( IServiceCollection services )
208
+ {
209
+ _ = services . AddControllers ( )
210
+ . AddApplicationPart ( typeof ( ActionFilterTest ) . Assembly ) ;
211
+ }
212
+ }
213
+ }
0 commit comments