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