Skip to content

Commit f443b31

Browse files
authored
Merge pull request #345 - Added AuthorizeAttribute test builder and ProducesResponseTypeAttribute test builder
Added AuthorizeAttribute test builder and ProducesResponseTypeAttribute test builder.
2 parents f4aa7cf + e6d2361 commit f443b31

File tree

12 files changed

+2073
-208
lines changed

12 files changed

+2073
-208
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Attributes
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using Contracts.Attributes;
7+
using Internal.TestContexts;
8+
using Microsoft.AspNetCore.Authorization;
9+
using Utilities.Extensions;
10+
11+
/// <summary>
12+
/// Used for testing <see cref="AuthorizeAttribute"/>.
13+
/// </summary>
14+
public class AuthorizeAttributeTestBuilder : BaseAttributeTestBuilder<AuthorizeAttribute>,
15+
IAndAuthorizeAttributeTestBuilder
16+
{
17+
/// <summary>
18+
/// Initializes a new instance of the <see cref="AuthorizeAttributeTestBuilder"/> class.
19+
/// </summary>
20+
/// <param name="testContext"><see cref="ComponentTestContext"/> containing data about the currently executed assertion chain.</param>
21+
/// <param name="failedValidationAction">Action to call in case of failed validation.</param>
22+
public AuthorizeAttributeTestBuilder(
23+
ComponentTestContext testContext,
24+
Action<string, string> failedValidationAction)
25+
: base(testContext, nameof(AuthorizeAttribute), failedValidationAction)
26+
=> this.Attribute = new AuthorizeAttribute();
27+
28+
/// <inheritdoc />
29+
public IAndAuthorizeAttributeTestBuilder WithPolicy(string policy)
30+
{
31+
if (!string.IsNullOrEmpty(policy))
32+
{
33+
this.Attribute = new AuthorizeAttribute(policy)
34+
{
35+
AuthenticationSchemes = this.Attribute.AuthenticationSchemes
36+
};
37+
38+
this.Validations.Add((expected, actual) =>
39+
{
40+
var expectedPolicy = expected.Policy;
41+
var actualPolicy = actual.Policy;
42+
43+
if (!string.Equals(expectedPolicy, actualPolicy, StringComparison.OrdinalIgnoreCase))
44+
{
45+
this.FailedValidationAction(
46+
$"{this.ExceptionMessagePrefix}'{expectedPolicy}' policy",
47+
$"in fact found '{actualPolicy}'");
48+
}
49+
});
50+
}
51+
52+
return this;
53+
}
54+
55+
/// <inheritdoc />
56+
public IAndAuthorizeAttributeTestBuilder WithRole(string role)
57+
{
58+
if (!string.IsNullOrEmpty(role))
59+
{
60+
this.Validations.Add((expected, actual) =>
61+
{
62+
var actualRoles = new List<string>();
63+
64+
if (!string.IsNullOrEmpty(actual.Roles))
65+
{
66+
actualRoles = actual.Roles
67+
.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)
68+
.ToList();
69+
}
70+
71+
if (!actualRoles.Contains(role))
72+
{
73+
this.FailedValidationAction(
74+
$"{this.ExceptionMessagePrefix}'{role}' role",
75+
$"in fact such was not found");
76+
}
77+
});
78+
}
79+
80+
return this;
81+
}
82+
83+
/// <inheritdoc />
84+
public IAndAuthorizeAttributeTestBuilder WithRoles(IEnumerable<string> roles)
85+
{
86+
if (roles != null && roles.Any())
87+
{
88+
roles.ForEach(role => this.WithRole(role));
89+
}
90+
91+
return this;
92+
}
93+
94+
/// <inheritdoc />
95+
public IAndAuthorizeAttributeTestBuilder WithRoles(params string[] roles)
96+
=> this.WithRoles(roles.AsEnumerable());
97+
98+
/// <inheritdoc />
99+
public IAndAuthorizeAttributeTestBuilder WithAuthenticationSchemes(string authenticationSchemes)
100+
{
101+
if (!string.IsNullOrEmpty(authenticationSchemes))
102+
{
103+
this.Attribute.AuthenticationSchemes = authenticationSchemes;
104+
105+
this.Validations.Add((expected, actual) =>
106+
{
107+
var expectedAuthenticationSchemes = expected.AuthenticationSchemes;
108+
var actualAuthenticationSchemes = actual.AuthenticationSchemes;
109+
110+
if (expectedAuthenticationSchemes != actualAuthenticationSchemes)
111+
{
112+
this.FailedValidationAction(
113+
$"{this.ExceptionMessagePrefix}'{expectedAuthenticationSchemes}' authentication schemes",
114+
$"in fact found '{actualAuthenticationSchemes}'");
115+
}
116+
});
117+
}
118+
119+
return this;
120+
}
121+
122+
/// <inheritdoc />
123+
public IAuthorizeAttributeTestBuilder AndAlso() => this;
124+
}
125+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Attributes
2+
{
3+
using System;
4+
using Contracts.Attributes;
5+
using Internal.TestContexts;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Utilities;
8+
9+
/// <summary>
10+
/// Used for testing <see cref="ProducesResponseTypeAttribute"/>.
11+
/// </summary>
12+
public class ProducesResponseTypeAttributeTestBuilder : BaseAttributeTestBuilder<ProducesResponseTypeAttribute>,
13+
IAndProducesResponseTypeAttributeTestBuilder
14+
{
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="ProducesResponseTypeAttributeTestBuilder"/> class.
17+
/// </summary>
18+
/// <param name="testContext"><see cref="ComponentTestContext"/> containing data about the currently executed assertion chain.</param>
19+
/// <param name="failedValidationAction">Action to call in case of failed validation.</param>
20+
public ProducesResponseTypeAttributeTestBuilder(
21+
ComponentTestContext testContext,
22+
Action<string, string> failedValidationAction)
23+
: base(testContext, nameof(ProducesResponseTypeAttribute), failedValidationAction)
24+
=> this.Attribute = new ProducesResponseTypeAttribute(HttpStatusCode.OK);
25+
26+
/// <inheritdoc />
27+
public IAndProducesResponseTypeAttributeTestBuilder OfType(Type type)
28+
{
29+
this.Attribute.Type = type;
30+
this.Validations.Add((expected, actual) =>
31+
{
32+
var expectedType = expected.Type;
33+
var actualType = actual.Type;
34+
35+
if (Reflection.AreDifferentTypes(expectedType, actualType))
36+
{
37+
this.FailedValidationAction(
38+
$"{this.ExceptionMessagePrefix}'{expectedType.ToFriendlyTypeName()}' type",
39+
$"in fact found '{actualType.ToFriendlyTypeName()}'");
40+
}
41+
});
42+
43+
return this;
44+
}
45+
46+
/// <inheritdoc />
47+
public IAndProducesResponseTypeAttributeTestBuilder WithStatusCode(int statusCode)
48+
{
49+
this.Attribute.StatusCode = statusCode;
50+
this.Validations.Add((expected, actual) =>
51+
{
52+
var expectedStatusCode = expected.StatusCode;
53+
var actualStatusCode = actual.StatusCode;
54+
55+
if (expectedStatusCode != actualStatusCode)
56+
{
57+
this.FailedValidationAction(
58+
$"{this.ExceptionMessagePrefix}'{expectedStatusCode}' status code",
59+
$"in fact found '{actualStatusCode}'");
60+
}
61+
});
62+
63+
return this;
64+
}
65+
66+
/// <inheritdoc />
67+
public IProducesResponseTypeAttributeTestBuilder AndAlso() => this;
68+
}
69+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Attributes
2+
{
3+
/// <summary>
4+
/// Used for adding AndAlso() to the <see cref="Microsoft.AspNetCore.Authorization.AuthorizeAttribute" /> tests.
5+
/// </summary>
6+
public interface IAndAuthorizeAttributeTestBuilder : IAuthorizeAttributeTestBuilder
7+
{
8+
/// <summary>
9+
/// AndAlso method for better readability when testing <see cref="Microsoft.AspNetCore.Authorization.AuthorizeAttribute" />
10+
/// </summary>
11+
/// <returns>The same <see cref="IAuthorizeAttributeTestBuilder"/>.</returns>
12+
IAuthorizeAttributeTestBuilder AndAlso();
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Attributes
2+
{
3+
/// <summary>
4+
/// Used for adding AndAlso() method to the <see cref="Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute" /> tests.
5+
/// </summary>
6+
public interface IAndProducesResponseTypeAttributeTestBuilder : IProducesResponseTypeAttributeTestBuilder
7+
{
8+
/// <summary>
9+
/// AndAlso method for better readability when testing <see cref="Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute" />
10+
/// </summary>
11+
/// <returns>The same <see cref="IProducesResponseTypeAttributeTestBuilder"/>.</returns>
12+
IProducesResponseTypeAttributeTestBuilder AndAlso();
13+
}
14+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Attributes
2+
{
3+
using System.Collections.Generic;
4+
5+
/// <summary>
6+
/// Used for testing <see cref="Microsoft.AspNetCore.Authorization.AuthorizeAttribute"/>.
7+
/// </summary>
8+
public interface IAuthorizeAttributeTestBuilder
9+
{
10+
/// <summary>
11+
/// Tests whether a <see cref="Microsoft.AspNetCore.Authorization.AuthorizeAttribute"/>
12+
/// has the same <see cref="Microsoft.AspNetCore.Authorization.AuthorizeAttribute.Policy"/> value as the provided one.
13+
/// </summary>
14+
/// <param name="policy">Expected policy.</param>
15+
/// <returns>The same <see cref="IAndAuthorizeAttributeTestBuilder"/>.</returns>
16+
IAndAuthorizeAttributeTestBuilder WithPolicy(string policy);
17+
18+
/// <summary>
19+
/// Tests whether a <see cref="Microsoft.AspNetCore.Authorization.AuthorizeAttribute"/>
20+
/// contains the provided role in its <see cref="Microsoft.AspNetCore.Authorization.AuthorizeAttribute.Roles"/> values.
21+
/// </summary>
22+
/// <param name="role">Expected role.</param>
23+
/// <returns>The same <see cref="IAndAuthorizeAttributeTestBuilder"/>.</returns>
24+
IAndAuthorizeAttributeTestBuilder WithRole(string role);
25+
26+
/// <summary>
27+
/// Tests whether a <see cref="Microsoft.AspNetCore.Authorization.AuthorizeAttribute"/>
28+
/// contains the provided roles in its <see cref="Microsoft.AspNetCore.Authorization.AuthorizeAttribute.Roles"/> values.
29+
/// </summary>
30+
/// <param name="roles">Expected roles.</param>
31+
/// <returns>The same <see cref="IAndAuthorizeAttributeTestBuilder"/>.</returns>
32+
IAndAuthorizeAttributeTestBuilder WithRoles(IEnumerable<string> roles);
33+
34+
/// <summary>
35+
/// Tests whether a <see cref="Microsoft.AspNetCore.Authorization.AuthorizeAttribute"/>
36+
/// contains the provided roles in its <see cref="Microsoft.AspNetCore.Authorization.AuthorizeAttribute.Roles"/> values.
37+
/// </summary>
38+
/// <param name="roles">Expected roles.</param>
39+
/// <returns>The same <see cref="IAndAuthorizeAttributeTestBuilder"/>.</returns>
40+
IAndAuthorizeAttributeTestBuilder WithRoles(params string[] roles);
41+
42+
/// <summary>
43+
/// Tests whether a <see cref="Microsoft.AspNetCore.Authorization.AuthorizeAttribute"/>
44+
/// has the same <see cref="Microsoft.AspNetCore.Authorization.AuthorizeAttribute.AuthenticationSchemes"/> value as the provided one.
45+
/// </summary>
46+
/// <param name="authenticationSchemes">Expected authentication schemes.</param>
47+
/// <returns>The same <see cref="IAndAuthorizeAttributeTestBuilder"/>.</returns>
48+
IAndAuthorizeAttributeTestBuilder WithAuthenticationSchemes(string authenticationSchemes);
49+
}
50+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Attributes
2+
{
3+
using System;
4+
5+
/// <summary>
6+
/// Used for testing <see cref="Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute"/>.
7+
/// </summary>
8+
public interface IProducesResponseTypeAttributeTestBuilder
9+
{
10+
/// <summary>
11+
/// Tests whether a <see cref="Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute"/>
12+
/// has the same <see cref="Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute.Type"/> value as the provided one.
13+
/// </summary>
14+
/// <param name="type">Expected type.</param>
15+
/// <returns>The same <see cref="IAndProducesResponseTypeAttributeTestBuilder"/>.</returns>
16+
IAndProducesResponseTypeAttributeTestBuilder OfType(Type type);
17+
18+
/// <summary>
19+
/// Tests whether a <see cref="Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute"/>
20+
/// has the same <see cref="Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute.StatusCode"/> value as the provided one.
21+
/// </summary>
22+
/// <param name="statusCode">Expected status code.</param>
23+
/// <returns>The same <see cref="IAndProducesResponseTypeAttributeTestBuilder"/>.</returns>
24+
IAndProducesResponseTypeAttributeTestBuilder WithStatusCode(int statusCode);
25+
}
26+
}

0 commit comments

Comments
 (0)