Skip to content

Commit

Permalink
Refactor the fake spaceid function to get fake space_id for non-exist…
Browse files Browse the repository at this point in the history
…ent user
  • Loading branch information
SagarGi committed Nov 30, 2022
1 parent 86df1bc commit 8fa69f8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 20 deletions.
14 changes: 14 additions & 0 deletions tests/TestHelpers/SpaceNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* @author Sagar Gurung <[email protected]>
*
*/
namespace TestHelpers;
use Exception;

/**
* Class SpaceNotFoundException
* Exception when space id for a user is not found
*/
class SpaceNotFoundException extends Exception {
}
58 changes: 42 additions & 16 deletions tests/TestHelpers/WebDavHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use DateTime;
use TestHelpers\SpaceNotFoundException;

/**
* Helper to make WebDav Requests
Expand Down Expand Up @@ -475,7 +476,9 @@ public static function getPersonalSpaceIdForUser(string $baseUrl, string $user,
);
// we expect to get a multipart XML response with status 207
$status = $response->getStatusCode();
if ($status !== 207) {
if ($status === 401) {
throw new SpaceNotFoundException(__METHOD__ . " Personal space not found for user " . $user);
} elseif ($status !== 207) {
throw new Exception(
__METHOD__ . " webdav propfind for user $user failed with status $status - so the personal space id cannot be discovered"
);
Expand Down Expand Up @@ -535,8 +538,39 @@ public static function getPersonalSpaceIdForUser(string $baseUrl, string $user,
self::$spacesIdRef[$user] = [];
self::$spacesIdRef[$user]["personal"] = $personalSpaceId;
return $personalSpaceId;
} else {
throw new SpaceNotFoundException(__METHOD__ . " Personal space not found for user " . $user);
}
}

/**
* First checks if a user exist to return its space ID
* In case of any exception, it returns a fake space ID
*
* @param string $baseUrl
* @param string $user
* @param string $password
* @param string $xRequestId
*
* @return string
* @throws Exception
*/
public static function getPersonalSpaceIdForUserOrFakeIfNotFound(string $baseUrl, string $user, string $password, string $xRequestId):string {
try {
$spaceId = self::getPersonalSpaceIdForUser(
$baseUrl,
$user,
$password,
$xRequestId,
);
} catch (SpaceNotFoundException $e) {
// if the fetch fails, and the user is not found, then a fake space id is prepared
// this is useful for testing when the personal space is of a non-existing user
$fakeSpaceId = self::generateUUIDv4();
self::$spacesIdRef[$user]["personal"] = $fakeSpaceId;
$spaceId = $fakeSpaceId;
}
throw new Exception(__METHOD__ . " Personal space not found for user " . $user);
return $spaceId;
}

/**
Expand Down Expand Up @@ -597,20 +631,12 @@ public static function makeDavRequest(

// get space id if testing with spaces dav
if (self::$SPACE_ID_FROM_OCIS === '' && $davPathVersionToUse === self::DAV_VERSION_SPACES) {
try {
$spaceId = self::getPersonalSpaceIdForUser(
$baseUrl,
$doDavRequestAsUser ?? $user,
$password,
$xRequestId,
);
} catch (Exception $e) {
// if the fetch fails, and the user is not found, then a fake space id is prepared
// this is useful for testing when the personal space is of a non-existing user
$fakeSpaceId = self::generateUUIDv4();
self::$spacesIdRef[$user]["personal"] = $fakeSpaceId;
$spaceId = $fakeSpaceId;
}
$spaceId = self::checkAndGetPersonalSpaceIdForUser(
$baseUrl,
$doDavRequestAsUser ?? $user,
$password,
$xRequestId
);
} else {
$spaceId = self::$SPACE_ID_FROM_OCIS;
}
Expand Down
5 changes: 1 addition & 4 deletions tests/acceptance/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3329,11 +3329,8 @@ public function substituteInLineCodes(
* @throws GuzzleException
*/
public function getPersonalSpaceIdForUser(string $user, bool $alwaysDoIt = false): ?string {
if ($user === 'non-existent-user') {
return WebDavHelper::generateUUIDv4();
}
if ($alwaysDoIt || ($this->getDavPathVersion() === WebDavHelper::DAV_VERSION_SPACES)) {
return WebDavHelper::getPersonalSpaceIdForUser(
return WebDavHelper::getPersonalSpaceIdForUserOrFakeIfNotFound(
$this->getBaseUrl(),
$user,
$this->getPasswordForUser($user),
Expand Down

0 comments on commit 8fa69f8

Please sign in to comment.