mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-02 18:18:39 +00:00
Add admin panel testing
This commit is contained in:
parent
b5767c6f21
commit
456921d0c7
1 changed files with 64 additions and 0 deletions
64
ProjectLighthouse.Tests.WebsiteTests/AdminTests.cs
Normal file
64
ProjectLighthouse.Tests.WebsiteTests/AdminTests.cs
Normal file
|
@ -0,0 +1,64 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Tests;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using OpenQA.Selenium;
|
||||
using Xunit;
|
||||
|
||||
namespace ProjectLighthouse.Tests.WebsiteTests
|
||||
{
|
||||
public class AdminTests : LighthouseWebTest
|
||||
{
|
||||
public const string AdminPanelButtonXPath = "/html/body/div/header/div/div/div/a[2]";
|
||||
|
||||
[DatabaseFact]
|
||||
public async Task ShouldShowAdminPanelButtonWhenAdmin()
|
||||
{
|
||||
await using Database database = new();
|
||||
Random random = new();
|
||||
User user = await database.CreateUser($"unitTestUser{random.Next()}", HashHelper.BCryptHash("i'm an engineering failure"));
|
||||
|
||||
WebToken webToken = new()
|
||||
{
|
||||
UserId = user.UserId,
|
||||
UserToken = HashHelper.GenerateAuthToken(),
|
||||
};
|
||||
|
||||
database.WebTokens.Add(webToken);
|
||||
user.IsAdmin = true;
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
this.Driver.Navigate().GoToUrl(this.BaseAddress + "/");
|
||||
this.Driver.Manage().Cookies.AddCookie(new Cookie("LighthouseToken", webToken.UserToken));
|
||||
this.Driver.Navigate().Refresh();
|
||||
|
||||
Assert.Contains("Admin Panel", this.Driver.FindElement(By.XPath(AdminPanelButtonXPath)).Text);
|
||||
}
|
||||
|
||||
[DatabaseFact]
|
||||
public async Task ShouldNotShowAdminPanelButtonWhenNotAdmin()
|
||||
{
|
||||
await using Database database = new();
|
||||
Random random = new();
|
||||
User user = await database.CreateUser($"unitTestUser{random.Next()}", HashHelper.BCryptHash("i'm an engineering failure"));
|
||||
|
||||
WebToken webToken = new()
|
||||
{
|
||||
UserId = user.UserId,
|
||||
UserToken = HashHelper.GenerateAuthToken(),
|
||||
};
|
||||
|
||||
database.WebTokens.Add(webToken);
|
||||
user.IsAdmin = false;
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
this.Driver.Navigate().GoToUrl(this.BaseAddress + "/");
|
||||
this.Driver.Manage().Cookies.AddCookie(new Cookie("LighthouseToken", webToken.UserToken));
|
||||
this.Driver.Navigate().Refresh();
|
||||
|
||||
Assert.DoesNotContain("Admin Panel", this.Driver.FindElement(By.XPath(AdminPanelButtonXPath)).Text);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue