diff --git a/samples/Autofac/Autofac.AssemblyInit.Test/Autofac.AssemblyInit.Test.csproj b/samples/Autofac/Autofac.AssemblyInit.Test/Autofac.AssemblyInit.Test.csproj index dc24cfba1..db94456e1 100644 --- a/samples/Autofac/Autofac.AssemblyInit.Test/Autofac.AssemblyInit.Test.csproj +++ b/samples/Autofac/Autofac.AssemblyInit.Test/Autofac.AssemblyInit.Test.csproj @@ -16,4 +16,8 @@ + + + + diff --git a/samples/Autofac/Autofac.NoContainerBuilder.Web/Autofac.NoContainerBuilder.Web.csproj b/samples/Autofac/Autofac.NoContainerBuilder.Web/Autofac.NoContainerBuilder.Web.csproj index 214c6dc8f..4b40493d0 100644 --- a/samples/Autofac/Autofac.NoContainerBuilder.Web/Autofac.NoContainerBuilder.Web.csproj +++ b/samples/Autofac/Autofac.NoContainerBuilder.Web/Autofac.NoContainerBuilder.Web.csproj @@ -11,4 +11,9 @@ + + + + + diff --git a/samples/Autofac/Autofac.NoContainerBuilder.Web/Properties/launchSettings.json b/samples/Autofac/Autofac.NoContainerBuilder.Web/Properties/launchSettings.json deleted file mode 100644 index 6fe01b0b2..000000000 --- a/samples/Autofac/Autofac.NoContainerBuilder.Web/Properties/launchSettings.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:64779", - "sslPort": 44367 - } - }, - "profiles": { - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "Autofac.NoContainerBuilder.Web": { - "commandName": "Project", - "launchBrowser": true, - "applicationUrl": "https://localhost:5001;http://localhost:5000", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - } - } -} \ No newline at end of file diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/BaseClaimsPrincipalUserBuilder.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/BaseClaimsPrincipalUserBuilder.cs new file mode 100644 index 000000000..2dcd0a964 --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/BaseClaimsPrincipalUserBuilder.cs @@ -0,0 +1,65 @@ +namespace MyTested.AspNetCore.Mvc.Builders.Authentication +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Security.Claims; + + /// + /// Base class for creating mocked authenticated . + /// + public class BaseClaimsPrincipalUserBuilder : BaseUserBuilder + { + private readonly ICollection identities; + + /// + /// Initializes a new instance of the class. + /// + public BaseClaimsPrincipalUserBuilder() + => this.identities = new List(); + + /// + /// Returns the principle based on provided identities and claims. + /// + /// This . + public ClaimsPrincipal GetClaimsPrincipal() + { + var claimIdentities = this.identities.Reverse().ToList(); + claimIdentities.Add(this.GetAuthenticatedClaimsIdentity()); + + var claimsPrincipal = new ClaimsPrincipal(claimIdentities); + + return claimsPrincipal; + } + + /// + /// Returns the principle based on provided claims only. + /// + /// This . + public ClaimsPrincipal GetClaimsPrincipalBasedOnClaimsOnly() + { + var claimsPrincipal = new ClaimsPrincipal(this.GetAuthenticatedClaimsIdentity()); + + return claimsPrincipal; + } + + /// + /// Static constructor for creating default authenticated claims principal with "TestId" identifier and "TestUser" username. + /// + /// Authenticated . + /// Result of type . + public static ClaimsPrincipal DefaultAuthenticated { get; } + = new ClaimsPrincipal(CreateAuthenticatedClaimsIdentity()); + + protected void AddIdentity(ClaimsIdentity identity) + => this.identities.Add(identity); + + protected void AddIdentities(IEnumerable identities) + { + foreach (var identity in identities) + { + this.AddIdentity(identity); + } + } + } +} diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/BaseUserBuilder.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/BaseUserBuilder.cs index 48299003d..333a77814 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/BaseUserBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/BaseUserBuilder.cs @@ -74,7 +74,7 @@ protected static ClaimsIdentity CreateAuthenticatedClaimsIdentity( /// Creates new authenticated claims identity by using the accumulated claims and authentication type. /// /// Mock of . - protected ClaimsIdentity GetAuthenticatedClaimsIdentity() + protected ClaimsIdentity GetAuthenticatedClaimsIdentity() => CreateAuthenticatedClaimsIdentity( this.claims, this.authenticationType, @@ -97,51 +97,91 @@ protected ClaimsIdentity GetAuthenticatedClaimsIdentity() /// Sets identifier claim to the built . /// /// Value of the identifier claim - . - protected void AddIdentifier(string identifier) + protected void AddIdentifier(string identifier) => this.AddClaim(ClaimTypes.NameIdentifier, identifier); /// /// Sets username claims to the built . /// /// Value of the username claim. Default claim type is . - protected void AddUsername(string username) + protected void AddUsername(string username) => this.AddClaim(this.nameType, username); /// /// Adds claim to the built . /// /// The to add. - protected void AddClaim(Claim claim) + protected void AddClaim(Claim claim) => this.claims.Add(claim); /// /// Adds claims to the built . /// /// Collection of to add. - protected void AddClaims(IEnumerable claims) + protected void AddClaims(IEnumerable claims) => claims.ForEach(this.AddClaim); /// /// Adds authentication type to the built . /// /// Authentication type to add. Default is "Passport". - protected void AddAuthenticationType(string authenticationType) + protected void AddAuthenticationType(string authenticationType) => this.authenticationType = authenticationType; /// /// Adds role to the built . /// /// Value of the role claim. Default claim type is . - protected void AddRole(string role) + protected void AddRole(string role) => this.AddClaim(this.roleType, role); /// /// Adds roles to the built . /// /// Collection of roles to add. - protected void AddRoles(IEnumerable roles) + protected void AddRoles(IEnumerable roles) => roles.ForEach(this.AddRole); + /// + /// Removes provided from the list of claims. + /// + /// Claim to be removed. + protected void RemoveClaim(Claim claim) + { + if (this.claims.Contains(claim)) + { + this.claims.Remove(claim); + } + } + + /// + /// Removes provided claim by its type and value from the list of claims. + /// + /// Claim's type. + /// Claim's value. + protected void RemoveClaim(string type, string value) + { + var claimsToRemove = this.claims + .Where(x => x.Type.Equals(type) && x.Value.Equals(value)) + .ToList(); + + claimsToRemove.ForEach(claim => this.claims.Remove(claim)); + } + + /// + /// Remove claim by providing its role. + /// + /// Claim's role. + protected void RemoveRole(string role) + => this.RemoveClaim(this.roleType, role); + + /// + /// Remove claim by providing its username. + /// + /// Claim's username. + protected void RemoveUsername(string username) + => this.RemoveClaim(this.nameType, username); + /// /// Adds claim to the built . /// diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/ClaimsPrincipalBuilder.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/ClaimsPrincipalBuilder.cs deleted file mode 100644 index 7eb54d3c2..000000000 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/ClaimsPrincipalBuilder.cs +++ /dev/null @@ -1,140 +0,0 @@ -namespace MyTested.AspNetCore.Mvc.Builders.Authentication -{ - using System; - using System.Collections.Generic; - using System.Linq; - using System.Security.Claims; - using System.Security.Principal; - using Contracts.Authentication; - - /// - /// Used for building mocked . - /// - public class ClaimsPrincipalBuilder : BaseUserBuilder, IAndClaimsPrincipalBuilder - { - private readonly ICollection identities; - - /// - /// Initializes a new instance of the class. - /// - public ClaimsPrincipalBuilder() - => this.identities = new List(); - - /// - /// Static constructor for creating default authenticated claims principal with "TestId" identifier and "TestUser" username. - /// - /// Authenticated . - /// Result of type . - public static ClaimsPrincipal DefaultAuthenticated { get; } - = new ClaimsPrincipal(CreateAuthenticatedClaimsIdentity()); - - /// - public IAndClaimsPrincipalBuilder WithNameType(string nameType) - { - this.AddNameType(nameType); - return this; - } - - /// - public IAndClaimsPrincipalBuilder WithRoleType(string roleType) - { - this.AddRoleType(roleType); - return this; - } - - /// - public IAndClaimsPrincipalBuilder WithIdentifier(string identifier) - { - this.AddIdentifier(identifier); - return this; - } - - /// - public IAndClaimsPrincipalBuilder WithUsername(string username) - { - this.AddUsername(username); - return this; - } - - /// - public IAndClaimsPrincipalBuilder WithClaim(string type, string value) - => this.WithClaim(new Claim(type, value)); - - /// - public IAndClaimsPrincipalBuilder WithClaim(Claim claim) - { - this.AddClaim(claim); - return this; - } - - /// - public IAndClaimsPrincipalBuilder WithClaims(IEnumerable claims) - { - this.AddClaims(claims); - return this; - } - - /// - public IAndClaimsPrincipalBuilder WithClaims(params Claim[] claims) - => this.WithClaims(claims.AsEnumerable()); - - /// - public IAndClaimsPrincipalBuilder WithAuthenticationType(string authenticationType) - { - this.AddAuthenticationType(authenticationType); - return this; - } - - /// - public IAndClaimsPrincipalBuilder InRole(string role) - { - this.AddRole(role); - return this; - } - - /// - public IAndClaimsPrincipalBuilder InRoles(IEnumerable roles) - { - this.AddRoles(roles); - return this; - } - - /// - public IAndClaimsPrincipalBuilder InRoles(params string[] roles) - => this.InRoles(roles.AsEnumerable()); - - /// - public IAndClaimsPrincipalBuilder WithIdentity(IIdentity identity) - { - if (!(identity is ClaimsIdentity claimsIdentity)) - { - claimsIdentity = new ClaimsIdentity(identity); - } - - this.identities.Add(claimsIdentity); - return this; - } - - /// - public IAndClaimsPrincipalBuilder WithIdentity(Action claimsIdentityBuilder) - { - var newClaimsIdentityBuilder = new ClaimsIdentityBuilder(); - claimsIdentityBuilder(newClaimsIdentityBuilder); - this.identities.Add(newClaimsIdentityBuilder.GetClaimsIdentity()); - return this; - } - - /// - public IClaimsPrincipalBuilder AndAlso() => this; - - public ClaimsPrincipal GetClaimsPrincipal() - { - var claimIdentities = this.identities.Reverse().ToList(); - claimIdentities.Add(this.GetAuthenticatedClaimsIdentity()); - - var claimsPrincipal = new ClaimsPrincipal(claimIdentities); - - return claimsPrincipal; - } - } -} diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/ClaimsIdentityBuilder.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/WithClaimsIdentityBuilder.cs similarity index 61% rename from src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/ClaimsIdentityBuilder.cs rename to src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/WithClaimsIdentityBuilder.cs index d46904ec2..1b3a3d6d8 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/ClaimsIdentityBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/WithClaimsIdentityBuilder.cs @@ -8,85 +8,85 @@ /// /// Used for creating mocked authenticated . /// - public class ClaimsIdentityBuilder : BaseUserBuilder, IAndClaimsIdentityBuilder + public class WithClaimsIdentityBuilder : BaseUserBuilder, IAndWithClaimsIdentityBuilder { /// - public IAndClaimsIdentityBuilder WithNameType(string nameType) + public IAndWithClaimsIdentityBuilder WithNameType(string nameType) { this.AddNameType(nameType); return this; } /// - public IAndClaimsIdentityBuilder WithRoleType(string roleType) + public IAndWithClaimsIdentityBuilder WithRoleType(string roleType) { this.AddRoleType(roleType); return this; } /// - public IAndClaimsIdentityBuilder WithIdentifier(string identifier) + public IAndWithClaimsIdentityBuilder WithIdentifier(string identifier) { this.AddIdentifier(identifier); return this; } /// - public IAndClaimsIdentityBuilder WithUsername(string username) + public IAndWithClaimsIdentityBuilder WithUsername(string username) { this.AddUsername(username); return this; } /// - public IAndClaimsIdentityBuilder WithClaim(string type, string value) + public IAndWithClaimsIdentityBuilder WithClaim(string type, string value) => this.WithClaim(new Claim(type, value)); /// - public IAndClaimsIdentityBuilder WithClaim(Claim claim) + public IAndWithClaimsIdentityBuilder WithClaim(Claim claim) { this.AddClaim(claim); return this; } /// - public IAndClaimsIdentityBuilder WithClaims(IEnumerable claims) + public IAndWithClaimsIdentityBuilder WithClaims(IEnumerable claims) { this.AddClaims(claims); return this; } /// - public IAndClaimsIdentityBuilder WithClaims(params Claim[] claims) + public IAndWithClaimsIdentityBuilder WithClaims(params Claim[] claims) => this.WithClaims(claims.AsEnumerable()); /// - public IAndClaimsIdentityBuilder WithAuthenticationType(string authenticationType) + public IAndWithClaimsIdentityBuilder WithAuthenticationType(string authenticationType) { this.AddAuthenticationType(authenticationType); return this; } /// - public IAndClaimsIdentityBuilder InRole(string role) + public IAndWithClaimsIdentityBuilder InRole(string role) { this.AddRole(role); return this; } /// - public IAndClaimsIdentityBuilder InRoles(IEnumerable roles) + public IAndWithClaimsIdentityBuilder InRoles(IEnumerable roles) { this.AddRoles(roles); return this; } /// - public IAndClaimsIdentityBuilder InRoles(params string[] roles) + public IAndWithClaimsIdentityBuilder InRoles(params string[] roles) => this.InRoles(roles.AsEnumerable()); /// - public IClaimsIdentityBuilder AndAlso() => this; + public IWithClaimsIdentityBuilder AndAlso() => this; internal ClaimsIdentity GetClaimsIdentity() => this.GetAuthenticatedClaimsIdentity(); diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/WithClaimsPrincipalBuilder.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/WithClaimsPrincipalBuilder.cs new file mode 100644 index 000000000..3a3cca12c --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/WithClaimsPrincipalBuilder.cs @@ -0,0 +1,115 @@ +namespace MyTested.AspNetCore.Mvc.Builders.Authentication +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Security.Claims; + using System.Security.Principal; + using Contracts.Authentication; + + /// + /// Used for building mocked . + /// + public class WithClaimsPrincipalBuilder : BaseClaimsPrincipalUserBuilder, IAndWithClaimsPrincipalBuilder + { + /// + public IAndWithClaimsPrincipalBuilder WithNameType(string nameType) + { + this.AddNameType(nameType); + return this; + } + + /// + public IAndWithClaimsPrincipalBuilder WithRoleType(string roleType) + { + this.AddRoleType(roleType); + return this; + } + + /// + public IAndWithClaimsPrincipalBuilder WithIdentifier(string identifier) + { + this.AddIdentifier(identifier); + return this; + } + + /// + public IAndWithClaimsPrincipalBuilder WithUsername(string username) + { + this.AddUsername(username); + return this; + } + + /// + public IAndWithClaimsPrincipalBuilder WithClaim(string type, string value) + => this.WithClaim(new Claim(type, value)); + + /// + public IAndWithClaimsPrincipalBuilder WithClaim(Claim claim) + { + this.AddClaim(claim); + return this; + } + + /// + public IAndWithClaimsPrincipalBuilder WithClaims(IEnumerable claims) + { + this.AddClaims(claims); + return this; + } + + /// + public IAndWithClaimsPrincipalBuilder WithClaims(params Claim[] claims) + => this.WithClaims(claims.AsEnumerable()); + + /// + public IAndWithClaimsPrincipalBuilder WithAuthenticationType(string authenticationType) + { + this.AddAuthenticationType(authenticationType); + return this; + } + + /// + public IAndWithClaimsPrincipalBuilder InRole(string role) + { + this.AddRole(role); + return this; + } + + /// + public IAndWithClaimsPrincipalBuilder InRoles(IEnumerable roles) + { + this.AddRoles(roles); + return this; + } + + /// + public IAndWithClaimsPrincipalBuilder InRoles(params string[] roles) + => this.InRoles(roles.AsEnumerable()); + + /// + public IAndWithClaimsPrincipalBuilder WithIdentity(IIdentity identity) + { + if (!(identity is ClaimsIdentity claimsIdentity)) + { + claimsIdentity = new ClaimsIdentity(identity); + } + + base.AddIdentity(claimsIdentity); + return this; + } + + /// + public IAndWithClaimsPrincipalBuilder WithIdentity(Action claimsIdentityBuilder) + { + var newClaimsIdentityBuilder = new WithClaimsIdentityBuilder(); + claimsIdentityBuilder(newClaimsIdentityBuilder); + + base.AddIdentity(newClaimsIdentityBuilder.GetClaimsIdentity()); + return this; + } + + /// + public IWithClaimsPrincipalBuilder AndAlso() => this; + } +} diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/WithoutClaimsPrincipalBuilder.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/WithoutClaimsPrincipalBuilder.cs new file mode 100644 index 000000000..ca1de5ad5 --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Authentication/WithoutClaimsPrincipalBuilder.cs @@ -0,0 +1,49 @@ +namespace MyTested.AspNetCore.Mvc.Builders.Authentication +{ + using System.Security.Claims; + using MyTested.AspNetCore.Mvc.Builders.Contracts.Authentication; + + /// + /// Used for building mocked . + /// + public class WithoutClaimsPrincipalBuilder : BaseClaimsPrincipalUserBuilder, IAndWithoutClaimsPrincipalBuilder + { + /// + /// Initializes a new instance of the class. + /// + public WithoutClaimsPrincipalBuilder(ClaimsPrincipal principal) + => base.AddClaims(principal.Claims); + + /// + public IAndWithoutClaimsPrincipalBuilder WithoutClaim(string type, string value) + { + base.RemoveClaim(type, value); + return this; + } + + /// + public IAndWithoutClaimsPrincipalBuilder WithoutClaim(Claim claim) + { + base.RemoveClaim(claim); + return this; + } + + /// + public IAndWithoutClaimsPrincipalBuilder WithoutRole(string role) + { + base.RemoveRole(role); + return this; + } + + /// + public IAndWithoutClaimsPrincipalBuilder WithoutUsername(string username) + { + base.RemoveUsername(username); + return this; + } + + /// + public IWithoutClaimsPrincipalBuilder AndAlso() + => this; + } +} diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IAndClaimsIdentityBuilder.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IAndWithClaimsIdentityBuilder.cs similarity index 65% rename from src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IAndClaimsIdentityBuilder.cs rename to src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IAndWithClaimsIdentityBuilder.cs index 6ef42cdff..a32ea9da3 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IAndClaimsIdentityBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IAndWithClaimsIdentityBuilder.cs @@ -3,12 +3,12 @@ /// /// Used for adding AndAlso() method to the builder. /// - public interface IAndClaimsIdentityBuilder : IClaimsIdentityBuilder + public interface IAndWithClaimsIdentityBuilder : IWithClaimsIdentityBuilder { /// /// AndAlso method for better readability when building mocked . /// - /// The same . - IClaimsIdentityBuilder AndAlso(); + /// The same . + IWithClaimsIdentityBuilder AndAlso(); } } diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IAndClaimsPrincipalBuilder.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IAndWithClaimsPrincipalBuilder.cs similarity index 65% rename from src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IAndClaimsPrincipalBuilder.cs rename to src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IAndWithClaimsPrincipalBuilder.cs index 80900c413..c48c9dc6d 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IAndClaimsPrincipalBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IAndWithClaimsPrincipalBuilder.cs @@ -3,12 +3,12 @@ /// /// Used for adding AndAlso() method to the builder. /// - public interface IAndClaimsPrincipalBuilder : IClaimsPrincipalBuilder + public interface IAndWithClaimsPrincipalBuilder : IWithClaimsPrincipalBuilder { /// /// AndAlso method for better readability when building . /// - /// The same . - IClaimsPrincipalBuilder AndAlso(); + /// The same . + IWithClaimsPrincipalBuilder AndAlso(); } } diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IAndWithoutClaimsPrincipalBuilder.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IAndWithoutClaimsPrincipalBuilder.cs new file mode 100644 index 000000000..b075fb1e2 --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IAndWithoutClaimsPrincipalBuilder.cs @@ -0,0 +1,14 @@ +namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Authentication +{ + /// + /// Used for adding AndAlso() method to the builder. + /// + public interface IAndWithoutClaimsPrincipalBuilder : IWithoutClaimsPrincipalBuilder + { + /// + /// AndAlso method for better readability when building . + /// + /// The same . + IWithoutClaimsPrincipalBuilder AndAlso(); + } +} diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IClaimsIdentityBuilder.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IWithClaimsIdentityBuilder.cs similarity index 60% rename from src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IClaimsIdentityBuilder.cs rename to src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IWithClaimsIdentityBuilder.cs index c020ea36d..143ec7d2b 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IClaimsIdentityBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IWithClaimsIdentityBuilder.cs @@ -6,91 +6,91 @@ /// /// Used for building mocked . /// - public interface IClaimsIdentityBuilder + public interface IWithClaimsIdentityBuilder { /// /// Sets type of the username claim. Default is . /// /// Type to set on the username claim. - /// The same . - IAndClaimsIdentityBuilder WithNameType(string nameType); + /// The same . + IAndWithClaimsIdentityBuilder WithNameType(string nameType); /// /// Sets type of the role claim. Default is . /// /// Type to set on the role claim. - /// The same . - IAndClaimsIdentityBuilder WithRoleType(string roleType); + /// The same . + IAndWithClaimsIdentityBuilder WithRoleType(string roleType); /// /// Sets identifier claim to the built . If such is not provided, "TestId" is used by default. /// /// Value of the identifier claim - . - /// The same . - IAndClaimsIdentityBuilder WithIdentifier(string identifier); + /// The same . + IAndWithClaimsIdentityBuilder WithIdentifier(string identifier); /// /// Sets username claims to the built . If such is not provided, "TestUser" is used by default. /// /// Value of the username claim. Default claim type is . - /// The same . - IAndClaimsIdentityBuilder WithUsername(string username); + /// The same . + IAndWithClaimsIdentityBuilder WithUsername(string username); /// /// Adds claim to the built . /// /// Type of the to add. /// Value of the to add. - /// The same . - IAndClaimsIdentityBuilder WithClaim(string type, string value); + /// The same . + IAndWithClaimsIdentityBuilder WithClaim(string type, string value); /// /// Adds claim to the built . /// /// The to add. - /// The same . - IAndClaimsIdentityBuilder WithClaim(Claim claim); + /// The same . + IAndWithClaimsIdentityBuilder WithClaim(Claim claim); /// /// Adds claims to the built . /// /// Collection of to add. - /// The same . - IAndClaimsIdentityBuilder WithClaims(IEnumerable claims); + /// The same . + IAndWithClaimsIdentityBuilder WithClaims(IEnumerable claims); /// /// Adds claims to the built . /// /// parameters to add. - /// The same . - IAndClaimsIdentityBuilder WithClaims(params Claim[] claims); + /// The same . + IAndWithClaimsIdentityBuilder WithClaims(params Claim[] claims); /// /// Adds authentication type to the built . If such is not provided, "Passport" is used by default. /// /// Authentication type to add. - /// The same . - IAndClaimsIdentityBuilder WithAuthenticationType(string authenticationType); + /// The same . + IAndWithClaimsIdentityBuilder WithAuthenticationType(string authenticationType); /// /// Adds role to the built . /// /// Value of the role claim. Default claim type is . - /// The same . - IAndClaimsIdentityBuilder InRole(string role); + /// The same . + IAndWithClaimsIdentityBuilder InRole(string role); /// /// Adds roles to the built . /// /// Collection of role names to add. - /// The same . - IAndClaimsIdentityBuilder InRoles(IEnumerable roles); + /// The same . + IAndWithClaimsIdentityBuilder InRoles(IEnumerable roles); /// /// Adds roles to the built . /// /// Role name parameters to add. - /// The same . - IAndClaimsIdentityBuilder InRoles(params string[] roles); + /// The same . + IAndWithClaimsIdentityBuilder InRoles(params string[] roles); } } diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IClaimsPrincipalBuilder.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IWithClaimsPrincipalBuilder.cs similarity index 60% rename from src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IClaimsPrincipalBuilder.cs rename to src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IWithClaimsPrincipalBuilder.cs index 902b7390f..522664a91 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IClaimsPrincipalBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IWithClaimsPrincipalBuilder.cs @@ -8,105 +8,105 @@ /// /// Used for building mocked . /// - public interface IClaimsPrincipalBuilder + public interface IWithClaimsPrincipalBuilder { /// /// Sets type of the username claim. Default is . /// /// Type to set on the username claim. - /// The same . - IAndClaimsPrincipalBuilder WithNameType(string nameType); + /// The same . + IAndWithClaimsPrincipalBuilder WithNameType(string nameType); /// /// Sets type of the role claim. Default is . /// /// Type to set on the role claim. - /// The same . - IAndClaimsPrincipalBuilder WithRoleType(string roleType); + /// The same . + IAndWithClaimsPrincipalBuilder WithRoleType(string roleType); /// /// Sets identifier (Id) claim to the built . If such is not provided, "TestId" is used by default. /// /// Value of the identifier claim - . - /// The same . - IAndClaimsPrincipalBuilder WithIdentifier(string identifier); + /// The same . + IAndWithClaimsPrincipalBuilder WithIdentifier(string identifier); /// /// Sets username claims to the built . If such is not provided, "TestUser" is used by default. /// /// Value of the username claim. Default claim type is . - /// The same . - IAndClaimsPrincipalBuilder WithUsername(string username); + /// The same . + IAndWithClaimsPrincipalBuilder WithUsername(string username); /// /// Adds claim to the built . /// /// Type of the to add. /// Value of the to add. - /// The same . - IAndClaimsPrincipalBuilder WithClaim(string type, string value); + /// The same . + IAndWithClaimsPrincipalBuilder WithClaim(string type, string value); /// /// Adds claim to the built . /// /// The to add. - /// The same . - IAndClaimsPrincipalBuilder WithClaim(Claim claim); + /// The same . + IAndWithClaimsPrincipalBuilder WithClaim(Claim claim); /// /// Adds claims to the built . /// /// Collection of to add. - /// The same . - IAndClaimsPrincipalBuilder WithClaims(IEnumerable claims); + /// The same . + IAndWithClaimsPrincipalBuilder WithClaims(IEnumerable claims); /// /// Adds claims to the built . /// /// parameters to add. - /// The same . - IAndClaimsPrincipalBuilder WithClaims(params Claim[] claims); + /// The same . + IAndWithClaimsPrincipalBuilder WithClaims(params Claim[] claims); /// /// Adds authentication type to the built . If such is not provided, "Passport" is used by default. /// /// Authentication type to add. - /// The same . - IAndClaimsPrincipalBuilder WithAuthenticationType(string authenticationType); + /// The same . + IAndWithClaimsPrincipalBuilder WithAuthenticationType(string authenticationType); /// /// Adds role to the built . /// /// Value of the role claim. Default claim type is . - /// The same . - IAndClaimsPrincipalBuilder InRole(string role); + /// The same . + IAndWithClaimsPrincipalBuilder InRole(string role); /// /// Adds roles to the built . /// /// Collection of role names to add. - /// The same . - IAndClaimsPrincipalBuilder InRoles(IEnumerable roles); + /// The same . + IAndWithClaimsPrincipalBuilder InRoles(IEnumerable roles); /// /// Adds roles to the built . /// /// Role name parameters to add. - /// The same . - IAndClaimsPrincipalBuilder InRoles(params string[] roles); + /// The same . + IAndWithClaimsPrincipalBuilder InRoles(params string[] roles); /// /// Adds to the built . /// /// to add. - /// The same . - IAndClaimsPrincipalBuilder WithIdentity(IIdentity identity); + /// The same . + IAndWithClaimsPrincipalBuilder WithIdentity(IIdentity identity); /// /// Adds to the built . /// /// Builder for creating mocked . - /// The same . - IAndClaimsPrincipalBuilder WithIdentity(Action claimsIdentityBuilder); + /// The same . + IAndWithClaimsPrincipalBuilder WithIdentity(Action claimsIdentityBuilder); } } diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IWithoutClaimsPrincipalBuilder.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IWithoutClaimsPrincipalBuilder.cs new file mode 100644 index 000000000..870f4da3e --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Authentication/IWithoutClaimsPrincipalBuilder.cs @@ -0,0 +1,39 @@ +namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Authentication +{ + using System.Security.Claims; + + /// + /// Used for building mocked . + /// + public interface IWithoutClaimsPrincipalBuilder + { + /// + /// Remove Claim with the provided role. + /// + /// The claim's role. + /// The same . + IAndWithoutClaimsPrincipalBuilder WithoutRole(string role); + + /// + /// Remove Claim with the provided username. + /// + /// The claim's username. + /// The same . + IAndWithoutClaimsPrincipalBuilder WithoutUsername(string username); + + /// + /// Remove Claim with the provided type and value. + /// + /// The claim's type. + /// The claim's value. + /// The same . + IAndWithoutClaimsPrincipalBuilder WithoutClaim(string type, string value); + + /// + /// Removes the provided claim. + /// + /// The claim. + /// The same . + IAndWithoutClaimsPrincipalBuilder WithoutClaim(Claim claim); + } +} diff --git a/src/MyTested.AspNetCore.Mvc.Authentication/ComponentBuilderAuthenticationExtensions.cs b/src/MyTested.AspNetCore.Mvc.Authentication/ComponentBuilderAuthenticationWithExtensions.cs similarity index 96% rename from src/MyTested.AspNetCore.Mvc.Authentication/ComponentBuilderAuthenticationExtensions.cs rename to src/MyTested.AspNetCore.Mvc.Authentication/ComponentBuilderAuthenticationWithExtensions.cs index c733fa4c7..7f2d494a0 100644 --- a/src/MyTested.AspNetCore.Mvc.Authentication/ComponentBuilderAuthenticationExtensions.cs +++ b/src/MyTested.AspNetCore.Mvc.Authentication/ComponentBuilderAuthenticationWithExtensions.cs @@ -11,7 +11,7 @@ /// /// Contains authentication extension methods for . /// - public static class ComponentBuilderAuthenticationExtensions + public static class ComponentBuilderAuthenticationWithExtensions { /// /// Sets an authenticated to the @@ -145,7 +145,7 @@ public static TBuilder WithUser(this IBaseTestBuilderWithComponentBuil { var actualBuilder = (BaseTestBuilderWithComponentBuilder)builder; - actualBuilder.HttpContext.User = ClaimsPrincipalBuilder.DefaultAuthenticated; + actualBuilder.HttpContext.User = BaseClaimsPrincipalUserBuilder.DefaultAuthenticated; return actualBuilder.Builder; } @@ -156,17 +156,17 @@ public static TBuilder WithUser(this IBaseTestBuilderWithComponentBuil /// /// Instance of type. /// - /// Action setting the by using . + /// Action setting the by using . /// /// The same component builder. public static TBuilder WithUser( this IBaseTestBuilderWithComponentBuilder builder, - Action userBuilder) + Action userBuilder) where TBuilder : IBaseTestBuilder { var actualBuilder = (BaseTestBuilderWithComponentBuilder)builder; - var newUserBuilder = new ClaimsPrincipalBuilder(); + var newUserBuilder = new WithClaimsPrincipalBuilder(); userBuilder(newUserBuilder); actualBuilder.HttpContext.User = newUserBuilder.GetClaimsPrincipal(); diff --git a/src/MyTested.AspNetCore.Mvc.Authentication/ComponentBuilderAuthenticationWithoutExtensions.cs b/src/MyTested.AspNetCore.Mvc.Authentication/ComponentBuilderAuthenticationWithoutExtensions.cs new file mode 100644 index 000000000..da2f7ec73 --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.Authentication/ComponentBuilderAuthenticationWithoutExtensions.cs @@ -0,0 +1,97 @@ +namespace MyTested.AspNetCore.Mvc +{ + using System; + using System.Security.Claims; + using MyTested.AspNetCore.Mvc.Builders.Authentication; + using MyTested.AspNetCore.Mvc.Builders.Base; + using MyTested.AspNetCore.Mvc.Builders.Contracts.Authentication; + using MyTested.AspNetCore.Mvc.Builders.Contracts.Base; + + /// + /// Contains authentication extension methods for . + /// + public static class ComponentBuilderAuthenticationWithoutExtensions + { + /// + /// Removes the that have the provided claimType and value from + /// + /// Class representing ASP.NET Core MVC test builder. + /// Instance of type. + /// The type of the claim that will be removed. + /// The value of the claim that will be removed. + /// The same component builder. + public static TBuilder WithoutUser( + this IBaseTestBuilderWithComponentBuilder builder, + string claimType, string value) + where TBuilder : IBaseTestBuilder + => builder.WithoutUser(user => user.WithoutClaim(claimType, value)); + + /// + /// Removes the provided from + /// + /// Class representing ASP.NET Core MVC test builder. + /// Instance of type. + /// The claim that will be removed. + /// The same component builder. + public static TBuilder WithoutUser( + this IBaseTestBuilderWithComponentBuilder builder, + Claim claim) + where TBuilder : IBaseTestBuilder + => builder + .WithoutUser(user => user + .WithoutClaim(claim)); + + /// + /// Removes the that have the provided role from + /// + /// Class representing ASP.NET Core MVC test builder. + /// Instance of type. + /// The role of the claim that will be removed. + /// The same component builder. + public static TBuilder WithoutUser( + this IBaseTestBuilderWithComponentBuilder builder, + string role) + where TBuilder : IBaseTestBuilder + => builder + .WithoutUser(user => user + .WithoutRole(role)); + + /// + /// Sets custom authenticated to the + /// built component using the provided user builder. + /// + /// Instance of type. + /// + /// Action setting the by using . + /// + /// The same component builder. + public static TBuilder WithoutUser( + this IBaseTestBuilderWithComponentBuilder builder, + Action userBuilder) + where TBuilder : IBaseTestBuilder + { + var actualBuilder = (BaseTestBuilderWithComponentBuilder)builder; + + var newUserBuilder = new WithoutClaimsPrincipalBuilder(actualBuilder.HttpContext.User); + userBuilder(newUserBuilder); + actualBuilder.HttpContext.User = newUserBuilder.GetClaimsPrincipalBasedOnClaimsOnly(); + + return actualBuilder.Builder; + } + + /// + /// Sets the default authenticated claims principal with "TestId" identifier and "TestUser" username. + /// + /// Instance of type. + /// The same component builder. + public static TBuilder WithoutUser( + this IBaseTestBuilderWithComponentBuilder builder) + where TBuilder : IBaseTestBuilder + { + var actualBuilder = (BaseTestBuilderWithComponentBuilder)builder; + actualBuilder.HttpContext.User = BaseClaimsPrincipalUserBuilder.DefaultAuthenticated; + + return actualBuilder.Builder; + } + } +} diff --git a/src/MyTested.AspNetCore.Mvc.Authentication/HttpRequestBuilderAuthenticationExtensions.cs b/src/MyTested.AspNetCore.Mvc.Authentication/HttpRequestBuilderAuthenticationWithExtensions.cs similarity index 95% rename from src/MyTested.AspNetCore.Mvc.Authentication/HttpRequestBuilderAuthenticationExtensions.cs rename to src/MyTested.AspNetCore.Mvc.Authentication/HttpRequestBuilderAuthenticationWithExtensions.cs index cee9a53de..337e6a737 100644 --- a/src/MyTested.AspNetCore.Mvc.Authentication/HttpRequestBuilderAuthenticationExtensions.cs +++ b/src/MyTested.AspNetCore.Mvc.Authentication/HttpRequestBuilderAuthenticationWithExtensions.cs @@ -11,7 +11,7 @@ /// /// Contains authentication extension methods for . /// - public static class HttpRequestBuilderAuthenticationExtensions + public static class HttpRequestBuilderAuthenticationWithExtensions { /// /// Sets default authenticated to the built request with "TestId" identifier and "TestUser" username. @@ -22,7 +22,7 @@ public static IAndHttpRequestBuilder WithUser(this IHttpRequestBuilder httpReque { var actualHttpRequestBuilder = (HttpRequestBuilder)httpRequestBuilder; - actualHttpRequestBuilder.HttpContext.User = ClaimsPrincipalBuilder.DefaultAuthenticated; + actualHttpRequestBuilder.HttpContext.User = WithClaimsPrincipalBuilder.DefaultAuthenticated; return actualHttpRequestBuilder; } @@ -122,15 +122,15 @@ public static IAndHttpRequestBuilder WithUser( /// Sets custom authenticated to the built request using the provided user builder. /// /// Instance of type. - /// Action setting the by using . + /// Action setting the by using . /// The same . public static IAndHttpRequestBuilder WithUser( this IHttpRequestBuilder httpRequestBuilder, - Action userBuilder) + Action userBuilder) { var actualHttpRequestBuilder = (HttpRequestBuilder)httpRequestBuilder; - var newUserBuilder = new ClaimsPrincipalBuilder(); + var newUserBuilder = new WithClaimsPrincipalBuilder(); userBuilder(newUserBuilder); actualHttpRequestBuilder.HttpContext.User = newUserBuilder.GetClaimsPrincipal(); diff --git a/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/IAndDistributedCacheBuilder.cs b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/WithDistributedCache/IAndWithDistributedCacheBuilder.cs similarity index 66% rename from src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/IAndDistributedCacheBuilder.cs rename to src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/WithDistributedCache/IAndWithDistributedCacheBuilder.cs index 91ae16c02..128ade048 100644 --- a/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/IAndDistributedCacheBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/WithDistributedCache/IAndWithDistributedCacheBuilder.cs @@ -1,14 +1,14 @@ -namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Data.DistributedCache +namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Data { /// /// Used for adding AndAlso() method to the builder. /// - public interface IAndDistributedCacheBuilder : IDistributedCacheBuilder + public interface IAndWithDistributedCacheBuilder : IWithDistributedCacheBuilder { /// /// AndAlso method for better readability when building . /// /// - IDistributedCacheBuilder AndAlso(); + IWithDistributedCacheBuilder AndAlso(); } } diff --git a/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/IDistributedCacheBuilder.cs b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/WithDistributedCache/IWithDistributedCacheBuilder.cs similarity index 61% rename from src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/IDistributedCacheBuilder.cs rename to src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/WithDistributedCache/IWithDistributedCacheBuilder.cs index a51d10db6..65dd37a47 100644 --- a/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/IDistributedCacheBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/WithDistributedCache/IWithDistributedCacheBuilder.cs @@ -1,29 +1,30 @@ -namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Data.DistributedCache +namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Data { using System; using System.Collections.Generic; using Microsoft.Extensions.Caching.Distributed; + using Builders.Contracts.Data.DistributedCache; /// /// Used for building mocked . /// - public interface IDistributedCacheBuilder + public interface IWithDistributedCacheBuilder { /// /// Adds cache entry to the mocked . /// /// Key of the cache entry. /// Value of the cache entry. - /// The same . - IAndDistributedCacheBuilder WithEntry(string key, byte[] value); + /// The same . + IAndWithDistributedCacheBuilder WithEntry(string key, byte[] value); /// /// Adds cache entry to the mocked . /// /// Key of the cache entry. /// String value of the cache entry. - /// The same . - IAndDistributedCacheBuilder WithEntry(string key, string value); + /// The same . + IAndWithDistributedCacheBuilder WithEntry(string key, string value); /// /// Adds cache entry to the mocked . @@ -31,8 +32,8 @@ public interface IDistributedCacheBuilder /// Key of the cache entry. /// Value of the cache entry. /// of the cache entry. - /// The same . - IAndDistributedCacheBuilder WithEntry(string key, byte[] value, DistributedCacheEntryOptions options); + /// The same . + IAndWithDistributedCacheBuilder WithEntry(string key, byte[] value, DistributedCacheEntryOptions options); /// /// Adds cache entry to the mocked . @@ -40,28 +41,28 @@ public interface IDistributedCacheBuilder /// Key of the cache entry. /// String value of the cache entry. /// of the cache entry. - /// The same . - IAndDistributedCacheBuilder WithEntry(string key, string value, DistributedCacheEntryOptions options); + /// The same . + IAndWithDistributedCacheBuilder WithEntry(string key, string value, DistributedCacheEntryOptions options); /// /// Adds cache entry to the mocked . /// /// Builder for creating cache entry. /// The same . - IAndDistributedCacheBuilder WithEntry(Action distributedCacheEntryBuilder); + IAndWithDistributedCacheBuilder WithEntry(Action distributedCacheEntryBuilder); /// /// Adds cache entries to the mocked . /// /// Dictionary of cache entries. - /// The same . - IAndDistributedCacheBuilder WithEntries(IDictionary entries); + /// The same . + IAndWithDistributedCacheBuilder WithEntries(IDictionary entries); /// /// Adds cache entries to the mocked . /// /// Dictionary of cache entries. - /// The same . - IAndDistributedCacheBuilder WithEntries(IDictionary entries); + /// The same . + IAndWithDistributedCacheBuilder WithEntries(IDictionary entries); } } diff --git a/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/WithoutDistributedCache/IAndWithoutDistributedCacheBuilder.cs b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/WithoutDistributedCache/IAndWithoutDistributedCacheBuilder.cs new file mode 100644 index 000000000..b69205260 --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/WithoutDistributedCache/IAndWithoutDistributedCacheBuilder.cs @@ -0,0 +1,14 @@ +namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Data +{ + /// + /// Used for adding AndAlso() method to the builder. + /// + public interface IAndWithoutDistributedCacheBuilder : IWithoutDistributedCacheBuilder + { + /// + /// AndAlso method for better readability when building . + /// + /// + IWithoutDistributedCacheBuilder AndAlso(); + } +} diff --git a/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/WithoutDistributedCache/IWithoutDistributedCacheBuilder.cs b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/WithoutDistributedCache/IWithoutDistributedCacheBuilder.cs new file mode 100644 index 000000000..728ce8266 --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/WithoutDistributedCache/IWithoutDistributedCacheBuilder.cs @@ -0,0 +1,38 @@ +namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Data +{ + using System.Collections.Generic; + using Microsoft.Extensions.Caching.Distributed; + + /// + /// Used for building mocked . + /// + public interface IWithoutDistributedCacheBuilder + { + /// + /// Remove cache entry to the mocked . + /// + /// Key of the cache entry. + /// The same . + IAndWithoutDistributedCacheBuilder WithoutEntry(string key); + + /// + /// Remove cache entries to the mocked . + /// + /// Keys of the cache entries. + /// The same . + IAndWithoutDistributedCacheBuilder WithoutEntries(IEnumerable keys); + + /// + /// Remove cache params to the mocked . + /// + /// Keys of the cache entries. + /// The same . + IAndWithoutDistributedCacheBuilder WithoutEntries(params string[] keys); + + /// + /// Clear all entries persisted into the . + /// + /// The same . + IAndWithoutDistributedCacheBuilder WithoutAllEntries(); + } +} diff --git a/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/IAndWithMemoryCacheBuilder.cs b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/MemoryCache/WithMemoryCache/IAndWithMemoryCacheBuilder.cs similarity index 100% rename from src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/IAndWithMemoryCacheBuilder.cs rename to src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/MemoryCache/WithMemoryCache/IAndWithMemoryCacheBuilder.cs diff --git a/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/IWithMemoryCacheBuilder.cs b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/MemoryCache/WithMemoryCache/IWithMemoryCacheBuilder.cs similarity index 100% rename from src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/IWithMemoryCacheBuilder.cs rename to src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/MemoryCache/WithMemoryCache/IWithMemoryCacheBuilder.cs diff --git a/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/IAndWithoutMemoryCacheBuilder.cs b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/MemoryCache/WithoutMemoryCache/IAndWithoutMemoryCacheBuilder.cs similarity index 100% rename from src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/IAndWithoutMemoryCacheBuilder.cs rename to src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/MemoryCache/WithoutMemoryCache/IAndWithoutMemoryCacheBuilder.cs diff --git a/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/IWithoutMemoryCacheBuilder.cs b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/MemoryCache/WithoutMemoryCache/IWithoutMemoryCacheBuilder.cs similarity index 100% rename from src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/IWithoutMemoryCacheBuilder.cs rename to src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/MemoryCache/WithoutMemoryCache/IWithoutMemoryCacheBuilder.cs diff --git a/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/DistributedCache/BaseDistributedCacheBuilder.cs b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/DistributedCache/BaseDistributedCacheBuilder.cs new file mode 100644 index 000000000..5b1e277d4 --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/DistributedCache/BaseDistributedCacheBuilder.cs @@ -0,0 +1,25 @@ +namespace MyTested.AspNetCore.Mvc.Builders.Data +{ + using System; + using Microsoft.Extensions.Caching.Distributed; + using Microsoft.Extensions.DependencyInjection; + + /// + /// Used for building mocked . + /// + public abstract class BaseDistributedCacheBuilder + { + /// + /// Abstract class. + /// + /// providing the current . + public BaseDistributedCacheBuilder(IServiceProvider services) + => this.DistributedCache = services.GetRequiredService(); + + /// + /// Gets the mocked . + /// + /// Built . + protected IDistributedCache DistributedCache { get; private set; } + } +} diff --git a/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/DistributedCache/DistributedCacheBuilder.cs b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/DistributedCache/WithDistributedCache/WithDistributedCacheBuilder.cs similarity index 57% rename from src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/DistributedCache/DistributedCacheBuilder.cs rename to src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/DistributedCache/WithDistributedCache/WithDistributedCacheBuilder.cs index 5854dab28..54abe8b75 100644 --- a/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/DistributedCache/DistributedCacheBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/DistributedCache/WithDistributedCache/WithDistributedCacheBuilder.cs @@ -1,54 +1,52 @@ -namespace MyTested.AspNetCore.Mvc.Builders.Data.DistributedCache +namespace MyTested.AspNetCore.Mvc.Builders.Data { using System; using System.Collections.Generic; + using Builders.Contracts.Data; + using Builders.Data.DistributedCache; using Contracts.Data.DistributedCache; using Microsoft.Extensions.Caching.Distributed; - using Microsoft.Extensions.DependencyInjection; using Utilities.Extensions; - public class DistributedCacheBuilder : IAndDistributedCacheBuilder + /// + public class WithDistributedCacheBuilder : BaseDistributedCacheBuilder, IAndWithDistributedCacheBuilder { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// providing the current . - public DistributedCacheBuilder(IServiceProvider services) - => this.DistributedCache = services.GetRequiredService(); - - /// - /// Gets the mocked . - /// - /// Built . - protected IDistributedCache DistributedCache { get; private set; } + public WithDistributedCacheBuilder(IServiceProvider services) + : base(services) + { + } /// - public IAndDistributedCacheBuilder WithEntry(string key, byte[] value) + public IAndWithDistributedCacheBuilder WithEntry(string key, byte[] value) { this.DistributedCache.Set(key, value); return this; } - public IAndDistributedCacheBuilder WithEntry(string key, string value) + public IAndWithDistributedCacheBuilder WithEntry(string key, string value) { this.DistributedCache.SetString(key, value); return this; } /// - public IAndDistributedCacheBuilder WithEntry(string key, byte[] value, DistributedCacheEntryOptions options) + public IAndWithDistributedCacheBuilder WithEntry(string key, byte[] value, DistributedCacheEntryOptions options) { this.DistributedCache.Set(key, value, options); return this; } - public IAndDistributedCacheBuilder WithEntry(string key, string value, DistributedCacheEntryOptions options) + public IAndWithDistributedCacheBuilder WithEntry(string key, string value, DistributedCacheEntryOptions options) { this.DistributedCache.SetString(key, value, options); return this; } - public IAndDistributedCacheBuilder WithEntry(Action distributedCacheEntryBuilder) + public IAndWithDistributedCacheBuilder WithEntry(Action distributedCacheEntryBuilder) { var newDistributedCacheEntryBuilder = new DistributedCacheEntryBuilder(); distributedCacheEntryBuilder(newDistributedCacheEntryBuilder); @@ -65,19 +63,19 @@ public IAndDistributedCacheBuilder WithEntry(Action - public IAndDistributedCacheBuilder WithEntries(IDictionary entries) + public IAndWithDistributedCacheBuilder WithEntries(IDictionary entries) { entries.ForEach(e => this.WithEntry(e.Key, e.Value)); return this; } - public IAndDistributedCacheBuilder WithEntries(IDictionary entries) + public IAndWithDistributedCacheBuilder WithEntries(IDictionary entries) { entries.ForEach(e => this.WithEntry(e.Key, e.Value)); return this; } /// - public IDistributedCacheBuilder AndAlso() => this; + public IWithDistributedCacheBuilder AndAlso() => this; } } diff --git a/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/DistributedCache/WithoutDistributedCache/WithoutDistributedCacheBuilder.cs b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/DistributedCache/WithoutDistributedCache/WithoutDistributedCacheBuilder.cs new file mode 100644 index 000000000..9cc9e656a --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/DistributedCache/WithoutDistributedCache/WithoutDistributedCacheBuilder.cs @@ -0,0 +1,52 @@ +namespace MyTested.AspNetCore.Mvc.Builders.Data.DistributedCache.WithoutDistributedCache +{ + using System; + using System.Collections.Generic; + using Microsoft.Extensions.Caching.Distributed; + using MyTested.AspNetCore.Mvc.Builders.Contracts.Data; + using MyTested.AspNetCore.Mvc.Utilities.Extensions; + + /// + public class WithoutDistributedCacheBuilder : BaseDistributedCacheBuilder, IAndWithoutDistributedCacheBuilder + { + /// + /// Initializes a new instance of the class. + /// + /// providing the current . + public WithoutDistributedCacheBuilder(IServiceProvider services) + : base(services) + { + } + + /// + public IAndWithoutDistributedCacheBuilder WithoutAllEntries() + { + this.DistributedCache.AsDistributedCacheMock().ClearCache(); + return this; + } + + /// + public IAndWithoutDistributedCacheBuilder WithoutEntries(IEnumerable keys) + { + this.DistributedCache.AsDistributedCacheMock().RemoveKeys(keys); + return this; + } + + /// + public IAndWithoutDistributedCacheBuilder WithoutEntries(params string[] keys) + { + this.DistributedCache.AsDistributedCacheMock().RemoveKeys(keys); + return this; + } + + /// + public IAndWithoutDistributedCacheBuilder WithoutEntry(string key) + { + this.DistributedCache.Remove(key); + return this; + } + + /// + public IWithoutDistributedCacheBuilder AndAlso() => this; + } +} diff --git a/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/BaseMemoryCacheBuilder.cs b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/MemoryCache/BaseMemoryCacheBuilder.cs similarity index 82% rename from src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/BaseMemoryCacheBuilder.cs rename to src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/MemoryCache/BaseMemoryCacheBuilder.cs index 0b618b91a..e30bcaaaf 100644 --- a/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/BaseMemoryCacheBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/MemoryCache/BaseMemoryCacheBuilder.cs @@ -16,6 +16,10 @@ public abstract class BaseMemoryCacheBuilder public BaseMemoryCacheBuilder(IServiceProvider services) => this.MemoryCache = services.GetRequiredService(); + /// + /// Gets the mocked . + /// + /// Built . protected IMemoryCache MemoryCache { get; private set; } } } diff --git a/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/WithMemoryCacheBuilder.cs b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/MemoryCache/WithMemoryCache/WithMemoryCacheBuilder.cs similarity index 99% rename from src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/WithMemoryCacheBuilder.cs rename to src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/MemoryCache/WithMemoryCache/WithMemoryCacheBuilder.cs index 426a336c9..8e92ca4d0 100644 --- a/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/WithMemoryCacheBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/MemoryCache/WithMemoryCache/WithMemoryCacheBuilder.cs @@ -9,7 +9,7 @@ using Microsoft.Extensions.DependencyInjection; using Utilities.Extensions; - /// 4 + /// public class WithMemoryCacheBuilder : BaseMemoryCacheBuilder, IAndWithMemoryCacheBuilder { /// diff --git a/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/WithoutMemoryCacheBuilder.cs b/src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/MemoryCache/WithoutMemoryCache/WithoutMemoryCacheBuilder.cs similarity index 100% rename from src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/WithoutMemoryCacheBuilder.cs rename to src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/MemoryCache/WithoutMemoryCache/WithoutMemoryCacheBuilder.cs diff --git a/src/MyTested.AspNetCore.Mvc.Caching/ComponentBuilderDistributedCacheExtensions.cs b/src/MyTested.AspNetCore.Mvc.Caching/ComponentBuilderDistributedCacheWithExtensions.cs similarity index 74% rename from src/MyTested.AspNetCore.Mvc.Caching/ComponentBuilderDistributedCacheExtensions.cs rename to src/MyTested.AspNetCore.Mvc.Caching/ComponentBuilderDistributedCacheWithExtensions.cs index bf774aa60..ac881c6c2 100644 --- a/src/MyTested.AspNetCore.Mvc.Caching/ComponentBuilderDistributedCacheExtensions.cs +++ b/src/MyTested.AspNetCore.Mvc.Caching/ComponentBuilderDistributedCacheWithExtensions.cs @@ -3,29 +3,29 @@ using System; using Builders.Base; using Builders.Contracts.Base; - using Builders.Contracts.Data.DistributedCache; - using Builders.Data.DistributedCache; + using Builders.Contracts.Data; + using Builders.Data; /// /// Contains extension methods for . /// - public static class ComponentBuilderDistributedCacheExtensions + public static class ComponentBuilderDistributedCacheWithExtensions { /// /// Sets initial values to the service. /// /// Class representing ASP.NET Core MVC test builder. /// Instance of type. - /// Action setting the values by using . + /// Action setting the values by using . /// The same component builder. public static TBuilder WithDistributedCache( this IBaseTestBuilderWithComponentBuilder builder, - Action distributedCacheBuilder) + Action distributedCacheBuilder) where TBuilder : IBaseTestBuilder { var actualBuilder = (BaseTestBuilderWithComponentBuilder)builder; - distributedCacheBuilder(new DistributedCacheBuilder(actualBuilder.TestContext.HttpContext.RequestServices)); + distributedCacheBuilder(new WithDistributedCacheBuilder(actualBuilder.TestContext.HttpContext.RequestServices)); return actualBuilder.Builder; } diff --git a/src/MyTested.AspNetCore.Mvc.Caching/ComponentBuilderDistributedCacheWithoutExtensions.cs b/src/MyTested.AspNetCore.Mvc.Caching/ComponentBuilderDistributedCacheWithoutExtensions.cs new file mode 100644 index 000000000..71699b2c8 --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.Caching/ComponentBuilderDistributedCacheWithoutExtensions.cs @@ -0,0 +1,91 @@ +namespace MyTested.AspNetCore.Mvc +{ + using System; + using System.Collections.Generic; + using Builders.Base; + using Builders.Contracts.Base; + using Builders.Contracts.Data; + using Builders.Data.DistributedCache.WithoutDistributedCache; + + /// + /// Contains extension methods for . + /// + public static class ComponentBuilderDistributedCacheWithoutExtensions + { + /// + /// Clear all entities from service. + /// + /// Class representing ASP.NET Core MVC test builder. + /// Instance of type. + /// The same component builder. + public static TBuilder WithoutDistributedCache( + this IBaseTestBuilderWithComponentBuilder builder) + where TBuilder : IBaseTestBuilder + => builder + .WithoutDistributedCache(cache => cache + .WithoutAllEntries()); + + /// + /// Remove given entity with key from service. + /// + /// Class representing ASP.NET Core MVC test builder. + /// Instance of type. + /// Key of the entity that will be removed. + /// The same component builder. + public static TBuilder WithoutDistributedCache( + this IBaseTestBuilderWithComponentBuilder builder, + string key) + where TBuilder : IBaseTestBuilder + => builder + .WithoutDistributedCache(cache => cache + .WithoutEntry(key)); + + /// + /// Remove given entities from service. + /// + /// Class representing ASP.NET Core MVC test builder. + /// Instance of type. + /// Keys of the entities that will be removed. + /// The same component builder. + public static TBuilder WithoutDistributedCache( + this IBaseTestBuilderWithComponentBuilder builder, + IEnumerable keys) + where TBuilder : IBaseTestBuilder + => builder + .WithoutDistributedCache(cache => cache + .WithoutEntries(keys)); + + /// + /// Remove given entities from service. + /// + /// Class representing ASP.NET Core MVC test builder. + /// Instance of type. + /// Keys of the entities that will be removed. + /// The same component builder. + public static TBuilder WithoutDistributedCache( + this IBaseTestBuilderWithComponentBuilder builder, + params string[] keys) + where TBuilder : IBaseTestBuilder + => builder + .WithoutDistributedCache(cache => cache + .WithoutEntries(keys)); + + /// + /// Remove entity or entities from service. + /// + /// Class representing ASP.NET Core MVC test builder. + /// Instance of type. + /// Action setting the values by using . + /// The same component builder. + public static TBuilder WithoutDistributedCache( + this IBaseTestBuilderWithComponentBuilder builder, + Action distributedCacheBuilder) + where TBuilder : IBaseTestBuilder + { + var actualBuilder = (BaseTestBuilderWithComponentBuilder)builder; + distributedCacheBuilder(new WithoutDistributedCacheBuilder(actualBuilder.TestContext.HttpContext.RequestServices)); + + return actualBuilder.Builder; + } + } +} diff --git a/src/MyTested.AspNetCore.Mvc.Caching/Internal/Caching/DistributedCacheMock.cs b/src/MyTested.AspNetCore.Mvc.Caching/Internal/Caching/DistributedCacheMock.cs index e9673a4d2..db5316240 100644 --- a/src/MyTested.AspNetCore.Mvc.Caching/Internal/Caching/DistributedCacheMock.cs +++ b/src/MyTested.AspNetCore.Mvc.Caching/Internal/Caching/DistributedCacheMock.cs @@ -79,7 +79,19 @@ public bool TryGetCacheEntryOptions(string key, out DistributedCacheEntryOptions public Dictionary GetCacheAsDictionary() => this.cache.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Value); - public void Dispose() + public void RemoveKeys(IEnumerable keys) + { + foreach (var key in keys) + { + this.Remove(key); + } + } + + public void ClearCache() => this.cache.Clear(); + + public void Dispose() + => this.ClearCache(); + } } diff --git a/src/MyTested.AspNetCore.Mvc.Caching/Internal/Contracts/IDistributedCacheMock.cs b/src/MyTested.AspNetCore.Mvc.Caching/Internal/Contracts/IDistributedCacheMock.cs index 9541d7541..5f00f0191 100644 --- a/src/MyTested.AspNetCore.Mvc.Caching/Internal/Contracts/IDistributedCacheMock.cs +++ b/src/MyTested.AspNetCore.Mvc.Caching/Internal/Contracts/IDistributedCacheMock.cs @@ -11,5 +11,9 @@ public interface IDistributedCacheMock : IDistributedCache, IDisposable bool TryGetCacheEntryOptions(string key, out DistributedCacheEntryOptions cacheEntryOptions); Dictionary GetCacheAsDictionary(); + + void RemoveKeys(IEnumerable keys); + + void ClearCache(); } } diff --git a/src/MyTested.AspNetCore.Mvc.Controllers.ActionResults/SignInTestBuilderExtensions.cs b/src/MyTested.AspNetCore.Mvc.Controllers.ActionResults/SignInTestBuilderExtensions.cs index 7c9879182..89597103a 100644 --- a/src/MyTested.AspNetCore.Mvc.Controllers.ActionResults/SignInTestBuilderExtensions.cs +++ b/src/MyTested.AspNetCore.Mvc.Controllers.ActionResults/SignInTestBuilderExtensions.cs @@ -81,11 +81,11 @@ public static IAndSignInTestBuilder WithPrincipal( /// The same . public static IAndSignInTestBuilder WithPrincipal( this ISignInTestBuilder signInTestBuilder, - Action principalBuilder) + Action principalBuilder) { var actualBuilder = (SignInTestBuilder)signInTestBuilder; - var newClaimsPrincipalBuilder = new ClaimsPrincipalBuilder(); + var newClaimsPrincipalBuilder = new WithClaimsPrincipalBuilder(); principalBuilder(newClaimsPrincipalBuilder); var expectedPrincipal = newClaimsPrincipalBuilder.GetClaimsPrincipal(); diff --git a/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Contracts/Models/IAndModelStateBuilder.cs b/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Contracts/Models/IAndWithModelStateBuilder.cs similarity index 69% rename from src/MyTested.AspNetCore.Mvc.ModelState/Builders/Contracts/Models/IAndModelStateBuilder.cs rename to src/MyTested.AspNetCore.Mvc.ModelState/Builders/Contracts/Models/IAndWithModelStateBuilder.cs index ebafed1ab..760360d8b 100644 --- a/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Contracts/Models/IAndModelStateBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Contracts/Models/IAndWithModelStateBuilder.cs @@ -3,12 +3,12 @@ /// /// Used for adding AndAlso() method to the builder. /// - public interface IAndModelStateBuilder : IModelStateBuilder + public interface IAndWithModelStateBuilder : IWithModelStateBuilder { /// /// AndAlso method for better readability when building . /// - /// The same . - IModelStateBuilder AndAlso(); + /// The same . + IWithModelStateBuilder AndAlso(); } } diff --git a/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Contracts/Models/IAndWithoutModelStateBuilder.cs b/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Contracts/Models/IAndWithoutModelStateBuilder.cs new file mode 100644 index 000000000..02a3ee81d --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Contracts/Models/IAndWithoutModelStateBuilder.cs @@ -0,0 +1,14 @@ +namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Models +{ + /// + /// Used for adding AndAlso() method to the builder. + /// + public interface IAndWithoutModelStateBuilder : IWithoutModelStateBuilder + { + /// + /// AndAlso method for better readability when building . + /// + /// The same . + IWithoutModelStateBuilder AndAlso(); + } +} diff --git a/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Contracts/Models/IModelStateBuilder.cs b/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Contracts/Models/IWithModelStateBuilder.cs similarity index 81% rename from src/MyTested.AspNetCore.Mvc.ModelState/Builders/Contracts/Models/IModelStateBuilder.cs rename to src/MyTested.AspNetCore.Mvc.ModelState/Builders/Contracts/Models/IWithModelStateBuilder.cs index 9a8fcd9e2..012c6e2ef 100644 --- a/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Contracts/Models/IModelStateBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Contracts/Models/IWithModelStateBuilder.cs @@ -5,7 +5,7 @@ /// /// Used for building . /// - public interface IModelStateBuilder + public interface IWithModelStateBuilder { /// /// Adds an error to the built @@ -13,20 +13,20 @@ public interface IModelStateBuilder /// Key to set as string. /// Error message to set as string. /// - IAndModelStateBuilder WithError(string key, string errorMessage); + IAndWithModelStateBuilder WithError(string key, string errorMessage); /// /// Adds model state entries to the built /// /// Model state entries as dictionary. /// - IAndModelStateBuilder WithErrors(IDictionary errors); + IAndWithModelStateBuilder WithErrors(IDictionary errors); /// /// Adds model state entries to the built /// /// Model state entries as anonymous object. /// - IAndModelStateBuilder WithErrors(object errors); + IAndWithModelStateBuilder WithErrors(object errors); } } diff --git a/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Contracts/Models/IWithoutModelStateBuilder.cs b/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Contracts/Models/IWithoutModelStateBuilder.cs new file mode 100644 index 000000000..f7f50bed4 --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Contracts/Models/IWithoutModelStateBuilder.cs @@ -0,0 +1,22 @@ +namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Models +{ + /// + /// Used for building . + /// + public interface IWithoutModelStateBuilder + { + /// + /// Removes all keys and values from this instance of + /// + /// The same . + IAndWithoutModelStateBuilder WithoutModelState(); + + /// + /// Removes the with the specified key + /// from + /// + /// The key of the model state to remove. + /// The same . + IAndWithoutModelStateBuilder WithoutModelState(string key); + } +} \ No newline at end of file diff --git a/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Models/BaseModelStateBuilder.cs b/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Models/BaseModelStateBuilder.cs new file mode 100644 index 000000000..9366c28d2 --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Models/BaseModelStateBuilder.cs @@ -0,0 +1,24 @@ +namespace MyTested.AspNetCore.Mvc.Builders.Models +{ + using Microsoft.AspNetCore.Mvc.ModelBinding; + using MyTested.AspNetCore.Mvc.Internal.TestContexts; + + /// + /// Used for testing specific . + /// + public abstract class BaseModelStateBuilder + { + /// + /// Abstract class. + /// + /// to build. + public BaseModelStateBuilder(ActionTestContext actionContext) + => this.ModelState = actionContext.ModelState; + + /// + /// Gets the + /// + /// The built + protected ModelStateDictionary ModelState { get; set; } + } +} diff --git a/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Models/ModelStateBuilder.cs b/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Models/WithModelStateBuilder.cs similarity index 60% rename from src/MyTested.AspNetCore.Mvc.ModelState/Builders/Models/ModelStateBuilder.cs rename to src/MyTested.AspNetCore.Mvc.ModelState/Builders/Models/WithModelStateBuilder.cs index 8b361ecd4..105ea95cc 100644 --- a/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Models/ModelStateBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Models/WithModelStateBuilder.cs @@ -10,37 +10,33 @@ /// /// Used for building /// - public class ModelStateBuilder : IAndModelStateBuilder + public class WithModelStateBuilder : BaseModelStateBuilder, IAndWithModelStateBuilder { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// to build. - public ModelStateBuilder(ActionTestContext actionContext) - => this.ModelState = actionContext.ModelState; - - /// - /// Gets the - /// - /// The built - protected ModelStateDictionary ModelState { get; set; } + public WithModelStateBuilder(ActionTestContext actionContext) + : base(actionContext) + { + } /// - public IAndModelStateBuilder WithError(string key, string errorMessage) + public IAndWithModelStateBuilder WithError(string key, string errorMessage) { this.AddError(key, errorMessage); return this; } /// - public IAndModelStateBuilder WithErrors(IDictionary errors) + public IAndWithModelStateBuilder WithErrors(IDictionary errors) { errors.ForEach(err => this.AddError(err.Key, err.Value)); return this; } /// - public IAndModelStateBuilder WithErrors(object errors) + public IAndWithModelStateBuilder WithErrors(object errors) { var errorsAsDictionary = new RouteValueDictionary(errors); errorsAsDictionary @@ -50,7 +46,7 @@ public IAndModelStateBuilder WithErrors(object errors) } /// - public IModelStateBuilder AndAlso() => this; + public IWithModelStateBuilder AndAlso() => this; private void AddError(string key, string errorMessage) => this.ModelState.AddModelError(key, errorMessage); diff --git a/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Models/WithoutModelStateBuilder.cs b/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Models/WithoutModelStateBuilder.cs new file mode 100644 index 000000000..dee2dbdd3 --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.ModelState/Builders/Models/WithoutModelStateBuilder.cs @@ -0,0 +1,43 @@ +namespace MyTested.AspNetCore.Mvc.Builders.Models +{ + using Microsoft.AspNetCore.Mvc.ModelBinding; + using MyTested.AspNetCore.Mvc.Builders.Contracts.Models; + using MyTested.AspNetCore.Mvc.Internal.TestContexts; + + /// + /// Used for building . + /// + public class WithoutModelStateBuilder : BaseModelStateBuilder, IAndWithoutModelStateBuilder + { + /// + /// Initializes a new instance of the class. + /// + /// to build. + public WithoutModelStateBuilder(ActionTestContext actionContext) + : base(actionContext) + { + } + + /// + public IAndWithoutModelStateBuilder WithoutModelState() + { + this.ModelState.Clear(); + return this; + } + + /// + public IAndWithoutModelStateBuilder WithoutModelState(string key) + { + if (this.ModelState.ContainsKey(key)) + { + this.ModelState.Remove(key); + } + + return this; + } + + /// + public IWithoutModelStateBuilder AndAlso() + => this; + } +} diff --git a/src/MyTested.AspNetCore.Mvc.ModelState/ComponentBuilderModelStateExtensions.cs b/src/MyTested.AspNetCore.Mvc.ModelState/ComponentBuilderModelStateExtensions.cs deleted file mode 100644 index ad7c89fa8..000000000 --- a/src/MyTested.AspNetCore.Mvc.ModelState/ComponentBuilderModelStateExtensions.cs +++ /dev/null @@ -1,27 +0,0 @@ -namespace MyTested.AspNetCore.Mvc -{ - using System; - using Builders.Base; - using Builders.Contracts.Base; - using Builders.Contracts.Models; - using Builders.Models; - using Internal.TestContexts; - - /// - /// Contains extension methods for . - /// - public static class ComponentBuilderModelStateExtensions - { - public static TBuilder WithModelState( - this IBaseTestBuilderWithComponentBuilder builder, - Action modelStateTestBuilder) - where TBuilder : IBaseTestBuilder - { - var actualBuilder = (BaseTestBuilderWithComponentBuilder)builder; - - modelStateTestBuilder(new ModelStateBuilder(actualBuilder.TestContext as ActionTestContext)); - - return actualBuilder.Builder; - } - } -} diff --git a/src/MyTested.AspNetCore.Mvc.ModelState/ComponentBuilderModelStateWithExtensions.cs b/src/MyTested.AspNetCore.Mvc.ModelState/ComponentBuilderModelStateWithExtensions.cs new file mode 100644 index 000000000..3d8f7f20b --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.ModelState/ComponentBuilderModelStateWithExtensions.cs @@ -0,0 +1,37 @@ +namespace MyTested.AspNetCore.Mvc +{ + using System; + using Builders.Base; + using Builders.Contracts.Base; + using Builders.Contracts.Models; + using Builders.Models; + using Internal.TestContexts; + using Microsoft.AspNetCore.Mvc.ModelBinding; + + /// + /// Contains extension methods for . + /// + public static class ComponentBuilderModelStateWithExtensions + { + /// + /// Used for providing a model state to the . + /// + /// Class representing ASP.NET Core MVC test builder. + /// Instance of type. + /// + /// Action setting the by using . + /// + /// The same component builder. + public static TBuilder WithModelState( + this IBaseTestBuilderWithComponentBuilder builder, + Action modelStateTestBuilder) + where TBuilder : IBaseTestBuilder + { + var actualBuilder = (BaseTestBuilderWithComponentBuilder)builder; + + modelStateTestBuilder(new WithModelStateBuilder(actualBuilder.TestContext as ActionTestContext)); + + return actualBuilder.Builder; + } + } +} diff --git a/src/MyTested.AspNetCore.Mvc.ModelState/ComponentBuilderModelStateWithoutExtensions.cs b/src/MyTested.AspNetCore.Mvc.ModelState/ComponentBuilderModelStateWithoutExtensions.cs new file mode 100644 index 000000000..42ff2f353 --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.ModelState/ComponentBuilderModelStateWithoutExtensions.cs @@ -0,0 +1,65 @@ +namespace MyTested.AspNetCore.Mvc +{ + using System; + using Builders.Base; + using Builders.Contracts.Base; + using Builders.Contracts.Models; + using Builders.Models; + using Internal.TestContexts; + using Microsoft.AspNetCore.Mvc.ModelBinding; + + /// + /// Contains extension methods for . + /// + public static class ComponentBuilderModelStateWithoutExtensions + { + /// + /// Removes the provided key from . + /// + /// Class representing ASP.NET Core MVC test builder. + /// Instance of type. + /// The model state key. + /// The same component builder. + public static TBuilder WithoutModelState( + this IBaseTestBuilderWithComponentBuilder builder, + string key) + where TBuilder : IBaseTestBuilder + => builder + .WithoutModelState(state => state + .WithoutModelState(key)); + + /// + /// Removes all keys and values from . + /// + /// Class representing ASP.NET Core MVC test builder. + /// Instance of type. + /// The same component builder. + public static TBuilder WithoutModelState( + this IBaseTestBuilderWithComponentBuilder builder) + where TBuilder : IBaseTestBuilder + => builder + .WithoutModelState(state => state + .WithoutModelState()); + + /// + /// Used for removing a model state from by using the builder. + /// + /// Class representing ASP.NET Core MVC test builder. + /// Instance of type. + /// + /// Action setting the by using . + /// + /// The same component builder. + public static TBuilder WithoutModelState( + this IBaseTestBuilderWithComponentBuilder builder, + Action modelStateTestBuilder) + where TBuilder : IBaseTestBuilder + { + var actualBuilder = (BaseTestBuilderWithComponentBuilder)builder; + + modelStateTestBuilder(new WithoutModelStateBuilder(actualBuilder.TestContext as ActionTestContext)); + + return actualBuilder.Builder; + } + } +} diff --git a/src/MyTested.AspNetCore.Mvc.TempData/Builders/Contracts/Data/IAndTempDataBuilder.cs b/src/MyTested.AspNetCore.Mvc.TempData/Builders/Contracts/Data/IAndWithTempDataBuilder.cs similarity index 69% rename from src/MyTested.AspNetCore.Mvc.TempData/Builders/Contracts/Data/IAndTempDataBuilder.cs rename to src/MyTested.AspNetCore.Mvc.TempData/Builders/Contracts/Data/IAndWithTempDataBuilder.cs index f7703d6a2..469cc8b91 100644 --- a/src/MyTested.AspNetCore.Mvc.TempData/Builders/Contracts/Data/IAndTempDataBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.TempData/Builders/Contracts/Data/IAndWithTempDataBuilder.cs @@ -3,12 +3,12 @@ /// /// Used for adding AndAlso() method to the builder. /// - public interface IAndTempDataBuilder : ITempDataBuilder + public interface IAndWithTempDataBuilder : IWithTempDataBuilder { /// /// AndAlso method for better readability when building . /// - /// The same . - ITempDataBuilder AndAlso(); + /// The same . + IWithTempDataBuilder AndAlso(); } } diff --git a/src/MyTested.AspNetCore.Mvc.TempData/Builders/Contracts/Data/IAndWithoutTempDataBuilder.cs b/src/MyTested.AspNetCore.Mvc.TempData/Builders/Contracts/Data/IAndWithoutTempDataBuilder.cs new file mode 100644 index 000000000..15b922990 --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.TempData/Builders/Contracts/Data/IAndWithoutTempDataBuilder.cs @@ -0,0 +1,14 @@ +namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Data +{ + /// + /// Used for adding AndAlso() method to the builder. + /// + public interface IAndWithoutTempDataBuilder : IWithoutTempDataBuilder + { + /// + /// AndAlso method for better readability when building . + /// + /// The same . + IWithoutTempDataBuilder AndAlso(); + } +} diff --git a/src/MyTested.AspNetCore.Mvc.TempData/Builders/Contracts/Data/ITempDataBuilder.cs b/src/MyTested.AspNetCore.Mvc.TempData/Builders/Contracts/Data/IWithTempDataBuilder.cs similarity index 84% rename from src/MyTested.AspNetCore.Mvc.TempData/Builders/Contracts/Data/ITempDataBuilder.cs rename to src/MyTested.AspNetCore.Mvc.TempData/Builders/Contracts/Data/IWithTempDataBuilder.cs index f15a7f3bb..84f90324c 100644 --- a/src/MyTested.AspNetCore.Mvc.TempData/Builders/Contracts/Data/ITempDataBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.TempData/Builders/Contracts/Data/IWithTempDataBuilder.cs @@ -5,7 +5,7 @@ /// /// Used for building . /// - public interface ITempDataBuilder + public interface IWithTempDataBuilder { /// /// Adds temp data entry to the built . @@ -13,20 +13,20 @@ public interface ITempDataBuilder /// Key of the temp data entry. /// Value of the temp data entry. /// The same . - IAndTempDataBuilder WithEntry(string key, object value); + IAndWithTempDataBuilder WithEntry(string key, object value); /// /// Adds temp data entries to the built . /// /// Dictionary of temp data entries. /// The same . - IAndTempDataBuilder WithEntries(IDictionary entries); + IAndWithTempDataBuilder WithEntries(IDictionary entries); /// /// Adds temp data entries to the built . /// /// Anonymous object of temp data entries. /// The same . - IAndTempDataBuilder WithEntries(object entries); + IAndWithTempDataBuilder WithEntries(object entries); } } diff --git a/src/MyTested.AspNetCore.Mvc.TempData/Builders/Contracts/Data/IWithoutTempDataBuilder.cs b/src/MyTested.AspNetCore.Mvc.TempData/Builders/Contracts/Data/IWithoutTempDataBuilder.cs new file mode 100644 index 000000000..1bc29bacd --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.TempData/Builders/Contracts/Data/IWithoutTempDataBuilder.cs @@ -0,0 +1,37 @@ +namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Data +{ + using System.Collections.Generic; + + /// + /// Used for building . + /// + public interface IWithoutTempDataBuilder + { + /// + /// Remove temp data entry by providing its key to the built . + /// + /// Key of the temp data entry. + /// The same . + IAndWithoutTempDataBuilder WithoutEntry(string key); + + /// + /// Remove temp data entries by providing their keys to the built . + /// + /// Keys of the temp data entries to be deleted. + /// The same . + IAndWithoutTempDataBuilder WithoutEntries(IEnumerable keys); + + /// + /// Remove temp data entries by providing their keys as params to the built . + /// + /// Keys of the temp data entries to be deleted. + /// The same . + IAndWithoutTempDataBuilder WithoutEntries(params string[] keys); + + /// + /// Clear all entities from the built . + /// + /// The same . + IAndWithoutTempDataBuilder WithoutEntries(); + } +} diff --git a/src/MyTested.AspNetCore.Mvc.TempData/Builders/Data/BaseTempDataBuilder.cs b/src/MyTested.AspNetCore.Mvc.TempData/Builders/Data/BaseTempDataBuilder.cs new file mode 100644 index 000000000..0eac6477c --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.TempData/Builders/Data/BaseTempDataBuilder.cs @@ -0,0 +1,27 @@ +namespace MyTested.AspNetCore.Mvc.Builders.Data +{ + using Microsoft.AspNetCore.Mvc.ViewFeatures; + using Utilities.Validators; + + /// + /// Used for building mocked . + /// + public abstract class BaseTempDataBuilder + { + /// + /// Abstract class. + /// + /// to built. + public BaseTempDataBuilder(ITempDataDictionary tempData) + { + CommonValidator.CheckForNullReference(tempData, nameof(ITempDataDictionary)); + this.TempData = tempData; + } + + /// + /// Gets the mocked . + /// + /// Built . + protected ITempDataDictionary TempData { get; private set; } + } +} diff --git a/src/MyTested.AspNetCore.Mvc.TempData/Builders/Data/TempDataBuilder.cs b/src/MyTested.AspNetCore.Mvc.TempData/Builders/Data/WithTempDataBuilder.cs similarity index 50% rename from src/MyTested.AspNetCore.Mvc.TempData/Builders/Data/TempDataBuilder.cs rename to src/MyTested.AspNetCore.Mvc.TempData/Builders/Data/WithTempDataBuilder.cs index 26a4187e3..028babaf1 100644 --- a/src/MyTested.AspNetCore.Mvc.TempData/Builders/Data/TempDataBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.TempData/Builders/Data/WithTempDataBuilder.cs @@ -5,48 +5,40 @@ using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Routing; using Utilities.Extensions; - using Utilities.Validators; /// /// Used for building mocked . /// - public class TempDataBuilder : IAndTempDataBuilder + public class WithTempDataBuilder : BaseTempDataBuilder, IAndWithTempDataBuilder { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// to built. - public TempDataBuilder(ITempDataDictionary tempData) + public WithTempDataBuilder(ITempDataDictionary tempData) + : base(tempData) { - CommonValidator.CheckForNullReference(tempData, nameof(ITempDataDictionary)); - this.TempData = tempData; } - /// - /// Gets the mocked . - /// - /// Built . - protected ITempDataDictionary TempData { get; private set; } - /// - public IAndTempDataBuilder WithEntry(string key, object value) + public IAndWithTempDataBuilder WithEntry(string key, object value) { this.TempData.Add(key, value); return this; } /// - public IAndTempDataBuilder WithEntries(IDictionary entries) + public IAndWithTempDataBuilder WithEntries(IDictionary entries) { entries.ForEach(e => this.WithEntry(e.Key, e.Value)); return this; } /// - public IAndTempDataBuilder WithEntries(object entries) + public IAndWithTempDataBuilder WithEntries(object entries) => this.WithEntries(new RouteValueDictionary(entries)); /// - public ITempDataBuilder AndAlso() => this; + public IWithTempDataBuilder AndAlso() => this; } } diff --git a/src/MyTested.AspNetCore.Mvc.TempData/Builders/Data/WithoutTempDataBuilder.cs b/src/MyTested.AspNetCore.Mvc.TempData/Builders/Data/WithoutTempDataBuilder.cs new file mode 100644 index 000000000..d472c503b --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.TempData/Builders/Data/WithoutTempDataBuilder.cs @@ -0,0 +1,59 @@ +namespace MyTested.AspNetCore.Mvc.Builders.Data +{ + using System.Collections.Generic; + using System.Linq; + using Contracts.Data; + using Microsoft.AspNetCore.Mvc.ViewFeatures; + + /// + /// Used for building mocked . + /// + public class WithoutTempDataBuilder : BaseTempDataBuilder, IAndWithoutTempDataBuilder + { + /// + /// Initializes a new instance of the class. + /// + /// to built. + public WithoutTempDataBuilder(ITempDataDictionary tempData) + : base(tempData) + { + } + + /// + public IAndWithoutTempDataBuilder WithoutEntries(IEnumerable entriesKeys) + { + foreach (var key in entriesKeys) + { + this.WithoutEntry(key); + } + + return this; + } + + /// + public IAndWithoutTempDataBuilder WithoutEntry(string key) + { + if (this.TempData.ContainsKey(key)) + { + this.TempData.Remove(key); + } + + return this; + } + + /// + public IAndWithoutTempDataBuilder WithoutEntries(params string[] keys) + => WithoutEntries(keys.AsEnumerable()); + + /// + public IAndWithoutTempDataBuilder WithoutEntries() + { + this.TempData.Clear(); + return this; + } + + /// + public IWithoutTempDataBuilder AndAlso() + => this; + } +} diff --git a/src/MyTested.AspNetCore.Mvc.TempData/ComponentBuilderTempDataExtensions.cs b/src/MyTested.AspNetCore.Mvc.TempData/ComponentBuilderTempDataWithExtensions.cs similarity index 83% rename from src/MyTested.AspNetCore.Mvc.TempData/ComponentBuilderTempDataExtensions.cs rename to src/MyTested.AspNetCore.Mvc.TempData/ComponentBuilderTempDataWithExtensions.cs index 668e4edcf..0a6eb9580 100644 --- a/src/MyTested.AspNetCore.Mvc.TempData/ComponentBuilderTempDataExtensions.cs +++ b/src/MyTested.AspNetCore.Mvc.TempData/ComponentBuilderTempDataWithExtensions.cs @@ -10,25 +10,25 @@ /// /// Contains extension methods for . /// - public static class ComponentBuilderTempDataExtensions + public static class ComponentBuilderTempDataWithExtensions { /// /// Sets initial values to the on the tested component. /// /// Class representing ASP.NET Core MVC test builder. /// Instance of type. - /// Action setting the values by using . + /// Action setting the values by using . /// The same component builder. public static TBuilder WithTempData( this IBaseTestBuilderWithComponentBuilder builder, - Action tempDataBuilder) + Action tempDataBuilder) where TBuilder : IBaseTestBuilder { var actualBuilder = (BaseTestBuilderWithComponentBuilder)builder; actualBuilder.TestContext.ComponentPreparationDelegate += () => { - tempDataBuilder(new TempDataBuilder(actualBuilder.TestContext.GetTempData())); + tempDataBuilder(new WithTempDataBuilder(actualBuilder.TestContext.GetTempData())); }; return actualBuilder.Builder; diff --git a/src/MyTested.AspNetCore.Mvc.TempData/ComponentBuilderTempDataWithoutExtensions.cs b/src/MyTested.AspNetCore.Mvc.TempData/ComponentBuilderTempDataWithoutExtensions.cs new file mode 100644 index 000000000..30f946c22 --- /dev/null +++ b/src/MyTested.AspNetCore.Mvc.TempData/ComponentBuilderTempDataWithoutExtensions.cs @@ -0,0 +1,96 @@ +namespace MyTested.AspNetCore.Mvc +{ + using System; + using System.Collections.Generic; + using Builders.Contracts.Base; + using Builders.Contracts.Data; + using Builders.Base; + using Builders.Data; + using Internal.TestContexts; + + /// + /// Contains extension methods for . + /// + public static class ComponentBuilderTempDataWithoutExtensions + { + /// + /// Remove entity by providing its key from . + /// + /// Class representing ASP.NET Core MVC test builder. + /// Instance of type. + /// The key of the entity to be deleted. + /// The same component builder. + public static TBuilder WithoutTempData( + this IBaseTestBuilderWithComponentBuilder builder, + string key) + where TBuilder : IBaseTestBuilder + => builder + .WithoutTempData(tempData => tempData + .WithoutEntry(key)); + + /// + /// Remove entities by providing their keys from . + /// + /// Class representing ASP.NET Core MVC test builder. + /// Instance of type. + /// The keys of the entities to be deleted. + /// The same component builder. + public static TBuilder WithoutTempData( + this IBaseTestBuilderWithComponentBuilder builder, + params string[] keys) + where TBuilder : IBaseTestBuilder + => builder + .WithoutTempData(tempData => tempData + .WithoutEntries(keys)); + + /// + /// Remove entities by providing their keys from . + /// + /// Class representing ASP.NET Core MVC test builder. + /// Instance of type. + /// The keys of the entities to be deleted. + /// The same component builder. + public static TBuilder WithoutTempData( + this IBaseTestBuilderWithComponentBuilder builder, + IEnumerable keys) + where TBuilder : IBaseTestBuilder + => builder + .WithoutTempData(tempData => tempData + .WithoutEntries(keys)); + + /// + /// Removing all entities from . + /// + /// Class representing ASP.NET Core MVC test builder. + /// Instance of type. + /// The same component builder. + public static TBuilder WithoutTempData( + this IBaseTestBuilderWithComponentBuilder builder) + where TBuilder : IBaseTestBuilder + => builder + .WithoutTempData(tempData => tempData + .WithoutEntries()); + + /// + /// Remove values from on the tested component. + /// + /// Class representing ASP.NET Core MVC test builder. + /// Instance of type. + /// Action setting the values by using . + /// The same component builder. + public static TBuilder WithoutTempData( + this IBaseTestBuilderWithComponentBuilder builder, + Action tempDataBuilder) + where TBuilder : IBaseTestBuilder + { + var actualBuilder = (BaseTestBuilderWithComponentBuilder)builder; + + actualBuilder.TestContext.ComponentPreparationDelegate += () => + { + tempDataBuilder(new WithoutTempDataBuilder(actualBuilder.TestContext.GetTempData())); + }; + + return actualBuilder.Builder; + } + } +} diff --git a/src/MyTested.AspNetCore.Mvc.ViewComponents/Internal/TestContexts/ViewComponentTestContext.cs b/src/MyTested.AspNetCore.Mvc.ViewComponents/Internal/TestContexts/ViewComponentTestContext.cs index 9a936d9ef..dee5deda8 100644 --- a/src/MyTested.AspNetCore.Mvc.ViewComponents/Internal/TestContexts/ViewComponentTestContext.cs +++ b/src/MyTested.AspNetCore.Mvc.ViewComponents/Internal/TestContexts/ViewComponentTestContext.cs @@ -15,7 +15,7 @@ public class ViewComponentTestContext : ActionTestContext public override string ExceptionMessagePrefix => $"When invoking {this.Component.GetName()} expected"; - public override ModelStateDictionary ModelState => this.viewComponentContext.ViewData.ModelState; + public override ModelStateDictionary ModelState => this.ViewComponentContext.ViewData.ModelState; protected override ViewContext DefaultComponentContext => ViewContextMock.Default(this); diff --git a/test/MyTested.AspNetCore.Mvc.Authentication.Test/BuildersTests/AuthenticationTests/ClaimsPrincipalBuilderTests.cs b/test/MyTested.AspNetCore.Mvc.Authentication.Test/BuildersTests/AuthenticationTests/ClaimsPrincipalBuilderTests.cs index a6d692577..b939ac6b9 100644 --- a/test/MyTested.AspNetCore.Mvc.Authentication.Test/BuildersTests/AuthenticationTests/ClaimsPrincipalBuilderTests.cs +++ b/test/MyTested.AspNetCore.Mvc.Authentication.Test/BuildersTests/AuthenticationTests/ClaimsPrincipalBuilderTests.cs @@ -8,6 +8,7 @@ using Setups.Controllers; using Microsoft.AspNetCore.Mvc; using Xunit; + using MyTested.AspNetCore.Mvc.Builders.Authentication; public class ClaimsPrincipalBuilderTests { @@ -27,7 +28,7 @@ public void WithNameTypeShouldOverrideDefaultClaimType() Assert.Equal("MyUsername", claim.Value); }); } - + [Fact] public void WithRoleTypeShouldOverrideDefaultClaimType() { @@ -69,7 +70,7 @@ public void WithIdentifierShouldSetCorrectIdentifier() .WithIdentifier("TestingId")) .ShouldPassForThe(controller => { - var claim = controller.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier); + var claim = controller.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier); Assert.NotNull(claim); Assert.Equal("TestingId", claim.Value); @@ -126,7 +127,7 @@ public void WithClaimsShouldSetCorrectClaim() Assert.Equal("MySecondValue", secondClaim.Value); }); } - + [Fact] public void WithClaimsAsEnumerableShouldSetCorrectClaim() { @@ -279,5 +280,71 @@ public void WithFullIdentityBuilderShouldSetProperClaims() Assert.Contains("AnotherListRole", userRoleClaims); }); } + + [Fact] + public void WithoutClaimShouldRemoveTheCorrectClaimOnly() + { + MyController + .Instance() + .WithUser(user => user + .WithNameType("CustomUsername")) + .WithoutUser("CustomUsername", "TestUser") + .ShouldPassForThe(controller => + { + var claimToBeDeleted = controller.User.Claims.FirstOrDefault(c => c.Type == "CustomUsername"); + + Assert.Null(claimToBeDeleted); + }); + } + + [Fact] + public void WithoutClaimByProvidingClaimsDirectlyShouldRemoveTheCorrectClaimOnly() + { + var claim = + new Claim("CustomUsername", "TestUser", "string", "issuer", "OGissuer", + new ClaimsIdentity("Password", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", "http://schemas.microsoft.com/ws/2008/06/identity/claims/role")); + + var builder = MyController + .Instance() + .WithUser(u => u.WithClaim(claim)); + + var claimToDelete = ((MyController)builder).HttpContext.User.Claims.First(x => x.Value.Equals("TestUser")); + builder.WithoutUser(claimToDelete) + .ShouldPassForThe(controller => + { + var deletedClaim = controller.User.Claims.FirstOrDefault(c => c.Type.Equals("CustomUsername")); + Assert.Null(deletedClaim); + }); + } + + [Fact] + public void WithoutRoleShouldRemoveTheCorrectRole() + { + MyController + .Instance() + .WithUser(user => user.InRole("randomRole")) + .WithoutUser("randomRole") + .ShouldPassForThe(controller => + { + var claimToBeDeleted = controller.User.Claims.FirstOrDefault(c => c.Type == "randomRole"); + + Assert.Null(claimToBeDeleted); + }); + } + + [Fact] + public void WithoutUserShouldReturnTheDefaultSetUser() + { + MyController + .Instance() + .WithUser(user => user.InRole("randomRole")) + .WithoutUser() + .ShouldPassForThe(controller => + { + var user = controller.User; + + Assert.Equal(BaseClaimsPrincipalUserBuilder.DefaultAuthenticated, user); + }); + } } } diff --git a/test/MyTested.AspNetCore.Mvc.Caching.Test/BuildersTests/DataTests/DistributedCacheBuilderTests.cs b/test/MyTested.AspNetCore.Mvc.Caching.Test/BuildersTests/DataTests/DistributedCacheBuilderTests.cs index 459db6687..f9ed11304 100644 --- a/test/MyTested.AspNetCore.Mvc.Caching.Test/BuildersTests/DataTests/DistributedCacheBuilderTests.cs +++ b/test/MyTested.AspNetCore.Mvc.Caching.Test/BuildersTests/DataTests/DistributedCacheBuilderTests.cs @@ -2,6 +2,8 @@ { using System; using System.Collections.Generic; + using System.Linq; + using System.Text; using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.DependencyInjection; @@ -268,6 +270,185 @@ public void WithCacheBuilderWithKeyBuilderAndAlsoShouldSetCorrectValues() .Ok(); } + [Fact] + public void WithoutDistributedCacheShouldReturnEmptyCache() + { + MyController + .Instance() + .WithDistributedCache(distributedCache => distributedCache + .WithEntries(new Dictionary + { + ["first"] = "firstValue", + ["second"] = "secondValue", + ["third"] = "thirdValue" + })) + .WithoutDistributedCache() + .Calling(c => c.GetCount(From.Services())) + .ShouldReturn() + .Ok(ok => ok.WithModel(0)); + } + + [Fact] + public void WithoutDistributedCacheShouldReturnEmptyCacheWhenClearingAlreadyEmptyCache() + { + MyController + .Instance() + .WithDistributedCache(distributedCache => distributedCache + .WithEntries(new Dictionary())) + .WithoutDistributedCache() + .Calling(c => c.GetCount(From.Services())) + .ShouldReturn() + .Ok(ok => ok.WithModel(0)); + } + + [Fact] + public void WithoutDistributedEntryCacheShouldReturnCorrectCacheData() + { + MyController + .Instance() + .WithDistributedCache(cache => cache + .WithEntries(new Dictionary + { + ["first"] = "firstValue", + ["second"] = "secondValue", + ["third"] = "thirdValue" + })) + .WithoutDistributedCache(cache => cache.WithoutEntry("second")) + .Calling(c => c.GetAllEntities(From.Services())) + .ShouldReturn() + .Ok(ok => ok + .WithModel(new SortedDictionary + { + ["first"] = GetByteArray("firstValue"), + ["third"] = GetByteArray("thirdValue") + })); + } + + [Fact] + public void WithoutDistributedCacheByKeyShouldReturnCorrectCacheData() + { + MyController + .Instance() + .WithDistributedCache(cache => cache + .WithEntries(new Dictionary + { + ["first"] = "firstValue", + ["second"] = "secondValue", + ["third"] = "thirdValue" + })) + .WithoutDistributedCache("second") + .Calling(c => c.GetAllEntities(From.Services())) + .ShouldReturn() + .Ok(ok => ok + .WithModel(new SortedDictionary + { + ["third"] = GetByteArray("thirdValue"), + ["first"] = GetByteArray("firstValue") + })); + } + + [Fact] + public void WithoutDistributedCacheByKeysShouldReturnCorrectCacheData() + { + var entities = new Dictionary + { + ["first"] = "firstValue", + ["second"] = "secondValue", + ["third"] = "thirdValue" + }; + + var entriesToDelete = entities.Select(x => x.Key).ToList(); + entriesToDelete.RemoveAt(0); + + MyController + .Instance() + .WithDistributedCache(cache => cache.WithEntries(entities)) + .WithoutDistributedCache(entriesToDelete) + .Calling(c => c.GetAllEntities(From.Services())) + .ShouldReturn() + .Ok(ok => ok + .WithModel(new SortedDictionary + { + ["first"] = GetByteArray("firstValue") + })); + } + + [Fact] + public void WithoutDistributedCacheByNonExistingKeysShouldReturnCorrectCacheData() + { + var entities = new Dictionary + { + ["first"] = GetByteArray("firstValue"), + ["second"] = GetByteArray("secondValue"), + ["third"] = GetByteArray("thirdValue") + }; + + var entitiesToDelete = new List { "key1", "key2" }; + MyController + .Instance() + .WithDistributedCache(cache => cache + .WithEntries(entities)) + .WithoutDistributedCache(entitiesToDelete) + .Calling(c => c.GetAllEntities(From.Services())) + .ShouldReturn() + .Ok(ok => ok + .WithModel(new SortedDictionary(entities))); + } + + [Fact] + public void WithoutDistributedCacheByNonExistingKeyShouldReturnCorrectCacheData() + { + var entities = new Dictionary + { + ["first"] = GetByteArray("firstValue"), + ["second"] = GetByteArray("secondValue"), + ["third"] = GetByteArray("thirdValue") + }; + + MyController + .Instance() + .WithDistributedCache(cache => cache + .WithEntries(entities)) + .WithoutDistributedCache("firstValue") + .Calling(c => c.GetAllEntities(From.Services())) + .ShouldReturn() + .Ok(ok => ok + .WithModel(new SortedDictionary(entities))); + } + + [Fact] + public void WithoutDistributedCacheByParamKeysShouldReturnCorrectCacheData() + { + var entities = new Dictionary + { + ["first"] = "firstValue", + ["second"] = "secondValue", + ["third"] = "thirdValue" + }; + + var entriesToDelete = entities.Select(x => x.Key).ToList(); + entriesToDelete.RemoveAt(0); + + var encodedValue = GetByteArray("firstValue"); + + MyController + .Instance() + .WithDistributedCache(cache => cache.WithEntries(entities)) + .WithoutDistributedCache(entriesToDelete[0], entriesToDelete[1]) + .Calling(c => c.GetAllEntities(From.Services())) + .ShouldReturn() + .Ok(ok => ok + .WithModel(new SortedDictionary + { + ["first"] = encodedValue + })); ; + } + public void Dispose() => MyApplication.StartsFrom(); + + private static byte[] GetByteArray(string str) + { + return Encoding.ASCII.GetBytes(str); + } } } diff --git a/test/MyTested.AspNetCore.Mvc.Caching.Test/Setups/Controllers/DistributedCacheController.cs b/test/MyTested.AspNetCore.Mvc.Caching.Test/Setups/Controllers/DistributedCacheController.cs index 719a5c2bb..c925301da 100644 --- a/test/MyTested.AspNetCore.Mvc.Caching.Test/Setups/Controllers/DistributedCacheController.cs +++ b/test/MyTested.AspNetCore.Mvc.Caching.Test/Setups/Controllers/DistributedCacheController.cs @@ -1,5 +1,7 @@ namespace MyTested.AspNetCore.Mvc.Test.Setups.Controllers { + using System; + using System.Collections.Generic; using Internal.Contracts; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Caching.Distributed; @@ -46,6 +48,16 @@ public IActionResult ValidDistributedCacheEntriesAction([FromServices] IDistribu return this.Ok(); } + public IActionResult GetCount([FromServices] IDistributedCache cache) + { + return this.Ok((cache as IDistributedCacheMock).GetCacheAsDictionary().Count); + } + + public IActionResult GetAllEntities([FromServices] IDistributedCache cache) + { + return this.Ok((new SortedDictionary((cache as IDistributedCacheMock).GetCacheAsDictionary()))); + } + private IActionResult InternalServerError() => this.StatusCode(500); } } diff --git a/test/MyTested.AspNetCore.Mvc.ModelState.Test/BuildersTests/ModelsTests/ModelStateBuilderTests.cs b/test/MyTested.AspNetCore.Mvc.ModelState.Test/BuildersTests/ModelsTests/ModelStateBuilderTests.cs index 9b43223b4..332387f6b 100644 --- a/test/MyTested.AspNetCore.Mvc.ModelState.Test/BuildersTests/ModelsTests/ModelStateBuilderTests.cs +++ b/test/MyTested.AspNetCore.Mvc.ModelState.Test/BuildersTests/ModelsTests/ModelStateBuilderTests.cs @@ -1,9 +1,10 @@ namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.ModelsTests { + using System.Collections.Generic; + using System.Linq; using Setups; using Setups.Controllers; using Xunit; - using System.Collections.Generic; public class ModelStateBuilderTests { @@ -58,5 +59,77 @@ public void WithModelStateWithErrorsObjectShouldWorkCorrectly() .ShouldReturn() .BadRequest(); } + + [Fact] + public void WithoutModelStateByProvidingExistingKeyShouldWorkCorrectly() + { + var keyValuePair = new KeyValuePair("Key1", "Value1"); + + MyController + .Instance() + .WithModelState(modelState => modelState + .WithError(keyValuePair.Key, keyValuePair.Value) + .WithError("PseudoRandomKey", "value")) + .WithoutModelState(keyValuePair.Key) + .Calling(c => c.GetModelStateKeys()) + .ShouldReturn() + .Ok(ok => ok + .WithModelOfType>() + .Passing(keys => keys.Count == 1 && + keys.Any(key => !key.Equals(keyValuePair.Key)))); + } + + [Fact] + public void WithoutModelStateByUsingTheBuilderShouldWorkCorrectly() + { + var keyValuePair = new KeyValuePair("Key1", "Value1"); + + MyController + .Instance() + .WithModelState(modelState => modelState + .WithError(keyValuePair.Key, keyValuePair.Value) + .WithError("PseudoRandomKey", "value")) + .WithoutModelState(modelState => modelState.WithoutModelState(keyValuePair.Key)) + .Calling(c => c.GetModelStateKeys()) + .ShouldReturn() + .Ok(ok => ok + .WithModelOfType>() + .Passing(keys => keys.Count == 1 && + keys.Any(key => !key.Equals(keyValuePair.Key)))); + } + + [Fact] + public void WithoutModelStateShouldWorkCorrectly() + { + MyController + .Instance() + .WithModelState(modelState => modelState + .WithError("PseudoRandomKey1", "value1") + .WithError("PseudoRandomKey2", "value2")) + .WithoutModelState() + .Calling(c => c.GetModelStateKeys()) + .ShouldReturn() + .Ok(ok => ok + .WithModelOfType>() + .Passing(keys => keys.Count == 0)); + } + + [Fact] + public void WithoutModelStateByRemovingNonExistingKeyShouldWorkCorrectly() + { + var keyValuePair = new KeyValuePair("Key1", "Value1"); + + MyController + .Instance() + .WithModelState(modelState => modelState + .WithError(keyValuePair.Key, keyValuePair.Value)) + .WithoutModelState("NonExistingKey") + .Calling(c => c.GetModelStateKeys()) + .ShouldReturn() + .Ok(ok => ok + .WithModelOfType>() + .Passing(keys => keys.Count == 1 && + keys.Any(key => key.Equals(keyValuePair.Key)))); + } } } diff --git a/test/MyTested.AspNetCore.Mvc.TempData.Test/BuildersTests/DataTests/TempDataBuilderTests.cs b/test/MyTested.AspNetCore.Mvc.TempData.Test/BuildersTests/DataTests/TempDataBuilderTests.cs index 6fe573031..15ab8e3b6 100644 --- a/test/MyTested.AspNetCore.Mvc.TempData.Test/BuildersTests/DataTests/TempDataBuilderTests.cs +++ b/test/MyTested.AspNetCore.Mvc.TempData.Test/BuildersTests/DataTests/TempDataBuilderTests.cs @@ -50,5 +50,101 @@ public void WithEntriesObjectShouldSetCorrectValues() .Ok(ok => ok .WithModel("Valid")); } + + [Fact] + public void WithoutEntryShouldReturnCorrectValues() + { + MyController + .Instance() + .WithTempData(tempData => tempData + .WithEntries(new Dictionary + { + ["Test"] = "Valid", + ["Second"] = "SecondValue", + })) + .WithoutTempData("Test") + .Calling(c => c.GetTempDataKeys()) + .ShouldReturn() + .Ok(ok => ok + .WithModel(new List + { + "Second" + })); + } + + [Fact] + public void WithoutEntriesShouldReturnCorrectValues() + { + MyController + .Instance() + .WithTempData(tempData => tempData + .WithEntries(new Dictionary + { + ["Test"] = "Valid", + ["Second"] = "SecondValue", + })) + .WithoutTempData(new List() { "Second" }) + .Calling(c => c.GetTempDataKeys()) + .ShouldReturn() + .Ok(ok => ok + .WithModel(new List + { + "Test" + })); + } + + [Fact] + public void WithoutEntriesByProvidingParamsShouldReturnCorrectValues() + { + MyController + .Instance() + .WithTempData(tempData => tempData + .WithEntries(new Dictionary + { + ["Test"] = "Valid", + ["Second"] = "SecondValue", + })) + .WithoutTempData("Second", "Test") + .Calling(c => c.GetTempDataKeys()) + .ShouldReturn() + .Ok(ok => ok + .WithModel(new List())); + } + + [Fact] + public void WithoutEntriesShouldRemoveAllEntities() + { + MyController + .Instance() + .WithTempData(tempData => tempData + .WithEntries(new Dictionary + { + ["Test"] = "Valid", + ["Second"] = "SecondValue", + })) + .WithoutTempData() + .Calling(c => c.GetTempDataKeys()) + .ShouldReturn() + .Ok(ok => ok + .WithModel(new List())); + } + + [Fact] + public void WithoutEntryNonExistingItemShouldReturnCorrectValues() + { + MyController + .Instance() + .WithTempData(tempData => tempData + .WithEntries(new Dictionary + { + ["Test"] = "Valid", + ["Second"] = "SecondValue", + })) + .WithoutTempData("NonExisting") + .Calling(c => c.GetTempDataKeys()) + .ShouldReturn() + .Ok(ok => ok + .WithModel(new List() { "Test" , "Second"})); + } } } diff --git a/test/MyTested.AspNetCore.Mvc.Test.Setups/Controllers/MvcController.cs b/test/MyTested.AspNetCore.Mvc.Test.Setups/Controllers/MvcController.cs index 8a5d0364c..efeee3ae3 100644 --- a/test/MyTested.AspNetCore.Mvc.Test.Setups/Controllers/MvcController.cs +++ b/test/MyTested.AspNetCore.Mvc.Test.Setups/Controllers/MvcController.cs @@ -483,14 +483,14 @@ public IActionResult ForbidWithEmptyAuthenticationProperties() public IActionResult SignInWithAuthenticationPropertiesAndScheme() { - return this.SignIn(ClaimsPrincipalBuilder.DefaultAuthenticated, + return this.SignIn(WithClaimsPrincipalBuilder.DefaultAuthenticated, TestObjectFactory.GetAuthenticationProperties(), AuthenticationScheme.Basic); } public IActionResult SignInWithEmptyAuthenticationPropertiesAndScheme() { - return this.SignIn(ClaimsPrincipalBuilder.DefaultAuthenticated, + return this.SignIn(WithClaimsPrincipalBuilder.DefaultAuthenticated, TestObjectFactory.GetEmptyAuthenticationProperties(), AuthenticationScheme.Basic); } @@ -802,6 +802,11 @@ public IActionResult BadRequestWithCustomError() return this.BadRequest(this.ResponseModel); } + public IActionResult GetModelStateKeys() + { + return this.Ok(this.ModelState.Keys.ToList()); + } + public IActionResult AcceptedAction() { return this.Accepted(); @@ -1191,6 +1196,11 @@ public IActionResult TempDataAction() return this.BadRequest(); } + public IActionResult GetTempDataKeys() + { + return this.Ok(this.TempData.Keys.ToList()); + } + public IActionResult SessionAction() { if (this.HttpContext.Session.GetString("test") != null)