Skip to content

Commit 75df681

Browse files
committed
Merge pull request #244 from ivaylokenov/authentication-claims
Version 1.2
2 parents 69821c9 + 88a871d commit 75df681

File tree

21 files changed

+281
-123
lines changed

21 files changed

+281
-123
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
MyTested.WebApi is unit testing library providing easy fluent interface to test the ASP.NET Web API 2 framework. It is testing framework agnostic, so you can combine it with the testing library of your choice (e.g. NUnit, xUnit, etc.). Inspired by [ChaiJS](https://github.com/chaijs/chai).
55

6-
[![Build status](https://ci.appveyor.com/api/projects/status/738pm1kuuv7yw1t5?svg=true)](https://ci.appveyor.com/project/ivaylokenov/mywebapi) [![NuGet Version](http://img.shields.io/nuget/v/MyTested.WebApi.svg?style=flat)](https://www.nuget.org/packages/MyTested.WebApi/) [![Coverage Status](https://coveralls.io/repos/ivaylokenov/MyTested.WebApi/badge.svg?branch=master&service=github&v=2)](https://coveralls.io/github/ivaylokenov/MyTested.WebApi?branch=master)
6+
[![Build status](https://ci.appveyor.com/api/projects/status/738pm1kuuv7yw1t5?svg=true)](https://ci.appveyor.com/project/ivaylokenov/mywebapi) [![NuGet Version](http://img.shields.io/nuget/v/MyTested.WebApi.svg?style=flat)](https://www.nuget.org/packages/MyTested.WebApi/) [![Coverage Status](https://coveralls.io/repos/ivaylokenov/MyTested.WebApi/badge.svg?branch=master&service=github&v=2)](https://coveralls.io/github/ivaylokenov/MyTested.WebApi?branch=master) [![License](https://img.shields.io/badge/license-apache-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0)
77

88
## Documentation
99

-93.4 KB
Binary file not shown.
94 KB
Binary file not shown.

documentation/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,16 @@ MyWebApi
521521
MyWebApi
522522
.Controller(myWebApiControllerInstance)
523523
.WithHttpConfiguration(config);
524+
525+
// sets the controller properties with a delegate
526+
MyWebApi
527+
.Controller<WebApiController>()
528+
.WithSetup(c =>
529+
{
530+
c.ActionContext = customActionContext;
531+
c.User = customUser;
532+
c.Configuration = customConfig;
533+
});
524534
```
525535

526536
[To top](#table-of-contents)
@@ -727,6 +737,11 @@ MyWebApi
727737
.Controller<WebApiController>()
728738
.WithAuthenticatedUser();
729739

740+
// sets custom IPrincipal object as user
741+
MyWebApi
742+
.Controller<WebApiController>()
743+
.WithAuthenticatedUser(customPrincipal);
744+
730745
// sets custom authenticated user using delegate action
731746
MyWebApi
732747
.Controller<WebApiController>()
@@ -736,7 +751,10 @@ MyWebApi
736751
MyWebApi
737752
.Controller<WebApiController>()
738753
.WithAuthenticatedUser(user => user
754+
.WithIdentifier("NewId") // adds claim of type NameIdentifier
739755
.WithUsername("NewUserName")
756+
.WithClaim(ClaimType.Actor, "NewActor") // adds custom claim to the user
757+
.WithClaim(ClaimType.DateOfBirth, DateTime.Now.ToString(0) // adds another claim to the user
740758
.InRoles("Moderator", "Administrator")); // or InRole("Moderator")
741759
```
742760

samples/Books Web API/Books.Tests/Books.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@
7878
<HintPath>..\packages\Moq.4.2.1510.2205\lib\net40\Moq.dll</HintPath>
7979
<Private>True</Private>
8080
</Reference>
81-
<Reference Include="MyTested.WebApi, Version=1.1.5.0, Culture=neutral, processorArchitecture=MSIL">
82-
<HintPath>..\packages\MyTested.WebApi.1.1.8\lib\net45\MyTested.WebApi.dll</HintPath>
81+
<Reference Include="MyTested.WebApi, Version=1.2.0.0, Culture=neutral, processorArchitecture=MSIL">
82+
<HintPath>..\packages\MyTested.WebApi.1.2.0\lib\net45\MyTested.WebApi.dll</HintPath>
8383
<Private>True</Private>
8484
</Reference>
8585
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">

samples/Books Web API/Books.Tests/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net45" />
1414
<package id="Microsoft.Owin.Testing" version="3.0.1" targetFramework="net45" />
1515
<package id="Moq" version="4.2.1510.2205" targetFramework="net45" />
16-
<package id="MyTested.WebApi" version="1.1.8" targetFramework="net45" />
16+
<package id="MyTested.WebApi" version="1.2.0" targetFramework="net45" />
1717
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net45" />
1818
<package id="Ninject" version="3.2.2.0" targetFramework="net45" />
1919
<package id="NUnit" version="2.6.4" targetFramework="net45" />

samples/Source Control System API/Tests/SourceControlSystem.Api.Tests/SourceControlSystem.Api.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@
9292
<HintPath>..\..\packages\Moq.4.2.1510.2205\lib\net40\Moq.dll</HintPath>
9393
<Private>True</Private>
9494
</Reference>
95-
<Reference Include="MyTested.WebApi, Version=1.1.5.0, Culture=neutral, processorArchitecture=MSIL">
96-
<HintPath>..\..\packages\MyTested.WebApi.1.1.8\lib\net45\MyTested.WebApi.dll</HintPath>
95+
<Reference Include="MyTested.WebApi, Version=1.2.0.0, Culture=neutral, processorArchitecture=MSIL">
96+
<HintPath>..\..\packages\MyTested.WebApi.1.2.0\lib\net45\MyTested.WebApi.dll</HintPath>
9797
<Private>True</Private>
9898
</Reference>
9999
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">

samples/Source Control System API/Tests/SourceControlSystem.Api.Tests/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<package id="Microsoft.Owin.Hosting" version="3.0.1" targetFramework="net452" />
1919
<package id="Microsoft.Owin.Testing" version="3.0.1" targetFramework="net452" />
2020
<package id="Moq" version="4.2.1510.2205" targetFramework="net452" />
21-
<package id="MyTested.WebApi" version="1.1.8" targetFramework="net452" />
21+
<package id="MyTested.WebApi" version="1.2.0" targetFramework="net452" />
2222
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
2323
<package id="Owin" version="1.0" targetFramework="net452" />
2424
</packages>

src/MyTested.WebApi.Tests/BuildersTests/ControllersTests/ControllerBuilderTests.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ namespace MyTested.WebApi.Tests.BuildersTests.ControllersTests
88
using System.Collections.Generic;
99
using System.Linq;
1010
using System.Net.Http;
11+
using System.Security.Claims;
1112
using System.Web.Http;
13+
using System.Web.Http.Controllers;
1214
using System.Web.Http.Results;
1315
using Builders.Contracts.Actions;
1416
using Builders.Contracts.Base;
@@ -102,6 +104,29 @@ public void CallingShouldHaveValidModelStateWhenThereAreNoModelErrors()
102104
Assert.AreEqual(0, modelState.Keys.Count);
103105
}
104106

107+
[Test]
108+
public void WithSetupShouldSetCorrectPropertiesToController()
109+
{
110+
var actionContext = new HttpActionContext();
111+
var user = TestObjectFactory.GetClaimsPrincipal();
112+
var config = new HttpConfiguration();
113+
114+
var controller = MyWebApi
115+
.Controller<WebApiController>()
116+
.WithSetup(c =>
117+
{
118+
c.ActionContext = actionContext;
119+
c.User = user;
120+
c.Configuration = config;
121+
})
122+
.AndProvideTheController();
123+
124+
Assert.NotNull(controller);
125+
Assert.AreSame(actionContext, controller.ActionContext);
126+
Assert.AreSame(user, controller.User);
127+
Assert.AreSame(config, controller.Configuration);
128+
}
129+
105130
[Test]
106131
public void WithoutValidationShouldNotValidateTheRequestModel()
107132
{
@@ -131,6 +156,15 @@ public void WithAuthenticatedUserShouldPopulateUserPropertyWithDefaultValues()
131156
Assert.AreEqual("TestUser", controllerUser.Identity.Name);
132157
Assert.AreEqual("Passport", controllerUser.Identity.AuthenticationType);
133158
Assert.AreEqual(true, controllerUser.Identity.IsAuthenticated);
159+
160+
var claimsIdentity = controllerUser.Identity as ClaimsIdentity;
161+
162+
Assert.NotNull(claimsIdentity);
163+
164+
var idClaim = claimsIdentity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier);
165+
166+
Assert.NotNull(idClaim);
167+
Assert.AreEqual("TestId", idClaim.Value);
134168
}
135169

136170
[Test]
@@ -139,8 +173,12 @@ public void WithAuthenticatedUserShouldPopulateProperUserWhenUserWithUserBuilder
139173
var controllerBuilder = MyWebApi
140174
.Controller<WebApiController>()
141175
.WithAuthenticatedUser(user => user
176+
.WithIdentifier("NewId")
142177
.WithUsername("NewUserName")
143178
.WithAuthenticationType("Custom")
179+
.WithClaim(new Claim(ClaimTypes.Actor, "CustomActor"))
180+
.AndAlso()
181+
.WithClaim(new Claim(ClaimTypes.Authentication, "Bearer"))
144182
.InRole("NormalUser")
145183
.InRoles("Moderator", "Administrator")
146184
.InRoles(new[]
@@ -165,6 +203,24 @@ public void WithAuthenticatedUserShouldPopulateProperUserWhenUserWithUserBuilder
165203
Assert.AreEqual(true, controllerUser.IsInRole("SuperUser"));
166204
Assert.AreEqual(true, controllerUser.IsInRole("MegaUser"));
167205
Assert.AreEqual(false, controllerUser.IsInRole("AnotherRole"));
206+
207+
var claimsIdentity = controllerUser.Identity as ClaimsIdentity;
208+
Assert.NotNull(claimsIdentity);
209+
210+
var idClaim = claimsIdentity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier);
211+
212+
Assert.NotNull(idClaim);
213+
Assert.AreEqual("NewId", idClaim.Value);
214+
215+
var actorClaim = claimsIdentity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Actor);
216+
217+
Assert.NotNull(actorClaim);
218+
Assert.AreEqual("CustomActor", actorClaim.Value);
219+
220+
var authenticationClaim = claimsIdentity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Authentication);
221+
222+
Assert.NotNull(authenticationClaim);
223+
Assert.AreEqual("Bearer", authenticationClaim.Value);
168224
}
169225

170226
[Test]
@@ -186,6 +242,25 @@ public void WithAuthenticatedNotCalledShouldNotHaveAuthorizedUser()
186242
Assert.AreEqual(false, controllerUser.Identity.IsAuthenticated);
187243
}
188244

245+
[Test]
246+
public void WithAuthenticatedUserUsingPrincipalShouldWorkCorrectly()
247+
{
248+
var controllerBuilder = MyWebApi
249+
.Controller<WebApiController>();
250+
251+
controllerBuilder
252+
.WithAuthenticatedUser(TestObjectFactory.GetClaimsPrincipal())
253+
.Calling(c => c.AuthorizedAction())
254+
.ShouldReturn()
255+
.NotFound();
256+
257+
var controllerUser = controllerBuilder.AndProvideTheController().User;
258+
259+
Assert.AreEqual("CustomUser", controllerUser.Identity.Name);
260+
Assert.AreEqual(null, controllerUser.Identity.AuthenticationType);
261+
Assert.IsFalse(controllerUser.Identity.IsAuthenticated);
262+
}
263+
189264
[Test]
190265
public void WithResolvedDependencyForShouldChooseCorrectConstructorWithLessDependencies()
191266
{

src/MyTested.WebApi.Tests/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
[assembly: ComVisible(false)]
1515
[assembly: Guid("29f6a15e-c6d1-4c93-83c8-18ff839950e8")]
1616

17-
[assembly: AssemblyVersion("1.1.9.0")]
18-
[assembly: AssemblyFileVersion("1.1.9.0")]
17+
[assembly: AssemblyVersion("1.2.0.0")]
18+
[assembly: AssemblyFileVersion("1.2.0.0")]

0 commit comments

Comments
 (0)