generated from freeCodeCamp/template
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: improve and cleanup invitation tests (#368)
Co-authored-by: yoko <[email protected]>
- Loading branch information
1 parent
d5942fb
commit 51efef9
Showing
4 changed files
with
152 additions
and
93 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { Page, Locator } from "@playwright/test"; | ||
|
||
export class UsersPage { | ||
private readonly activeUsers: Locator; | ||
private readonly invitedUsers: Locator; | ||
|
||
constructor(public readonly page: Page) { | ||
this.activeUsers = page.locator('[data-testid="active-user"]'); | ||
this.invitedUsers = page.locator('[data-testid="invited-user"]'); | ||
} | ||
|
||
async getInvitedUser(email: string) { | ||
return this.invitedUsers.filter({ hasText: email }); | ||
} | ||
|
||
async getActiveUser(email: string) { | ||
return this.activeUsers.filter({ hasText: email }); | ||
} | ||
|
||
async inviteUser(email: string) { | ||
await this.page.getByRole("button", { name: "Invite user" }).click(); | ||
await this.page.getByLabel("Email*").click(); | ||
await this.page.getByLabel("Email*").fill(email); | ||
await this.page.getByRole("button", { name: "Send invitation" }).click(); | ||
} | ||
|
||
async revokeUser(email: string) { | ||
const revokeButton = (await this.getInvitedUser(email)).getByRole( | ||
"button", | ||
{ name: "Revoke" } | ||
); | ||
await revokeButton.click(); | ||
} | ||
|
||
async goto() { | ||
await this.page.goto("/users"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { test, expect } from "@playwright/test"; | ||
|
||
import { UsersPage } from "./pages/users"; | ||
import { useCredentialsForAuth, signIn, deleteUser } from "./helpers/user"; | ||
|
||
const NEW_USER_CREDENTIALS = { | ||
identifier: "[email protected]", | ||
password: "password", | ||
}; | ||
|
||
test.describe("inviting a user", () => { | ||
let usersPage: UsersPage; | ||
|
||
test.beforeEach(async ({ browser }) => { | ||
// To avoid using the invitee's credentials, we have to create a new context | ||
const editorContext = await browser.newContext({ | ||
storageState: "playwright/.auth/editor.json", | ||
}); | ||
usersPage = new UsersPage(await editorContext.newPage()); | ||
await usersPage.goto(); | ||
}); | ||
|
||
test.afterEach(async ({ request }) => { | ||
// Delete the user if it exists | ||
await deleteUser(request, NEW_USER_CREDENTIALS); | ||
}); | ||
|
||
test("invitations can be created and revoked", async () => { | ||
await usersPage.inviteUser(NEW_USER_CREDENTIALS.identifier); | ||
await expect( | ||
await usersPage.getInvitedUser(NEW_USER_CREDENTIALS.identifier) | ||
).toBeVisible(); | ||
|
||
await usersPage.revokeUser(NEW_USER_CREDENTIALS.identifier); | ||
await expect( | ||
await usersPage.getInvitedUser(NEW_USER_CREDENTIALS.identifier) | ||
).toBeHidden(); | ||
}); | ||
|
||
test("invited users become active by signing in", async ({ | ||
page, | ||
request, | ||
}) => { | ||
await usersPage.inviteUser(NEW_USER_CREDENTIALS.identifier); | ||
|
||
// Allow the user to sign in with email/password, not Auth0. | ||
await useCredentialsForAuth(request, NEW_USER_CREDENTIALS); | ||
|
||
await signIn(page, NEW_USER_CREDENTIALS); | ||
|
||
// After signing in, the invited user should be in the active list | ||
await usersPage.page.reload(); | ||
await expect( | ||
await usersPage.getActiveUser(NEW_USER_CREDENTIALS.identifier) | ||
).toBeVisible(); | ||
}); | ||
}); |