Skip to content

Commit 16f28fe

Browse files
committed
#283 Adding small refactoring of the authentication builders in order to continue with the WithoutUser implementation.
1 parent af7fea4 commit 16f28fe

11 files changed

Lines changed: 171 additions & 76 deletions
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Authentication
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Security.Claims;
7+
8+
public class BaseClaimsPrincipalUserBuilder : BaseUserBuilder
9+
{
10+
private readonly ICollection<ClaimsIdentity> _identities;
11+
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="BaseClaimsPrincipalUserBuilder"/> class.
14+
/// </summary>
15+
public BaseClaimsPrincipalUserBuilder()
16+
=> this._identities = new List<ClaimsIdentity>();
17+
18+
public ClaimsPrincipal GetClaimsPrincipal()
19+
{
20+
var claimIdentities = this._identities.Reverse().ToList();
21+
claimIdentities.Add(this.GetAuthenticatedClaimsIdentity());
22+
23+
var claimsPrincipal = new ClaimsPrincipal(claimIdentities);
24+
25+
return claimsPrincipal;
26+
}
27+
28+
/// <summary>
29+
/// Static constructor for creating default authenticated claims principal with "TestId" identifier and "TestUser" username.
30+
/// </summary>
31+
/// <returns>Authenticated <see cref="ClaimsPrincipal"/>.</returns>
32+
/// <value>Result of type <see cref="ClaimsPrincipal"/>.</value>
33+
public static ClaimsPrincipal DefaultAuthenticated { get; }
34+
= new ClaimsPrincipal(CreateAuthenticatedClaimsIdentity());
35+
36+
protected void AddIdentity(ClaimsIdentity identity)
37+
{
38+
this._identities.Add(identity);
39+
}
40+
}
41+
}

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/BaseUserBuilder.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected static ClaimsIdentity CreateAuthenticatedClaimsIdentity(
7474
/// Creates new authenticated claims identity by using the accumulated claims and authentication type.
7575
/// </summary>
7676
/// <returns>Mock of <see cref="ClaimsIdentity"/>.</returns>
77-
protected ClaimsIdentity GetAuthenticatedClaimsIdentity()
77+
protected ClaimsIdentity GetAuthenticatedClaimsIdentity()
7878
=> CreateAuthenticatedClaimsIdentity(
7979
this.claims,
8080
this.authenticationType,
@@ -97,51 +97,71 @@ protected ClaimsIdentity GetAuthenticatedClaimsIdentity()
9797
/// Sets identifier claim to the built <see cref="ClaimsIdentity"/>.
9898
/// </summary>
9999
/// <param name="identifier">Value of the identifier claim - <see cref="ClaimTypes.NameIdentifier"/>.</param>
100-
protected void AddIdentifier(string identifier)
100+
protected void AddIdentifier(string identifier)
101101
=> this.AddClaim(ClaimTypes.NameIdentifier, identifier);
102102

103103
/// <summary>
104104
/// Sets username claims to the built <see cref="ClaimsIdentity"/>.
105105
/// </summary>
106106
/// <param name="username">Value of the username claim. Default claim type is <see cref="ClaimTypes.Name"/>.</param>
107-
protected void AddUsername(string username)
107+
protected void AddUsername(string username)
108108
=> this.AddClaim(this.nameType, username);
109109

110110
/// <summary>
111111
/// Adds claim to the built <see cref="ClaimsIdentity"/>.
112112
/// </summary>
113113
/// <param name="claim">The <see cref="Claim"/> to add.</param>
114-
protected void AddClaim(Claim claim)
114+
protected void AddClaim(Claim claim)
115115
=> this.claims.Add(claim);
116116

117117
/// <summary>
118118
/// Adds claims to the built <see cref="ClaimsIdentity"/>.
119119
/// </summary>
120120
/// <param name="claims">Collection of <see cref="Claim"/> to add.</param>
121-
protected void AddClaims(IEnumerable<Claim> claims)
121+
protected void AddClaims(IEnumerable<Claim> claims)
122122
=> claims.ForEach(this.AddClaim);
123123

124124
/// <summary>
125125
/// Adds authentication type to the built <see cref="ClaimsIdentity"/>.
126126
/// </summary>
127127
/// <param name="authenticationType">Authentication type to add. Default is "Passport".</param>
128-
protected void AddAuthenticationType(string authenticationType)
128+
protected void AddAuthenticationType(string authenticationType)
129129
=> this.authenticationType = authenticationType;
130130

131131
/// <summary>
132132
/// Adds role to the built <see cref="ClaimsIdentity"/>.
133133
/// </summary>
134134
/// <param name="role">Value of the role claim. Default claim type is <see cref="ClaimTypes.Role"/>.</param>
135-
protected void AddRole(string role)
135+
protected void AddRole(string role)
136136
=> this.AddClaim(this.roleType, role);
137137

138138
/// <summary>
139139
/// Adds roles to the built <see cref="ClaimsIdentity"/>.
140140
/// </summary>
141141
/// <param name="roles">Collection of roles to add.</param>
142-
protected void AddRoles(IEnumerable<string> roles)
142+
protected void AddRoles(IEnumerable<string> roles)
143143
=> roles.ForEach(this.AddRole);
144144

145+
protected void RemoveClaim(Claim claim)
146+
{
147+
if (this.claims.Contains(claim))
148+
this.claims.Remove(claim);
149+
}
150+
151+
protected void RemoveClaim(string type, string value)
152+
{
153+
var claimsToRemove =
154+
this.claims.Where(x => x.Type.Equals(type) && x.Value.Equals(value));
155+
156+
claimsToRemove.ForEach(claim => this.claims.Remove(claim));
157+
}
158+
159+
protected void RemoveRole(string role)
160+
=> this.RemoveClaim(this.roleType, role);
161+
162+
protected void RemoveUsername(string username)
163+
=> this.RemoveClaim(this.nameType, username);
164+
145165
/// <summary>
146166
/// Adds claim to the built <see cref="ClaimsIdentity"/>.
147167
/// </summary>

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/WithClaimsPrincipalBuilder.cs

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,8 @@
1010
/// <summary>
1111
/// Used for building mocked <see cref="ClaimsPrincipal"/>.
1212
/// </summary>
13-
public class WithClaimsPrincipalBuilder : BaseUserBuilder, IAndWithClaimsPrincipalBuilder
13+
public class WithClaimsPrincipalBuilder : BaseClaimsPrincipalUserBuilder, IAndWithClaimsPrincipalBuilder
1414
{
15-
private readonly ICollection<ClaimsIdentity> identities;
16-
17-
/// <summary>
18-
/// Initializes a new instance of the <see cref="WithClaimsPrincipalBuilder"/> class.
19-
/// </summary>
20-
public WithClaimsPrincipalBuilder()
21-
=> this.identities = new List<ClaimsIdentity>();
22-
23-
/// <summary>
24-
/// Static constructor for creating default authenticated claims principal with "TestId" identifier and "TestUser" username.
25-
/// </summary>
26-
/// <returns>Authenticated <see cref="ClaimsPrincipal"/>.</returns>
27-
/// <value>Result of type <see cref="ClaimsPrincipal"/>.</value>
28-
public static ClaimsPrincipal DefaultAuthenticated { get; }
29-
= new ClaimsPrincipal(CreateAuthenticatedClaimsIdentity());
30-
3115
/// <inheritdoc />
3216
public IAndWithClaimsPrincipalBuilder WithNameType(string nameType)
3317
{
@@ -111,7 +95,7 @@ public IAndWithClaimsPrincipalBuilder WithIdentity(IIdentity identity)
11195
claimsIdentity = new ClaimsIdentity(identity);
11296
}
11397

114-
this.identities.Add(claimsIdentity);
98+
base.AddIdentity(claimsIdentity);
11599
return this;
116100
}
117101

@@ -120,21 +104,12 @@ public IAndWithClaimsPrincipalBuilder WithIdentity(Action<IWithClaimsIdentityBui
120104
{
121105
var newClaimsIdentityBuilder = new WithClaimsIdentityBuilder();
122106
claimsIdentityBuilder(newClaimsIdentityBuilder);
123-
this.identities.Add(newClaimsIdentityBuilder.GetClaimsIdentity());
107+
108+
base.AddIdentity(newClaimsIdentityBuilder.GetClaimsIdentity());
124109
return this;
125110
}
126111

127112
/// <inheritdoc />
128113
public IWithClaimsPrincipalBuilder AndAlso() => this;
129-
130-
public ClaimsPrincipal GetClaimsPrincipal()
131-
{
132-
var claimIdentities = this.identities.Reverse().ToList();
133-
claimIdentities.Add(this.GetAuthenticatedClaimsIdentity());
134-
135-
var claimsPrincipal = new ClaimsPrincipal(claimIdentities);
136-
137-
return claimsPrincipal;
138-
}
139114
}
140115
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Authentication
2+
{
3+
using System.Security.Claims;
4+
using MyTested.AspNetCore.Mvc.Builders.Contracts.Authentication;
5+
6+
public class WithoutClaimsPrincipalBuilder : BaseClaimsPrincipalUserBuilder, IAndWithoutClaimsPrincipalBuilder
7+
{
8+
public IAndWithoutClaimsPrincipalBuilder WithoutClaim(string type, string value)
9+
{
10+
base.RemoveClaim(type, value);
11+
12+
return this;
13+
}
14+
15+
public IAndWithoutClaimsPrincipalBuilder WithoutClaim(Claim claim)
16+
{
17+
base.RemoveClaim(claim);
18+
19+
return this;
20+
}
21+
22+
public IAndWithoutClaimsPrincipalBuilder WithoutRole(string role)
23+
{
24+
base.RemoveRole(role);
25+
26+
return this;
27+
}
28+
29+
public IAndWithoutClaimsPrincipalBuilder WithoutUser()
30+
{
31+
return this;
32+
}
33+
34+
public IAndWithoutClaimsPrincipalBuilder WithoutUsername(string username)
35+
{
36+
base.RemoveUsername(username);
37+
38+
return this;
39+
}
40+
41+
public IWithoutClaimsPrincipalBuilder AndAlso()
42+
=> this;
43+
}
44+
}

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IAndWithoutClaimsIdentityBuilder.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Authentication
2+
{
3+
public interface IAndWithoutClaimsPrincipalBuilder : IWithoutClaimsPrincipalBuilder
4+
{
5+
IWithoutClaimsPrincipalBuilder AndAlso();
6+
}
7+
}

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IWithoutClaimsIdentityBuilder.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Authentication
2+
{
3+
using System.Security.Claims;
4+
5+
public interface IWithoutClaimsPrincipalBuilder
6+
{
7+
IAndWithoutClaimsPrincipalBuilder WithoutUser();
8+
9+
IAndWithoutClaimsPrincipalBuilder WithoutRole(string role);
10+
11+
IAndWithoutClaimsPrincipalBuilder WithoutUsername(string username);
12+
13+
IAndWithoutClaimsPrincipalBuilder WithoutClaim(string type, string value);
14+
15+
IAndWithoutClaimsPrincipalBuilder WithoutClaim(Claim claim);
16+
}
17+
}

src/MyTested.AspNetCore.Mvc.Authentication/ComponentBuilderAuthenticationWithExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public static TBuilder WithUser<TBuilder>(this IBaseTestBuilderWithComponentBuil
145145
{
146146
var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder;
147147

148-
actualBuilder.HttpContext.User = WithClaimsPrincipalBuilder.DefaultAuthenticated;
148+
actualBuilder.HttpContext.User = BaseClaimsPrincipalUserBuilder.DefaultAuthenticated;
149149

150150
return actualBuilder.Builder;
151151
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
namespace MyTested.AspNetCore.Mvc
22
{
3+
using MyTested.AspNetCore.Mvc.Builders.Authentication;
4+
using MyTested.AspNetCore.Mvc.Builders.Base;
5+
using MyTested.AspNetCore.Mvc.Builders.Contracts.Authentication;
6+
using MyTested.AspNetCore.Mvc.Builders.Contracts.Base;
7+
using System;
8+
39
public static class ComponentBuilderAuthenticationWithoutExtensions
410
{
11+
public static TBuilder WithoutUser<TBuilder>(
12+
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder,
13+
Action<IWithoutClaimsPrincipalBuilder> userBuilder)
14+
where TBuilder : IBaseTestBuilder
15+
{
16+
var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder;
17+
18+
var newUserBuilder = new WithoutClaimsPrincipalBuilder();
19+
userBuilder(newUserBuilder);
20+
actualBuilder.HttpContext.User = newUserBuilder.GetClaimsPrincipal();
21+
22+
return actualBuilder.Builder;
23+
}
24+
25+
public static TBuilder WithoutUser<TBuilder>(
26+
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder)
27+
where TBuilder : IBaseTestBuilder
28+
{
29+
var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder;
30+
actualBuilder.HttpContext.User = BaseClaimsPrincipalUserBuilder.DefaultAuthenticated;
31+
32+
return actualBuilder.Builder;
33+
}
534
}
635
}

0 commit comments

Comments
 (0)