mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-06-25 16:31:27 +00:00
Add registration tests
This commit is contained in:
parent
89857b99bc
commit
b5767c6f21
3 changed files with 135 additions and 39 deletions
|
@ -5,43 +5,14 @@ using LBPUnion.ProjectLighthouse;
|
||||||
using LBPUnion.ProjectLighthouse.Helpers;
|
using LBPUnion.ProjectLighthouse.Helpers;
|
||||||
using LBPUnion.ProjectLighthouse.Tests;
|
using LBPUnion.ProjectLighthouse.Tests;
|
||||||
using LBPUnion.ProjectLighthouse.Types;
|
using LBPUnion.ProjectLighthouse.Types;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.Hosting.Server.Features;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using OpenQA.Selenium;
|
using OpenQA.Selenium;
|
||||||
using OpenQA.Selenium.Chrome;
|
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace ProjectLighthouse.Tests.WebsiteTests
|
namespace ProjectLighthouse.Tests.WebsiteTests
|
||||||
{
|
{
|
||||||
public class AuthenticationTests : IDisposable
|
public class AuthenticationTests : LighthouseWebTest
|
||||||
{
|
{
|
||||||
public readonly IWebHost WebHost = new WebHostBuilder().UseKestrel().UseStartup<TestStartup>().UseWebRoot("StaticFiles").Build();
|
|
||||||
public readonly string BaseAddress;
|
|
||||||
|
|
||||||
public readonly IWebDriver Driver;
|
|
||||||
|
|
||||||
public AuthenticationTests()
|
|
||||||
{
|
|
||||||
this.WebHost.Start();
|
|
||||||
|
|
||||||
IServerAddressesFeature? serverAddressesFeature = WebHost.ServerFeatures.Get<IServerAddressesFeature>();
|
|
||||||
if (serverAddressesFeature == null) throw new ArgumentNullException();
|
|
||||||
|
|
||||||
this.BaseAddress = serverAddressesFeature.Addresses.First();
|
|
||||||
|
|
||||||
ChromeOptions chromeOptions = new();
|
|
||||||
if (Convert.ToBoolean(Environment.GetEnvironmentVariable("CI") ?? "false"))
|
|
||||||
{
|
|
||||||
chromeOptions.AddArgument("headless");
|
|
||||||
chromeOptions.AddArgument("no-sandbox");
|
|
||||||
chromeOptions.AddArgument("disable-dev-shm-usage");
|
|
||||||
Console.WriteLine("We are in a CI environment, so chrome headless mode has been enabled.");
|
|
||||||
}
|
|
||||||
|
|
||||||
this.Driver = new ChromeDriver(chromeOptions);
|
|
||||||
}
|
|
||||||
|
|
||||||
[DatabaseFact]
|
[DatabaseFact]
|
||||||
public async Task ShouldLoginWithPassword()
|
public async Task ShouldLoginWithPassword()
|
||||||
{
|
{
|
||||||
|
@ -131,14 +102,5 @@ namespace ProjectLighthouse.Tests.WebsiteTests
|
||||||
|
|
||||||
await database.RemoveUser(user);
|
await database.RemoveUser(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
this.Driver.Close();
|
|
||||||
this.Driver.Dispose();
|
|
||||||
this.WebHost.Dispose();
|
|
||||||
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
50
ProjectLighthouse.Tests.WebsiteTests/LighthouseWebTest.cs
Normal file
50
ProjectLighthouse.Tests.WebsiteTests/LighthouseWebTest.cs
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using LBPUnion.ProjectLighthouse;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.Hosting.Server.Features;
|
||||||
|
using OpenQA.Selenium;
|
||||||
|
using OpenQA.Selenium.Chrome;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace ProjectLighthouse.Tests.WebsiteTests
|
||||||
|
{
|
||||||
|
[Collection(nameof(LighthouseWebTest))]
|
||||||
|
public class LighthouseWebTest : IDisposable
|
||||||
|
{
|
||||||
|
public readonly IWebHost WebHost = new WebHostBuilder().UseKestrel().UseStartup<TestStartup>().UseWebRoot("StaticFiles").Build();
|
||||||
|
public readonly string BaseAddress;
|
||||||
|
|
||||||
|
public readonly IWebDriver Driver;
|
||||||
|
|
||||||
|
public LighthouseWebTest()
|
||||||
|
{
|
||||||
|
this.WebHost.Start();
|
||||||
|
|
||||||
|
IServerAddressesFeature? serverAddressesFeature = WebHost.ServerFeatures.Get<IServerAddressesFeature>();
|
||||||
|
if (serverAddressesFeature == null) throw new ArgumentNullException();
|
||||||
|
|
||||||
|
this.BaseAddress = serverAddressesFeature.Addresses.First();
|
||||||
|
|
||||||
|
ChromeOptions chromeOptions = new();
|
||||||
|
if (Convert.ToBoolean(Environment.GetEnvironmentVariable("CI") ?? "false"))
|
||||||
|
{
|
||||||
|
chromeOptions.AddArgument("headless");
|
||||||
|
chromeOptions.AddArgument("no-sandbox");
|
||||||
|
chromeOptions.AddArgument("disable-dev-shm-usage");
|
||||||
|
Console.WriteLine("We are in a CI environment, so chrome headless mode has been enabled.");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.Driver = new ChromeDriver(chromeOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
this.Driver.Close();
|
||||||
|
this.Driver.Dispose();
|
||||||
|
this.WebHost.Dispose();
|
||||||
|
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
84
ProjectLighthouse.Tests.WebsiteTests/RegisterTests.cs
Normal file
84
ProjectLighthouse.Tests.WebsiteTests/RegisterTests.cs
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using LBPUnion.ProjectLighthouse;
|
||||||
|
using LBPUnion.ProjectLighthouse.Helpers;
|
||||||
|
using LBPUnion.ProjectLighthouse.Tests;
|
||||||
|
using LBPUnion.ProjectLighthouse.Types;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using OpenQA.Selenium;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace ProjectLighthouse.Tests.WebsiteTests
|
||||||
|
{
|
||||||
|
public class RegisterTests : LighthouseWebTest
|
||||||
|
{
|
||||||
|
[DatabaseFact]
|
||||||
|
public async Task ShouldRegister()
|
||||||
|
{
|
||||||
|
await using Database database = new();
|
||||||
|
|
||||||
|
string username = "unitTestUser" + new Random().Next();
|
||||||
|
string password = HashHelper.Sha256Hash(HashHelper.GenerateRandomBytes(64).ToArray());
|
||||||
|
|
||||||
|
this.Driver.Navigate().GoToUrl(this.BaseAddress + "/register");
|
||||||
|
|
||||||
|
this.Driver.FindElement(By.Id("text")).SendKeys(username);
|
||||||
|
|
||||||
|
this.Driver.FindElement(By.Id("password")).SendKeys(password);
|
||||||
|
this.Driver.FindElement(By.Id("confirmPassword")).SendKeys(password);
|
||||||
|
|
||||||
|
this.Driver.FindElement(By.Id("submit")).Click();
|
||||||
|
|
||||||
|
User? user = await database.Users.FirstOrDefaultAsync(u => u.Username == username);
|
||||||
|
Assert.NotNull(user);
|
||||||
|
|
||||||
|
await database.RemoveUser(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DatabaseFact]
|
||||||
|
public async Task ShouldNotRegisterWithMismatchingPasswords()
|
||||||
|
{
|
||||||
|
await using Database database = new();
|
||||||
|
|
||||||
|
string username = "unitTestUser" + new Random().Next();
|
||||||
|
string password = HashHelper.Sha256Hash(HashHelper.GenerateRandomBytes(64).ToArray());
|
||||||
|
|
||||||
|
this.Driver.Navigate().GoToUrl(this.BaseAddress + "/register");
|
||||||
|
|
||||||
|
this.Driver.FindElement(By.Id("text")).SendKeys(username);
|
||||||
|
|
||||||
|
this.Driver.FindElement(By.Id("password")).SendKeys(password);
|
||||||
|
this.Driver.FindElement(By.Id("confirmPassword")).SendKeys(password + "a");
|
||||||
|
|
||||||
|
this.Driver.FindElement(By.Id("submit")).Click();
|
||||||
|
|
||||||
|
User? user = await database.Users.FirstOrDefaultAsync(u => u.Username == username);
|
||||||
|
Assert.Null(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DatabaseFact]
|
||||||
|
public async Task ShouldNotRegisterWithTakenUsername()
|
||||||
|
{
|
||||||
|
await using Database database = new();
|
||||||
|
|
||||||
|
string username = "unitTestUser" + new Random().Next();
|
||||||
|
string password = HashHelper.Sha256Hash(HashHelper.GenerateRandomBytes(64).ToArray());
|
||||||
|
|
||||||
|
await database.CreateUser(username, HashHelper.BCryptHash(password));
|
||||||
|
User? user = await database.Users.FirstOrDefaultAsync(u => u.Username == username);
|
||||||
|
Assert.NotNull(user);
|
||||||
|
|
||||||
|
this.Driver.Navigate().GoToUrl(this.BaseAddress + "/register");
|
||||||
|
|
||||||
|
this.Driver.FindElement(By.Id("text")).SendKeys(username);
|
||||||
|
|
||||||
|
this.Driver.FindElement(By.Id("password")).SendKeys(password);
|
||||||
|
this.Driver.FindElement(By.Id("confirmPassword")).SendKeys(password);
|
||||||
|
|
||||||
|
this.Driver.FindElement(By.Id("submit")).Click();
|
||||||
|
|
||||||
|
Assert.Contains("The username you've chosen is already taken.", this.Driver.PageSource);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue