Skip to content

Commit b9e0cdb

Browse files
committed
Fixes #42
1 parent 56fe3ee commit b9e0cdb

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/Simplify.Web.Tests/Modules/WebContextTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#nullable disable
22

33
using System.Collections.Generic;
4+
using System.Security.Claims;
5+
using System.Security.Principal;
46
using Microsoft.AspNetCore.Http;
57
using Microsoft.Extensions.Primitives;
68
using 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
}

src/Simplify.Web/Modules/IWebContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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>

src/Simplify.Web/Modules/WebContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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>

0 commit comments

Comments
 (0)