Skip to content

Commit

Permalink
refactor: made remoteUsername a required named parameter in SshnpdCha…
Browse files Browse the repository at this point in the history
…nnel.resolveTunnelUsername

style: ran dart format
test: added unit tests of SshnpdChannel for tunnelUsername supplied and tunnelUsername not supplied
  • Loading branch information
gkc committed Nov 22, 2023
1 parent b5e67b6 commit 63c05d7
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 23 deletions.
23 changes: 12 additions & 11 deletions packages/noports_core/lib/src/sshnp/models/sshnp_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ class SshnpParams {
final SupportedSshAlgorithm sshAlgorithm;

/// Special Arguments
final String?
profileName; // automatically populated with the filename if from a configFile
/// automatically populated with the filename if from a configFile
final String? profileName;

/// Operation flags
final bool listDevices;
Expand Down Expand Up @@ -96,7 +97,7 @@ class SshnpParams {
atKeysFilePath: params2.atKeysFilePath ?? params1.atKeysFilePath,
identityFile: params2.identityFile ?? params1.identityFile,
identityPassphrase:
params2.identityPassphrase ?? params1.identityPassphrase,
params2.identityPassphrase ?? params1.identityPassphrase,
sendSshPublicKey: params2.sendSshPublicKey ?? params1.sendSshPublicKey,
localSshOptions: params2.localSshOptions ?? params1.localSshOptions,
remoteUsername: params2.remoteUsername ?? params1.remoteUsername,
Expand All @@ -109,7 +110,7 @@ class SshnpParams {
remoteSshdPort: params2.remoteSshdPort ?? params1.remoteSshdPort,
idleTimeout: params2.idleTimeout ?? params1.idleTimeout,
addForwardsToTunnel:
params2.addForwardsToTunnel ?? params1.addForwardsToTunnel,
params2.addForwardsToTunnel ?? params1.addForwardsToTunnel,
sshClient: params2.sshClient ?? params1.sshClient,
sshAlgorithm: params2.sshAlgorithm ?? params1.sshAlgorithm,
);
Expand Down Expand Up @@ -137,9 +138,9 @@ class SshnpParams {
identityFile: partial.identityFile,
identityPassphrase: partial.identityPassphrase,
sendSshPublicKey:
partial.sendSshPublicKey ?? DefaultSshnpArgs.sendSshPublicKey,
partial.sendSshPublicKey ?? DefaultSshnpArgs.sendSshPublicKey,
localSshOptions:
partial.localSshOptions ?? DefaultSshnpArgs.localSshOptions,
partial.localSshOptions ?? DefaultSshnpArgs.localSshOptions,
verbose: partial.verbose ?? DefaultArgs.verbose,
remoteUsername: partial.remoteUsername,
tunnelUsername: partial.tunnelUsername,
Expand All @@ -151,7 +152,7 @@ class SshnpParams {
remoteSshdPort: partial.remoteSshdPort ?? DefaultArgs.remoteSshdPort,
idleTimeout: partial.idleTimeout ?? DefaultArgs.idleTimeout,
addForwardsToTunnel:
partial.addForwardsToTunnel ?? DefaultArgs.addForwardsToTunnel,
partial.addForwardsToTunnel ?? DefaultArgs.addForwardsToTunnel,
sshClient: partial.sshClient ?? DefaultSshnpArgs.sshClient,
sshAlgorithm: partial.sshAlgorithm ?? DefaultArgs.sshAlgorithm,
);
Expand Down Expand Up @@ -205,7 +206,7 @@ class SshnpParams {
SshnpArg.sshAlgorithmArg.name: sshAlgorithm.toString(),
};
args.removeWhere(
(key, value) => !parserType.shouldParse(SshnpArg.fromName(key).parseWhen),
(key, value) => !parserType.shouldParse(SshnpArg.fromName(key).parseWhen),
);
return args;
}
Expand Down Expand Up @@ -294,7 +295,7 @@ class SshnpPartialParams {
atKeysFilePath: params2.atKeysFilePath ?? params1.atKeysFilePath,
identityFile: params2.identityFile ?? params1.identityFile,
identityPassphrase:
params2.identityPassphrase ?? params1.identityPassphrase,
params2.identityPassphrase ?? params1.identityPassphrase,
sendSshPublicKey: params2.sendSshPublicKey ?? params1.sendSshPublicKey,
localSshOptions: params2.localSshOptions ?? params1.localSshOptions,
remoteUsername: params2.remoteUsername ?? params1.remoteUsername,
Expand All @@ -307,7 +308,7 @@ class SshnpPartialParams {
remoteSshdPort: params2.remoteSshdPort ?? params1.remoteSshdPort,
idleTimeout: params2.idleTimeout ?? params1.idleTimeout,
addForwardsToTunnel:
params2.addForwardsToTunnel ?? params1.addForwardsToTunnel,
params2.addForwardsToTunnel ?? params1.addForwardsToTunnel,
sshClient: params2.sshClient ?? params1.sshClient,
sshAlgorithm: params2.sshAlgorithm ?? params1.sshAlgorithm,
);
Expand Down Expand Up @@ -362,7 +363,7 @@ class SshnpPartialParams {
sshAlgorithm: args[SshnpArg.sshAlgorithmArg.name] == null
? null
: SupportedSshAlgorithm.fromString(
args[SshnpArg.sshAlgorithmArg.name]),
args[SshnpArg.sshAlgorithmArg.name]),
);
}

Expand Down
3 changes: 2 additions & 1 deletion packages/noports_core/lib/src/sshnp/sshnp_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ abstract class SshnpCore
remoteUsername = await sshnpdChannel.resolveRemoteUsername();

/// Set the username to use for the initial ssh tunnel
tunnelUsername = await sshnpdChannel.resolveTunnelUsername(remoteUsername);
tunnelUsername = await sshnpdChannel.resolveTunnelUsername(
remoteUsername: remoteUsername);

/// Shares the public key if required
await sshnpdChannel.sharePublicKeyIfRequired(identityKeyPair);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ abstract class SshnpdChannel with AsyncInitialization, AtClientBindings {
/// Resolve the username to use in the initial ssh tunnel
/// If [params.tunnelUsername] is set, it will be used.
/// Otherwise, the username will be set to [remoteUsername]
Future<String?> resolveTunnelUsername(String? remoteUsername) async {
Future<String?> resolveTunnelUsername(
{required String? remoteUsername}) async {
if (params.tunnelUsername != null) {
return params.tunnelUsername!;
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/noports_core/lib/sshrv.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
library noports_core_sshrv;

export 'src/sshrv/sshrv.dart';
export 'src/sshrv/sshrv.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ void main() {
test('ConfigFileRepository.atKeyFromProfileName test', () async {
String profileName = 'myProfileName';

expect(ConfigFileRepository.getDefaultSshnpConfigDirectory(getHomeDirectory()!), isA<String>());
expect(ConfigFileRepository.fromProfileName(profileName), isA<Future<String>>());
expect(
ConfigFileRepository.getDefaultSshnpConfigDirectory(
getHomeDirectory()!),
isA<String>());
expect(ConfigFileRepository.fromProfileName(profileName),
isA<Future<String>>());
expect(ConfigFileRepository.fromProfileName(profileName), completes);
expect(
await ConfigFileRepository.fromProfileName(profileName, basenameOnly: false),
equals(path.join(getHomeDirectory()!, '.sshnp', 'config', '$profileName.env')),
await ConfigFileRepository.fromProfileName(profileName,
basenameOnly: false),
equals(path.join(
getHomeDirectory()!, '.sshnp', 'config', '$profileName.env')),
);
expect(await ConfigFileRepository.fromProfileName(profileName, basenameOnly: true), equals('$profileName.env'));
expect(
await ConfigFileRepository.fromProfileName(profileName,
basenameOnly: true),
equals('$profileName.env'));
});

group('[depends on ConfigFileRepository.atKeyFromProfileName]', () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,8 @@ void main() {
expect(argMap[SshnpArg.localSshOptionsArg.name],
equals(['-L 127.0.01:8080:127.0.0.1:80']));
expect(argMap[SshnpArg.remoteUserNameArg.name], equals('myUsername'));
expect(argMap[SshnpArg.tunnelUserNameArg.name], equals('myTunnelUsername'));
expect(argMap[SshnpArg.tunnelUserNameArg.name],
equals('myTunnelUsername'));
expect(argMap[SshnpArg.verboseArg.name], equals(true));
expect(argMap[SshnpArg.rootDomainArg.name], equals('root.atsign.wtf'));
expect(argMap[SshnpArg.localSshdPortArg.name], equals(4567));
Expand Down
42 changes: 41 additions & 1 deletion packages/noports_core/test/sshnp/sshnp_core_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ void main() {
.thenAnswer((_) async {});
when(() => mockSshnpdChannel.resolveRemoteUsername())
.thenAnswer((_) async => 'myRemoteUsername');
when(() => mockSshnpdChannel.resolveTunnelUsername(any()))
when(() => mockSshnpdChannel.resolveTunnelUsername(
remoteUsername: any(named: 'remoteUsername')))
.thenAnswer((_) async => 'myTunnelUsername');
when(() => mockSshnpdChannel.sharePublicKeyIfRequired(
identityKeyPair ?? any())).thenAnswer((_) async {});
Expand Down Expand Up @@ -131,6 +132,8 @@ void main() {
() => stubbedInitialize(),
() => mockSshnpdChannel.callInitialization(),
() => mockSshnpdChannel.resolveRemoteUsername(),
() => mockSshnpdChannel.resolveTunnelUsername(
remoteUsername: 'myRemoteUsername'),
() => mockSshnpdChannel
.sharePublicKeyIfRequired(sshnpCore.identityKeyPair),
() => mockSshrvdChannel.callInitialization(),
Expand All @@ -142,6 +145,8 @@ void main() {
verifyNever(() => stubbedInitialize());
verifyNever(() => mockSshnpdChannel.callInitialization());
verifyNever(() => mockSshnpdChannel.resolveRemoteUsername());
verifyNever(() => mockSshnpdChannel.resolveTunnelUsername(
remoteUsername: 'myRemoteUsername'));
verifyNever(() => mockSshnpdChannel
.sharePublicKeyIfRequired(sshnpCore.identityKeyPair));
verifyNever(() => mockSshrvdChannel.callInitialization());
Expand All @@ -155,6 +160,41 @@ void main() {
verifyNever(() => stubbedCompleteInitialization());
verifyNever(() => mockSshrvdChannel.callInitialization());
});
test('tunnelUsername not supplied', () async {
final params = SshnpParams(
clientAtSign: '@client',
sshnpdAtSign: '@daemon',
host: 'foo.bar.test',
remoteUsername: 'alice');
final channel = SshnpdDefaultChannel(
atClient: mockAtClient,
params: params,
sessionId: 'test_tunnelUsername_not_supplied',
namespace: 'test');
final remoteUsername = await channel.resolveRemoteUsername();
expect(remoteUsername, 'alice');
expect(
await channel.resolveTunnelUsername(remoteUsername: remoteUsername),
'alice');
});
test('tunnelUsername supplied', () async {
final params = SshnpParams(
clientAtSign: '@client',
sshnpdAtSign: '@daemon',
host: 'foo.bar.test',
remoteUsername: 'alice',
tunnelUsername: 'bob');
final channel = SshnpdDefaultChannel(
atClient: mockAtClient,
params: params,
sessionId: 'test_tunnelUsername_supplied',
namespace: 'test');
final remoteUsername = await channel.resolveRemoteUsername();
expect(remoteUsername, 'alice');
expect(
await channel.resolveTunnelUsername(remoteUsername: remoteUsername),
'bob');
});
}); // group Initialization
}); // group SshnpCore
}
1 change: 0 additions & 1 deletion packages/noports_core/test/sshnp/sshnp_mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,3 @@ abstract class StartProcessCaller {
}

class StartProcessStub extends Mock implements StartProcessCaller {}

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ void main() {
.thenAnswer((_) async {});
when(() => mockSshnpdChannel.resolveRemoteUsername())
.thenAnswer((_) async => 'myRemoteUsername');
when(() => mockSshnpdChannel.resolveTunnelUsername(any()))
when(() => mockSshnpdChannel.resolveTunnelUsername(
remoteUsername: any(named: 'remoteUsername')))
.thenAnswer((_) async => 'myTunnelUsername');
when(() => mockSshnpdChannel.sharePublicKeyIfRequired(identityKeyPair))
.thenAnswer((_) async {});
Expand Down

0 comments on commit 63c05d7

Please sign in to comment.