Skip to content

Commit 4252131

Browse files
committed
Merge branch 'development' of https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc into tests/controllers-views-actionresults
2 parents 6ade11b + 3747d54 commit 4252131

File tree

24 files changed

+2089
-38
lines changed

24 files changed

+2089
-38
lines changed

src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/Reflection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ private static bool AreDeeplyEqual(object expected, object actual, ConditionalWe
446446
return false;
447447
}
448448

449-
if (expectedType.GetTypeInfo().IsPrimitive && actualType.GetTypeInfo().IsPrimitive)
449+
if (expectedType.GetTypeInfo().IsPrimitive || expectedType.GetTypeInfo().IsEnum)
450450
{
451451
return expected.ToString() == actual.ToString();
452452
}

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

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Authorization;
77
using Microsoft.AspNetCore.Mvc;
88
using Microsoft.Extensions.DependencyInjection;
9+
using Setups.Common;
910
using Setups.Controllers;
1011
using Setups.Models;
1112
using Setups.Services;
@@ -449,6 +450,24 @@ public void AreDeeplyEqualShouldWorkCorrectlyWithPrimitiveAndStructTypes()
449450
Assert.False(Reflection.AreDeeplyEqual(new DateTime(2015, 10, 19), new DateTime(2015, 10, 20)));
450451
}
451452

453+
[Fact]
454+
public void AreDeeplyEqualShouldWorkCorrectlyWithEnumerations()
455+
{
456+
// Enum with default values.
457+
Assert.True(Reflection.AreDeeplyEqual(DateTimeKind.Unspecified, DateTimeKind.Unspecified));
458+
Assert.False(Reflection.AreDeeplyEqual(DateTimeKind.Local, DateTimeKind.Utc));
459+
460+
//Enum with overridden values.
461+
Assert.True(Reflection.AreDeeplyEqual(AttributeTargets.Delegate, AttributeTargets.Delegate));
462+
Assert.False(Reflection.AreDeeplyEqual(AttributeTargets.Assembly, AttributeTargets.All));
463+
Assert.False(Reflection.AreDeeplyEqual(AttributeTargets.Assembly, AttributeTargets.Module));
464+
465+
//Enum with default and overriden values.
466+
Assert.True(Reflection.AreDeeplyEqual(CustomEnum.DefaultConstant, CustomEnum.DefaultConstant));
467+
Assert.False(Reflection.AreDeeplyEqual(CustomEnum.DefaultConstant, CustomEnum.ConstantWithCustomValue));
468+
Assert.False(Reflection.AreDeeplyEqual(CustomEnum.DefaultConstant, CustomEnum.CombinedConstant));
469+
}
470+
452471
[Fact]
453472
public void AreDeeplyEqualsShouldWorkCorrectlyWithNormalObjects()
454473
{
@@ -463,6 +482,10 @@ public void AreDeeplyEqualsShouldWorkCorrectlyWithNormalObjects()
463482
Assert.True(Reflection.AreDeeplyEqual(new EqualsModel { Integer = 1, String = "test" }, new EqualsModel { Integer = 1, String = "another" }));
464483
Assert.True(Reflection.AreDeeplyEqual(new EqualityOperatorModel { Integer = 1, String = "test" }, new EqualityOperatorModel { Integer = 1, String = "another" }));
465484
Assert.False(Reflection.AreDeeplyEqual(new object(), "test"));
485+
Assert.False(Reflection.AreDeeplyEqual(new object(), AttributeTargets.All));
486+
Assert.False(Reflection.AreDeeplyEqual(AttributeTargets.All, new object()));
487+
Assert.True(Reflection.AreDeeplyEqual(AttributeTargets.All, (object)AttributeTargets.All));
488+
Assert.True(Reflection.AreDeeplyEqual((object)AttributeTargets.All, AttributeTargets.All));
466489
Assert.False(Reflection.AreDeeplyEqual(DateTime.Now, "test"));
467490
Assert.False(Reflection.AreDeeplyEqual("test", DateTime.Now));
468491
Assert.False(Reflection.AreDeeplyEqual(true, new object()));
@@ -488,12 +511,14 @@ public void AreDeeplyEqualsShouldWorkCorrectlyWithNestedObjects()
488511
{
489512
Integer = 1,
490513
String = "test1",
514+
Enum = CustomEnum.ConstantWithCustomValue,
491515
Nested = new NestedModel { Integer = 2, String = "test2", Nested = new NestedModel { Integer = 3, String = "test3" } }
492516
},
493517
new NestedModel
494518
{
495519
Integer = 1,
496520
String = "test1",
521+
Enum = CustomEnum.ConstantWithCustomValue,
497522
Nested = new NestedModel { Integer = 2, String = "test2", Nested = new NestedModel { Integer = 3, String = "test3" } }
498523
}));
499524

@@ -502,12 +527,14 @@ public void AreDeeplyEqualsShouldWorkCorrectlyWithNestedObjects()
502527
{
503528
Integer = 1,
504529
String = "test",
530+
Enum = CustomEnum.ConstantWithCustomValue,
505531
Nested = new NestedModel { Integer = 2, String = "test2", Nested = new NestedModel { Integer = 3, String = "test3" } }
506532
},
507533
new NestedModel
508534
{
509535
Integer = 1,
510536
String = "test",
537+
Enum = CustomEnum.ConstantWithCustomValue,
511538
Nested = new NestedModel { Integer = 2, String = "test1", Nested = new NestedModel { Integer = 3, String = "test3" } }
512539
}));
513540

@@ -516,12 +543,14 @@ public void AreDeeplyEqualsShouldWorkCorrectlyWithNestedObjects()
516543
{
517544
Integer = 1,
518545
String = "test1",
546+
Enum = CustomEnum.ConstantWithCustomValue,
519547
Nested = new NestedModel { Integer = 2, String = "test2", Nested = new NestedModel { Integer = 3, String = "test2" } }
520548
},
521549
new NestedModel
522550
{
523551
Integer = 1,
524552
String = "test1",
553+
Enum = CustomEnum.ConstantWithCustomValue,
525554
Nested = new NestedModel { Integer = 2, String = "test2", Nested = new NestedModel { Integer = 3, String = "test3" } }
526555
}));
527556
}
@@ -535,27 +564,27 @@ public void AreDeeplyEqualShouldWorkCorrectlyWithCollections()
535564
new NestedModel
536565
{
537566
Integer = 1, String = "test1",
538-
Nested = new NestedModel { Integer = 2, String = "test2", Nested = new NestedModel { Integer = 3, String = "test3" } }
567+
Nested = new NestedModel { Integer = 2, String = "test2", Enum = CustomEnum.CombinedConstant, Nested = new NestedModel { Integer = 3, String = "test3" } }
539568
},
540569
new NestedModel
541570
{
542571
Integer = 1,
543572
String = "test1",
544-
Nested = new NestedModel { Integer = 2, String = "test2", Nested = new NestedModel { Integer = 3, String = "test3" } }
573+
Nested = new NestedModel { Integer = 2, String = "test2", Enum = CustomEnum.CombinedConstant, Nested = new NestedModel { Integer = 3, String = "test3" } }
545574
}
546575
},
547576
new List<NestedModel>
548577
{
549578
new NestedModel
550579
{
551580
Integer = 1, String = "test1",
552-
Nested = new NestedModel { Integer = 2, String = "test2", Nested = new NestedModel { Integer = 3, String = "test3" } }
581+
Nested = new NestedModel { Integer = 2, String = "test2", Enum = CustomEnum.CombinedConstant, Nested = new NestedModel { Integer = 3, String = "test3" } }
553582
},
554583
new NestedModel
555584
{
556585
Integer = 1,
557586
String = "test1",
558-
Nested = new NestedModel { Integer = 2, String = "test2", Nested = new NestedModel { Integer = 3, String = "test3" } }
587+
Nested = new NestedModel { Integer = 2, String = "test2", Enum = CustomEnum.CombinedConstant, Nested = new NestedModel { Integer = 3, String = "test3" } }
559588
}
560589
}));
561590

@@ -565,23 +594,27 @@ public void AreDeeplyEqualShouldWorkCorrectlyWithCollections()
565594
{
566595
Integer = 1,
567596
String = "test1",
597+
Enum = CustomEnum.ConstantWithCustomValue,
568598
Nested =
569599
new NestedModel
570600
{
571601
Integer = 2,
572602
String = "test2",
603+
Enum = CustomEnum.ConstantWithCustomValue,
573604
Nested = new NestedModel { Integer = 3, String = "test3" }
574605
}
575606
},
576607
new NestedModel
577608
{
578609
Integer = 1,
579610
String = "test1",
611+
Enum = CustomEnum.ConstantWithCustomValue,
580612
Nested =
581613
new NestedModel
582614
{
583615
Integer = 2,
584616
String = "test2",
617+
Enum = CustomEnum.ConstantWithCustomValue,
585618
Nested = new NestedModel { Integer = 3, String = "test3" }
586619
}
587620
}
@@ -593,23 +626,27 @@ public void AreDeeplyEqualShouldWorkCorrectlyWithCollections()
593626
{
594627
Integer = 1,
595628
String = "test1",
629+
Enum = CustomEnum.ConstantWithCustomValue,
596630
Nested =
597631
new NestedModel
598632
{
599633
Integer = 2,
600634
String = "test2",
635+
Enum = CustomEnum.ConstantWithCustomValue,
601636
Nested = new NestedModel { Integer = 3, String = "test3" }
602637
}
603638
},
604639
new NestedModel
605640
{
606641
Integer = 1,
607642
String = "test1",
643+
Enum = CustomEnum.ConstantWithCustomValue,
608644
Nested =
609645
new NestedModel
610646
{
611647
Integer = 2,
612648
String = "test2",
649+
Enum = CustomEnum.ConstantWithCustomValue,
613650
Nested = new NestedModel { Integer = 3, String = "test3" }
614651
}
615652
}
@@ -894,27 +931,27 @@ public void AreDeeplyEqualShouldWorkCorrectlyWithDictionaries()
894931

895932
var firstDictionaryWithObject = new Dictionary<string, NestedModel>
896933
{
897-
{ "Key", new NestedModel { Integer = 1, String = "Text" } },
934+
{ "Key", new NestedModel { Integer = 1, String = "Text", Enum = CustomEnum.ConstantWithCustomValue} },
898935
{ "AnotherKey", new NestedModel { Integer = 2, String = "AnotherText" } }
899936
};
900937

901938
var secondDictionaryWithObject = new Dictionary<string, NestedModel>
902939
{
903-
{ "Key", new NestedModel { Integer = 1, String = "Text" } },
940+
{ "Key", new NestedModel { Integer = 1, String = "Text", Enum = CustomEnum.ConstantWithCustomValue } },
904941
{ "AnotherKey", new NestedModel { Integer = 2, String = "AnotherText" } }
905942
};
906943

907944
Assert.True(Reflection.AreDeeplyEqual(firstDictionaryWithObject, secondDictionaryWithObject));
908945

909946
firstDictionaryWithObject = new Dictionary<string, NestedModel>
910947
{
911-
{ "Key", new NestedModel { Integer = 1, String = "Text" } },
948+
{ "Key", new NestedModel { Integer = 1, String = "Text", Enum = CustomEnum.ConstantWithCustomValue } },
912949
{ "AnotherKey", new NestedModel { Integer = 2, String = "Text" } }
913950
};
914951

915952
secondDictionaryWithObject = new Dictionary<string, NestedModel>
916953
{
917-
{ "Key", new NestedModel { Integer = 1, String = "Text" } },
954+
{ "Key", new NestedModel { Integer = 1, String = "Text", } },
918955
{ "AnotherKey", new NestedModel { Integer = 2, String = "AnotherText" } }
919956
};
920957

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.HttpRequestTests
2+
{
3+
using Microsoft.AspNetCore.Http;
4+
using MyTested.AspNetCore.Mvc.Test.Setups.Controllers;
5+
using System.Collections.Generic;
6+
using Xunit;
7+
8+
public class HttpRequestBuilderTests
9+
{
10+
[Fact]
11+
public void WithAuthenticatedUserShouldPopulateUserNameAndRolesFromListProperly()
12+
{
13+
MyController<MvcController>
14+
.Instance()
15+
.WithHttpRequest(request => request
16+
.WithUser("TestUserName", new List<string>
17+
{
18+
"Administrator",
19+
"Moderator"
20+
}))
21+
.ShouldPassForThe<HttpRequest>(builtRequest =>
22+
{
23+
Assert.Equal(0, builtRequest.Query.Count);
24+
Assert.False(builtRequest.HasFormContentType);
25+
Assert.Equal(0, builtRequest.Cookies.Count);
26+
});
27+
}
28+
29+
[Fact]
30+
public void WithAuthenticatedUserShouldPopulateUserNameAndRolesProperly()
31+
{
32+
MyController<MvcController>
33+
.Instance()
34+
.WithHttpRequest(request => request
35+
.WithUser("TestUserName", new string[]
36+
{
37+
"Administrator",
38+
"Moderator"
39+
}))
40+
.ShouldPassForThe<HttpRequest>(builtRequest =>
41+
{
42+
Assert.Equal(0, builtRequest.Query.Count);
43+
Assert.False(builtRequest.HasFormContentType);
44+
Assert.Equal(0, builtRequest.Cookies.Count);
45+
});
46+
}
47+
48+
[Fact]
49+
public void WithUserShouldPopulateUserIdentifierAndNameAndRolesProperly()
50+
{
51+
MyController<MvcController>
52+
.Instance()
53+
.WithHttpRequest(request => request
54+
.WithUser("TestIdentifier", "TestUserName", new List<string>
55+
{
56+
"Administrator",
57+
"Moderator"
58+
}))
59+
.ShouldPassForThe<HttpRequest>(builtRequest =>
60+
{
61+
Assert.Equal(0, builtRequest.Query.Count);
62+
Assert.False(builtRequest.HasFormContentType);
63+
Assert.Equal(0, builtRequest.Cookies.Count);
64+
});
65+
}
66+
67+
[Fact]
68+
public void WithAuthenticatedUserShouldPopulateUserIdentifierAndNameAndRolesProperly()
69+
{
70+
MyController<MvcController>
71+
.Instance()
72+
.WithHttpRequest(request => request
73+
.WithUser("TestIdentifier", "TestUserName", new string[]
74+
{
75+
"Administrator",
76+
"Moderator"
77+
}))
78+
.ShouldPassForThe<HttpRequest>(builtRequest =>
79+
{
80+
Assert.Equal(0, builtRequest.Query.Count);
81+
Assert.False(builtRequest.HasFormContentType);
82+
Assert.Equal(0, builtRequest.Cookies.Count);
83+
});
84+
}
85+
86+
[Fact]
87+
public void WithAuthenticatedUserShouldPopulateUserRolesProperly()
88+
{
89+
MyController<MvcController>
90+
.Instance()
91+
.WithHttpRequest(request => request
92+
.WithUser(new List<string>
93+
{
94+
"Administrator",
95+
"Moderator"
96+
}))
97+
.ShouldPassForThe<HttpRequest>(builtRequest =>
98+
{
99+
Assert.Equal(0, builtRequest.Query.Count);
100+
Assert.False(builtRequest.HasFormContentType);
101+
Assert.Equal(0, builtRequest.Cookies.Count);
102+
});
103+
}
104+
105+
[Fact]
106+
public void WithAuthenticatedUserShouldWorkCorrectly()
107+
{
108+
MyController<MvcController>
109+
.Instance()
110+
.WithHttpRequest(request => request
111+
.WithUser(user => user
112+
.WithIdentity(identity => identity
113+
.WithIdentifier("IdentityIdentifier")
114+
.WithUsername("IdentityUsername")
115+
.InRole("IdentityRole"))))
116+
.ShouldPassForThe<HttpRequest>(builtRequest =>
117+
{
118+
Assert.Equal(0, builtRequest.Query.Count);
119+
Assert.False(builtRequest.HasFormContentType);
120+
Assert.Equal(0, builtRequest.Cookies.Count);
121+
});
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)