using System.Collections.Generic;
using System.Security.Cryptography;
using Microsoft.VisualStudio.TestTools.UnitTesting;
public class RegistrationSystem
private static bool _enableLinkInvitation;
private static bool _requireConfirmation;
private static Dictionary<string, User> _userConfirmations = new Dictionary<string, User>();
public static Uri SetLinkInvitationSetting(bool enableLinkInvitation, bool requireConfirmation)
_enableLinkInvitation = enableLinkInvitation;
_requireConfirmation = requireConfirmation;
if (enableLinkInvitation)
return new Uri("https://example.com/register");
public static class Account
public static Uri GetRegistrationConfirmUri(User user)
string token = GenerateConfirmationToken();
_userConfirmations[token] = user;
return new Uri($"https://example.com/confirm?token={token}");
private static string GenerateConfirmationToken()
using (var rng = new RNGCryptoServiceProvider())
var tokenBytes = new byte[32];
rng.GetBytes(tokenBytes);
return Convert.ToBase64String(tokenBytes);
public static void JoinPortalNewUser(User user, Uri confirmUri, bool expectConfirmationPage)
Console.WriteLine($"User {user.Email} opening confirmation link: {confirmUri}");
if (expectConfirmationPage)
Console.WriteLine("Confirmation page displayed.");
string token = confirmUri.Query.Substring(confirmUri.Query.IndexOf("token=") + 6);
if (_userConfirmations.ContainsKey(token))
User confirmingUser = _userConfirmations[token];
confirmingUser.IsConfirmed = true;
_userConfirmations.Remove(token);
Console.WriteLine($"User confirmed: {confirmingUser.Email}");
Console.WriteLine($"Confirmation Failed. Token {token} is invalid");
public static void RejectUsrInvitationWithAssertAuthImpossabilityCommonPart(User user)
RejectUserInvitCommon(user);
Console.WriteLine($"Login attempt failed for user: {user.Email}");
public static void RejectUserInvitCommon(User user)
Console.WriteLine($"User {user.Email} Rejected Invitation");
public static string getTextForRegCompletePage()
return "Registration Complete!";
public string Email { get; set; }
public string Password { get; set; }
public bool IsConfirmed { get; set; }
public bool IsRejected { get; set; }
public static void Main()
Console.WriteLine("š Discovering and Running Tests...\n");
int totalPassed = 0, totalFailed = 0;
var testClasses = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.GetCustomAttribute<TestClassAttribute>() != null);
foreach (var testClass in testClasses)
Console.WriteLine($"š Running tests in: {testClass.Name}");
(int passed, int failed) = RunTests(testClass);
Console.WriteLine("\nš FINAL SUMMARY:");
Console.WriteLine($"ā
Total Passed: {totalPassed}");
Console.WriteLine($"ā Total Failed: {totalFailed}");
Console.WriteLine($"š Total Tests: {totalPassed + totalFailed}");
Console.WriteLine("\nā
Test execution completed.");
private static (int passed, int failed) RunTests(Type testClassType)
object testInstance = Activator.CreateInstance(testClassType);
MethodInfo[] testMethods = testClassType.GetMethods(BindingFlags.Instance | BindingFlags.Public).Where(m => m.GetCustomAttribute<TestMethodAttribute>() != null).ToArray();
int passed = 0, failed = 0;
foreach (var method in testMethods)
method.Invoke(testInstance, null);
Console.WriteLine($" ā
{method.Name} PASSED");
catch (TargetInvocationException ex)
Console.WriteLine($" ā {method.Name} FAILED: {ex.InnerException?.Message ?? ex.Message}");
Console.WriteLine($" ā ļø {method.Name} ERROR: {ex.Message}");
public class RegistrationSystemTests
public void SetLinkInvitationSetting_EnableLinkInvitation_ReturnsValidUri()
Uri result = RegistrationSystem.SetLinkInvitationSetting(true, true);
Assert.IsNotNull(result);
Assert.AreEqual("https://example.com/register", result.ToString());
public void SetLinkInvitationSetting_DisableLinkInvitation_ReturnsNull()
Uri result = RegistrationSystem.SetLinkInvitationSetting(false, true);
public void GetRegistrationConfirmUri_ReturnsUriWithToken()
Email = "test@example.com"
Uri confirmUri = RegistrationSystem.Account.GetRegistrationConfirmUri(user);
Assert.IsNotNull(confirmUri);
Assert.IsTrue(confirmUri.ToString().StartsWith("https://example.com/confirm?token="));
public void JoinPortalNewUser_ExpectConfirmationPage_ConfirmsUser()
Email = "test@example.com"
Uri confirmUri = RegistrationSystem.Account.GetRegistrationConfirmUri(user);
RegistrationSystem.JoinPortalNewUser(user, confirmUri, true);
Assert.IsTrue(user.IsConfirmed);
public void JoinPortalNewUser_DoNotExpectConfirmationPage_DoesNotConfirm()
Email = "test1@example.com"
Uri confirmUri = RegistrationSystem.Account.GetRegistrationConfirmUri(user);
RegistrationSystem.JoinPortalNewUser(user, confirmUri, false);
Assert.IsFalse(user.IsConfirmed);
public void RejectUsrInvitationWithAssertAuthImpossabilityCommonPart_RejectsUser()
Email = "test@example.com",
RegistrationSystem.RejectUsrInvitationWithAssertAuthImpossabilityCommonPart(user);
Assert.IsTrue(user.IsRejected);
public void RejectUserInvitCommon_RejectsUser()
Email = "test@example.com"
RegistrationSystem.RejectUserInvitCommon(user);
Assert.IsTrue(user.IsRejected);
public void GetTextForRegCompletePage_ReturnsCorrectText()
string text = RegistrationSystem.getTextForRegCompletePage();
Assert.AreEqual("Registration Complete!", text);