Files
privacy.sexy/tests/unit/infrastructure/RuntimeEnvironment/RuntimeEnvironmentFactory.spec.ts
undergroundwires b404a91ada Fix invisible script execution on Windows #264
This commit addresses an issue in the privacy.sexy desktop application
where scripts executed as administrator on Windows were running in the
background. This was observed in environments like Windows Pro VMs on
Azure, where operations typically run with administrative privileges.

Previously, the application used the `"$path"` shell command to execute
scripts. This mechanism failed to activate the logic for requesting
admin privileges if the app itself was running as an administrator.
To resolve this, the script execution process has been modified to
explicitly ask for administrator privileges using the `VerbAs` method.
This ensures that the script always runs in a new `cmd.exe` window,
enhancing visibility and user interaction.

Other supporting changes:

- Rename the generated script file from `run-{timestamp}-{extension}` er
  to `{timestamp}-privacy-script-{extension}` for clearer identification
  and better file sorting.
- Refactor `ScriptFileCreator` to parameterize file extension and
  script name.
- Rename `OsTimestampedFilenameGenerator` to
  `TimestampedFilenameGenerator` to better reflect its new and more
  scoped functionality after refactoring mentioned abvoe.
- Remove `setAppName()` due to ineffective behavior in Windows.
- Update `SECURITY.md` to highlight that the app doesn't require admin
  rights for standard operations.
- Add `.editorconfig` settings for PowerShell scripts.
- Add a integration test for script execution logic. Improve environment
  detection for more reliable test execution.
- Disable application logging during unit/integration tests to keep test
  outputs clean and focused.
2024-01-09 20:44:06 +01:00

137 lines
4.8 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import {
BrowserRuntimeEnvironmentFactory, NodeRuntimeEnvironmentFactory,
GlobalAccessor as GlobalPropertiesAccessor, determineAndCreateRuntimeEnvironment,
} from '@/infrastructure/RuntimeEnvironment/RuntimeEnvironmentFactory';
import { RuntimeEnvironmentStub } from '@tests/unit/shared/Stubs/RuntimeEnvironmentStub';
describe('RuntimeEnvironmentFactory', () => {
describe('determineAndCreateRuntimeEnvironment', () => {
it('uses browser environment when window exists', () => {
// arrange
const expectedEnvironment = new RuntimeEnvironmentStub();
const context = new RuntimeEnvironmentFactoryTestSetup()
.withWindowAccessor(() => createWindowStub())
.withBrowserEnvironmentFactory(() => expectedEnvironment);
// act
const actualEnvironment = context.buildEnvironment();
// assert
expect(actualEnvironment).to.equal(expectedEnvironment);
});
it('passes correct window to browser environment', () => {
// arrange
const expectedWindow = createWindowStub();
let actualWindow: Window | undefined;
const browserEnvironmentFactoryMock: BrowserRuntimeEnvironmentFactory = (providedWindow) => {
actualWindow = providedWindow;
return new RuntimeEnvironmentStub();
};
const context = new RuntimeEnvironmentFactoryTestSetup()
.withWindowAccessor(() => expectedWindow)
.withBrowserEnvironmentFactory(browserEnvironmentFactoryMock);
// act
context.buildEnvironment();
// assert
expect(actualWindow).to.equal(expectedWindow);
});
it('uses node environment when window is absent', () => {
// arrange
const expectedEnvironment = new RuntimeEnvironmentStub();
const context = new RuntimeEnvironmentFactoryTestSetup()
.withWindowAccessor(() => undefined)
.withProcessAccessor(() => createProcessStub())
.withNodeEnvironmentFactory(() => expectedEnvironment);
// act
const actualEnvironment = context.buildEnvironment();
// assert
expect(actualEnvironment).to.equal(expectedEnvironment);
});
it('uses node environment when window is present too', () => { // This allows running integration tests
// arrange
const expectedEnvironment = new RuntimeEnvironmentStub();
const context = new RuntimeEnvironmentFactoryTestSetup()
.withWindowAccessor(() => createWindowStub())
.withProcessAccessor(() => createProcessStub())
.withNodeEnvironmentFactory(() => expectedEnvironment);
// act
const actualEnvironment = context.buildEnvironment();
// assert
expect(actualEnvironment).to.equal(expectedEnvironment);
});
it('throws if both node and window are missing', () => {
// arrange
const expectedError = 'Unsupported runtime environment: The current context is neither a recognized browser nor a Node.js environment.';
const context = new RuntimeEnvironmentFactoryTestSetup()
.withWindowAccessor(() => undefined)
.withProcessAccessor(() => undefined);
// act
const act = () => context.buildEnvironment();
// assert
expect(act).to.throw(expectedError);
});
});
});
function createWindowStub(): Window {
return {} as Window;
}
function createProcessStub(): NodeJS.Process {
return {} as NodeJS.Process;
}
type WindowAccessor = GlobalPropertiesAccessor['getGlobalWindow'];
type ProcessAccessor = GlobalPropertiesAccessor['getGlobalProcess'];
export class RuntimeEnvironmentFactoryTestSetup {
private windowAccessor: WindowAccessor = () => undefined;
private processAccessor: ProcessAccessor = () => undefined;
private browserEnvironmentFactory
: BrowserRuntimeEnvironmentFactory = () => new RuntimeEnvironmentStub();
private nodeEnvironmentFactory
: NodeRuntimeEnvironmentFactory = () => new RuntimeEnvironmentStub();
public withWindowAccessor(windowAccessor: WindowAccessor): this {
this.windowAccessor = windowAccessor;
return this;
}
public withProcessAccessor(processAccessor: ProcessAccessor): this {
this.processAccessor = processAccessor;
return this;
}
public withNodeEnvironmentFactory(
nodeEnvironmentFactory: NodeRuntimeEnvironmentFactory,
): this {
this.nodeEnvironmentFactory = nodeEnvironmentFactory;
return this;
}
public withBrowserEnvironmentFactory(
browserEnvironmentFactory: BrowserRuntimeEnvironmentFactory,
): this {
this.browserEnvironmentFactory = browserEnvironmentFactory;
return this;
}
public buildEnvironment(): ReturnType<typeof determineAndCreateRuntimeEnvironment> {
return determineAndCreateRuntimeEnvironment(
{
getGlobalProcess: this.processAccessor,
getGlobalWindow: this.windowAccessor,
},
this.browserEnvironmentFactory,
this.nodeEnvironmentFactory,
);
}
}