File tree Expand file tree Collapse file tree 3 files changed +53
-0
lines changed
Simplify.Web.Tests/Modules Expand file tree Collapse file tree 3 files changed +53
-0
lines changed Original file line number Diff line number Diff line change 11#nullable disable
22
33using System . Collections . Generic ;
4+ using System . Security . Claims ;
5+ using System . Security . Principal ;
46using Microsoft . AspNetCore . Http ;
57using Microsoft . Extensions . Primitives ;
68using Moq ;
@@ -200,5 +202,46 @@ public void Constructor_SpecificRoute_SetCorrectly()
200202
201203 Assert . AreEqual ( "/test" , context . Route ) ;
202204 }
205+
206+ [ Test ]
207+ public void IsAuthenticated_UserIsNotNullAndAuthenticated_True ( )
208+ {
209+ // Arrange
210+
211+ _owinContext . SetupGet ( x => x . User )
212+ . Returns ( Mock . Of < ClaimsPrincipal > ( f => f . Identity == Mock . Of < IIdentity > ( i => i . IsAuthenticated ) ) ) ;
213+
214+ var context = new WebContext ( _owinContext . Object ) ;
215+
216+ // Act & Assert
217+ Assert . IsTrue ( context . IsAuthenticated ) ;
218+ }
219+
220+ [ Test ]
221+ public void IsAuthenticated_UserIsNotNullAndNotAuthenticated_False ( )
222+ {
223+ // Arrange
224+
225+ _owinContext . SetupGet ( x => x . User )
226+ . Returns ( Mock . Of < ClaimsPrincipal > ( f => f . Identity == Mock . Of < IIdentity > ( i => i . IsAuthenticated == false ) ) ) ;
227+
228+ var context = new WebContext ( _owinContext . Object ) ;
229+
230+ // Act & Assert
231+ Assert . IsFalse ( context . IsAuthenticated ) ;
232+ }
233+
234+ [ Test ]
235+ public void IsAuthenticated_UserIsNull_False ( )
236+ {
237+ // Arrange
238+
239+ _owinContext . SetupGet ( x => x . User ) . Returns ( ( ClaimsPrincipal ) null ) ;
240+
241+ var context = new WebContext ( _owinContext . Object ) ;
242+
243+ // Act & Assert
244+ Assert . IsFalse ( context . IsAuthenticated ) ;
245+ }
203246 }
204247}
Original file line number Diff line number Diff line change @@ -59,6 +59,11 @@ public interface IWebContext
5959 /// </value>
6060 bool IsAjax { get ; }
6161
62+ /// <summary>
63+ /// Gets a value indicating whether current request context user is not null and is authenticated.
64+ /// </summary>
65+ bool IsAuthenticated { get ; }
66+
6267 /// <summary>
6368 /// Gets the request body.
6469 /// </summary>
Original file line number Diff line number Diff line change @@ -102,6 +102,11 @@ public IFormCollection Form
102102 /// </value>
103103 public bool IsAjax { get ; }
104104
105+ /// <summary>
106+ /// Gets a value indicating whether current request context user is not null and is authenticated.
107+ /// </summary>
108+ public bool IsAuthenticated => Context . User != null && Context . User . Identity . IsAuthenticated ;
109+
105110 /// <summary>
106111 /// Gets or sets the request body.
107112 /// </summary>
You can’t perform that action at this time.
0 commit comments