Skip to content

Commit dbf7f81

Browse files
committed
Added unit tests for covering MyTested.AspNetCore.Mvc.Authentication
1 parent b98b8f9 commit dbf7f81

File tree

2 files changed

+224
-0
lines changed

2 files changed

+224
-0
lines changed
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+
}

test/MyTested.AspNetCore.Mvc.Authentication.Test/BuildersTests/ViewComponentsTests/ViewComponentBuilderTests.cs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.ViewComponentsTests
22
{
33
using Setups.ViewComponents;
4+
using System.Collections.Generic;
45
using System.Security.Claims;
56
using Xunit;
67

@@ -63,5 +64,104 @@ public void WithAuthenticatedUserShouldPopulateProperUserWhenUserWithUserBuilder
6364
});
6465
}
6566

67+
[Fact]
68+
public void WithAuthenticatedUserShouldPopulateUserRoleProperly()
69+
{
70+
MyViewComponent<UserComponent>
71+
.Instance()
72+
.WithUser(new List<string>
73+
{
74+
"Administrator"
75+
})
76+
.InvokedWith(c => c.Invoke())
77+
.ShouldReturn()
78+
.View()
79+
.ShouldPassForThe<UserComponent>(viewComponent =>
80+
{
81+
var user = viewComponent.User as ClaimsPrincipal;
82+
83+
Assert.NotNull(user);
84+
Assert.True(user.IsInRole("Administrator"));
85+
Assert.Equal("Passport", user.Identity.AuthenticationType);
86+
Assert.True(user.Identity.IsAuthenticated);
87+
});
88+
}
89+
90+
[Fact]
91+
public void WithAuthenticatedUserShouldPopulateUserNameAndRoleProperly()
92+
{
93+
MyViewComponent<UserComponent>
94+
.Instance()
95+
.WithUser("NewUserName",
96+
new List<string>
97+
{
98+
"Administrator"
99+
})
100+
.InvokedWith(c => c.Invoke())
101+
.ShouldReturn()
102+
.View()
103+
.ShouldPassForThe<UserComponent>(viewComponent =>
104+
{
105+
var user = viewComponent.User as ClaimsPrincipal;
106+
107+
Assert.NotNull(user);
108+
Assert.True(user.IsInRole("Administrator"));
109+
Assert.Equal("NewUserName", user.Identity.Name);
110+
Assert.True(user.HasClaim(ClaimTypes.Name, "NewUserName"));
111+
Assert.Equal("Passport", user.Identity.AuthenticationType);
112+
Assert.True(user.Identity.IsAuthenticated);
113+
});
114+
}
115+
116+
[Fact]
117+
public void WithAuthenticatedUserShouldPopulateIdAndUserNameAndRoleProperly()
118+
{
119+
MyViewComponent<UserComponent>
120+
.Instance()
121+
.WithUser("IdentityIdentifier", "NewUserName",
122+
new List<string>
123+
{
124+
"Administrator"
125+
})
126+
.InvokedWith(c => c.Invoke())
127+
.ShouldReturn()
128+
.View()
129+
.ShouldPassForThe<UserComponent>(viewComponent =>
130+
{
131+
var user = viewComponent.User as ClaimsPrincipal;
132+
var usernameClaim = viewComponent.User.Identity;
133+
134+
Assert.NotNull(user);
135+
Assert.True(user.IsInRole("Administrator"));
136+
Assert.True(user.HasClaim(ClaimTypes.NameIdentifier, "IdentityIdentifier"));
137+
Assert.Equal("NewUserName", user.Identity.Name);
138+
Assert.True(user.HasClaim(ClaimTypes.Name, "NewUserName"));
139+
Assert.Equal("Passport", user.Identity.AuthenticationType);
140+
Assert.True(user.Identity.IsAuthenticated);
141+
});
142+
}
143+
144+
[Fact]
145+
public void WithAuthenticatedUserShouldPopulateRoleProperly()
146+
{
147+
MyViewComponent<UserComponent>
148+
.Instance()
149+
.WithUser(new string[]
150+
{
151+
"Administrator"
152+
})
153+
.InvokedWith(c => c.Invoke())
154+
.ShouldReturn()
155+
.View()
156+
.ShouldPassForThe<UserComponent>(viewComponent =>
157+
{
158+
var user = viewComponent.User as ClaimsPrincipal;
159+
160+
Assert.NotNull(user);
161+
Assert.True(user.IsInRole("Administrator"));
162+
Assert.Equal("Passport", user.Identity.AuthenticationType);
163+
Assert.True(user.Identity.IsAuthenticated);
164+
});
165+
}
66166
}
67167
}

0 commit comments

Comments
 (0)