Files
privacy.sexy/tests/unit/presentation/bootstrapping/ClientLoggerFactory.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

160 lines
5.3 KiB
TypeScript

// eslint-disable-next-line max-classes-per-file
import { describe, it } from 'vitest';
import { RuntimeEnvironment } from '@/infrastructure/RuntimeEnvironment/RuntimeEnvironment';
import { ClientLoggerFactory, LoggerCreationFunction, WindowAccessor } from '@/presentation/bootstrapping/ClientLoggerFactory';
import { Logger } from '@/application/Common/Log/Logger';
import { RuntimeEnvironmentStub } from '@tests/unit/shared/Stubs/RuntimeEnvironmentStub';
import { itIsSingleton } from '@tests/unit/shared/TestCases/SingletonTests';
import { LoggerStub } from '@tests/unit/shared/Stubs/LoggerStub';
describe('ClientLoggerFactory', () => {
describe('Current', () => {
describe('singleton behavior', () => {
itIsSingleton({
getter: () => ClientLoggerFactory.Current,
expectedType: ClientLoggerFactory,
});
});
});
describe('logger instantiation based on environment', () => {
const testCases: Array<{
readonly description: string,
readonly build: (
builder: ClientLoggerFactoryBuilder,
expectedLogger: Logger,
) => ClientLoggerFactory,
}> = [
{
description: 'production desktop environment',
build: (b, expectedLogger) => b
.withWindowInjectedLoggerFactory(() => expectedLogger)
.withEnvironment(new RuntimeEnvironmentStub()
.withIsDesktop(true)
.withIsNonProduction(false))
.withWindowAccessor(() => createWindowWithLogger())
.build(),
},
{
description: 'non-production desktop environment',
build: (b, expectedLogger) => b
.withWindowInjectedLoggerFactory(() => expectedLogger)
.withEnvironment(new RuntimeEnvironmentStub()
.withIsDesktop(true)
.withIsNonProduction(true))
.withWindowAccessor(() => createWindowWithLogger())
.build(),
},
{
description: 'non-production web environment',
build: (b, expectedLogger) => b
.withConsoleLoggerFactory(() => expectedLogger)
.withEnvironment(new RuntimeEnvironmentStub()
.withIsDesktop(false)
.withIsNonProduction(true))
.withWindowAccessor(() => createWindowWithLogger())
.build(),
},
{
description: 'production web environment',
build: (b, expectedLogger) => b
.withNoopLoggerFactory(() => expectedLogger)
.withEnvironment(new RuntimeEnvironmentStub()
.withIsDesktop(false)
.withIsNonProduction(false))
.withWindowAccessor(() => createWindowWithLogger())
.build(),
},
{
description: 'unit/integration tests environment',
build: (b, expectedLogger) => b
.withNoopLoggerFactory(() => expectedLogger)
.withEnvironment(new RuntimeEnvironmentStub().withIsDesktop(true))
.withWindowAccessor(() => createWindowWithLogger(null))
.build(),
},
];
testCases.forEach(({ description, build }) => {
it(`creates correct logger for ${description}`, () => {
// arrange
const expectedLogger = new LoggerStub();
const factory = build(new ClientLoggerFactoryBuilder(), expectedLogger);
// act
const { logger } = factory;
// assert
expect(logger).to.equal(expectedLogger);
});
});
});
});
function createWindowWithLogger(logger: Logger | null = new LoggerStub()): Window {
return {
log: logger,
} as unknown as Window;
}
class ClientLoggerFactoryBuilder {
private environment: RuntimeEnvironment = new RuntimeEnvironmentStub();
private windowAccessor: WindowAccessor = () => ({} as Window);
private noopLoggerFactory: LoggerCreationFunction = () => new LoggerStub();
private windowInjectedLoggerFactory: LoggerCreationFunction = () => new LoggerStub();
private consoleLoggerFactory: LoggerCreationFunction = () => new LoggerStub();
public withWindowAccessor(windowAccessor: WindowAccessor): this {
this.windowAccessor = windowAccessor;
return this;
}
public withEnvironment(environment: RuntimeEnvironment): this {
this.environment = environment;
return this;
}
public withNoopLoggerFactory(factory: LoggerCreationFunction): this {
this.noopLoggerFactory = factory;
return this;
}
public withWindowInjectedLoggerFactory(factory: LoggerCreationFunction): this {
this.windowInjectedLoggerFactory = factory;
return this;
}
public withConsoleLoggerFactory(factory: LoggerCreationFunction): this {
this.consoleLoggerFactory = factory;
return this;
}
public build(): ClientLoggerFactory {
return new TestableClientLoggerFactory(
this.environment,
this.windowAccessor,
this.noopLoggerFactory,
this.windowInjectedLoggerFactory,
this.consoleLoggerFactory,
);
}
}
class TestableClientLoggerFactory extends ClientLoggerFactory {
public constructor(
environment: RuntimeEnvironment,
windowAccessor: WindowAccessor,
noopLoggerFactory: LoggerCreationFunction,
windowInjectedLoggerFactory: LoggerCreationFunction,
consoleLoggerFactory: LoggerCreationFunction,
) {
super(
environment,
windowAccessor,
noopLoggerFactory,
windowInjectedLoggerFactory,
consoleLoggerFactory,
);
}
}