Skip to content

Commit

Permalink
Merge pull request #320 from ITU-BDSA2024-GROUP10/refactor-test-code-…
Browse files Browse the repository at this point in the history
…to-be-less-redundant

Refactor test code to be less redundant
RasmusLarsen02 authored Nov 21, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 505c968 + 3193829 commit 24cae22
Showing 15 changed files with 367 additions and 370 deletions.
25 changes: 5 additions & 20 deletions Chirp/test/PlaywrightTests/CrossSiteScriptingTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Chirp.Infrastructure.Model;
using Microsoft.Playwright;
using PlaywrightTests.Utils;
using PlaywrightTests.Utils.PageTests;
using TestUtilities;

namespace PlaywrightTests;
@@ -39,28 +40,12 @@ public async Task CrossSiteScripting_loginForm()
[Test]
public async Task CrossSiteScripting_CheepForm()
{
var user = TestUtils.CreateTestAuthor("Mr. test");
var password = "Password123!";
var testAuthor = new TestAuthorBuilder(RazorFactory.GetUserManager())
.WithDefault()
.Create();

await Page.GotoAsync("/Identity/Account/Register");
await Page.GetByPlaceholder("name", new() { Exact = true }).ClickAsync();
await Page.GetByPlaceholder("name", new() { Exact = true }).FillAsync(user.UserName!);
await Page.GetByPlaceholder("name@example.com").ClickAsync();
await Page.GetByPlaceholder("name@example.com").FillAsync(user.Email!);
await Page.GetByLabel("Password", new() { Exact = true }).ClickAsync();
await Page.GetByLabel("Password", new() { Exact = true }).FillAsync(password);
await Page.GetByLabel("Confirm Password").ClickAsync();
await Page.GetByLabel("Confirm Password").FillAsync(password);
await Page.GetByRole(AriaRole.Button, new() { Name = "Register" }).ClickAsync();
await Page.GetByRole(AriaRole.Link, new() { Name = "Click here to confirm your account" }).ClickAsync();
await Expect(Page.GetByText("Thank you for confirming")).ToBeVisibleAsync();
await RazorPageUtils.Login(testAuthor);

await Page.GetByRole(AriaRole.Link, new() { Name = "login" }).ClickAsync();
await Page.GetByPlaceholder("Username").ClickAsync();
await Page.GetByPlaceholder("Username").FillAsync(user.UserName!);
await Page.GetByPlaceholder("password").ClickAsync();
await Page.GetByPlaceholder("password").FillAsync(password);
await Page.GetByRole(AriaRole.Button, new() { Name = "Log in" }).ClickAsync();
await Page.GetByRole(AriaRole.Link, new() { Name = "my timeline" }).ClickAsync();
await Page.Locator("#Message").ClickAsync();
await Page.Locator("#Message").FillAsync("<script>alert('alert!');</script>");
31 changes: 16 additions & 15 deletions Chirp/test/PlaywrightTests/EndToEndTests.cs
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
using Chirp.Infrastructure.Model;
using Microsoft.Playwright;
using PlaywrightTests.Utils;
using PlaywrightTests.Utils.PageTests;
using TestUtilities;

namespace PlaywrightTests;
@@ -11,32 +12,32 @@ namespace PlaywrightTests;
[NonParallelizable]
public class EndToEndTests : PageTestWithRazorPlaywrightWebApplicationFactory
{
[Test]
public async Task EndToEnd_RegisterLoginCheepAndLogout()
private async Task Register(Author author, string password)
{
var user = TestUtils.CreateTestAuthor("Mr. test");
var password = "Password123!";

await Page.GotoAsync("/");
await Page.GetByRole(AriaRole.Link, new() { Name = "register" }).ClickAsync();
await Page.GetByPlaceholder("name", new() { Exact = true }).ClickAsync();
await Page.GetByPlaceholder("name", new() { Exact = true }).FillAsync(user.UserName!);
await Page.GetByPlaceholder("name", new() { Exact = true }).FillAsync(author.UserName!);
await Page.GetByPlaceholder("name@example.com").ClickAsync();
await Page.GetByPlaceholder("name@example.com").FillAsync(user.Email!);
await Page.GetByPlaceholder("name@example.com").FillAsync(author.Email!);
await Page.GetByLabel("Password", new() { Exact = true }).ClickAsync();
await Page.GetByLabel("Password", new() { Exact = true }).FillAsync(password);
await Page.GetByLabel("Confirm Password").ClickAsync();
await Page.GetByLabel("Confirm Password").FillAsync(password);
await Page.GetByRole(AriaRole.Button, new() { Name = "Register" }).ClickAsync();
await Page.GetByRole(AriaRole.Link, new() { Name = "Click here to confirm your account" }).ClickAsync();
}

[Test]
public async Task EndToEnd_RegisterLoginCheepAndLogout()
{
var author = TestUtils.CreateTestAuthor("Mr. test");
var password = "Password123!";

await Page.GotoAsync("/");
await Register(author, password);
await Expect(Page.GetByText("Thank you for confirming")).ToBeVisibleAsync();

await Page.GetByRole(AriaRole.Link, new() { Name = "login" }).ClickAsync();
await Page.GetByPlaceholder("Username").ClickAsync();
await Page.GetByPlaceholder("Username").FillAsync(user.UserName!);
await Page.GetByPlaceholder("password").ClickAsync();
await Page.GetByPlaceholder("password").FillAsync(password);
await Page.GetByRole(AriaRole.Button, new() { Name = "Log in" }).ClickAsync();
await RazorPageUtils.Login(author.UserName!, password);

await Page.GetByRole(AriaRole.Link, new() { Name = "my timeline" }).ClickAsync();
await Page.Locator("#Message").ClickAsync();
@@ -50,7 +51,7 @@ public async Task EndToEnd_RegisterLoginCheepAndLogout()
await Page.GetByRole(AriaRole.Button, new() { Name = "Share" }).ClickAsync();
await Expect(Page.Locator("#messagelist")).ToContainTextAsync("Cheep in public timeline");

await Page.GetByRole(AriaRole.Link, new() { Name = $"logout [{user.UserName!}]" }).ClickAsync();
await Page.GetByRole(AriaRole.Link, new() { Name = $"logout [{author.UserName!}]" }).ClickAsync();
await Page.GetByRole(AriaRole.Button, new() { Name = "Click here to Logout" }).ClickAsync();

//assert
83 changes: 37 additions & 46 deletions Chirp/test/PlaywrightTests/ExternalTests.cs
Original file line number Diff line number Diff line change
@@ -1,93 +1,85 @@
using Microsoft.Playwright;
using PlaywrightTests.Utils;
using PlaywrightTests.Utils.PageTests;

namespace PlaywrightTests;

public class ExternalTests : PageTestWithDuende
{
[Test]
public async Task CreateUserUsingExternalProvider_AllInfo()
private async Task registerViaExternalProvider(string username, string password)
{
await Page.GotoAsync($"{RazorBaseUrl}/Identity/Account/Register");
await Page.GetByRole(AriaRole.Button, new() { Name = "OpenIdConnect" }).ClickAsync();
await Page.WaitForURLAsync($"{DuendeBaseUrl}/Account/Login**");
await Page.GetByPlaceholder("Username", new() { Exact = true }).ClickAsync();
await Page.GetByPlaceholder("Username", new() { Exact = true }).FillAsync("TestWithAllInfo");
await Page.GetByPlaceholder("Password", new() { Exact = true }).ClickAsync();
await Page.GetByPlaceholder("Password", new() { Exact = true }).FillAsync("password");
await Page.GetByPlaceholder("Username").ClickAsync();
await Page.GetByPlaceholder("Username").FillAsync(username);
await Page.GetByPlaceholder("Password").ClickAsync();
await Page.GetByPlaceholder("Password").FillAsync(password);
await Page.GetByRole(AriaRole.Button, new() { Name = "Login" }).ClickAsync();
await Page.WaitForURLAsync($"{RazorBaseUrl}/**");

await Expect(Page.Locator("body")).ToContainTextAsync("logout [Mr. test with all info]");

await Page.GetByRole(AriaRole.Link, new() { Name = "logout [Mr. test with all" }).ClickAsync();
}

private async Task logout()
{
await Page.GetByRole(AriaRole.Link, new() { Name = "logout " }).ClickAsync();
await Page.GetByRole(AriaRole.Button, new() { Name = "Click here to Logout" }).ClickAsync();
}

private async Task loginWithExternalProvider()
{
await Page.GetByRole(AriaRole.Link, new() { Name = "login" }).ClickAsync();
await Page.GetByRole(AriaRole.Button, new() { Name = "OpenIdConnect" }).ClickAsync();
}

[Test]
public async Task CreateUserUsingExternalProvider_AllInfo()
{
await registerViaExternalProvider("TestWithAllInfo", "password");
await Expect(Page.Locator("body")).ToContainTextAsync("logout [Mr. test with all info]");

await logout();

await loginWithExternalProvider();
await Expect(Page.Locator("body")).ToContainTextAsync("logout [Mr. test with all info]");

}

[Test]
public async Task CreateUserUsingExternalProvider_OnlyEmail()
{
await Page.GotoAsync($"{RazorBaseUrl}/Identity/Account/Register");
await Page.GetByRole(AriaRole.Button, new() { Name = "OpenIdConnect" }).ClickAsync();
await Page.GetByPlaceholder("Username").ClickAsync();
await Page.GetByPlaceholder("Username").FillAsync("TestWithEmail");
await Page.GetByPlaceholder("Password").ClickAsync();
await Page.GetByPlaceholder("Password").FillAsync("password");
await Page.GetByRole(AriaRole.Button, new() { Name = "Login" }).ClickAsync();
await Page.WaitForURLAsync($"{RazorBaseUrl}/**");
await registerViaExternalProvider("TestWithEmail", "password");

await Expect(Page.Locator("label")).ToContainTextAsync("Username");
await Page.GetByPlaceholder("Please enter your username").ClickAsync();
await Page.GetByPlaceholder("Please enter your username").FillAsync("Mr. test with email");
await Page.GetByRole(AriaRole.Button, new() { Name = "Register" }).ClickAsync();
await Expect(Page.Locator("body")).ToContainTextAsync("logout [Mr. test with email]");

await Page.GetByRole(AriaRole.Link, new() { Name = "logout [Mr. test with email]" }).ClickAsync();
await Page.GetByRole(AriaRole.Button, new() { Name = "Click here to Logout" }).ClickAsync();
await Page.GetByRole(AriaRole.Link, new() { Name = "login" }).ClickAsync();
await Page.GetByRole(AriaRole.Button, new() { Name = "OpenIdConnect" }).ClickAsync();

await logout();
await loginWithExternalProvider();
await Expect(Page.Locator("body")).ToContainTextAsync("logout [Mr. test with email]");
}

[Test]
public async Task CreateUserUsingExternalProvider_OnlyUsername()
{
await Page.GotoAsync($"{RazorBaseUrl}/Identity/Account/Register");
await Page.GetByRole(AriaRole.Button, new() { Name = "OpenIdConnect" }).ClickAsync();
await Page.GetByPlaceholder("Username").ClickAsync();
await Page.GetByPlaceholder("Username").FillAsync("TestWithUserName");
await Page.GetByPlaceholder("Password").ClickAsync();
await Page.GetByPlaceholder("Password").FillAsync("password");
await Page.GetByRole(AriaRole.Button, new() { Name = "Login" }).ClickAsync();
await Page.WaitForURLAsync($"{RazorBaseUrl}/**");
await registerViaExternalProvider("TestWithUserName", "password");

await Expect(Page.Locator("label")).ToContainTextAsync("Email");
await Page.GetByPlaceholder("Please enter your email.").ClickAsync();
await Page.GetByPlaceholder("Please enter your email.").FillAsync("Test@withUsername.com");
await Page.GetByRole(AriaRole.Button, new() { Name = "Register" }).ClickAsync();
await Page.GetByRole(AriaRole.Link, new() { Name = "Click here to confirm your" }).ClickAsync();
await Expect(Page.GetByText("Thank you for confirming your")).ToBeVisibleAsync();

await Page.GetByRole(AriaRole.Link, new() { Name = "login" }).ClickAsync();
await Page.GetByRole(AriaRole.Button, new() { Name = "OpenIdConnect" }).ClickAsync();

await loginWithExternalProvider();
await Expect(Page.Locator("body")).ToContainTextAsync("logout [mr. Test with username]");

}

[Test]
public async Task CreateUserUsingExternalProvider_NoInfo()
{
await Page.GotoAsync($"{RazorBaseUrl}/Identity/Account/Register");
await Page.GetByRole(AriaRole.Button, new() { Name = "OpenIdConnect" }).ClickAsync();
await Page.GetByPlaceholder("Username").ClickAsync();
await Page.GetByPlaceholder("Username").FillAsync("TestWithNoInfo");
await Page.GetByPlaceholder("Password").ClickAsync();
await Page.GetByPlaceholder("Password").FillAsync("password");
await Page.GetByRole(AriaRole.Button, new() { Name = "Login" }).ClickAsync();
await Page.WaitForURLAsync($"{RazorBaseUrl}/**");
await registerViaExternalProvider("TestWithNoInfo", "password");

await Expect(Page.Locator("form")).ToContainTextAsync("Email");
await Expect(Page.Locator("form")).ToContainTextAsync("Username");
@@ -98,9 +90,8 @@ public async Task CreateUserUsingExternalProvider_NoInfo()
await Page.GetByRole(AriaRole.Button, new() { Name = "Register" }).ClickAsync();
await Page.GetByRole(AriaRole.Link, new() { Name = "Click here to confirm your" }).ClickAsync();
await Expect(Page.GetByText("Thank you for confirming your")).ToBeVisibleAsync();

await Page.GetByRole(AriaRole.Link, new() { Name = "login" }).ClickAsync();
await Page.GetByRole(AriaRole.Button, new() { Name = "OpenIdConnect" }).ClickAsync();

await loginWithExternalProvider();
await Expect(Page.Locator("body")).ToContainTextAsync("logout [mr. test with no info]");
}
}
Loading

0 comments on commit 24cae22

Please sign in to comment.