Skip to content

Commit 0ec95bd

Browse files
committed
Added documentation for authentication builders
1 parent e3f9a91 commit 0ec95bd

File tree

7 files changed

+149
-107
lines changed

7 files changed

+149
-107
lines changed

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

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,34 @@
55
using System.Security.Claims;
66
using Utilities.Extensions;
77

8+
/// <summary>
9+
/// Base class for creating mocked authenticated <see cref="ClaimsIdentity"/>.
10+
/// </summary>
811
public abstract class BaseUserBuilder
912
{
13+
/// <summary>
14+
/// Default identifier (Id) of the built <see cref="ClaimsIdentity"/> - "TestId".
15+
/// </summary>
1016
protected const string DefaultIdentifier = "TestId";
17+
18+
/// <summary>
19+
/// Default username of the built <see cref="ClaimsIdentity"/> - "TestUser".
20+
/// </summary>
1121
protected const string DefaultUsername = "TestUser";
22+
23+
/// <summary>
24+
/// Default authentication type of the built <see cref="ClaimsIdentity"/> - "Passport".
25+
/// </summary>
1226
protected const string DefaultAuthenticationType = "Passport";
27+
28+
/// <summary>
29+
/// Default type of the name claim - <see cref="ClaimTypes.Name"/>.
30+
/// </summary>
1331
protected const string DefaultNameType = ClaimTypes.Name;
32+
33+
/// <summary>
34+
/// Default type of the role claim - <see cref="ClaimTypes.Role"/>.
35+
/// </summary>
1436
protected const string DefaultRoleType = ClaimTypes.Role;
1537

1638
private readonly ICollection<Claim> claims;
@@ -19,6 +41,9 @@ public abstract class BaseUserBuilder
1941
private string nameType;
2042
private string roleType;
2143

44+
/// <summary>
45+
/// Initializes a new instance of the <see cref="BaseUserBuilder" /> class.
46+
/// </summary>
2247
protected BaseUserBuilder()
2348
{
2449
this.authenticationType = DefaultAuthenticationType;
@@ -27,7 +52,15 @@ protected BaseUserBuilder()
2752

2853
this.claims = new List<Claim>();
2954
}
30-
55+
56+
/// <summary>
57+
/// Creates new authenticated claims identity by using the provided claims and authentication type.
58+
/// </summary>
59+
/// <param name="claims">Collection of claims to add to the authenticated identity.</param>
60+
/// <param name="authenticationType">Authentication type to use for the authenticated identity.</param>
61+
/// <param name="nameType">Type of the username claim. Default is <see cref="ClaimTypes.Name"/>.</param>
62+
/// <param name="roleType">Type of the role claim. Default is <see cref="ClaimTypes.Role"/>.</param>
63+
/// <returns>Mocked <see cref="ClaimsIdentity"/>.</returns>
3164
protected static ClaimsIdentity CreateAuthenticatedClaimsIdentity(
3265
ICollection<Claim> claims = null,
3366
string authenticationType = null,
@@ -52,6 +85,10 @@ protected static ClaimsIdentity CreateAuthenticatedClaimsIdentity(
5285
return new ClaimsIdentity(claims, authenticationType, nameType, roleType);
5386
}
5487

88+
/// <summary>
89+
/// Creates new authenticated claims identity by using the accumulated claims and authentication type.
90+
/// </summary>
91+
/// <returns>Mocked <see cref="ClaimsIdentity"/>.</returns>
5592
protected ClaimsIdentity GetAuthenticatedClaimsIdentity()
5693
{
5794
return CreateAuthenticatedClaimsIdentity(
@@ -61,51 +98,92 @@ protected ClaimsIdentity GetAuthenticatedClaimsIdentity()
6198
this.roleType);
6299
}
63100

101+
/// <summary>
102+
/// Sets type of the username claim. Default is <see cref="ClaimTypes.Name"/>.
103+
/// </summary>
104+
/// <param name="nameType">Type to set on the username claim.</param>
64105
protected void AddNameType(string nameType)
65106
{
66107
this.nameType = nameType;
67108
}
68109

110+
/// <summary>
111+
/// Sets type of the role claim. Default is <see cref="ClaimTypes.Role"/>.
112+
/// </summary>
113+
/// <param name="roleType">Type to set on the role claim.</param>
69114
protected void AddRoleType(string roleType)
70115
{
71116
this.roleType = roleType;
72117
}
73118

119+
/// <summary>
120+
/// Sets identifier claim to the built <see cref="ClaimsIdentity"/>.
121+
/// </summary>
122+
/// <param name="identifier">Value of the identifier claim - <see cref="ClaimTypes.NameIdentifier"/>.</param>
74123
protected void AddIdentifier(string identifier)
75124
{
76125
this.AddClaim(ClaimTypes.NameIdentifier, identifier);
77126
}
78127

128+
/// <summary>
129+
/// Sets username claims to the built <see cref="ClaimsIdentity"/>.
130+
/// </summary>
131+
/// <param name="username">Value of the username claim. Default claim type is <see cref="ClaimTypes.Name"/>.</param>
79132
protected void AddUsername(string username)
80133
{
81134
this.AddClaim(this.nameType, username);
82135
}
83136

137+
/// <summary>
138+
/// Adds claim to the built <see cref="ClaimsIdentity"/>.
139+
/// </summary>
140+
/// <param name="claim">The <see cref="Claim"/> to add.</param>
84141
protected void AddClaim(Claim claim)
85142
{
86143
this.claims.Add(claim);
87144
}
88145

146+
/// <summary>
147+
/// Adds claims to the built <see cref="ClaimsIdentity"/>.
148+
/// </summary>
149+
/// <param name="claims">Enumerable of <see cref="Claim"/> to add.</param>
89150
protected void AddClaims(IEnumerable<Claim> claims)
90151
{
91152
claims.ForEach(c => this.AddClaim(c));
92153
}
93154

155+
/// <summary>
156+
/// Adds authentication type to the built <see cref="ClaimsIdentity"/>.
157+
/// </summary>
158+
/// <param name="authenticationType">Authentication type to add. Default is "Passport".</param>
94159
protected void AddAuthenticationType(string authenticationType)
95160
{
96161
this.authenticationType = authenticationType;
97162
}
98163

164+
/// <summary>
165+
/// Adds role to the built <see cref="ClaimsIdentity"/>.
166+
/// </summary>
167+
/// <param name="role">Value of the role claim. Default claim type is <see cref="ClaimTypes.Role"/>.</param>
99168
protected void AddRole(string role)
100169
{
101170
this.AddClaim(this.roleType, role);
102171
}
103172

173+
/// <summary>
174+
/// Adds roles to the built <see cref="ClaimsIdentity"/>.
175+
/// </summary>
176+
/// <param name="roles">Enumerable of roles to add.</param>
104177
protected void AddRoles(IEnumerable<string> roles)
105178
{
106179
roles.ForEach(r => this.AddRole(r));
107180
}
108181

182+
/// <summary>
183+
/// Adds claim to the built <see cref="ClaimsIdentity"/>.
184+
/// </summary>
185+
/// <param name="type">Type of the claim to add.</param>
186+
/// <param name="value">Value of the claim to add.</param>
109187
private void AddClaim(string type, string value)
110188
{
111189
this.claims.Add(new Claim(type, value));

src/MyTested.Mvc/Builders/Authentication/ClaimsIdentityBuilder.cs

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,122 +5,93 @@
55
using System.Security.Claims;
66
using Contracts.Authentication;
77

8+
/// <summary>
9+
/// Used for creating mocked authenticated <see cref="ClaimsIdentity"/>.
10+
/// </summary>
811
public class ClaimsIdentityBuilder : BaseUserBuilder, IAndClaimsIdentityBuilder
912
{
13+
/// <inheritdoc />
1014
public IAndClaimsIdentityBuilder WithNameType(string nameType)
1115
{
1216
this.AddNameType(nameType);
1317
return this;
1418
}
1519

20+
/// <inheritdoc />
1621
public IAndClaimsIdentityBuilder WithRoleType(string roleType)
1722
{
1823
this.AddRoleType(roleType);
1924
return this;
2025
}
2126

22-
/// <summary>
23-
/// Used for setting ID to the claims identity. If such is not provided, "TestId" is used by default.
24-
/// </summary>
25-
/// <param name="identifier">The user Id to set.</param>
26-
/// <returns>The same claims identity builder.</returns>
27+
/// <inheritdoc />
2728
public IAndClaimsIdentityBuilder WithIdentifier(string identifier)
2829
{
2930
this.AddIdentifier(identifier);
3031
return this;
3132
}
3233

33-
/// <summary>
34-
/// Used for setting username to the claims identity. If such is not provided, "TestUser" is used by default.
35-
/// </summary>
36-
/// <param name="username">The username to set.</param>
37-
/// <returns>The same claims identity builder.</returns>
34+
/// <inheritdoc />
3835
public IAndClaimsIdentityBuilder WithUsername(string username)
3936
{
4037
this.AddUsername(username);
4138
return this;
4239
}
43-
40+
41+
/// <inheritdoc />
4442
public IAndClaimsIdentityBuilder WithClaim(string type, string value)
4543
{
4644
return this.WithClaim(new Claim(type, value));
4745
}
4846

49-
/// <summary>
50-
/// Used for adding claim to the claims identity.
51-
/// </summary>
52-
/// <param name="claim">The claim to add.</param>
53-
/// <returns>The same claims identity builder.</returns>
47+
/// <inheritdoc />
5448
public IAndClaimsIdentityBuilder WithClaim(Claim claim)
5549
{
5650
this.AddClaim(claim);
5751
return this;
5852
}
5953

60-
/// <summary>
61-
/// Used for adding claims to the claims identity.
62-
/// </summary>
63-
/// <param name="claims">The claims to add.</param>
64-
/// <returns>The same claims identity builder.</returns>
54+
/// <inheritdoc />
6555
public IAndClaimsIdentityBuilder WithClaims(IEnumerable<Claim> claims)
6656
{
6757
this.AddClaims(claims);
6858
return this;
6959
}
7060

71-
/// <summary>
72-
/// Used for adding claims to the claims identity.
73-
/// </summary>
74-
/// <param name="claims">The claims to add.</param>
75-
/// <returns>The same claims identity builder.</returns>
61+
/// <inheritdoc />
7662
public IAndClaimsIdentityBuilder WithClaims(params Claim[] claims)
7763
{
7864
return this.WithClaims(claims.AsEnumerable());
7965
}
8066

81-
/// <summary>
82-
/// Used for setting authentication type to the claims identity. If such is not provided, "Passport" is used by default.
83-
/// </summary>
84-
/// <param name="authenticationType">The authentication type to set.</param>
85-
/// <returns>The same claims identity builder.</returns>
67+
/// <inheritdoc />
8668
public IAndClaimsIdentityBuilder WithAuthenticationType(string authenticationType)
8769
{
8870
this.AddAuthenticationType(authenticationType);
8971
return this;
9072
}
9173

92-
/// <summary>
93-
/// Used for adding role to claims identity.
94-
/// </summary>
95-
/// <param name="role">The role to add.</param>
96-
/// <returns>The same claims identity builder.</returns>
74+
/// <inheritdoc />
9775
public IAndClaimsIdentityBuilder InRole(string role)
9876
{
9977
this.AddRole(role);
10078
return this;
10179
}
10280

103-
/// <summary>
104-
/// Used for adding multiple roles to claims identity.
105-
/// </summary>
106-
/// <param name="roles">Collection of roles to add.</param>
107-
/// <returns>The same claims identity builder.</returns>
81+
/// <inheritdoc />
10882
public IAndClaimsIdentityBuilder InRoles(IEnumerable<string> roles)
10983
{
11084
this.AddRoles(roles);
11185
return this;
11286
}
11387

114-
/// <summary>
115-
/// Used for adding multiple roles to claims identity.
116-
/// </summary>
117-
/// <param name="roles">Roles to add.</param>
118-
/// <returns>The same claims identity builder.</returns>
88+
/// <inheritdoc />
11989
public IAndClaimsIdentityBuilder InRoles(params string[] roles)
12090
{
12191
return this.InRoles(roles.AsEnumerable());
12292
}
12393

94+
/// <inheritdoc />
12495
public IClaimsIdentityBuilder AndAlso()
12596
{
12697
return this;

0 commit comments

Comments
 (0)