Skip to content

Commit 57bddb6

Browse files
committed
fix: restore guest id hashing after scenarios
Signed-off-by: Vitor Mattos <vitor@php.rio>
1 parent 4fbb5d4 commit 57bddb6

1 file changed

Lines changed: 41 additions & 3 deletions

File tree

src/NextcloudApiContext.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class NextcloudApiContext implements Context {
3939
* @var string[]
4040
*/
4141
protected static array $createdUsers = [];
42+
protected static bool $shouldRestoreGuestIdHashing = false;
43+
protected static ?bool $guestIdHashingOriginalValue = null;
4244
protected ResponseInterface $response;
4345
/** @var CookieJar[] */
4446
protected $cookieJars;
@@ -74,6 +76,8 @@ public static function beforeSuite(BeforeSuiteScope $scope):void {
7476
public static function beforeScenario(): void {
7577
self::$createdUsers = [];
7678
self::$environments = [];
79+
self::$shouldRestoreGuestIdHashing = false;
80+
self::$guestIdHashingOriginalValue = null;
7781
}
7882

7983
#[Given('as user :user')]
@@ -96,9 +100,7 @@ public function assureUserExists(string $user): void {
96100

97101
#[Given('guest :guest exists')]
98102
public function assureGuestExists(string $guest): void {
99-
// Recent guests versions hash user IDs by default, but downstream tests
100-
// still authenticate using the guest identifier passed in the scenario.
101-
self::runCommand('config:app:set guests hash_user_ids --value false --type boolean');
103+
self::disableGuestIdHashing();
102104
$response = $this->userExists($guest);
103105
if ($response->getStatusCode() !== 200) {
104106
static::createAnEnvironmentWithValueToBeUsedByOccCommand('OC_PASS', '123456');
@@ -118,6 +120,41 @@ protected function userExists(string $user): ResponseInterface {
118120
return $this->response;
119121
}
120122

123+
private static function disableGuestIdHashing(): void {
124+
if (self::$shouldRestoreGuestIdHashing) {
125+
return;
126+
}
127+
128+
// nextcloud/guests 555cf1bc hashes guest IDs by default, but guest
129+
// scenarios still use the raw identifier as the login/user reference.
130+
$currentValue = self::runCommand('config:app:get guests hash_user_ids');
131+
self::$shouldRestoreGuestIdHashing = true;
132+
self::$guestIdHashingOriginalValue = $currentValue['resultCode'] === 0
133+
? trim(implode("\n", $currentValue['output'])) === '1'
134+
: null;
135+
self::runCommandWithResultCode('config:app:set guests hash_user_ids --value false --type boolean', 0);
136+
}
137+
138+
private static function restoreGuestIdHashing(): void {
139+
if (!self::$shouldRestoreGuestIdHashing) {
140+
return;
141+
}
142+
143+
$originalValue = self::$guestIdHashingOriginalValue;
144+
self::$shouldRestoreGuestIdHashing = false;
145+
self::$guestIdHashingOriginalValue = null;
146+
147+
if ($originalValue === null) {
148+
self::runCommandWithResultCode('config:app:delete guests hash_user_ids', 0);
149+
return;
150+
}
151+
152+
self::runCommandWithResultCode(
153+
'config:app:set guests hash_user_ids --value ' . ($originalValue ? 'true' : 'false') . ' --type boolean',
154+
0
155+
);
156+
}
157+
121158
protected function createUser(string $user): void {
122159
$currentUser = $this->currentUser;
123160
$this->setCurrentUser('admin');
@@ -645,6 +682,7 @@ public function tearDown(): void {
645682
foreach (self::$createdUsers as $user) {
646683
$this->deleteUser($user);
647684
}
685+
self::restoreGuestIdHashing();
648686
}
649687

650688
protected function deleteUser(string $user): ResponseInterface {

0 commit comments

Comments
 (0)