Skip to content

Commit

Permalink
Add settings test
Browse files Browse the repository at this point in the history
  • Loading branch information
raksooo committed Sep 9, 2024
1 parent d563285 commit 84c1ba2
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
85 changes: 85 additions & 0 deletions gui/test/e2e/installed/state-dependent/settings.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import path from 'path';
import { execSync } from 'child_process';
import { expect, Page, test } from '@playwright/test';
import { startInstalledApp } from '../installed-utils';
import { fileExists, TestUtils } from '../../utils';

if (process.env.HOME === undefined) {
throw new Error('$HOME not set');
}

const AUTOSTART_PATH = path.join(process.env.HOME, '.config', 'autostart', 'mullvad-vpn.desktop');

let page: Page;
let util: TestUtils;

test.beforeAll(async () => {
({ page, util } = await startInstalledApp());
});

test.afterAll(async () => {
await page.close();
});

test.describe('VPN Settings', () => {
test('Auto-connect setting', async () => {
// Navigate to the VPN settings view
await util.waitForNavigation(async () => await page.click('button[aria-label="Settings"]'));
await util.waitForNavigation(async () => await page.click('text=VPN settings'));

// Find the auto-connect toggle
const autoConnectToggle = page.getByText('Auto-connect').locator('..').getByRole('checkbox');

// Check initial state
const initialCliState = execSync('mullvad auto-connect get').toString().trim();
expect(initialCliState).toMatch(/off$/);
await expect(autoConnectToggle).toHaveAttribute('aria-checked', 'false')

// Toggle auto-connect
await autoConnectToggle.click();

// Verify the setting was applied correctly
await expect(autoConnectToggle).toHaveAttribute('aria-checked', 'true')
const newCliState = execSync('mullvad auto-connect get').toString().trim();
expect(newCliState).toMatch(/off$/);
});

test('Launch on startup setting', async () => {
// Find the auto-connect toggle
const launchOnStartupToggle =
page.getByText('Launch app on start-up').locator('..').getByRole('checkbox');

// Check initial state
const initialCliState = execSync('mullvad auto-connect get').toString().trim();
expect(initialCliState).toMatch(/off$/);
await expect(launchOnStartupToggle).toHaveAttribute('aria-checked', 'false')
expect(fileExists(AUTOSTART_PATH)).toBeFalsy();

// Toggle auto-connect
await launchOnStartupToggle.click();

// Verify the setting was applied correctly
await expect(launchOnStartupToggle).toHaveAttribute('aria-checked', 'true')
expect(fileExists(AUTOSTART_PATH)).toBeTruthy();
const newCliState = execSync('mullvad auto-connect get').toString().trim();
expect(newCliState).toMatch(/on$/);
});

test('LAN settings', async () => {
// Find the LAN toggle
const lanToggle = page.getByText('Local network sharing').locator('..').getByRole('checkbox');

// Check initial state
const initialCliState = execSync('mullvad lan get').toString().trim();
expect(initialCliState).toMatch(/block$/);
await expect(lanToggle).toHaveAttribute('aria-checked', 'false')

// Toggle LAN setting
await lanToggle.click();

// Verify the setting was applied correctly
await expect(lanToggle).toHaveAttribute('aria-checked', 'true')
const newState = execSync('mullvad lan get').toString().trim();
expect(newState).toMatch(/allow$/);
});
});
11 changes: 11 additions & 0 deletions gui/test/e2e/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs';
import { Locator, Page, _electron as electron, ElectronApplication } from 'playwright';

export interface StartAppResponse {
Expand Down Expand Up @@ -127,3 +128,13 @@ export function anyOf(...values: string[]): RegExp {
export function escapeRegExp(regexp: string): string {
return regexp.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}


export function fileExists(filePath: string): boolean {
try {
fs.accessSync(filePath);
return true;
} catch (e) {
return false;
}
}
8 changes: 8 additions & 0 deletions test/test-manager/src/tests/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,11 @@ pub async fn test_import_settings_ui(_: TestContext, rpc: ServiceClient) -> Resu
assert!(ui_result.success());
Ok(())
}

/// Test settings in the GUI
#[test_function]
pub async fn test_settings_ui(_: TestContext, rpc: ServiceClient) -> Result<(), Error> {
let ui_result = run_test(&rpc, &["settings.spec"]).await?;
assert!(ui_result.success());
Ok(())
}

0 comments on commit 84c1ba2

Please sign in to comment.