Skip to content

Commit

Permalink
Organization invitation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Timshel committed Aug 20, 2024
1 parent 175e395 commit db23c12
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 63 deletions.
18 changes: 18 additions & 0 deletions playwright/global-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ const { spawn } = require('node:child_process');
function loadEnv(){
var myEnv = dotenv.config({ path: 'test.env' });
dotenvExpand.expand(myEnv);

return {
user1: {
email: process.env.TEST_USER_MAIL,
name: process.env.TEST_USER_MAIL,
password: process.env.TEST_USER_PASSWORD,
},
user2: {
email: process.env.TEST_USER2_MAIL,
name: process.env.TEST_USER2_MAIL,
password: process.env.TEST_USER2_PASSWORD,
},
user3: {
email: process.env.TEST_USER3_MAIL,
name: process.env.TEST_USER3_MAIL,
password: process.env.TEST_USER3_PASSWORD,
},
}
}

async function waitFor(url: String, browser: Browser) {
Expand Down
12 changes: 10 additions & 2 deletions playwright/test.env
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ MAILDEV_HOST=127.0.0.1
# Test user #
#############
TEST_USER=test
TEST_USER_PASSWORD=${TEST_USER}
TEST_USER_MAIL="${TEST_USER}@example.com"
TEST_USER_PASSWORD=Master Password
TEST_USER_MAIL=${TEST_USER}@example.com

TEST_USER2=test2
TEST_USER2_PASSWORD=Master Password
TEST_USER2_MAIL=${TEST_USER2}@example.com

TEST_USER3=test3
TEST_USER3_PASSWORD=Master Password
TEST_USER3_MAIL=${TEST_USER3}@example.com

######################
# Vaultwarden Config #
Expand Down
40 changes: 9 additions & 31 deletions playwright/tests/login.smtp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { test, expect, type TestInfo } from '@playwright/test';
import { MailDev } from 'maildev';

const utils = require('../global-utils');
const Stream = require('stream')
import { createAccount, logUser } from './setups/user';

utils.loadEnv();
let users = utils.loadEnv();

var mailserver;
let mailserver;

test.beforeAll('Setup', async ({ browser }, testInfo: TestInfo) => {
mailserver = new MailDev({
Expand All @@ -29,32 +29,21 @@ test.afterAll('Teardown', async ({}) => {
});

test('Account creation', async ({ page }) => {
const emails = mailserver.iterator(process.env.TEST_USER_MAIL);
const emails = mailserver.iterator(users.user1.email);

// Landing page
await page.goto('/');
await page.getByRole('link', { name: 'Create account' }).click();

// Back to Vault create account
await expect(page).toHaveTitle(/Create account | Vaultwarden Web/);
await page.getByLabel(/Email address/).fill(process.env.TEST_USER_MAIL);
await page.getByLabel('Name').fill(process.env.TEST_USER);
await page.getByLabel('Master password\n (required)', { exact: true }).fill('Master password');
await page.getByLabel('Re-type master password').fill('Master password');
await page.getByRole('button', { name: 'Create account' }).click();
await createAccount(test, page, users.user1);

const { value: created } = await emails.next();
expect(created.subject).toBe("Welcome");
expect(created.to[0]?.address).toBe(process.env.TEST_USER_MAIL);
expect(created.from[0]?.address).toBe(process.env.VAULTWARDEN_SMTP_FROM);

// Back to the login page
await expect(page).toHaveTitle('Vaultwarden Web');
await page.getByLabel('Your new account has been created')
await page.getByTestId("toast-message").getByLabel('Your new account has been created')
await page.getByRole('button', { name: 'Continue' }).click();

// Unlock page
await page.getByLabel('Master password').fill('Master password');
await page.getByLabel('Master password').fill(users.user1.password);
await page.getByRole('button', { name: 'Log in with master password' }).click();

// We are now in the default vault page
Expand All @@ -69,23 +58,12 @@ test('Account creation', async ({ page }) => {
});

test('Master password login', async ({ page }) => {
const emails = mailserver.iterator("new");

// Landing page
await page.goto('/');
await page.getByLabel(/Email address/).fill(process.env.TEST_USER_MAIL);
await page.getByRole('button', { name: 'Continue' }).click();
const emails = mailserver.iterator(users.user1.email);

// Unlock page
await page.getByLabel('Master password').fill('Master password');
await page.getByRole('button', { name: 'Log in with master password' }).click();

// We are now in the default vault page
await expect(page).toHaveTitle(/Vaults/);
await logUser(test, page, users.user1);

const { value: logged } = await emails.next();
expect(logged.subject).toBe("New Device Logged In From Firefox");
expect(logged.to[0]?.address).toBe(process.env.TEST_USER_MAIL);
expect(logged.from[0]?.address).toBe(process.env.VAULTWARDEN_SMTP_FROM);

emails.return();
Expand Down
38 changes: 9 additions & 29 deletions playwright/tests/login.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { test, expect, type TestInfo } from '@playwright/test';
const utils = require('../global-utils');
import { test, expect, type Page, type TestInfo } from '@playwright/test';

utils.loadEnv();
import * as utils from "../global-utils";
import { createAccount, logUser } from './setups/user';

let users = utils.loadEnv();

test.beforeAll('Setup', async ({ browser }, testInfo: TestInfo) => {
await utils.startVaultwarden(browser, testInfo, {});
Expand All @@ -13,40 +15,18 @@ test.afterAll('Teardown', async ({}, testInfo: TestInfo) => {

test('Account creation', async ({ page }) => {
// Landing page
await page.goto('/');
await page.getByRole('link', { name: 'Create account' }).click();

// Back to Vault create account
await expect(page).toHaveTitle(/Create account | Vaultwarden Web/);
await page.getByLabel(/Email address/).fill(process.env.TEST_USER_MAIL);
await page.getByLabel('Name').fill(process.env.TEST_USER);
await page.getByLabel('Master password\n (required)', { exact: true }).fill('Master password');
await page.getByLabel('Re-type master password').fill('Master password');
await page.getByRole('button', { name: 'Create account' }).click();

// Back to the login page
await expect(page).toHaveTitle('Vaultwarden Web');
await page.getByLabel('Your new account has been created')
await createAccount(test, page, users.user1);

await page.getByRole('button', { name: 'Continue' }).click();

// Unlock page
await page.getByLabel('Master password').fill('Master password');
await page.getByLabel('Master password').fill(users.user1.password);
await page.getByRole('button', { name: 'Log in with master password' }).click();

// We are now in the default vault page
await expect(page).toHaveTitle(/Vaults/);
});

test('Master password login', async ({ page }) => {
// Landing page
await page.goto('/');
await page.getByLabel(/Email address/).fill(process.env.TEST_USER_MAIL);
await page.getByRole('button', { name: 'Continue' }).click();

// Unlock page
await page.getByLabel('Master password').fill('Master password');
await page.getByRole('button', { name: 'Log in with master password' }).click();

// We are now in the default vault page
await expect(page).toHaveTitle(/Vaults/);
await logUser(test, page, users.user1);
});
166 changes: 166 additions & 0 deletions playwright/tests/organization.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import { test, expect, type TestInfo } from '@playwright/test';
import { MailDev } from 'maildev';

import * as utils from "../global-utils";
import { createAccount, logUser } from './setups/user';

let users = utils.loadEnv();

let mailserver, user1Mails, user2Mails, user3Mails;

test.beforeAll('Setup', async ({ browser }, testInfo: TestInfo) => {
mailserver = new MailDev({
port: process.env.MAILDEV_PORT
})

await mailserver.listen();

await utils.startVaultwarden(browser, testInfo, {
SMTP_HOST: process.env.MAILDEV_HOST,
SMTP_FROM: process.env.VAULTWARDEN_SMTP_FROM,
});

user1Mails = mailserver.iterator(users.user1.email);
user2Mails = mailserver.iterator(users.user2.email);
user3Mails = mailserver.iterator(users.user3.email);
});

test.afterAll('Teardown', async ({}, testInfo: TestInfo) => {
utils.stopVaultwarden(testInfo);
if( mailserver ){
await mailserver.close();

[user1Mails, user2Mails, user3Mails].map((mails) => {
if(mails){
mails.return();
}
});
}
});

test('Create user3', async ({ page }) => {
await createAccount(test, page, users.user3, user3Mails);
});

test('Invite users', async ({ page }) => {
await createAccount(test, page, users.user1, user1Mails);
await logUser(test, page, users.user1, user1Mails);

await test.step('Create Org', async () => {
await page.getByRole('link', { name: 'New organization' }).click();
await page.getByLabel('Organization name (required)').fill('Test');
await page.getByRole('button', { name: 'Submit' }).click();
await page.locator('div').filter({ hasText: 'Members' }).nth(2).click();
});

await test.step('Invite user2', async () => {
await page.getByRole('button', { name: 'Invite member' }).click();
await page.getByLabel('Email (required)').fill(users.user2.email);
await page.getByRole('tab', { name: 'Collections' }).click();
await page.locator('label').filter({ hasText: 'Grant access to all current' }).click();
await page.getByRole('button', { name: 'Save' }).click();
await page.getByTestId("toast-message").getByLabel('Success User(s) invited')
});

await test.step('Invite user3', async () => {
await page.getByRole('button', { name: 'Invite member' }).click();
await page.getByLabel('Email (required)').fill(users.user3.email);
await page.getByRole('tab', { name: 'Collections' }).click();
await page.locator('label').filter({ hasText: 'Grant access to all current' }).click();
await page.getByRole('button', { name: 'Save' }).click();
await page.getByTestId("toast-message").getByLabel('Success User(s) invited')
});
});

test('invited with new account', async ({ page }) => {
const { value: invited } = await user2Mails.next();
expect(invited.subject).toContain("Join Test")

await test.step('Create account', async () => {
await page.setContent(invited.html);
const link = await page.getByTestId("invite").getAttribute("href");
await page.goto(link);
await expect(page).toHaveTitle(/Create account | Vaultwarden Web/);

await page.getByLabel('Name').fill(users.user2.name);
await page.getByLabel('Master password\n (required)', { exact: true }).fill(users.user2.password);
await page.getByLabel('Re-type master password').fill(users.user2.password);
await page.getByRole('button', { name: 'Create account' }).click();

// Back to the login page
await expect(page).toHaveTitle('Vaultwarden Web');
await page.getByTestId("toast-message").getByLabel('Your new account has been created')

const { value: welcome } = await user2Mails.next();
expect(welcome.subject).toContain("Welcome")
});

await test.step('Login', async () => {
await page.getByLabel(/Email address/).fill(users.user2.email);
await page.getByRole('button', { name: 'Continue' }).click();

// Unlock page
await page.getByLabel('Master password').fill(users.user2.password);
await page.getByRole('button', { name: 'Log in with master password' }).click();

// We are now in the default vault page
await expect(page).toHaveTitle(/Vaults/);
await page.getByTestId("toast-message").getByLabel('Invitation accepted');

const { value: logged } = await user2Mails.next();
expect(logged.subject).toContain("New Device Logged");
});

const { value: accepted } = await user1Mails.next();
expect(accepted.subject).toContain("Invitation to Test accepted")
});

test('invited with existing account', async ({ page }) => {
const { value: invited } = await user3Mails.next();
expect(invited.subject).toContain("Join Test")

await page.setContent(invited.html);
const link = await page.getByTestId("invite").getAttribute("href");

await page.goto(link);

// We should be on login page with email prefilled
await expect(page).toHaveTitle(/Vaultwarden Web/);
await page.getByRole('button', { name: 'Continue' }).click();

// Unlock page
await page.getByLabel('Master password').fill(users.user3.password);
await page.getByRole('button', { name: 'Log in with master password' }).click();

// We are now in the default vault page
await expect(page).toHaveTitle(/Vaults/);
await page.getByTestId("toast-message").getByLabel('Invitation accepted');

const { value: logged } = await user3Mails.next();
expect(logged.subject).toContain("New Device Logged")

const { value: accepted } = await user1Mails.next();
expect(accepted.subject).toContain("Invitation to Test accepted")
});

test('Confirm invited user', async ({ page }) => {
await logUser(test, page, users.user1, user1Mails);
await page.getByLabel('Switch products').click();
await page.getByRole('link', { name: ' Admin Console' }).click();
await page.getByLabel('Members').click();

await test.step('Accept user2', async () => {
await page.getByRole('row', { name: users.user2.name }).getByLabel('Options').click();
await page.getByRole('menuitem', { name: 'Confirm' }).click();
await page.getByRole('button', { name: 'Confirm' }).click();
await page.getByTestId("toast-message").getByLabel('Success');

const { value: logged } = await user2Mails.next();
expect(logged.subject).toContain("Invitation to Test confirmed");
});
});

test('Organization is visible', async ({ page }) => {
await logUser(test, page, users.user2, user2Mails);
await page.getByLabel('vault: Test').click();
});
47 changes: 47 additions & 0 deletions playwright/tests/setups/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { expect, type Browser,Page } from '@playwright/test';

export async function createAccount(test, page: Page, user: { email: string, name: string, password: string }, emails) {
await test.step('Create user', async () => {
// Landing page
await page.goto('/');
await page.getByRole('link', { name: 'Create account' }).click();

// Back to Vault create account
await expect(page).toHaveTitle(/Create account | Vaultwarden Web/);
await page.getByLabel(/Email address/).fill(user.email);
await page.getByLabel('Name').fill(user.name);
await page.getByLabel('Master password\n (required)', { exact: true }).fill(user.password);
await page.getByLabel('Re-type master password').fill(user.password);
await page.getByRole('button', { name: 'Create account' }).click();

// Back to the login page
await expect(page).toHaveTitle('Vaultwarden Web');
await page.getByTestId("toast-message").getByLabel('Your new account has been created');

if( emails ){
const { value: welcome } = await emails.next();
expect(welcome.subject).toContain("Welcome");
}
});
}

export async function logUser(test, page: Page, user: { email: string, password: string }, emails) {
await test.step('Log user', async () => {
// Landing page
await page.goto('/');
await page.getByLabel(/Email address/).fill(user.email);
await page.getByRole('button', { name: 'Continue' }).click();

// Unlock page
await page.getByLabel('Master password').fill(user.password);
await page.getByRole('button', { name: 'Log in with master password' }).click();

// We are now in the default vault page
await expect(page).toHaveTitle(/Vaults/);

if( emails ){
const { value: logged } = await emails.next();
expect(logged.subject).toContain("New Device Logged");
}
});
}
Loading

0 comments on commit db23c12

Please sign in to comment.