1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-13 17:33:22 +08:00

Add test coverage of 2FA flow

This commit is contained in:
Dean Herbert 2023-11-16 18:16:02 +09:00
parent 285f740e2a
commit f7fa9c90d6
No known key found for this signature in database
2 changed files with 20 additions and 17 deletions

View File

@ -10,6 +10,7 @@ using osu.Framework.Testing;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Overlays;
using osu.Game.Overlays.Login;
using osu.Game.Users.Drawables;
using osuTK.Input;
@ -36,28 +37,25 @@ namespace osu.Game.Tests.Visual.Menus
AddStep("show login overlay", () => loginOverlay.Show());
}
[Test]
public void TestLoginTwoFactorSuccess()
{
AddStep("logout", () =>
{
API.Logout();
((DummyAPIAccess)API).RequireTwoFactor();
});
AddStep("enter password", () => loginOverlay.ChildrenOfType<OsuPasswordTextBox>().First().Text = "password");
AddStep("submit", () => loginOverlay.ChildrenOfType<OsuButton>().First(b => b.Text.ToString() == "Sign in").TriggerClick());
}
[Test]
public void TestLoginSuccess()
{
AddStep("logout", () => API.Logout());
assertAPIState(APIState.Offline);
AddStep("enter password", () => loginOverlay.ChildrenOfType<OsuPasswordTextBox>().First().Text = "password");
AddStep("submit", () => loginOverlay.ChildrenOfType<OsuButton>().First(b => b.Text.ToString() == "Sign in").TriggerClick());
assertAPIState(APIState.RequiresSecondFactorAuth);
AddUntilStep("wait for second factor auth form", () => loginOverlay.ChildrenOfType<SecondFactorAuthForm>().SingleOrDefault(), () => Is.Not.Null);
AddStep("enter code", () => loginOverlay.ChildrenOfType<OsuTextBox>().First().Text = "88800088");
assertAPIState(APIState.Online);
}
private void assertAPIState(APIState expected) =>
AddUntilStep($"login state is {expected}", () => API.State.Value, () => Is.EqualTo(expected));
[Test]
public void TestLoginFailure()
{

View File

@ -55,7 +55,7 @@ namespace osu.Game.Online.API
private bool shouldFailNextLogin;
private bool stayConnectingNextLogin;
private bool requiresTwoFactor;
private bool requiredSecondFactorAuth = true;
/// <summary>
/// The current connectivity state of the API.
@ -116,13 +116,15 @@ namespace osu.Game.Online.API
Id = DUMMY_USER_ID,
};
if (requiresTwoFactor)
if (requiredSecondFactorAuth)
{
state.Value = APIState.RequiresSecondFactorAuth;
requiresTwoFactor = false;
}
else
{
state.Value = APIState.Online;
requiredSecondFactorAuth = true;
}
}
public void AuthenticateSecondFactor(string code)
@ -154,7 +156,10 @@ namespace osu.Game.Online.API
IBindableList<APIUser> IAPIProvider.Friends => Friends;
IBindable<UserActivity> IAPIProvider.Activity => Activity;
public void RequireTwoFactor() => requiresTwoFactor = true;
/// <summary>
/// Skip 2FA requirement for next login.
/// </summary>
public void SkipSecondFactor() => requiredSecondFactorAuth = false;
/// <summary>
/// During the next simulated login, the process will fail immediately.