Skip to content

Commit d102c0d

Browse files
authored
Add login control unit tests (#102)
* Add login control unit tests * Reduce magic strings
1 parent 9ea1b4f commit d102c0d

File tree

6 files changed

+232
-3
lines changed

6 files changed

+232
-3
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
@inherits TestComponentBase
2+
@using System.Security.Claims;
3+
@using Microsoft.AspNetCore.Components.Authorization
4+
@using BlazorWebFormsComponents.LoginControls;
5+
@using Moq;
6+
7+
<Fixture Test="AuthenticateTest">
8+
<ComponentUnderTest>
9+
<BlazorWebFormsComponents.LoginControls.Login @ref="loginControl" ID="loginControl" OnAuthenticate="Login_Authenticate" />
10+
</ComponentUnderTest>
11+
</Fixture>
12+
13+
@code {
14+
Login loginControl = new Login();
15+
bool isAuthenticated = false;
16+
17+
void AuthenticateTest()
18+
{
19+
var principal = new ClaimsPrincipal();
20+
var identity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, "James Bond") }, "Test authentication");
21+
principal.AddIdentity(identity);
22+
23+
var authenticationStateProviderMock = new Mock<AuthenticationStateProvider>();
24+
authenticationStateProviderMock.Setup(x => x.GetAuthenticationStateAsync()).Returns(Task.FromResult(new AuthenticationState(principal)));
25+
26+
Services.AddSingleton<AuthenticationStateProvider>(authenticationStateProviderMock.Object);
27+
28+
var navigationManagerMock = new Mock<NavigationManager>();
29+
Services.AddSingleton<NavigationManager>(navigationManagerMock.Object);
30+
31+
var cut = GetComponentUnderTest();
32+
33+
cut.Find("#loginControl_UserName").Change(Credential.InvalidCredential.Username);
34+
cut.Find("#loginControl_Password").Change(Credential.InvalidCredential.Username);
35+
cut.Find("form").Submit();
36+
cut.Markup.ShouldContain("Your login attempt was not successful");
37+
isAuthenticated.ShouldBeFalse();
38+
39+
cut.Find("#loginControl_UserName").Change(Credential.ValidCredential.Username);
40+
cut.Find("#loginControl_Password").Change(Credential.ValidCredential.Password);
41+
cut.Find("form").Submit();
42+
cut.Markup.ShouldNotContain("Your login attempt was not successful");
43+
isAuthenticated.ShouldBeTrue();
44+
}
45+
46+
void Login_Authenticate(AuthenticateEventArgs args)
47+
{
48+
if (loginControl.UserName == Credential.ValidCredential.Username && loginControl.Password == Credential.ValidCredential.Password)
49+
{
50+
isAuthenticated = args.Authenticated = true;
51+
}
52+
else
53+
{
54+
isAuthenticated = args.Authenticated = false;
55+
}
56+
}
57+
58+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace BlazorWebFormsComponents.Test.LoginControls.Login
2+
{
3+
public class Credential
4+
{
5+
public static readonly Credential ValidCredential = new Credential { Username = "admin", Password = "FakePassword" };
6+
7+
public static readonly Credential InvalidCredential = new Credential { Username = "admin", Password = "SecRet P@ssw0rd" };
8+
9+
public string Username { get; set; }
10+
11+
public string Password { get; set; }
12+
}
13+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
@inherits TestComponentBase
2+
@using System.Security.Claims;
3+
@using Microsoft.AspNetCore.Components.Authorization
4+
@using BlazorWebFormsComponents.LoginControls;
5+
@using Moq;
6+
7+
<Fixture Test="LoggedInTest">
8+
<ComponentUnderTest>
9+
<BlazorWebFormsComponents.LoginControls.Login @ref="loginControl" ID="loginControl" OnLoggedIn="Login_LoggedIn" OnAuthenticate="Login_Authenticate" />
10+
</ComponentUnderTest>
11+
</Fixture>
12+
13+
14+
@code {
15+
Login loginControl = new Login();
16+
bool isLoggedIn = false;
17+
18+
void LoggedInTest()
19+
{
20+
var principal = new ClaimsPrincipal();
21+
var identity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, "James Bond") }, "Test authentication");
22+
principal.AddIdentity(identity);
23+
24+
var authenticationStateProviderMock = new Mock<AuthenticationStateProvider>();
25+
authenticationStateProviderMock.Setup(x => x.GetAuthenticationStateAsync()).Returns(Task.FromResult(new AuthenticationState(principal)));
26+
27+
Services.AddSingleton<AuthenticationStateProvider>(authenticationStateProviderMock.Object);
28+
29+
var navigationManagerMock = new Mock<NavigationManager>();
30+
Services.AddSingleton<NavigationManager>(navigationManagerMock.Object);
31+
32+
var cut = GetComponentUnderTest();
33+
34+
cut.Find("#loginControl_UserName").Change(Credential.InvalidCredential.Username);
35+
cut.Find("#loginControl_Password").Change(Credential.InvalidCredential.Password);
36+
cut.Find("form").Submit();
37+
isLoggedIn.ShouldBeTrue();
38+
}
39+
40+
void Login_LoggedIn(EventArgs args)
41+
{
42+
isLoggedIn = true;
43+
}
44+
45+
void Login_Authenticate(AuthenticateEventArgs args)
46+
{
47+
args.Authenticated = true;
48+
}
49+
50+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
@inherits TestComponentBase
2+
@using System.Security.Claims;
3+
@using Microsoft.AspNetCore.Components.Authorization
4+
@using BlazorWebFormsComponents.LoginControls;
5+
@using Moq;
6+
7+
<Fixture Test="LoggingInTest">
8+
<ComponentUnderTest>
9+
<BlazorWebFormsComponents.LoginControls.Login @ref="loginControl" ID="loginControl" OnLoggingIn="Login_LoggingIn" />
10+
</ComponentUnderTest>
11+
</Fixture>
12+
13+
14+
@code {
15+
Login loginControl = new Login();
16+
bool isLoginCanceled = false;
17+
18+
void LoggingInTest()
19+
{
20+
var principal = new ClaimsPrincipal();
21+
var identity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, "James Bond") }, "Test authentication");
22+
principal.AddIdentity(identity);
23+
24+
var authenticationStateProviderMock = new Mock<AuthenticationStateProvider>();
25+
authenticationStateProviderMock.Setup(x => x.GetAuthenticationStateAsync()).Returns(Task.FromResult(new AuthenticationState(principal)));
26+
27+
Services.AddSingleton<AuthenticationStateProvider>(authenticationStateProviderMock.Object);
28+
29+
var navigationManagerMock = new Mock<NavigationManager>();
30+
Services.AddSingleton<NavigationManager>(navigationManagerMock.Object);
31+
32+
var cut = GetComponentUnderTest();
33+
34+
cut.Find("#loginControl_UserName").Change(Credential.InvalidCredential.Username);
35+
cut.Find("#loginControl_Password").Change(Credential.InvalidCredential.Password);
36+
cut.Find("form").Submit();
37+
isLoginCanceled.ShouldBeTrue();
38+
39+
cut.Find("#loginControl_UserName").Change(Credential.ValidCredential.Username);
40+
cut.Find("#loginControl_Password").Change(Credential.ValidCredential.Password);
41+
cut.Find("form").Submit();
42+
isLoginCanceled.ShouldBeFalse();
43+
}
44+
45+
void Login_LoggingIn(LoginCancelEventArgs args)
46+
{
47+
if(loginControl.UserName == Credential.ValidCredential.Username && loginControl.Password == Credential.ValidCredential.Password)
48+
{
49+
isLoginCanceled = args.Cancel = false;
50+
}
51+
else
52+
{
53+
isLoginCanceled = args.Cancel = true;
54+
}
55+
}
56+
57+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
@inherits TestComponentBase
2+
@using System.Security.Claims;
3+
@using Microsoft.AspNetCore.Components.Authorization
4+
@using BlazorWebFormsComponents.LoginControls;
5+
@using Moq;
6+
7+
<Fixture Test="LoginErrorTest">
8+
<ComponentUnderTest>
9+
<BlazorWebFormsComponents.LoginControls.Login @ref="loginControl" ID="loginControl" OnLoginError="Login_LoginError" OnAuthenticate="Login_Authenticate" />
10+
</ComponentUnderTest>
11+
</Fixture>
12+
13+
14+
@code {
15+
Login loginControl = new Login();
16+
bool isLoginError = false;
17+
18+
void LoginErrorTest()
19+
{
20+
var principal = new ClaimsPrincipal();
21+
var identity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, "James Bond") }, "Test authentication");
22+
principal.AddIdentity(identity);
23+
24+
var authenticationStateProviderMock = new Mock<AuthenticationStateProvider>();
25+
authenticationStateProviderMock.Setup(x => x.GetAuthenticationStateAsync()).Returns(Task.FromResult(new AuthenticationState(principal)));
26+
27+
Services.AddSingleton<AuthenticationStateProvider>(authenticationStateProviderMock.Object);
28+
29+
var navigationManagerMock = new Mock<NavigationManager>();
30+
Services.AddSingleton<NavigationManager>(navigationManagerMock.Object);
31+
32+
var cut = GetComponentUnderTest();
33+
34+
cut.Find("#loginControl_UserName").Change(Credential.InvalidCredential.Username);
35+
cut.Find("#loginControl_Password").Change(Credential.InvalidCredential.Password);
36+
cut.Find("form").Submit();
37+
isLoginError.ShouldBeTrue();
38+
}
39+
40+
void Login_LoginError(EventArgs args)
41+
{
42+
isLoginError = true;
43+
}
44+
45+
void Login_Authenticate(AuthenticateEventArgs args)
46+
{
47+
args.Authenticated = false;
48+
}
49+
50+
}

src/BlazorWebFormsComponents/LoginControls/Login.razor.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ public partial class Login : BaseWebFormsComponent
3636
[Parameter] public string LoginButtonImageUrl { get; set; }
3737
[Parameter] public string LoginButtonText { get; set; } = "Log In";
3838
[Parameter] public ButtonType LoginButtonType { get; set; } = ButtonType.Button;
39-
39+
[Parameter] public string Password { get => Model?.Password ?? string.Empty; set => Model.Password = value; }
4040
[Parameter] public string PasswordLabelText { get; set; } = "Password:";
4141
[Parameter] public string PasswordRecoveryText { get; set; }
4242
[Parameter] public string PasswordRecoveryUrl { get; set; }
4343
[Parameter] public string PasswordRecoveryIconUrl { get; set; }
4444
[Parameter] public string PasswordRequiredErrorMessage { get; set; } = "Password is required.";
45-
[Parameter] public bool RememberMeSet { get; set; }
45+
[Parameter] public bool RememberMeSet { get => Model?.RememberMe ?? false; set => Model.RememberMe = value; }
4646
[Parameter] public string RememberMeText { get; set; } = "Remember me next time.";
4747
[Parameter] public string TitleText { get; set; } = "Log In";
48-
[Parameter] public string UserName { get; set; }
48+
[Parameter] public string UserName { get => Model?.Username ?? string.Empty; set => Model.Username = value; }
4949
[Parameter] public string UserNameLabelText { get; set; } = "User Name:";
5050
[Parameter] public string UserNameRequiredErrorMessage { get; set; } = "User Name is required.";
5151
[Parameter] public bool VisibleWhenLoggedIn { get; set; } = true;
@@ -196,6 +196,7 @@ protected override async Task OnInitializedAsync()
196196
Model = new LoginModel
197197
{
198198
Username = UserName,
199+
Password = Password,
199200
RememberMe = RememberMeSet
200201
};
201202

0 commit comments

Comments
 (0)