Skip to content

Commit e883b09

Browse files
committed
Extracted Http and Authentication extensions methods (#125)
1 parent 19a6e5c commit e883b09

23 files changed

+7388
-1015
lines changed

MyTested.AspNetCore.Mvc.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.Rou
7878
EndProject
7979
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.Authentication", "src\MyTested.AspNetCore.Mvc.Authentication\MyTested.AspNetCore.Mvc.Authentication.xproj", "{18B6C20B-9C4B-4444-AD61-8DAF57012599}"
8080
EndProject
81+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.Controllers", "src\MyTested.AspNetCore.Mvc.Controllers\MyTested.AspNetCore.Mvc.Controllers.xproj", "{9B2A773E-3A37-44F3-83D0-5D156526FA84}"
82+
EndProject
8183
Global
8284
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8385
Debug|Any CPU = Debug|Any CPU
@@ -204,6 +206,10 @@ Global
204206
{18B6C20B-9C4B-4444-AD61-8DAF57012599}.Debug|Any CPU.Build.0 = Debug|Any CPU
205207
{18B6C20B-9C4B-4444-AD61-8DAF57012599}.Release|Any CPU.ActiveCfg = Release|Any CPU
206208
{18B6C20B-9C4B-4444-AD61-8DAF57012599}.Release|Any CPU.Build.0 = Release|Any CPU
209+
{9B2A773E-3A37-44F3-83D0-5D156526FA84}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
210+
{9B2A773E-3A37-44F3-83D0-5D156526FA84}.Debug|Any CPU.Build.0 = Debug|Any CPU
211+
{9B2A773E-3A37-44F3-83D0-5D156526FA84}.Release|Any CPU.ActiveCfg = Release|Any CPU
212+
{9B2A773E-3A37-44F3-83D0-5D156526FA84}.Release|Any CPU.Build.0 = Release|Any CPU
207213
EndGlobalSection
208214
GlobalSection(SolutionProperties) = preSolution
209215
HideSolutionNode = FALSE
@@ -241,5 +247,6 @@ Global
241247
{5B60F95F-8A87-4166-8C75-1328E7063365} = {09353A03-2B0C-496B-8EB1-2CB6A22D758B}
242248
{2101DDC1-36A4-4D4E-A4D8-EF24D47116A4} = {09353A03-2B0C-496B-8EB1-2CB6A22D758B}
243249
{18B6C20B-9C4B-4444-AD61-8DAF57012599} = {09353A03-2B0C-496B-8EB1-2CB6A22D758B}
250+
{9B2A773E-3A37-44F3-83D0-5D156526FA84} = {09353A03-2B0C-496B-8EB1-2CB6A22D758B}
244251
EndGlobalSection
245252
EndGlobal
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace MyTested.AspNetCore.Mvc
2+
{
3+
using System;
4+
using Builders.Authentication;
5+
using Builders.Base;
6+
using Builders.Contracts.Authentication;
7+
using Builders.Contracts.Base;
8+
9+
/// <summary>
10+
/// Contains authentication extension methods for <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/>.
11+
/// </summary>
12+
public static class ComponentBuilderAuthenticationExtensions
13+
{
14+
/// <summary>
15+
/// Sets default authenticated <see cref="Microsoft.AspNetCore.Http.HttpContext.User"/> to the built component with "TestId" identifier and "TestUser" username.
16+
/// </summary>
17+
/// <typeparam name="TBuilder">Class representing ASP.NET Core MVC test builder.</typeparam>
18+
/// <param name="builder">Instance of <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/> type.</param>
19+
/// <returns>The same component builder.</returns>
20+
public static TBuilder WithAuthenticatedUser<TBuilder>(this IBaseTestBuilderWithComponentBuilder<TBuilder> builder)
21+
where TBuilder : IBaseTestBuilder
22+
{
23+
var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder;
24+
25+
actualBuilder.HttpContext.User = ClaimsPrincipalBuilder.DefaultAuthenticated;
26+
27+
return actualBuilder.Builder;
28+
}
29+
30+
/// <summary>
31+
/// Sets custom authenticated <see cref="Microsoft.AspNetCore.Http.HttpContext.User"/> to the built component using the provided user builder.
32+
/// </summary>
33+
/// <param name="builder">Instance of <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/> type.</param>
34+
/// <param name="userBuilder">Action setting the <see cref="Microsoft.AspNetCore.Http.HttpContext.User"/> by using <see cref="IClaimsPrincipalBuilder"/>.</param>
35+
/// <returns>The same component builder.</returns>
36+
public static TBuilder WithAuthenticatedUser<TBuilder>(
37+
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder,
38+
Action<IClaimsPrincipalBuilder> userBuilder)
39+
where TBuilder : IBaseTestBuilder
40+
{
41+
var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder;
42+
43+
var newUserBuilder = new ClaimsPrincipalBuilder();
44+
userBuilder(newUserBuilder);
45+
actualBuilder.HttpContext.User = newUserBuilder.GetClaimsPrincipal();
46+
47+
return actualBuilder.Builder;
48+
}
49+
}
50+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
8+
<PropertyGroup Label="Globals">
9+
<ProjectGuid>9b2a773e-3a37-44f3-83d0-5d156526fa84</ProjectGuid>
10+
<RootNamespace>MyTested.AspNetCore.Mvc</RootNamespace>
11+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
13+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
14+
</PropertyGroup>
15+
<PropertyGroup>
16+
<SchemaVersion>2.0</SchemaVersion>
17+
</PropertyGroup>
18+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
19+
<Target Name="AfterBuild">
20+
<Exec Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'" Command="dotnet pack --configuration Release --output ../../artifacts --no-build" />
21+
</Target>
22+
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyConfiguration("")]
9+
[assembly: AssemblyCompany("")]
10+
[assembly: AssemblyProduct("MyTested.AspNetCore.Mvc.Controllers")]
11+
[assembly: AssemblyTrademark("")]
12+
13+
// Setting ComVisible to false makes the types in this assembly not visible
14+
// to COM components. If you need to access a type in this assembly from
15+
// COM, set the ComVisible attribute to true on that type.
16+
[assembly: ComVisible(false)]
17+
18+
// The following GUID is for the ID of the typelib if this project is exposed to COM
19+
[assembly: Guid("9b2a773e-3a37-44f3-83d0-5d156526fa84")]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"title": "MyTested.AspNetCore.Mvc.Controllers",
3+
"description": "My Tested ASP.NET Core MVC controller components.",
4+
"version": "1.0.0-preview-final",
5+
"authors": [ "Ivaylo Kenov" ],
6+
"packOptions": {
7+
"projectUrl": "https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc",
8+
"licenseUrl": "https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc/blob/master/LICENSE",
9+
"iconUrl": "https://raw.githubusercontent.com/ivaylokenov/MyTested.AspNetCore.Mvc/master/tools/nuget-logo.png",
10+
"owners": [ "Ivaylo Kenov" ],
11+
"tags": [ "aspnetcore", "aspnetcoremvc", "testing", "unit", "tests", "fluent", "testing", "framework", "asp", "net", "core", "mvc", "test", "mymvc", "mytested" ],
12+
"requireLicenseAcceptance": true,
13+
"repository": {
14+
"type": "git",
15+
"url": "git://github.com/ivaylokenov/MyTested.AspNetCore.Mvc"
16+
}
17+
},
18+
19+
"buildOptions": {
20+
"warningsAsErrors": true,
21+
"xmlDoc": true,
22+
"optimize": true,
23+
"nowarn": [
24+
"CS1591"
25+
],
26+
"keyFile": "../../tools/Key.snk"
27+
},
28+
29+
"dependencies": {
30+
"MyTested.AspNetCore.Mvc.Abstractions": "1.0.0-*"
31+
},
32+
33+
"frameworks": {
34+
"net451": {},
35+
"netstandard1.6": {}
36+
}
37+
}

0 commit comments

Comments
 (0)