1
+ using System ;
2
+ using System . Linq ;
3
+ using System . Net ;
4
+ using System . Net . Http ;
5
+ using System . Net . Http . Headers ;
6
+ using System . Text ;
7
+ using System . Threading . Tasks ;
8
+ using Abp . AspNetCore . TestBase ;
9
+ using Abp . Authorization . Users ;
10
+ using Abp . Extensions ;
11
+ using Abp . Json ;
12
+ using Abp . MultiTenancy ;
13
+ using Abp . Web . Models ;
14
+ using AbpCompanyName . AbpProjectName . EntityFrameworkCore ;
15
+ using AbpCompanyName . AbpProjectName . Models . TokenAuth ;
16
+ using AbpCompanyName . AbpProjectName . Web . Startup ;
17
+ using AngleSharp . Html . Dom ;
18
+ using AngleSharp . Html . Parser ;
19
+ using Microsoft . AspNetCore . Hosting ;
20
+ using Newtonsoft . Json ;
21
+ using Newtonsoft . Json . Serialization ;
22
+ using Shouldly ;
23
+
24
+ namespace AbpCompanyName . AbpProjectName . Web . Tests
25
+ {
26
+ public abstract class AbpProjectNameWebTestBase : AbpAspNetCoreIntegratedTestBase < Startup >
27
+ {
28
+ protected static readonly Lazy < string > ContentRootFolder ;
29
+
30
+ static AbpProjectNameWebTestBase ( )
31
+ {
32
+ ContentRootFolder = new Lazy < string > ( WebContentDirectoryFinder . CalculateContentRootFolder , true ) ;
33
+ }
34
+
35
+ protected override IWebHostBuilder CreateWebHostBuilder ( )
36
+ {
37
+ return base
38
+ . CreateWebHostBuilder ( )
39
+ . UseContentRoot ( ContentRootFolder . Value )
40
+ . UseSetting ( WebHostDefaults . ApplicationKey , typeof ( AbpProjectNameWebMvcModule ) . Assembly . FullName ) ;
41
+ }
42
+
43
+ #region Get response
44
+
45
+ protected async Task < T > GetResponseAsObjectAsync < T > ( string url ,
46
+ HttpStatusCode expectedStatusCode = HttpStatusCode . OK )
47
+ {
48
+ var strResponse = await GetResponseAsStringAsync ( url , expectedStatusCode ) ;
49
+ return JsonConvert . DeserializeObject < T > ( strResponse , new JsonSerializerSettings
50
+ {
51
+ ContractResolver = new CamelCasePropertyNamesContractResolver ( )
52
+ } ) ;
53
+ }
54
+
55
+ protected async Task < string > GetResponseAsStringAsync ( string url ,
56
+ HttpStatusCode expectedStatusCode = HttpStatusCode . OK )
57
+ {
58
+ var response = await GetResponseAsync ( url , expectedStatusCode ) ;
59
+ return await response . Content . ReadAsStringAsync ( ) ;
60
+ }
61
+
62
+ protected async Task < HttpResponseMessage > GetResponseAsync ( string url ,
63
+ HttpStatusCode expectedStatusCode = HttpStatusCode . OK )
64
+ {
65
+ var response = await Client . GetAsync ( url ) ;
66
+ response . StatusCode . ShouldBe ( expectedStatusCode ) ;
67
+ return response ;
68
+ }
69
+
70
+ #endregion
71
+
72
+ #region Authenticate
73
+
74
+ /// <summary>
75
+ /// /api/TokenAuth/Authenticate
76
+ /// TokenAuthController
77
+ /// </summary>
78
+ /// <param name="tenancyName"></param>
79
+ /// <param name="input"></param>
80
+ /// <returns></returns>
81
+ protected async Task AuthenticateAsync ( string tenancyName , AuthenticateModel input )
82
+ {
83
+ if ( tenancyName . IsNullOrWhiteSpace ( ) )
84
+ {
85
+ var tenant = UsingDbContext ( context => context . Tenants . FirstOrDefault ( t => t . TenancyName == tenancyName ) ) ;
86
+ if ( tenant != null )
87
+ {
88
+ AbpSession . TenantId = tenant . Id ;
89
+ Client . DefaultRequestHeaders . Add ( "Abp.TenantId" , tenant . Id . ToString ( ) ) ; //Set TenantId
90
+ }
91
+ }
92
+
93
+ var response = await Client . PostAsync ( "/api/TokenAuth/Authenticate" ,
94
+ new StringContent ( input . ToJsonString ( ) , Encoding . UTF8 , "application/json" ) ) ;
95
+ response . StatusCode . ShouldBe ( HttpStatusCode . OK ) ;
96
+ var result =
97
+ JsonConvert . DeserializeObject < AjaxResponse < AuthenticateResultModel > > (
98
+ await response . Content . ReadAsStringAsync ( ) ) ;
99
+ Client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , result . Result . AccessToken ) ;
100
+
101
+ AbpSession . UserId = result . Result . UserId ;
102
+ }
103
+
104
+ #endregion
105
+
106
+ #region Login
107
+
108
+ protected void LoginAsHostAdmin ( )
109
+ {
110
+ LoginAsHost ( AbpUserBase . AdminUserName ) ;
111
+ }
112
+
113
+ protected void LoginAsDefaultTenantAdmin ( )
114
+ {
115
+ LoginAsTenant ( AbpTenantBase . DefaultTenantName , AbpUserBase . AdminUserName ) ;
116
+ }
117
+
118
+ protected void LoginAsHost ( string userName )
119
+ {
120
+ AbpSession . TenantId = null ;
121
+
122
+ var user =
123
+ UsingDbContext (
124
+ context =>
125
+ context . Users . FirstOrDefault ( u => u . TenantId == AbpSession . TenantId && u . UserName == userName ) ) ;
126
+ if ( user == null )
127
+ {
128
+ throw new Exception ( "There is no user: " + userName + " for host." ) ;
129
+ }
130
+
131
+ AbpSession . UserId = user . Id ;
132
+ }
133
+
134
+ protected void LoginAsTenant ( string tenancyName , string userName )
135
+ {
136
+ var tenant = UsingDbContext ( context => context . Tenants . FirstOrDefault ( t => t . TenancyName == tenancyName ) ) ;
137
+ if ( tenant == null )
138
+ {
139
+ throw new Exception ( "There is no tenant: " + tenancyName ) ;
140
+ }
141
+
142
+ AbpSession . TenantId = tenant . Id ;
143
+
144
+ var user =
145
+ UsingDbContext (
146
+ context =>
147
+ context . Users . FirstOrDefault ( u => u . TenantId == AbpSession . TenantId && u . UserName == userName ) ) ;
148
+ if ( user == null )
149
+ {
150
+ throw new Exception ( "There is no user: " + userName + " for tenant: " + tenancyName ) ;
151
+ }
152
+
153
+ AbpSession . UserId = user . Id ;
154
+ }
155
+
156
+ #endregion
157
+
158
+
159
+ #region UsingDbContext
160
+
161
+ protected void UsingDbContext ( Action < AbpProjectNameDbContext > action )
162
+ {
163
+ using ( var context = IocManager . Resolve < AbpProjectNameDbContext > ( ) )
164
+ {
165
+ action ( context ) ;
166
+ context . SaveChanges ( ) ;
167
+ }
168
+ }
169
+
170
+ protected T UsingDbContext < T > ( Func < AbpProjectNameDbContext , T > func )
171
+ {
172
+ T result ;
173
+
174
+ using ( var context = IocManager . Resolve < AbpProjectNameDbContext > ( ) )
175
+ {
176
+ result = func ( context ) ;
177
+ context . SaveChanges ( ) ;
178
+ }
179
+
180
+ return result ;
181
+ }
182
+
183
+ protected async Task UsingDbContextAsync ( Func < AbpProjectNameDbContext , Task > action )
184
+ {
185
+ using ( var context = IocManager . Resolve < AbpProjectNameDbContext > ( ) )
186
+ {
187
+ await action ( context ) ;
188
+ await context . SaveChangesAsync ( true ) ;
189
+ }
190
+ }
191
+
192
+ protected async Task < T > UsingDbContextAsync < T > ( Func < AbpProjectNameDbContext , Task < T > > func )
193
+ {
194
+ T result ;
195
+
196
+ using ( var context = IocManager . Resolve < AbpProjectNameDbContext > ( ) )
197
+ {
198
+ result = await func ( context ) ;
199
+ await context . SaveChangesAsync ( true ) ;
200
+ }
201
+
202
+ return result ;
203
+ }
204
+
205
+ #endregion
206
+
207
+ #region ParseHtml
208
+
209
+ protected IHtmlDocument ParseHtml ( string htmlString )
210
+ {
211
+ return new HtmlParser ( ) . ParseDocument ( htmlString ) ;
212
+ }
213
+
214
+ #endregion
215
+ }
216
+ }
0 commit comments