Skip to content

Commit

Permalink
fix: Fix unit tests, change function parameter from sftp username to …
Browse files Browse the repository at this point in the history
…panel username
  • Loading branch information
MinerPL committed Feb 4, 2025
1 parent f5523e6 commit 9ef9ea3
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
6 changes: 2 additions & 4 deletions app/Http/Controllers/Api/Client/Servers/SubuserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ public function update(UpdateSubuserRequest $request, Server $server): array
try {
$this->serverRepository->setServer($server)->revokeUserJTI($subuser->user_id);
if (in_array(Permission::ACTION_FILE_SFTP, $current) && !in_array(Permission::ACTION_FILE_SFTP, $permissions)) {
$sftpUsername = $subuser->user->username . '.' . $server->uuidShort;
$this->serverRepository->setServer($server)->disconnectSFTP($sftpUsername);
$this->serverRepository->setServer($server)->disconnectSFTP($subuser->user->username);
}
} catch (DaemonConnectionException $exception) {
// Don't block this request if we can't connect to the Wings instance. Chances are it is
Expand Down Expand Up @@ -155,8 +154,7 @@ public function delete(DeleteSubuserRequest $request, Server $server): JsonRespo

try {
$this->serverRepository->setServer($server)->revokeUserJTI($subuser->user_id);
$sftpUsername = $subuser->user->username . '.' . $server->uuidShort;
$this->serverRepository->setServer($server)->disconnectSFTP($sftpUsername);
$this->serverRepository->setServer($server)->disconnectSFTP($subuser->user->username);
} catch (DaemonConnectionException $exception) {
// Don't block this request if we can't connect to the Wings instance.
Log::warning($exception, ['user_id' => $subuser->user_id, 'server_id' => $server->id]);
Expand Down
3 changes: 2 additions & 1 deletion app/Repositories/Wings/DaemonServerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,12 @@ protected function revokeJTIs(array $jtis): void
public function disconnectSFTP(string $username): void
{
Assert::isInstanceOf($this->server, Server::class);
$sftpUsername = $username . '.' . $this->server->uuidShort;

try {
$this->getHttpClient()
->delete(sprintf('/api/servers/%s/sftp/disconnect', $this->server->uuid), [
'json' => ['username' => $username],
'json' => ['username' => $sftpUsername],
]);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testCorrectSubuserIsDeletedFromServer()
$uuid = $differentUser->id . substr($real, strlen((string) $differentUser->id));

/** @var User $subuser */
$subuser = User::factory()->create(['uuid' => $uuid]);
$subuser = User::factory()->create(['uuid' => $uuid, 'username' => 'test']);

Subuser::query()->forceCreate([
'user_id' => $subuser->id,
Expand All @@ -45,14 +45,15 @@ public function testCorrectSubuserIsDeletedFromServer()
]);

$mock->expects('setServer->revokeUserJTI')->with($subuser->id)->andReturnUndefined();
$mock->expects('setServer->disconnectSFTP')->with($subuser->username)->andReturnUndefined();

$this->actingAs($user)->deleteJson($this->link($server) . "/users/$subuser->uuid")->assertNoContent();

// Try the same test, but this time with a UUID that if cast to an int (shouldn't) line up with
// anything in the database.
$uuid = '18180000' . substr(Uuid::uuid4()->toString(), 8);
/** @var User $subuser */
$subuser = User::factory()->create(['uuid' => $uuid]);
$subuser = User::factory()->create(['uuid' => $uuid, 'username' => 'test1']);

Subuser::query()->forceCreate([
'user_id' => $subuser->id,
Expand All @@ -61,6 +62,7 @@ public function testCorrectSubuserIsDeletedFromServer()
]);

$mock->expects('setServer->revokeUserJTI')->with($subuser->id)->andReturnUndefined();
$mock->expects('setServer->disconnectSFTP')->with($subuser->username)->andReturnUndefined();

$this->actingAs($user)->deleteJson($this->link($server) . "/users/$subuser->uuid")->assertNoContent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function testUserCannotAccessResourceBelongingToOtherServers(string $meth
$this->instance(DaemonServerRepository::class, $mock = \Mockery::mock(DaemonServerRepository::class));
if ($method === 'DELETE') {
$mock->expects('setServer->revokeUserJTI')->with($internal->id)->andReturnUndefined();
$mock->expects('setServer->disconnectSFTP')->with($internal->username)->andReturnUndefined();
}

// This route is acceptable since they're accessing a subuser on their own server.
Expand Down

0 comments on commit 9ef9ea3

Please sign in to comment.