Skip to content

Commit 846daf2

Browse files
committed
Merge branch 'development' of https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc into tests/mvc-universe
2 parents c17f96b + 2d7d6bb commit 846daf2

File tree

15 files changed

+429
-14
lines changed

15 files changed

+429
-14
lines changed

BACKERS.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ Funds donated via both platforms are used for development and marketing purposes
3232

3333
## Generous Backers
3434
- Plamen Petkov
35+
- [Svetlin Nakov](https://nakov.com/)
3536

3637
## Kickstarters
3738
- Ines Ivanova from [C# In 15 Minutes](https://www.youtube.com/channel/UCljus-YO63ae0vwuuPWDasA)
38-
- Rusko Kumovski
3939

4040
## Top Backers
4141
- Bace Kar pod prekor Silovio aka Brudlordo
@@ -51,6 +51,7 @@ Funds donated via both platforms are used for development and marketing purposes
5151
- Viktor Klisurski
5252
- Desi Zlatanova
5353
- Tsenko Tsenov
54+
- [Ivan Ivanov](https://github.com/csyntax)
5455

5556
## Generous One-Time Donations
5657
- My-Amazing-Store
@@ -64,13 +65,15 @@ Funds donated via both platforms are used for development and marketing purposes
6465
- Pavel Doychinov
6566

6667
## Backers
67-
Aleksandur Gyuzelov, Bogomil Stoev, Desislav Stoyanov, Emil Venkov, Georgi Krasimirov Georgiev, Kristiyan Mihailov, Lyubomir Krastanov, Nikolay Boyadzhiev, Nikolay Mihaylov, Rositsa Nenova, Slavi Bozhikov, Vasil Bonev, Yuriy Georgiev, Калин Ценков, Velina Getova, Gabriel Daskalov
68+
Aleksandur Gyuzelov, Bogomil Stoev, Desislav Stoyanov, Emil Venkov, Georgi Krasimirov Georgiev, Kristiyan Mihailov, Lyubomir Krastanov, Nikolay Boyadzhiev, Nikolay Mihaylov, Rositsa Nenova, Slavi Bozhikov, Vasil Bonev, Yuriy Georgiev, Калин Ценков, Velina Getova
6869

6970
## One-Time Donations
70-
Philip Shishov, Mariya Georgieva, Tanya Georgieva, Danny Berova, German Dimitrov, Aleksandar Evangelatov, Anton Petrov, Hristo Ivanov, Anna Stambolieva, Nedelcho Penev, Yulian Ashikov, Boyana Aleksova, Plamen Haralambiev, Victoria Karamanova, Ivaylo Goranov, Aneliya Drazheva, Zdravko Yakimov, Vanya Kuncheva, Georgi Petrov, Stanimir Pavlov, Teodor Stefanov, Stefan Minchev, Simon Valentinova Kochova, Marin Marinov, Petar Peshev, Radoslav Astardzhiev, Dimitar Radkov, Aleksandar Tsvetkov, Georgi Dragnev, Veselin Neychev, Kaloyan Kolev, Andrey Blagoev, Yordan Penev, Ventsislav Yordanov, Nikolay Georgiev, Siyana Yasenova Zdravkova, Ani Kalpachka, Gergana Damyanova, Petya Koleva, Dyanko Petkov, Nikola Kolchakov, Borislava Hranova
71+
Yuliyana Tahova, Philip Shishov, Mariya Georgieva, Tanya Georgieva, Danny Berova, German Dimitrov, Aleksandar Evangelatov, Anton Petrov, Hristo Ivanov, Anna Stambolieva, Nedelcho Penev, Yulian Ashikov, Boyana Aleksova, Plamen Haralambiev, Victoria Karamanova, Ivaylo Goranov, Aneliya Drazheva, Zdravko Yakimov, Vanya Kuncheva, Georgi Petrov, Stanimir Pavlov, Teodor Stefanov, Stefan Minchev, Simon Valentinova Kochova, Marin Marinov, Petar Peshev, Radoslav Astardzhiev, Dimitar Radkov, Aleksandar Tsvetkov, Georgi Dragnev, Veselin Neychev, Kaloyan Kolev, Andrey Blagoev, Yordan Penev, Ventsislav Yordanov, Nikolay Georgiev, Siyana Yasenova Zdravkova, Ani Kalpachka, Gergana Damyanova, Petya Koleva, Dyanko Petkov, Nikola Kolchakov, Borislava Hranova
7172

7273
## Previous Supporters
74+
- Gabriel Daskalov
75+
- Rusko Kumovski
7376
- Kris Petrov
7477
- Mihail Duchev
7578
- Nikola Stankov
76-
- Peter
79+
- Peter
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Attributes
2+
{
3+
using System;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Contracts.Attributes;
6+
using Internal.TestContexts;
7+
using Utilities;
8+
9+
/// <summary>
10+
/// Used for testing <see cref="MiddlewareFilterAttribute"/>.
11+
/// </summary>
12+
public class MiddlewareFilterAttributeTestBuilder : BaseAttributeTestBuilderWithOrder<MiddlewareFilterAttribute>,
13+
IAndMiddlewareFilterAttributeTestBuilder
14+
{
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="MiddlewareFilterAttributeTestBuilder"/> 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 MiddlewareFilterAttributeTestBuilder(
21+
ComponentTestContext testContext,
22+
Action<string, string> failedValidationAction)
23+
: base(testContext, nameof(MiddlewareFilterAttribute), failedValidationAction)
24+
=> this.Attribute = new MiddlewareFilterAttribute(typeof(object));
25+
26+
/// <inheritdoc />
27+
public IAndMiddlewareFilterAttributeTestBuilder OfType(Type configurationType)
28+
{
29+
this.Attribute = new MiddlewareFilterAttribute(configurationType);
30+
this.Validations.Add((expected, actual) =>
31+
{
32+
var expectedType = expected.ConfigurationType;
33+
var actualType = actual.ConfigurationType;
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 IAndMiddlewareFilterAttributeTestBuilder WithOrder(int order)
48+
{
49+
this.ValidateOrder(order);
50+
return this;
51+
}
52+
53+
/// <inheritdoc />
54+
public IMiddlewareFilterAttributeTestBuilder AndAlso() => this;
55+
}
56+
}

src/MyTested.AspNetCore.Mvc.Controllers.Attributes/Builders/Attributes/ProducesAttributeTestBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public IAndProducesAttributeTestBuilder WithContentTypes(string contentType, par
7070
=> this.WithContentTypes(new List<string>(otherContentTypes) { contentType });
7171

7272
/// <inheritdoc />
73-
public IAndProducesAttributeTestBuilder WithType(Type type)
73+
public IAndProducesAttributeTestBuilder OfType(Type type)
7474
{
7575
this.Attribute.Type = type;
7676
this.Validations.Add((expected, actual) =>
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.MiddlewareFilterAttribute"/> tests.
5+
/// </summary>
6+
public interface IAndMiddlewareFilterAttributeTestBuilder : IMiddlewareFilterAttributeTestBuilder
7+
{
8+
/// <summary>
9+
/// AndAlso method for better readability when testing <see cref="Microsoft.AspNetCore.Mvc.MiddlewareFilterAttribute"/>.
10+
/// </summary>
11+
/// <returns>The same <see cref="IMiddlewareFilterAttributeTestBuilder"/>.</returns>
12+
IMiddlewareFilterAttributeTestBuilder AndAlso();
13+
}
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Attributes
2+
{
3+
using System;
4+
5+
/// <summary>
6+
/// Used for testing <see cref="Microsoft.AspNetCore.Mvc.MiddlewareFilterAttribute"/>.
7+
/// </summary>
8+
public interface IMiddlewareFilterAttributeTestBuilder : IBaseAttributeTestBuilderWithOrder<IAndMiddlewareFilterAttributeTestBuilder>
9+
{
10+
/// <summary>
11+
/// Tests whether a <see cref="Microsoft.AspNetCore.Mvc.MiddlewareFilterAttribute"/>
12+
/// has the same <see cref="Microsoft.AspNetCore.Mvc.MiddlewareFilterAttribute.ConfigurationType"/> value as the provided one.
13+
/// </summary>
14+
/// <param name="configurationType">A type which configures a middleware pipeline.</param>
15+
/// <returns>The same <see cref="IMiddlewareFilterAttributeTestBuilder"/>.</returns>
16+
IAndMiddlewareFilterAttributeTestBuilder OfType(Type configurationType);
17+
}
18+
}

src/MyTested.AspNetCore.Mvc.Controllers.Attributes/Builders/Contracts/Attributes/IProducesAttributeTestBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ public interface IProducesAttributeTestBuilder : IBaseAttributeTestBuilderWithOr
3939
/// </summary>
4040
/// <param name="type">Expected type value.</param>
4141
/// <returns>The same <see cref="IAndProducesAttributeTestBuilder"/>.</returns>
42-
IAndProducesAttributeTestBuilder WithType(Type type);
42+
IAndProducesAttributeTestBuilder OfType(Type type);
4343
}
4444
}

src/MyTested.AspNetCore.Mvc.Controllers.Attributes/ControllerActionAttributesTestBuilderExtensions.cs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public static TAttributesTestBuilder SpecifyingProduction<TAttributesTestBuilder
272272
Type withType)
273273
where TAttributesTestBuilder : IControllerActionAttributesTestBuilder<TAttributesTestBuilder>
274274
=> controllerActionAttributesTestBuilder
275-
.SpecifyingProduction(production => production.WithType(withType));
275+
.SpecifyingProduction(production => production.OfType(withType));
276276

277277
/// <summary>
278278
/// Tests whether the collected attributes contain <see cref="ProducesAttribute"/>.
@@ -290,7 +290,7 @@ public static TAttributesTestBuilder SpecifyingProduction<TAttributesTestBuilder
290290
where TAttributesTestBuilder : IControllerActionAttributesTestBuilder<TAttributesTestBuilder>
291291
=> controllerActionAttributesTestBuilder
292292
.SpecifyingProduction(production => production
293-
.WithType(withType)
293+
.OfType(withType)
294294
.WithContentTypes(withContentTypes));
295295

296296
/// <summary>
@@ -345,6 +345,56 @@ public static TAttributesTestBuilder SpecifyingProduction<TAttributesTestBuilder
345345
return actualBuilder.AttributesTestBuilder;
346346
}
347347

348+
/// <summary>
349+
/// Tests whether the collected attributes contain <see cref="MiddlewareFilterAttribute"/>.
350+
/// </summary>
351+
/// <param name="controllerActionAttributesTestBuilder">
352+
/// Instance of <see cref="IControllerActionAttributesTestBuilder{TAttributesTestBuilder}"/> type.
353+
/// </param>
354+
/// <param name="configurationType">A type which configures a middleware pipeline.</param>
355+
/// <returns>The same attributes test builder.</returns>
356+
public static TAttributesTestBuilder SpecifyingMiddleware<TAttributesTestBuilder>(
357+
this IControllerActionAttributesTestBuilder<TAttributesTestBuilder> controllerActionAttributesTestBuilder,
358+
Type configurationType)
359+
where TAttributesTestBuilder : IControllerActionAttributesTestBuilder<TAttributesTestBuilder>
360+
=> controllerActionAttributesTestBuilder
361+
.SpecifyingMiddleware(middleware => middleware.OfType(configurationType));
362+
363+
/// <summary>
364+
/// Tests whether the collected attributes contain <see cref="MiddlewareFilterAttribute"/>.
365+
/// </summary>
366+
/// <param name="controllerActionAttributesTestBuilder">
367+
/// Instance of <see cref="IControllerActionAttributesTestBuilder{TAttributesTestBuilder}"/> type.
368+
/// </param>
369+
/// <param name="middlewareFilterAttributeBuilder">Expected <see cref="MiddlewareFilterAttribute"/> builder.</param>
370+
/// <returns>The same attributes test builder.</returns>
371+
public static TAttributesTestBuilder SpecifyingMiddleware<TAttributesTestBuilder>(
372+
this IControllerActionAttributesTestBuilder<TAttributesTestBuilder> controllerActionAttributesTestBuilder,
373+
Action<IMiddlewareFilterAttributeTestBuilder> middlewareFilterAttributeBuilder)
374+
where TAttributesTestBuilder : IControllerActionAttributesTestBuilder<TAttributesTestBuilder>
375+
{
376+
var actualBuilder = (BaseAttributesTestBuilder<TAttributesTestBuilder>)controllerActionAttributesTestBuilder;
377+
378+
actualBuilder.ContainingAttributeOfType<MiddlewareFilterAttribute>();
379+
380+
actualBuilder.Validations.Add(attrs =>
381+
{
382+
var newMiddlewareFilterAttributeBuilder = new MiddlewareFilterAttributeTestBuilder(
383+
actualBuilder.TestContext,
384+
actualBuilder.ThrowNewAttributeAssertionException);
385+
386+
middlewareFilterAttributeBuilder(newMiddlewareFilterAttributeBuilder);
387+
388+
var expectedAttribute = newMiddlewareFilterAttributeBuilder.GetAttribute();
389+
var actualAttribute = actualBuilder.GetAttributeOfType<MiddlewareFilterAttribute>(attrs);
390+
391+
var validations = newMiddlewareFilterAttributeBuilder.GetAttributeValidations();
392+
validations.ForEach(v => v(expectedAttribute, actualAttribute));
393+
});
394+
395+
return actualBuilder.AttributesTestBuilder;
396+
}
397+
348398
/// <summary>
349399
/// Tests whether the collected attributes contain <see cref="RequireHttpsAttribute"/>.
350400
/// </summary>

test/MyTested.AspNetCore.Mvc.Abstractions.Test/UtilitiesTests/ExpressionParserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public void GetMethodAttributesShouldReturnProperAttributes()
153153
};
154154

155155
Assert.NotNull(attributes);
156-
Assert.Equal(8, attributes.Count);
156+
Assert.Equal(9, attributes.Count);
157157

158158
var allAttributesArePresent = expectedTypes.All(attributes.Contains);
159159
Assert.True(allAttributesArePresent);

test/MyTested.AspNetCore.Mvc.Controllers.Attributes.Test/BuildersTests/AttributesTests/ActionAttributesTestBuilderTests.cs

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using Exceptions;
55
using Microsoft.AspNetCore.Mvc;
6+
using Setups.Pipelines;
67
using Setups;
78
using Setups.Controllers;
89
using Setups.Models;
@@ -499,7 +500,7 @@ public void SpecifyingProductionShouldNotThrowExceptionWithCorrectAttributeTypeA
499500
.ShouldHave()
500501
.ActionAttributes(attributes => attributes
501502
.SpecifyingProduction(production => production
502-
.WithType(typeof(RequestModel))
503+
.OfType(typeof(RequestModel))
503504
.WithContentTypes("application/javascript", "application/pdf")
504505
.WithOrder(2)));
505506
}
@@ -525,7 +526,7 @@ public void SpecifyingProductionShouldNotThrowExceptionWithCorrectAttributeTypeA
525526
.ShouldHave()
526527
.ActionAttributes(attributes => attributes
527528
.SpecifyingProduction(production => production
528-
.WithType(typeof(RequestModel))
529+
.OfType(typeof(RequestModel))
529530
.AndAlso()
530531
.WithContentTypes(new List<string> { "application/pdf", "application/javascript" })
531532
.AndAlso()
@@ -549,6 +550,138 @@ public void SpecifyingProductionShouldThrowExceptionWithCorrectAttributeAndWrong
549550
"When calling Post action in ApiController expected action to have ProducesAttribute with order of 1, but in fact found 2.");
550551
}
551552

553+
[Fact]
554+
public void SpecifyingMiddlewareShouldNotThrowExceptionWithCorrectAttribute()
555+
{
556+
MyController<MvcController>
557+
.Instance()
558+
.Calling(c => c.VariousAttributesAction())
559+
.ShouldHave()
560+
.ActionAttributes(attributes => attributes
561+
.SpecifyingMiddleware(typeof(MyPipeline)));
562+
}
563+
564+
[Fact]
565+
public void SpecifyingMiddlewareShouldThrowExceptionWithMissingAttribute()
566+
{
567+
Test.AssertException<AttributeAssertionException>(
568+
() =>
569+
{
570+
MyController<MvcController>
571+
.Instance()
572+
.Calling(c => c.NormalActionWithAttributes())
573+
.ShouldHave()
574+
.ActionAttributes(attributes => attributes
575+
.SpecifyingMiddleware(typeof(MyPipeline)));
576+
},
577+
"When calling NormalActionWithAttributes action in MvcController expected action to have MiddlewareFilterAttribute, but in fact such was not found.");
578+
}
579+
580+
[Fact]
581+
public void SpecifyingMiddlewareShouldThrowExceptionWithCorrectAttributeAndWrongConfigurationType()
582+
{
583+
Test.AssertException<AttributeAssertionException>(
584+
() =>
585+
{
586+
MyController<MvcController>
587+
.Instance()
588+
.Calling(c => c.VariousAttributesAction())
589+
.ShouldHave()
590+
.ActionAttributes(attributes => attributes
591+
.SpecifyingMiddleware(typeof(MyOtherPipeline)));
592+
},
593+
"When calling VariousAttributesAction action in MvcController expected action to have MiddlewareFilterAttribute with 'MyOtherPipeline' type, but in fact found 'MyPipeline'.");
594+
}
595+
596+
[Fact]
597+
public void SpecifyingMiddlewareShouldNotThrowExceptionWithCorrectAttributeConfigurationType()
598+
{
599+
MyController<MvcController>
600+
.Instance()
601+
.Calling(c => c.VariousAttributesAction())
602+
.ShouldHave()
603+
.ActionAttributes(attributes => attributes
604+
.SpecifyingMiddleware(middleware => middleware
605+
.OfType(typeof(MyPipeline))));
606+
}
607+
608+
[Fact]
609+
public void SpecifyingMiddlewareShouldThrowExceptionWithWrongConfigurationType()
610+
{
611+
Test.AssertException<AttributeAssertionException>(
612+
() =>
613+
{
614+
MyController<MvcController>
615+
.Instance()
616+
.Calling(c => c.VariousAttributesAction())
617+
.ShouldHave()
618+
.ActionAttributes(attributes => attributes
619+
.SpecifyingMiddleware(middleware => middleware
620+
.OfType(typeof(MyOtherPipeline))));
621+
},
622+
"When calling VariousAttributesAction action in MvcController expected action to have MiddlewareFilterAttribute with 'MyOtherPipeline' type, but in fact found 'MyPipeline'.");
623+
}
624+
625+
[Fact]
626+
public void SpecifyingMiddlewareShouldThrowExceptionWithCorrectAttributeAndWrongOrder()
627+
{
628+
Test.AssertException<AttributeAssertionException>(
629+
() =>
630+
{
631+
MyController<MvcController>
632+
.Instance()
633+
.Calling(c => c.VariousAttributesAction())
634+
.ShouldHave()
635+
.ActionAttributes(attributes => attributes
636+
.SpecifyingMiddleware(middleware => middleware.WithOrder(1)));
637+
},
638+
"When calling VariousAttributesAction action in MvcController expected action to have MiddlewareFilterAttribute with order of 1, but in fact found 2.");
639+
}
640+
641+
[Fact]
642+
public void SpecifyingMiddlewareShouldNotThrowExceptionWithCorrectAttributeTypeAndUsingBuilderForOrder()
643+
{
644+
MyController<MvcController>
645+
.Instance()
646+
.Calling(c => c.VariousAttributesAction())
647+
.ShouldHave()
648+
.ActionAttributes(attributes => attributes
649+
.SpecifyingMiddleware(middleware => middleware.WithOrder(2)));
650+
}
651+
652+
[Fact]
653+
public void SpecifyingMiddlewareShouldNotThrowExceptionWithCorrectAttributeConfigTypeAndUsingBuilderForOrder()
654+
{
655+
MyController<MvcController>
656+
.Instance()
657+
.Calling(c => c.VariousAttributesAction())
658+
.ShouldHave()
659+
.ActionAttributes(attributes => attributes
660+
.SpecifyingMiddleware(middleware => middleware
661+
.OfType(typeof(MyPipeline))
662+
.AndAlso()
663+
.WithOrder(2)));
664+
}
665+
666+
[Fact]
667+
public void SpecifyingMiddlewareShouldThrowExceptionWithCorrectAttributeConfigTypeAndUsingBuilderForOrderWithWrongOrder()
668+
{
669+
Test.AssertException<AttributeAssertionException>(
670+
() =>
671+
{
672+
MyController<MvcController>
673+
.Instance()
674+
.Calling(c => c.VariousAttributesAction())
675+
.ShouldHave()
676+
.ActionAttributes(attributes => attributes
677+
.SpecifyingMiddleware(middleware => middleware
678+
.OfType(typeof(MyPipeline))
679+
.AndAlso()
680+
.WithOrder(1)));
681+
},
682+
"When calling VariousAttributesAction action in MvcController expected action to have MiddlewareFilterAttribute with order of 1, but in fact found 2.");
683+
}
684+
552685
[Fact]
553686
public void RequiringHttpsShouldNotThrowExceptionWithTheAttribute()
554687
{

0 commit comments

Comments
 (0)